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