From fbbadce952696ba5ca3ac89de9120f63d69c1927 Mon Sep 17 00:00:00 2001 From: Philippe Caseiro Date: Thu, 30 Jun 2022 15:46:38 +0200 Subject: [PATCH] Try 07 --- pkg/templater/services.go | 17 ++++++++++---- pkg/templater/system_users.go | 43 +++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 4 deletions(-) diff --git a/pkg/templater/services.go b/pkg/templater/services.go index bca11cb..5946b1d 100644 --- a/pkg/templater/services.go +++ b/pkg/templater/services.go @@ -44,14 +44,23 @@ func (s *Service) manageRepos(repos map[string]Repository) error { func (s *Service) Manage(templateDir string, rootDir string) error { // Manage packages repositories - log.Printf("Managing package repositories") + log.Print("Managing package repositories") err := s.manageRepos(s.Repos) if err != nil { return err } + // Create system users + log.Print("Managing system users") + for _, user := range s.Users { + err := user.Manage() + if err != nil { + return err + } + } + // Manage system packages - log.Printf("Installing packages") + log.Print("Installing packages") for _, pack := range s.Packages { err := pack.Manage() if err != nil { @@ -60,13 +69,13 @@ func (s *Service) Manage(templateDir string, rootDir string) error { log.Printf("\tPackage %s installed\n", pack.Name) } - log.Printf("Generating configuration files\n") + log.Print("Generating configuration files\n") err = processConfigFiles(s.ConfigFiles, s.Vars, templateDir, rootDir) if err != nil { return fmt.Errorf("ProcessingTemplatesFailed with error: %v", err) } - log.Printf("Managing services:\n") + log.Print("Managing services:\n") for _, daemon := range s.Daemons { err = daemon.Manage() if err != nil { diff --git a/pkg/templater/system_users.go b/pkg/templater/system_users.go index 9bcbd37..bd870a9 100644 --- a/pkg/templater/system_users.go +++ b/pkg/templater/system_users.go @@ -1,8 +1,51 @@ package templater +import ( + "log" + + "forge.cadoles.com/pcaseiro/templatefile/pkg/utils" +) + type SystemUser struct { UserName string `json:"username"` Group string `json:"group"` Home string `json:"home"` Shell string `json:"shell"` } + +func (su *SystemUser) exists() (bool, error) { + _, _, err := utils.RunSystemCommand("getent", "passwd", su.UserName) + if err != nil { + return false, err + } + return true, nil +} + +func (su *SystemUser) Manage() error { + exist, _ := su.exists() + if exist { + log.Printf("\tUser %s already exists", su.UserName) + return nil + } + return su.Create() +} + +func (su *SystemUser) Create() error { + _, _, err := utils.RunSystemCommand("useradd", "-b", su.Home, "-m", "-G", su.Group, su.UserName) + if err != nil { + return err + } + return nil +} + +func (su *SystemUser) Delete() error { + _, _, err := utils.RunSystemCommand("userdel", su.UserName) + if err != nil { + return err + } + return nil +} + +func (su *SystemUser) Update() error { + return nil +}