fix(bootstraper): adding system group management

This commit is contained in:
Philippe Caseiro 2022-07-01 09:58:22 +02:00
parent a28563e8d6
commit 9bb4e93d65
2 changed files with 58 additions and 1 deletions

View File

@ -0,0 +1,49 @@
package templater
import (
"fmt"
"log"
"forge.cadoles.com/pcaseiro/templatefile/pkg/utils"
)
type SystemGroup struct {
GroupName string `json:"groupname"`
}
func (sg *SystemGroup) exists() (bool, error) {
_, _, err := utils.RunSystemCommand("getent", "group", sg.GroupName)
if err != nil {
return false, err
}
return true, nil
}
func (sg *SystemGroup) Manage() error {
exist, _ := sg.exists()
if exist {
log.Printf("\tGroup %s already exists", sg.GroupName)
return nil
}
return sg.Create()
}
func (sg *SystemGroup) Create() error {
_, stdErr, err := utils.RunSystemCommand("groupadd", "-r", sg.GroupName)
if err != nil {
return fmt.Errorf("Group %s creation failed with error: %s %v", sg.GroupName, stdErr, err)
}
return nil
}
func (sg *SystemGroup) Delete() error {
_, _, err := utils.RunSystemCommand("userdel", sg.GroupName)
if err != nil {
return err
}
return nil
}
func (sg *SystemGroup) Update() error {
return nil
}

View File

@ -1,6 +1,7 @@
package templater
import (
"fmt"
"log"
"forge.cadoles.com/pcaseiro/templatefile/pkg/utils"
@ -31,10 +32,17 @@ func (su *SystemUser) Manage() error {
}
func (su *SystemUser) Create() error {
_, _, err := utils.RunSystemCommand("useradd", "-b", su.Home, "-m", "-G", su.Group, su.UserName)
// Manage System Group
grp := SystemGroup{GroupName: su.Group}
err := grp.Manage()
if err != nil {
return err
}
_, stdErr, err := utils.RunSystemCommand("useradd", "-b", su.Home, "-m", "-N", "-g", su.Group, su.UserName)
if err != nil {
return fmt.Errorf("User %s creation failed with error: %s %v", su.UserName, stdErr, err)
}
return nil
}