templatefile/pkg/templater/system_services.go

117 lines
3.0 KiB
Go
Raw Normal View History

package templater
import (
"fmt"
"os"
"forge.cadoles.com/pcaseiro/templatefile/pkg/utils"
)
type SystemService struct {
Name string `json:"name"`
Enabled bool `json:"enabled"`
Type string `json:"type"`
2022-06-27 15:58:37 +02:00
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"
}
}
2022-06-27 15:58:37 +02:00
func (sys *SystemService) Action() error {
if sys.ToStart {
return sys.Start()
}
return nil
}
func (sys *SystemService) Manage() error {
2022-06-27 15:58:37 +02:00
// 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
}
2022-06-27 15:58:37 +02:00
if err = sys.Action(); err != nil {
return err
}
} else {
fmt.Printf("Nothing to do for daemon %s\n", sys.Name)
}
return nil
}
func (sys *SystemService) Start() error {
2022-06-27 15:58:37 +02:00
fmt.Printf("Starting 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")
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 {
2022-06-27 15:58:37 +02:00
fmt.Printf("Stoping 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) 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)
}
} else if sys.Type == "openrc" {
2022-06-27 15:58:37 +02:00
_, 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)
}
} else {
return fmt.Errorf("Unsupported service type %s for service %s", sys.Type, sys.Name)
}
return nil
}