134 lines
3.3 KiB
Go
134 lines
3.3 KiB
Go
package templater
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
"os"
|
|
|
|
"forge.cadoles.com/pcaseiro/templatefile/pkg/utils"
|
|
)
|
|
|
|
type SystemService struct {
|
|
Name string `json:"name"`
|
|
Enabled bool `json:"enabled"`
|
|
Type string `json:"type"`
|
|
ToStart bool `json:"start"`
|
|
}
|
|
|
|
func (sys *SystemService) SetType() {
|
|
systemdRunDirectory := "/run/systemd/system"
|
|
openRcBinaryFile := "/sbin/openrc"
|
|
|
|
// Check if the configuration path is a Directory or a file
|
|
fileInfo, err := os.Stat(systemdRunDirectory)
|
|
if err == nil {
|
|
if fileInfo.IsDir() {
|
|
sys.Type = "systemd"
|
|
}
|
|
}
|
|
|
|
fileInfo, err = os.Stat(openRcBinaryFile)
|
|
if err == nil {
|
|
if fileInfo.IsDir() {
|
|
return
|
|
}
|
|
sys.Type = "openrc"
|
|
}
|
|
}
|
|
|
|
func (sys *SystemService) Action() error {
|
|
if sys.ToStart {
|
|
return sys.Start()
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (sys *SystemService) Manage() error {
|
|
// By default if the property sys.ToStart is empty
|
|
if sys.Type == "" || sys.Type == "auto" {
|
|
sys.SetType()
|
|
}
|
|
if sys.Enabled {
|
|
err := sys.Enable()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if err = sys.Action(); err != nil {
|
|
return err
|
|
}
|
|
|
|
} else {
|
|
log.Printf("\nNothing to do for daemon %s\n", sys.Name)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (sys *SystemService) Start() error {
|
|
log.Printf("\tStarting system service : %s\n", sys.Name)
|
|
if sys.Type == "systemd" {
|
|
_, stdErr, err := utils.RunSystemCommand("systemctl", "start", sys.Name)
|
|
if err != nil {
|
|
return fmt.Errorf("System service %s \n * Start error:\n - %s", sys.Name, stdErr)
|
|
}
|
|
} else if sys.Type == "openrc" {
|
|
_, stdErr, err := utils.RunSystemCommand("service", sys.Name, "stop")
|
|
fmt.Printf("ERROR [%v]", err)
|
|
if err != nil {
|
|
return fmt.Errorf("System service %s \n * Enable error:\n - %s", sys.Name, stdErr)
|
|
}
|
|
} else {
|
|
return fmt.Errorf("Unsupported service type %s for service %s", sys.Type, sys.Name)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (sys *SystemService) Stop() error {
|
|
log.Printf("\tStopping system service : %s\n", sys.Name)
|
|
if sys.Type == "systemd" {
|
|
_, stdErr, err := utils.RunSystemCommand("systemctl", "stop", sys.Name)
|
|
if err != nil {
|
|
return fmt.Errorf("System service %s \n * Stop error:\n - %s", sys.Name, stdErr)
|
|
}
|
|
} else if sys.Type == "openrc" {
|
|
_, stdErr, err := utils.RunSystemCommand("service", sys.Name, "stop")
|
|
if err != nil {
|
|
return fmt.Errorf("System service %s \n * Enable error:\n - %s", sys.Name, stdErr)
|
|
}
|
|
} else {
|
|
return fmt.Errorf("Unsupported service type %s for service %s", sys.Type, sys.Name)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (sys *SystemService) Restart() error {
|
|
if sys.Type == "" || sys.Type == "auto" {
|
|
sys.SetType()
|
|
}
|
|
if err := sys.Stop(); err != nil {
|
|
return err
|
|
}
|
|
if err := sys.Start(); err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (sys *SystemService) Enable() error {
|
|
if sys.Type == "systemd" {
|
|
_, stdErr, err := utils.RunSystemCommand("systemctl", "enable", sys.Name)
|
|
if err != nil {
|
|
return fmt.Errorf("System service %s \n * Enable error:\n - %s", sys.Name, stdErr)
|
|
}
|
|
log.Printf("\tSystemd service %s enabled", sys.Name)
|
|
} else if sys.Type == "openrc" {
|
|
_, stdErr, err := utils.RunSystemCommand("rc-update", "add", sys.Name, "default")
|
|
if err != nil {
|
|
return fmt.Errorf("System service %s \n * Enable error:\n - %s", sys.Name, stdErr)
|
|
}
|
|
log.Printf("\tOpenRC service %s enabled", sys.Name)
|
|
} else {
|
|
return fmt.Errorf("Unsupported service type %s for service %s", sys.Type, sys.Name)
|
|
}
|
|
return nil
|
|
}
|