2022-06-15 17:29:45 +02:00
|
|
|
package templater
|
|
|
|
|
|
|
|
import (
|
2022-07-01 09:58:22 +02:00
|
|
|
"fmt"
|
2022-06-15 17:29:45 +02:00
|
|
|
"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 {
|
2022-07-01 09:58:22 +02:00
|
|
|
// Manage System Group
|
|
|
|
grp := SystemGroup{GroupName: su.Group}
|
|
|
|
err := grp.Manage()
|
2022-06-15 17:29:45 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-07-01 09:58:22 +02:00
|
|
|
|
|
|
|
_, 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)
|
|
|
|
}
|
2022-06-15 17:29:45 +02:00
|
|
|
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
|
|
|
|
}
|