mirror of
https://github.com/EnumeratedDev/enit.git
synced 2026-09-26 15:36:12 +00:00
Compare commits
6
Commits
f470c0ec78
..
0.3.0
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6d34559335
|
||
|
|
09d8b70009
|
||
|
|
8ed8efa9b6
|
||
|
|
2b4feb60ec
|
||
|
|
085bc676a0
|
||
|
|
d83c822c73
|
+10
-2
@@ -128,7 +128,7 @@ func main() {
|
||||
}
|
||||
|
||||
// Get service stage
|
||||
stage := 2
|
||||
stage := 3
|
||||
if len(flag.Args()) > 3 {
|
||||
flagStr := flag.Arg(3)
|
||||
_stage, err := strconv.ParseInt(flagStr, 10, 32)
|
||||
@@ -255,6 +255,7 @@ func main() {
|
||||
serviceState := returnedJsonData["state"].(string)
|
||||
serviceEnabled := returnedJsonData["is_enabled"].(bool)
|
||||
serviceStage := int(returnedJsonData["stage"].(float64))
|
||||
processID := int(returnedJsonData["process_id"].(float64))
|
||||
|
||||
fmt.Printf("Name: %s\n", flag.Arg(2))
|
||||
fmt.Printf("State: %s\n", serviceState)
|
||||
@@ -263,6 +264,9 @@ func main() {
|
||||
} else {
|
||||
fmt.Printf("Enabled: %t\n", serviceEnabled)
|
||||
}
|
||||
if serviceState == "running" {
|
||||
fmt.Printf("Process ID: %d\n", processID)
|
||||
}
|
||||
|
||||
return
|
||||
} else if flag.Arg(1) == "list" {
|
||||
@@ -317,7 +321,8 @@ func main() {
|
||||
serviceName := serviceMap.(map[string]any)["name"].(string)
|
||||
serviceState := serviceMap.(map[string]any)["state"].(string)
|
||||
serviceEnabled := serviceMap.(map[string]any)["is_enabled"].(bool)
|
||||
serviceStage := serviceMap.(map[string]any)["stage"].(int)
|
||||
serviceStage := int(serviceMap.(map[string]any)["stage"].(float64))
|
||||
processID := int(serviceMap.(map[string]any)["process_id"].(float64))
|
||||
|
||||
fmt.Printf("Name: %s\n", serviceName)
|
||||
fmt.Printf("State: %s\n", serviceState)
|
||||
@@ -326,6 +331,9 @@ func main() {
|
||||
} else {
|
||||
fmt.Printf("Enabled: %t\n", serviceEnabled)
|
||||
}
|
||||
if serviceState == "running" {
|
||||
fmt.Printf("Process ID: %d\n", processID)
|
||||
}
|
||||
fmt.Println()
|
||||
}
|
||||
|
||||
|
||||
+4
-15
@@ -152,10 +152,10 @@ func Init() {
|
||||
StopCmd: "",
|
||||
Restart: "",
|
||||
CrashOnSafeExit: true,
|
||||
ServiceRunPath: "",
|
||||
restartCount: 0,
|
||||
stopChannel: make(chan bool),
|
||||
LogOutput: true,
|
||||
state: EnitServiceUnloaded,
|
||||
}
|
||||
if err := yaml.Unmarshal(bytes, &service); err != nil {
|
||||
logger.Printf("Error: could not read service file %s", path.Join(serviceConfigDir, "services", entry.Name()))
|
||||
@@ -188,18 +188,7 @@ func Init() {
|
||||
service.Restart = "false"
|
||||
}
|
||||
|
||||
service.ServiceRunPath = path.Join(runtimeServiceDir, service.Name)
|
||||
err = os.MkdirAll(path.Join(service.ServiceRunPath), 0755)
|
||||
if err != nil {
|
||||
logger.Fatalf("Error: could not initialize ESVM: %s", err)
|
||||
}
|
||||
|
||||
err = service.setCurrentState(EnitServiceUnloaded)
|
||||
if err != nil {
|
||||
logger.Fatalf("Error: could not initialize ESVM: %s", err)
|
||||
}
|
||||
|
||||
Services = append(Services, service)
|
||||
Services = append(Services, &service)
|
||||
|
||||
logger.Printf("Service (%s) has been initialized!\n", service.Name)
|
||||
}
|
||||
@@ -211,7 +200,7 @@ func Init() {
|
||||
// Start enabled services
|
||||
stages := slices.Collect(maps.Keys(EnabledServices))
|
||||
slices.Sort(stages)
|
||||
for stage := 0; stage <= stages[len(stages)-1]; stage++ {
|
||||
for stage := 1; stage <= stages[len(stages)-1]; stage++ {
|
||||
logger.Printf("Starting stage %d services...", stage)
|
||||
|
||||
services := EnabledServices[stage]
|
||||
@@ -261,7 +250,7 @@ func Destroy() {
|
||||
func GetServiceByName(name string) *EnitService {
|
||||
for _, service := range Services {
|
||||
if service.Name == name {
|
||||
return &service
|
||||
return service
|
||||
}
|
||||
}
|
||||
return nil
|
||||
|
||||
+28
-79
@@ -6,7 +6,6 @@ import (
|
||||
"os/exec"
|
||||
"path"
|
||||
"slices"
|
||||
"strconv"
|
||||
"strings"
|
||||
"syscall"
|
||||
"time"
|
||||
@@ -45,12 +44,13 @@ type EnitService struct {
|
||||
StopCmd string `yaml:"stop_cmd,omitempty"`
|
||||
Restart string `yaml:"restart,omitempty"`
|
||||
LogOutput bool `yaml:"log_output,omitempty"`
|
||||
ServiceRunPath string
|
||||
state EnitServiceState
|
||||
processID int
|
||||
restartCount int
|
||||
stopChannel chan bool
|
||||
}
|
||||
|
||||
var Services = make([]EnitService, 0)
|
||||
var Services = make([]*EnitService, 0)
|
||||
var EnabledServices = make(map[int][]string)
|
||||
var startedServicesOrder = make([]string, 0)
|
||||
|
||||
@@ -74,51 +74,11 @@ func (service *EnitService) GetUnmetDependencies() (missingDependencies []string
|
||||
}
|
||||
|
||||
func (service *EnitService) GetProcess() *os.Process {
|
||||
bytes, err := os.ReadFile(path.Join(service.ServiceRunPath, "process"))
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
pid, err := strconv.Atoi(strings.TrimSpace(string(bytes)))
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
process, err := os.FindProcess(pid)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
process, _ := os.FindProcess(service.processID)
|
||||
|
||||
return process
|
||||
}
|
||||
|
||||
func (service *EnitService) setProcessID(pid int) error {
|
||||
if err := os.WriteFile(path.Join(service.ServiceRunPath, "process"), []byte(strconv.Itoa(pid)), 0644); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (service *EnitService) GetCurrentState() EnitServiceState {
|
||||
bytes, err := os.ReadFile(path.Join(service.ServiceRunPath, "state"))
|
||||
if err != nil {
|
||||
return EnitServiceUnknown
|
||||
}
|
||||
|
||||
state, err := strconv.Atoi(strings.TrimSpace(string(bytes)))
|
||||
if err != nil {
|
||||
return EnitServiceUnknown
|
||||
}
|
||||
return EnitServiceState(state)
|
||||
}
|
||||
|
||||
func (service *EnitService) setCurrentState(state EnitServiceState) error {
|
||||
if err := os.WriteFile(path.Join(service.ServiceRunPath, "state"), []byte(strconv.Itoa(int(state))), 0644); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (service *EnitService) GetLogFile() (file *os.File, err error) {
|
||||
// Create esvm log directory
|
||||
err = os.MkdirAll("/var/log/esvm", 0755)
|
||||
@@ -157,7 +117,7 @@ func (service *EnitService) StartService() error {
|
||||
if service == nil {
|
||||
return nil
|
||||
}
|
||||
if service.GetCurrentState() == EnitServiceRunning {
|
||||
if service.state == EnitServiceRunning {
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -187,25 +147,8 @@ func (service *EnitService) StartService() error {
|
||||
return err
|
||||
}
|
||||
|
||||
err := service.setProcessID(cmd.Process.Pid)
|
||||
if err != nil {
|
||||
// Close log file if not nil
|
||||
if logFile != nil {
|
||||
logFile.Close()
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
err = service.setCurrentState(EnitServiceRunning)
|
||||
if err != nil {
|
||||
// Close log file if not nil
|
||||
if logFile != nil {
|
||||
logFile.Close()
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
service.processID = cmd.Process.Pid
|
||||
service.state = EnitServiceRunning
|
||||
|
||||
go func() {
|
||||
err := cmd.Wait()
|
||||
@@ -222,18 +165,18 @@ func (service *EnitService) StartService() error {
|
||||
if service.Type == "simple" && err == nil {
|
||||
service.restartCount = 0
|
||||
if service.ExitMethod != "stop_command" {
|
||||
_ = service.setCurrentState(EnitServiceCompleted)
|
||||
service.state = EnitServiceCompleted
|
||||
} else {
|
||||
_ = service.setCurrentState(EnitServiceRunning)
|
||||
service.state = EnitServiceRunning
|
||||
}
|
||||
return
|
||||
}
|
||||
if !service.CrashOnSafeExit {
|
||||
logger.Printf("Service (%s) has exited\n", service.Name)
|
||||
_ = service.setCurrentState(EnitServiceStopped)
|
||||
service.state = EnitServiceStopped
|
||||
} else {
|
||||
logger.Printf("Service (%s) has crashed!\n", service.Name)
|
||||
_ = service.setCurrentState(EnitServiceCrashed)
|
||||
service.state = EnitServiceCrashed
|
||||
}
|
||||
|
||||
if service.Restart == "always" {
|
||||
@@ -243,6 +186,8 @@ func (service *EnitService) StartService() error {
|
||||
_ = service.StartService()
|
||||
}
|
||||
}
|
||||
|
||||
service.processID = 0
|
||||
}()
|
||||
|
||||
// Add to started services order slice
|
||||
@@ -256,19 +201,20 @@ func (service *EnitService) StartService() error {
|
||||
}
|
||||
|
||||
func (service *EnitService) StopService() error {
|
||||
if service.GetCurrentState() != EnitServiceRunning {
|
||||
if service.state != EnitServiceRunning {
|
||||
return nil
|
||||
}
|
||||
|
||||
logger.Printf("Stopping service (%s)...", service.Name)
|
||||
|
||||
newServiceStatus := EnitServiceCrashed
|
||||
defer service.setCurrentState(newServiceStatus)
|
||||
defer service.setProcessID(0)
|
||||
defer func() {
|
||||
service.state = newServiceStatus
|
||||
service.processID = 0
|
||||
}()
|
||||
|
||||
if service.ExitMethod == "kill" {
|
||||
process := service.GetProcess()
|
||||
if err := process.Signal(syscall.Signal(0)); err != nil {
|
||||
if err := service.GetProcess().Signal(syscall.Signal(0)); err != nil {
|
||||
newServiceStatus = EnitServiceStopped
|
||||
logger.Printf("Service (%s) has stopped (Process already dead)", service.Name)
|
||||
return nil
|
||||
@@ -277,8 +223,8 @@ func (service *EnitService) StopService() error {
|
||||
go func() { service.stopChannel <- true }()
|
||||
|
||||
// Send SIGTERM signal to process
|
||||
if err := process.Signal(syscall.SIGTERM); err != nil {
|
||||
process.Signal(syscall.SIGKILL)
|
||||
if err := service.GetProcess().Signal(syscall.SIGTERM); err != nil {
|
||||
service.GetProcess().Signal(syscall.SIGKILL)
|
||||
return fmt.Errorf("could not stop process gracefully")
|
||||
}
|
||||
|
||||
@@ -286,7 +232,7 @@ func (service *EnitService) StopService() error {
|
||||
exited := make(chan bool)
|
||||
go func() {
|
||||
for {
|
||||
if err := process.Signal(syscall.Signal(0)); err != nil {
|
||||
if err := service.GetProcess().Signal(syscall.Signal(0)); err != nil {
|
||||
break
|
||||
}
|
||||
}
|
||||
@@ -296,7 +242,7 @@ func (service *EnitService) StopService() error {
|
||||
select {
|
||||
case <-exited:
|
||||
case <-time.After(5 * time.Second):
|
||||
process.Signal(syscall.SIGKILL)
|
||||
service.GetProcess().Signal(syscall.SIGKILL)
|
||||
return fmt.Errorf("could not stop process gracefully")
|
||||
}
|
||||
} else {
|
||||
@@ -346,14 +292,17 @@ func (service *EnitService) SetEnabled(stage int) error {
|
||||
}
|
||||
|
||||
// Remove service from current stage
|
||||
if s != 0 {
|
||||
EnabledServices[s] = slices.DeleteFunc(EnabledServices[s], func(name string) bool {
|
||||
return name == service.Name
|
||||
})
|
||||
if len(EnabledServices[s]) == 0 {
|
||||
delete(EnabledServices, s)
|
||||
}
|
||||
|
||||
// Add service to stage
|
||||
if stage != 0 {
|
||||
EnabledServices[stage] = append(EnabledServices[stage], service.Name)
|
||||
}
|
||||
|
||||
// Save enabled services to file
|
||||
data, err := yaml.Marshal(EnabledServices)
|
||||
@@ -378,7 +327,7 @@ func ReadEnabledServices() error {
|
||||
if err != nil {
|
||||
// Assume old plain text format
|
||||
for _, service := range strings.Split(strings.TrimSpace(string(data)), "\n") {
|
||||
EnabledServices[2] = append(EnabledServices[2], service)
|
||||
EnabledServices[3] = append(EnabledServices[3], service)
|
||||
}
|
||||
|
||||
// Update enabled_services file
|
||||
|
||||
+9
-2
@@ -197,7 +197,12 @@ func handleSetEnabledServiceCommand(conn net.Conn, jsonData map[string]any) {
|
||||
return
|
||||
}
|
||||
|
||||
if serviceStage == 0 {
|
||||
conn.Write(wrapSuccessMsgInJson(fmt.Sprintf("Service (%s) was disabled sucessfully", serviceName.(string))))
|
||||
} else {
|
||||
conn.Write(wrapSuccessMsgInJson(fmt.Sprintf("Service (%s) was enabled sucessfully", serviceName.(string))))
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func handleStatusServiceCommand(conn net.Conn, jsonData map[string]any) {
|
||||
@@ -217,7 +222,8 @@ func handleStatusServiceCommand(conn net.Conn, jsonData map[string]any) {
|
||||
|
||||
statusMap := make(map[string]any)
|
||||
statusMap["name"] = service.Name
|
||||
statusMap["state"] = EnitServiceStateNames[service.GetCurrentState()]
|
||||
statusMap["state"] = EnitServiceStateNames[service.state]
|
||||
statusMap["process_id"] = service.processID
|
||||
statusMap["is_enabled"], statusMap["stage"] = service.isEnabled()
|
||||
|
||||
// Encode map to json string
|
||||
@@ -238,7 +244,8 @@ func handleListServicesCommand(conn net.Conn, _ map[string]any) {
|
||||
for _, service := range Services {
|
||||
statusMap := make(map[string]any)
|
||||
statusMap["name"] = service.Name
|
||||
statusMap["state"] = EnitServiceStateNames[service.GetCurrentState()]
|
||||
statusMap["state"] = EnitServiceStateNames[service.state]
|
||||
statusMap["process_id"] = service.processID
|
||||
statusMap["is_enabled"], statusMap["stage"] = service.isEnabled()
|
||||
servicesMap["services"] = append(servicesMap["services"].([]map[string]any), statusMap)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user