mirror of
https://github.com/EnumeratedDev/enit.git
synced 2026-09-16 10:36:12 +00:00
Move service enable/disable functionality to esvm
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"io"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path"
|
||||
@@ -308,3 +309,64 @@ func (service *EnitService) RestartService() error {
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Functions will be rewritten at some point to allow enabling unloaded services
|
||||
|
||||
func (service *EnitService) isEnabled() bool {
|
||||
contents, err := os.ReadFile(path.Join(serviceConfigDir, "enabled_services"))
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
for _, line := range strings.Split(string(contents), "\n") {
|
||||
line = strings.TrimSpace(line)
|
||||
if line == "" {
|
||||
continue
|
||||
}
|
||||
|
||||
if line == service.Name {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
func (service *EnitService) SetEnabled(isEnabled bool) error {
|
||||
// Return if service is already in correct state
|
||||
if service.isEnabled() == isEnabled {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Create or open enabled_services file
|
||||
file, err := os.OpenFile(path.Join(serviceConfigDir, "enabled_services"), os.O_CREATE|os.O_RDWR, 0644)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
// Get enabled_services file contents
|
||||
contents, err := io.ReadAll(file)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Modify contents
|
||||
strContents := string(contents)
|
||||
if isEnabled {
|
||||
strContents += service.Name + "\n"
|
||||
} else {
|
||||
strContents = strings.ReplaceAll(strContents, service.Name+"\n", "")
|
||||
}
|
||||
|
||||
// Write new contents to file
|
||||
file.Truncate(0)
|
||||
file.Seek(0, 0)
|
||||
_, err = file.WriteString(strContents)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
file.Sync()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -20,6 +20,8 @@ func initSocket() (socket net.Listener, err error) {
|
||||
commandHandlers["start"] = handleStartServiceCommand
|
||||
commandHandlers["stop"] = handleStopServiceCommand
|
||||
commandHandlers["restart"] = handleRestartServiceCommand
|
||||
commandHandlers["enable"] = handleEnableServiceCommand
|
||||
commandHandlers["disable"] = handleDisableServiceCommand
|
||||
|
||||
return socket, nil
|
||||
}
|
||||
@@ -143,6 +145,68 @@ func handleRestartServiceCommand(conn net.Conn, jsonData map[string]any) {
|
||||
conn.Write(wrapSuccessMsgInJson(fmt.Sprintf("Service (%s) has restarted sucessfully", serviceName.(string))))
|
||||
}
|
||||
|
||||
func handleEnableServiceCommand(conn net.Conn, jsonData map[string]any) {
|
||||
// Get service name from json data
|
||||
serviceName, ok := jsonData["service"]
|
||||
if !ok {
|
||||
conn.Write(wrapErrorInJson(fmt.Errorf("'service' field missing")))
|
||||
return
|
||||
}
|
||||
|
||||
// Ensure service exists
|
||||
service := GetServiceByName(serviceName.(string))
|
||||
if service == nil {
|
||||
conn.Write(wrapErrorInJson(fmt.Errorf("Service (%s) not found", serviceName.(string))))
|
||||
return
|
||||
}
|
||||
|
||||
// Check if service is already enabled
|
||||
if service.isEnabled() {
|
||||
conn.Write(wrapSuccessMsgInJson(fmt.Sprintf("Service (%s) is already enabled", serviceName.(string))))
|
||||
return
|
||||
}
|
||||
|
||||
// Enable service
|
||||
err := service.SetEnabled(true)
|
||||
if err != nil {
|
||||
conn.Write(wrapErrorInJson(fmt.Errorf("Could not enable service! Error: %s", err)))
|
||||
return
|
||||
}
|
||||
|
||||
conn.Write(wrapSuccessMsgInJson(fmt.Sprintf("Service (%s) was enabled sucessfully", serviceName.(string))))
|
||||
}
|
||||
|
||||
func handleDisableServiceCommand(conn net.Conn, jsonData map[string]any) {
|
||||
// Get service name from json data
|
||||
serviceName, ok := jsonData["service"]
|
||||
if !ok {
|
||||
conn.Write(wrapErrorInJson(fmt.Errorf("'service' field missing")))
|
||||
return
|
||||
}
|
||||
|
||||
// Ensure service exists
|
||||
service := GetServiceByName(serviceName.(string))
|
||||
if service == nil {
|
||||
conn.Write(wrapErrorInJson(fmt.Errorf("Service (%s) not found", serviceName.(string))))
|
||||
return
|
||||
}
|
||||
|
||||
// Check if service is already disabled
|
||||
if !service.isEnabled() {
|
||||
conn.Write(wrapSuccessMsgInJson(fmt.Sprintf("Service (%s) is already disabled", serviceName.(string))))
|
||||
return
|
||||
}
|
||||
|
||||
// Disable service
|
||||
err := service.SetEnabled(false)
|
||||
if err != nil {
|
||||
conn.Write(wrapErrorInJson(fmt.Errorf("Could not disable service! Error: %s", err)))
|
||||
return
|
||||
}
|
||||
|
||||
conn.Write(wrapSuccessMsgInJson(fmt.Sprintf("Service (%s) was disabled sucessfully", serviceName.(string))))
|
||||
}
|
||||
|
||||
func wrapErrorInJson(err error) []byte {
|
||||
// Wrap error in struct
|
||||
type jsonErrorStruct struct {
|
||||
|
||||
Reference in New Issue
Block a user