This commit is contained in:
Philippe Caseiro 2022-06-30 15:46:38 +02:00
parent 8178939245
commit fbbadce952
2 changed files with 56 additions and 4 deletions

View File

@ -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 {

View File

@ -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
}