mirror of
https://github.com/EnumeratedDev/enit.git
synced 2026-09-16 10:36:12 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
88f41adea4
|
||
|
|
fa17840ad5
|
||
|
|
06f5e6564f
|
||
|
|
354fa510fd
|
||
|
|
b45f739bc3
|
||
|
|
26a6952091
|
||
|
|
e1a51649ae
|
||
|
|
bc6ea591c3
|
||
|
|
8abc4cf49c
|
||
|
|
e5c465610c
|
||
|
|
bcff6fecf8
|
||
|
|
5ee42a5966
|
||
|
|
7bbfa9f198
|
||
|
|
81421b0cc4
|
||
|
|
2aef0c0ff9
|
||
|
|
33e7479044
|
||
|
|
92587d8099
|
@@ -7,19 +7,7 @@ LOCALSTATEDIR ?= $(PREFIX)/var
|
|||||||
RUNSTATEDIR ?= $(LOCALSTATEDIR)/run
|
RUNSTATEDIR ?= $(LOCALSTATEDIR)/run
|
||||||
GO ?= $(shell type -a -P go | head -n 1)
|
GO ?= $(shell type -a -P go | head -n 1)
|
||||||
|
|
||||||
# Set version variable
|
VERSION ?= $(shell git describe --tags --dirty)
|
||||||
ifeq ($(VERSION),)
|
|
||||||
COMMIT := $(shell git rev-parse --short HEAD)
|
|
||||||
TAG_COMMIT := $(shell git rev-list --abbrev-commit --tags --max-count=1)
|
|
||||||
TAG := $(shell git describe --abbrev=0 --tags ${TAG_COMMIT} 2>/dev/null || true)
|
|
||||||
VERSION := $(COMMIT)
|
|
||||||
ifeq ($(COMMIT), $(TAG_COMMIT))
|
|
||||||
VERSION := $(TAG)
|
|
||||||
endif
|
|
||||||
ifneq ($(shell git status --porcelain),)
|
|
||||||
VERSION := $(VERSION)-dirty
|
|
||||||
endif
|
|
||||||
endif
|
|
||||||
|
|
||||||
build:
|
build:
|
||||||
mkdir -p build
|
mkdir -p build
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
name: lo-interface
|
name: lo-interface
|
||||||
description: Enable loopback interface on boot
|
description: Enable loopback interface on boot
|
||||||
dependencies: ["/usr/sbin/ip"]
|
|
||||||
type: simple
|
type: simple
|
||||||
start_cmd: ip link set lo up
|
start_cmd: ip link set lo up
|
||||||
exit_method: kill
|
exit_method: kill
|
||||||
|
|||||||
@@ -1,3 +1,8 @@
|
|||||||
module ectl
|
module ectl
|
||||||
|
|
||||||
go 1.23.4
|
go 1.23.4
|
||||||
|
|
||||||
|
require (
|
||||||
|
github.com/spf13/pflag v1.0.10
|
||||||
|
gopkg.in/yaml.v3 v3.0.1
|
||||||
|
)
|
||||||
|
|||||||
@@ -0,0 +1,6 @@
|
|||||||
|
github.com/spf13/pflag v1.0.10 h1:4EBh2KAYBwaONj6b2Ye1GiHfwjqyROoF4RwYO+vPwFk=
|
||||||
|
github.com/spf13/pflag v1.0.10/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
|
||||||
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
||||||
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
|
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||||
|
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
|
|||||||
+21
-399
@@ -1,18 +1,10 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"encoding/json"
|
|
||||||
"flag"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
|
||||||
"log"
|
"log"
|
||||||
"net"
|
|
||||||
"os"
|
"os"
|
||||||
"path"
|
|
||||||
"strconv"
|
|
||||||
"syscall"
|
"syscall"
|
||||||
"time"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Build-time variables
|
// Build-time variables
|
||||||
@@ -20,413 +12,43 @@ var version = "dev"
|
|||||||
var sysconfdir = "/etc/"
|
var sysconfdir = "/etc/"
|
||||||
var runstatedir = "/var/run/"
|
var runstatedir = "/var/run/"
|
||||||
|
|
||||||
var conn net.Conn
|
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|
||||||
// Set and parse flags
|
// Show usage if no arguments specified
|
||||||
printVersion := flag.Bool("version", false, "print version and exit")
|
if len(os.Args) == 1 {
|
||||||
printJson := flag.Bool("json", false, "print output in json format")
|
|
||||||
flag.Parse()
|
|
||||||
|
|
||||||
// Dial esvm socket
|
|
||||||
dialSocket()
|
|
||||||
defer conn.Close()
|
|
||||||
|
|
||||||
if flag.NArg() < 1 {
|
|
||||||
printUsage()
|
printUsage()
|
||||||
os.Exit(1)
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if *printVersion || flag.Args()[0] == "version" {
|
subcommand := os.Args[1]
|
||||||
|
|
||||||
|
switch subcommand {
|
||||||
|
case "v", "version":
|
||||||
fmt.Printf("Enit Control version %s\n", version)
|
fmt.Printf("Enit Control version %s\n", version)
|
||||||
return
|
case "shutdown", "poweroff", "halt":
|
||||||
} else if flag.Args()[0] == "help" {
|
|
||||||
printUsage()
|
|
||||||
return
|
|
||||||
} else if flag.Args()[0] == "shutdown" || flag.Args()[0] == "poweroff" || flag.Args()[0] == "halt" {
|
|
||||||
err := syscall.Kill(1, syscall.SIGUSR1)
|
err := syscall.Kill(1, syscall.SIGUSR1)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("Could not send shutdown signal! Error: %s\n", err)
|
log.Fatalf("Could not send shutdown signal! Error: %s\n", err)
|
||||||
}
|
}
|
||||||
return
|
case "reboot", "restart", "reset":
|
||||||
} else if flag.Args()[0] == "reboot" || flag.Args()[0] == "restart" || flag.Args()[0] == "reset" {
|
|
||||||
err := syscall.Kill(1, syscall.SIGTERM)
|
err := syscall.Kill(1, syscall.SIGTERM)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("Could not send shutdown signal! Error: %s\n", err)
|
log.Fatalf("Could not send reboot signal! Error: %s\n", err)
|
||||||
}
|
}
|
||||||
return
|
case "sv", "service":
|
||||||
} else if flag.Args()[0] == "service" || flag.Args()[0] == "sv" {
|
handleServiceSubcommand()
|
||||||
if len(flag.Args()) <= 1 {
|
default:
|
||||||
fmt.Println("Usage: ectl service <reload/start/stop/enable/disable/status/list> [service]")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if flag.Arg(1) == "reload" {
|
|
||||||
type ServiceCommandJsonStruct struct {
|
|
||||||
Command string `json:"command"`
|
|
||||||
Service string `json:"service"`
|
|
||||||
}
|
|
||||||
serviceCommandJson := ServiceCommandJsonStruct{
|
|
||||||
Command: flag.Arg(1),
|
|
||||||
}
|
|
||||||
|
|
||||||
// Encode struct to json string
|
|
||||||
jsonData, err := json.Marshal(serviceCommandJson)
|
|
||||||
if err != nil {
|
|
||||||
log.Fatalf("Could not encode JSON data! Error: %s\n", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
_, err = conn.Write(jsonData)
|
|
||||||
if err != nil {
|
|
||||||
log.Fatalf("Could not write JSON data to socket! Error: %s\n", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Read data from the connection.
|
|
||||||
data, err := readAllConn(conn)
|
|
||||||
if err != nil {
|
|
||||||
log.Fatalf("Could not read data from socket! Error: %s\n", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// Print json data if flag is set
|
|
||||||
if *printJson {
|
|
||||||
fmt.Println(string(data))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// Decoode JSON data
|
|
||||||
var returnedJsonData map[string]any
|
|
||||||
err = json.Unmarshal(data, &returnedJsonData)
|
|
||||||
if err != nil {
|
|
||||||
log.Fatalf("Could not decode JSON data from connection!")
|
|
||||||
}
|
|
||||||
|
|
||||||
if err, ok := returnedJsonData["error"]; ok {
|
|
||||||
log.Fatal(err)
|
|
||||||
} else if msg, ok := returnedJsonData["success"]; ok {
|
|
||||||
fmt.Println(msg)
|
|
||||||
} else {
|
|
||||||
log.Fatal("Connection returned empty string!")
|
|
||||||
}
|
|
||||||
|
|
||||||
return
|
|
||||||
} else if flag.Arg(1) == "start" || flag.Arg(1) == "stop" || flag.Arg(1) == "restart" {
|
|
||||||
// Ensure service name argument has been set
|
|
||||||
if len(flag.Args()) <= 2 {
|
|
||||||
fmt.Printf("Usage: ectl service %s <service>\n", flag.Args()[1])
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
type ServiceCommandJsonStruct struct {
|
|
||||||
Command string `json:"command"`
|
|
||||||
Service string `json:"service"`
|
|
||||||
}
|
|
||||||
serviceCommandJson := ServiceCommandJsonStruct{
|
|
||||||
Command: flag.Arg(1),
|
|
||||||
Service: flag.Arg(2),
|
|
||||||
}
|
|
||||||
|
|
||||||
// Encode struct to json string
|
|
||||||
jsonData, err := json.Marshal(serviceCommandJson)
|
|
||||||
if err != nil {
|
|
||||||
log.Fatalf("Could not encode JSON data! Error: %s\n", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
_, err = conn.Write(jsonData)
|
|
||||||
if err != nil {
|
|
||||||
log.Fatalf("Could not write JSON data to socket! Error: %s\n", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Read data from the connection.
|
|
||||||
data, err := readAllConn(conn)
|
|
||||||
if err != nil {
|
|
||||||
log.Fatalf("Could not read data from socket! Error: %s\n", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// Print json data if flag is set
|
|
||||||
if *printJson {
|
|
||||||
fmt.Println(string(data))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// Decoode JSON data
|
|
||||||
var returnedJsonData map[string]any
|
|
||||||
err = json.Unmarshal(data, &returnedJsonData)
|
|
||||||
if err != nil {
|
|
||||||
log.Fatalf("Could not decode JSON data from connection!")
|
|
||||||
}
|
|
||||||
|
|
||||||
if err, ok := returnedJsonData["error"]; ok {
|
|
||||||
log.Fatal(err)
|
|
||||||
} else if msg, ok := returnedJsonData["success"]; ok {
|
|
||||||
fmt.Println(msg)
|
|
||||||
} else {
|
|
||||||
log.Fatal("Connection returned empty string!")
|
|
||||||
}
|
|
||||||
|
|
||||||
return
|
|
||||||
} else if flag.Arg(1) == "enable" || flag.Arg(1) == "disable" {
|
|
||||||
// Ensure service name argument has been set
|
|
||||||
if len(flag.Args()) <= 2 {
|
|
||||||
fmt.Printf("Usage: ectl service %s <service> [stage]\n", flag.Args()[1])
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get service stage
|
|
||||||
stage := 3
|
|
||||||
if len(flag.Args()) > 3 {
|
|
||||||
flagStr := flag.Arg(3)
|
|
||||||
_stage, err := strconv.ParseInt(flagStr, 10, 32)
|
|
||||||
if err != nil {
|
|
||||||
log.Fatalf("Error: could not parse stage number: %s", err)
|
|
||||||
}
|
|
||||||
stage = int(_stage)
|
|
||||||
} else if flag.Arg(1) == "disable" {
|
|
||||||
stage = 0
|
|
||||||
}
|
|
||||||
|
|
||||||
type ServiceCommandJsonStruct struct {
|
|
||||||
Command string `json:"command"`
|
|
||||||
Service string `json:"service"`
|
|
||||||
Stage int `json:"stage"`
|
|
||||||
}
|
|
||||||
serviceCommandJson := ServiceCommandJsonStruct{
|
|
||||||
Command: "set_enabled",
|
|
||||||
Service: flag.Arg(2),
|
|
||||||
Stage: stage,
|
|
||||||
}
|
|
||||||
|
|
||||||
// Encode struct to json string
|
|
||||||
jsonData, err := json.Marshal(serviceCommandJson)
|
|
||||||
if err != nil {
|
|
||||||
log.Fatalf("Could not encode JSON data! Error: %s\n", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
_, err = conn.Write(jsonData)
|
|
||||||
if err != nil {
|
|
||||||
log.Fatalf("Could not write JSON data to socket! Error: %s\n", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Read data from the connection.
|
|
||||||
data, err := readAllConn(conn)
|
|
||||||
if err != nil {
|
|
||||||
log.Fatalf("Could not read data from socket! Error: %s\n", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// Print json data if flag is set
|
|
||||||
if *printJson {
|
|
||||||
fmt.Println(string(data))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// Decoode JSON data
|
|
||||||
var returnedJsonData map[string]any
|
|
||||||
err = json.Unmarshal(data, &returnedJsonData)
|
|
||||||
if err != nil {
|
|
||||||
log.Fatalf("Could not decode JSON data from connection!")
|
|
||||||
}
|
|
||||||
|
|
||||||
if err, ok := returnedJsonData["error"]; ok {
|
|
||||||
log.Fatal(err)
|
|
||||||
} else if msg, ok := returnedJsonData["success"]; ok {
|
|
||||||
fmt.Println(msg)
|
|
||||||
} else {
|
|
||||||
log.Fatal("Connection returned empty string!")
|
|
||||||
}
|
|
||||||
|
|
||||||
return
|
|
||||||
} else if flag.Args()[1] == "status" {
|
|
||||||
// Ensure service name argument has been set
|
|
||||||
if len(flag.Args()) <= 2 {
|
|
||||||
fmt.Printf("Usage: ectl service %s <service>\n", flag.Args()[1])
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
type ServiceCommandJsonStruct struct {
|
|
||||||
Command string `json:"command"`
|
|
||||||
Service string `json:"service"`
|
|
||||||
}
|
|
||||||
serviceCommandJson := ServiceCommandJsonStruct{
|
|
||||||
Command: flag.Arg(1),
|
|
||||||
Service: flag.Arg(2),
|
|
||||||
}
|
|
||||||
|
|
||||||
// Encode struct to json string
|
|
||||||
jsonData, err := json.Marshal(serviceCommandJson)
|
|
||||||
if err != nil {
|
|
||||||
log.Fatalf("Could not encode JSON data! Error: %s\n", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
_, err = conn.Write(jsonData)
|
|
||||||
if err != nil {
|
|
||||||
log.Fatalf("Could not write JSON data to socket! Error: %s\n", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Read data from the connection.
|
|
||||||
data, err := readAllConn(conn)
|
|
||||||
if err != nil {
|
|
||||||
log.Fatalf("Could not read data from socket! Error: %s\n", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// Print json data if flag is set
|
|
||||||
if *printJson {
|
|
||||||
fmt.Println(string(data))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// Decoode JSON data
|
|
||||||
var returnedJsonData map[string]any
|
|
||||||
err = json.Unmarshal(data, &returnedJsonData)
|
|
||||||
if err != nil {
|
|
||||||
log.Fatalf("Could not decode JSON data from connection!")
|
|
||||||
}
|
|
||||||
|
|
||||||
if err, ok := returnedJsonData["error"]; ok {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
serviceState := returnedJsonData["state"].(string)
|
|
||||||
serviceDescription := returnedJsonData["description"].(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("Description: %s\n", serviceDescription)
|
|
||||||
fmt.Printf("State: %s\n", serviceState)
|
|
||||||
if serviceEnabled {
|
|
||||||
fmt.Printf("Enabled: %t (Stage %d)\n", serviceEnabled, serviceStage)
|
|
||||||
} else {
|
|
||||||
fmt.Printf("Enabled: %t\n", serviceEnabled)
|
|
||||||
}
|
|
||||||
if serviceState == "running" {
|
|
||||||
fmt.Printf("Process ID: %d\n", processID)
|
|
||||||
}
|
|
||||||
|
|
||||||
return
|
|
||||||
} else if flag.Arg(1) == "list" {
|
|
||||||
type ServiceCommandJsonStruct struct {
|
|
||||||
Command string `json:"command"`
|
|
||||||
}
|
|
||||||
serviceCommandJson := ServiceCommandJsonStruct{
|
|
||||||
Command: flag.Arg(1),
|
|
||||||
}
|
|
||||||
|
|
||||||
// Encode struct to json string
|
|
||||||
jsonData, err := json.Marshal(serviceCommandJson)
|
|
||||||
if err != nil {
|
|
||||||
log.Fatalf("Could not encode JSON data! Error: %s\n", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
_, err = conn.Write(jsonData)
|
|
||||||
if err != nil {
|
|
||||||
log.Fatalf("Could not write JSON data to socket! Error: %s\n", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Read data from the connection.
|
|
||||||
data, err := readAllConn(conn)
|
|
||||||
if err != nil {
|
|
||||||
log.Fatalf("Could not read data from socket! Error: %s\n", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// Print json data if flag is set
|
|
||||||
if *printJson {
|
|
||||||
fmt.Println(string(data))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// Decoode JSON data
|
|
||||||
var returnedJsonData map[string]any
|
|
||||||
err = json.Unmarshal(data, &returnedJsonData)
|
|
||||||
if err != nil {
|
|
||||||
log.Fatalf("Could not decode JSON data from connection!")
|
|
||||||
}
|
|
||||||
|
|
||||||
if err, ok := returnedJsonData["error"]; ok {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, serviceMap := range returnedJsonData["services"].([]any) {
|
|
||||||
serviceName := serviceMap.(map[string]any)["name"].(string)
|
|
||||||
serviceDescription := serviceMap.(map[string]any)["description"].(string)
|
|
||||||
serviceState := serviceMap.(map[string]any)["state"].(string)
|
|
||||||
serviceEnabled := serviceMap.(map[string]any)["is_enabled"].(bool)
|
|
||||||
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("Description: %s\n", serviceDescription)
|
|
||||||
fmt.Printf("State: %s\n", serviceState)
|
|
||||||
if serviceEnabled {
|
|
||||||
fmt.Printf("Enabled: %t (Stage %d)\n", serviceEnabled, serviceStage)
|
|
||||||
} else {
|
|
||||||
fmt.Printf("Enabled: %t\n", serviceEnabled)
|
|
||||||
}
|
|
||||||
if serviceState == "running" {
|
|
||||||
fmt.Printf("Process ID: %d\n", processID)
|
|
||||||
}
|
|
||||||
fmt.Println()
|
|
||||||
}
|
|
||||||
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
printUsage()
|
printUsage()
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func printUsage() {
|
func printUsage() {
|
||||||
fmt.Println("Available sucommands:")
|
fmt.Println("Usage: ectl <subcommand> [options]")
|
||||||
fmt.Println("ectl version | Show enit version")
|
fmt.Println("Description: Shutdown, reboot and manage system services")
|
||||||
fmt.Println("ectl shutdown/poweroff/halt | Shutdown the system")
|
fmt.Println("Sucommands:")
|
||||||
fmt.Println("ectl reboot/restart | Reboot the system")
|
fmt.Println(" v, version Show enit version")
|
||||||
fmt.Println("ectl help | Show command explanations")
|
fmt.Println(" shutdown, poweroff, halt Shutdown the system")
|
||||||
fmt.Println("ectl sv/service start <service> | Start a service")
|
fmt.Println(" reboot, restart, reset Reboot the system")
|
||||||
fmt.Println("ectl sv/service stop <service> | Stop a service")
|
fmt.Println(" sv, service Manage system services")
|
||||||
fmt.Println("ectl sv/service enable <service> | Enable a service at startup")
|
|
||||||
fmt.Println("ectl sv/service disable <service> | Disable a service at startup")
|
|
||||||
fmt.Println("ectl sv/service status <service> | Show service status")
|
|
||||||
fmt.Println("ectl sv/service list | Show all enabled services")
|
|
||||||
}
|
|
||||||
|
|
||||||
func dialSocket() {
|
|
||||||
if _, err := os.Stat(path.Join(runstatedir, "esvm/esvm.sock")); err != nil {
|
|
||||||
log.Fatalf("Could not find esvm.sock! Error: %s\n", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
var err error
|
|
||||||
conn, err = net.Dial("unix", path.Join(runstatedir, "esvm/esvm.sock"))
|
|
||||||
if err != nil {
|
|
||||||
log.Fatalf("Failed to connect to esvm.sock! Error: %s\n", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := conn.SetDeadline(time.Now().Add(5 * time.Second)); err != nil {
|
|
||||||
log.Fatalf("Failed to set write deadline! Error: %s\n", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func readAllConn(conn net.Conn) ([]byte, error) {
|
|
||||||
var buf bytes.Buffer
|
|
||||||
|
|
||||||
for {
|
|
||||||
dataChunk := make([]byte, 1024)
|
|
||||||
|
|
||||||
n, err := conn.Read(dataChunk)
|
|
||||||
if err != nil && err != io.EOF {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
buf.Write(dataChunk[:n])
|
|
||||||
|
|
||||||
if n < 1024 {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return buf.Bytes(), nil
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,476 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"log"
|
||||||
|
"net"
|
||||||
|
"os"
|
||||||
|
"path"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
flag "github.com/spf13/pflag"
|
||||||
|
)
|
||||||
|
|
||||||
|
var currentFlagSet *flag.FlagSet
|
||||||
|
var conn net.Conn
|
||||||
|
|
||||||
|
func handleServiceSubcommand() {
|
||||||
|
if len(os.Args) == 2 {
|
||||||
|
printSvUsage()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
subcommand := os.Args[2]
|
||||||
|
|
||||||
|
switch subcommand {
|
||||||
|
case "start", "stop", "restart":
|
||||||
|
// Setup flags and help
|
||||||
|
currentFlagSet = flag.NewFlagSet(subcommand, flag.ExitOnError)
|
||||||
|
currentFlagSet.BoolP("json", "j", false, "Return output in json format")
|
||||||
|
setupFlagsAndHelp(currentFlagSet, fmt.Sprintf("ectl %s %s <options> <service>", os.Args[1], subcommand), fmt.Sprintf("%s the specified service", strings.Title(subcommand)), os.Args[3:])
|
||||||
|
|
||||||
|
// Dial esvm socket
|
||||||
|
dialSocket()
|
||||||
|
defer conn.Close()
|
||||||
|
|
||||||
|
startStopRestartService(subcommand)
|
||||||
|
case "enable", "disable":
|
||||||
|
// Setup flags and help
|
||||||
|
currentFlagSet = flag.NewFlagSet(subcommand, flag.ExitOnError)
|
||||||
|
currentFlagSet.BoolP("json", "j", false, "Return output in json format")
|
||||||
|
setupFlagsAndHelp(currentFlagSet, fmt.Sprintf("ectl %s %s <options> <service>", os.Args[1], subcommand), fmt.Sprintf("%s the specified service", strings.Title(subcommand)), os.Args[3:])
|
||||||
|
|
||||||
|
enableDisableService(subcommand)
|
||||||
|
case "status":
|
||||||
|
// Setup flags and help
|
||||||
|
currentFlagSet = flag.NewFlagSet("status", flag.ExitOnError)
|
||||||
|
currentFlagSet.BoolP("json", "j", false, "Return output in json format")
|
||||||
|
setupFlagsAndHelp(currentFlagSet, fmt.Sprintf("ectl %s status <options> <service>", os.Args[1]), "Show service status", os.Args[3:])
|
||||||
|
|
||||||
|
// Dial esvm socket
|
||||||
|
dialSocket()
|
||||||
|
defer conn.Close()
|
||||||
|
|
||||||
|
showServiceStatus()
|
||||||
|
case "list":
|
||||||
|
// Setup flags and help
|
||||||
|
currentFlagSet = flag.NewFlagSet("list", flag.ExitOnError)
|
||||||
|
currentFlagSet.BoolP("json", "j", false, "Return output in json format")
|
||||||
|
setupFlagsAndHelp(currentFlagSet, fmt.Sprintf("ectl %s reload <options>", os.Args[1]), "List all services", os.Args[3:])
|
||||||
|
|
||||||
|
// Dial esvm socket
|
||||||
|
dialSocket()
|
||||||
|
defer conn.Close()
|
||||||
|
|
||||||
|
listAllServices()
|
||||||
|
case "reload":
|
||||||
|
// Setup flags and help
|
||||||
|
currentFlagSet = flag.NewFlagSet("reload", flag.ExitOnError)
|
||||||
|
currentFlagSet.BoolP("json", "j", false, "Return output in json format")
|
||||||
|
setupFlagsAndHelp(currentFlagSet, fmt.Sprintf("ectl %s reload <options>", os.Args[1]), "Reload all services", os.Args[3:])
|
||||||
|
|
||||||
|
// Dial esvm socket
|
||||||
|
dialSocket()
|
||||||
|
defer conn.Close()
|
||||||
|
|
||||||
|
reloadAllServices()
|
||||||
|
default:
|
||||||
|
printSvUsage()
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func startStopRestartService(subcommand string) {
|
||||||
|
// Get flags
|
||||||
|
printJson, _ := currentFlagSet.GetBool("json")
|
||||||
|
|
||||||
|
// Ensure service name argument has been set
|
||||||
|
if currentFlagSet.NArg() == 0 {
|
||||||
|
fmt.Printf("Usage: ectl service %s <service>\n", subcommand)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
type ServiceCommandJsonStruct struct {
|
||||||
|
Command string `json:"command"`
|
||||||
|
Service string `json:"service"`
|
||||||
|
}
|
||||||
|
serviceCommandJson := ServiceCommandJsonStruct{
|
||||||
|
Command: subcommand,
|
||||||
|
Service: currentFlagSet.Arg(0),
|
||||||
|
}
|
||||||
|
|
||||||
|
// Encode struct to json string
|
||||||
|
jsonData, err := json.Marshal(serviceCommandJson)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Could not encode JSON data! Error: %s\n", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = conn.Write(jsonData)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Could not write JSON data to socket! Error: %s\n", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Read data from the connection.
|
||||||
|
data, err := readAllConn(conn)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Could not read data from socket! Error: %s\n", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Print json data if flag is set
|
||||||
|
if printJson {
|
||||||
|
fmt.Println(string(data))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Decoode JSON data
|
||||||
|
var returnedJsonData map[string]any
|
||||||
|
err = json.Unmarshal(data, &returnedJsonData)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Could not decode JSON data from connection!")
|
||||||
|
}
|
||||||
|
|
||||||
|
if err, ok := returnedJsonData["error"]; ok {
|
||||||
|
log.Fatal(err)
|
||||||
|
} else if msg, ok := returnedJsonData["success"]; ok {
|
||||||
|
fmt.Println(msg)
|
||||||
|
} else {
|
||||||
|
log.Fatal("Connection returned empty string!")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func enableDisableService(subcommand string) {
|
||||||
|
// Get flags
|
||||||
|
printJson, _ := currentFlagSet.GetBool("json")
|
||||||
|
|
||||||
|
// Ensure service name argument has been set
|
||||||
|
if currentFlagSet.NArg() == 0 {
|
||||||
|
fmt.Printf("Usage: ectl service %s <service> [stage]\n", subcommand)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
service := currentFlagSet.Arg(0)
|
||||||
|
|
||||||
|
// Get service stage
|
||||||
|
stage := 3
|
||||||
|
if subcommand == "disable" {
|
||||||
|
stage = 0
|
||||||
|
} else if len(currentFlagSet.Args()) > 1 {
|
||||||
|
flagStr := currentFlagSet.Arg(1)
|
||||||
|
_stage, err := strconv.ParseInt(flagStr, 10, 32)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Error: could not parse stage number: %s", err)
|
||||||
|
}
|
||||||
|
stage = int(_stage)
|
||||||
|
}
|
||||||
|
|
||||||
|
verb := "enabled"
|
||||||
|
if stage == 0 {
|
||||||
|
verb = "disabled"
|
||||||
|
}
|
||||||
|
|
||||||
|
// Return if service is already enabled
|
||||||
|
if _, enabledStage := isServiceEnabled(service); enabledStage == stage {
|
||||||
|
if printJson {
|
||||||
|
fmt.Printf("{\"success\":\"Service (%s) is already %s\"}\n", service, verb)
|
||||||
|
} else {
|
||||||
|
fmt.Printf("Service (%s) is already %s\n", service, verb)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Enable service
|
||||||
|
err := setServiceEnabled(service, stage)
|
||||||
|
if err != nil {
|
||||||
|
verb := "enable"
|
||||||
|
if stage == 0 {
|
||||||
|
verb = "disable"
|
||||||
|
}
|
||||||
|
|
||||||
|
if printJson {
|
||||||
|
fmt.Printf("{\"error\":\"Could not %s service! Error: %s\"}\n", verb, err)
|
||||||
|
} else {
|
||||||
|
fmt.Printf("Could not %s service! Error: %s\n", verb, err)
|
||||||
|
}
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
if printJson {
|
||||||
|
fmt.Printf("{\"success\":\"Service (%s) was %s sucessfully\"}\n", service, verb)
|
||||||
|
return
|
||||||
|
} else {
|
||||||
|
fmt.Printf("Service (%s) was %s sucessfully\n", service, verb)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func showServiceStatus() {
|
||||||
|
// Get flags
|
||||||
|
printJson, _ := currentFlagSet.GetBool("json")
|
||||||
|
|
||||||
|
// Ensure service name argument has been set
|
||||||
|
if len(currentFlagSet.Args()) == 0 {
|
||||||
|
fmt.Println("Usage: ectl service status <service>")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
type ServiceCommandJsonStruct struct {
|
||||||
|
Command string `json:"command"`
|
||||||
|
Service string `json:"service"`
|
||||||
|
}
|
||||||
|
serviceCommandJson := ServiceCommandJsonStruct{
|
||||||
|
Command: "status",
|
||||||
|
Service: currentFlagSet.Arg(0),
|
||||||
|
}
|
||||||
|
|
||||||
|
// Encode struct to json string
|
||||||
|
jsonData, err := json.Marshal(serviceCommandJson)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Could not encode JSON data! Error: %s\n", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = conn.Write(jsonData)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Could not write JSON data to socket! Error: %s\n", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Read data from the connection.
|
||||||
|
data, err := readAllConn(conn)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Could not read data from socket! Error: %s\n", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Decoode JSON data
|
||||||
|
var returnedJsonData map[string]any
|
||||||
|
err = json.Unmarshal(data, &returnedJsonData)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Could not decode JSON data from connection!")
|
||||||
|
}
|
||||||
|
|
||||||
|
if err, ok := returnedJsonData["error"]; ok {
|
||||||
|
if printJson {
|
||||||
|
fmt.Println(string(data))
|
||||||
|
os.Exit(1)
|
||||||
|
} else {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set is_enabled and stage fields in json data
|
||||||
|
returnedJsonData["is_enabled"], returnedJsonData["stage"] = isServiceEnabled(currentFlagSet.Arg(0))
|
||||||
|
|
||||||
|
// Print json data if flag is set
|
||||||
|
if printJson {
|
||||||
|
data, _ = json.Marshal(returnedJsonData)
|
||||||
|
fmt.Println(string(data))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
serviceState := returnedJsonData["state"].(string)
|
||||||
|
serviceDescription := returnedJsonData["description"].(string)
|
||||||
|
serviceEnabled := returnedJsonData["is_enabled"].(bool)
|
||||||
|
serviceStage := returnedJsonData["stage"].(int)
|
||||||
|
processID := int(returnedJsonData["process_id"].(float64))
|
||||||
|
|
||||||
|
fmt.Printf("Name: %s\n", currentFlagSet.Arg(0))
|
||||||
|
fmt.Printf("Description: %s\n", serviceDescription)
|
||||||
|
fmt.Printf("State: %s\n", serviceState)
|
||||||
|
if serviceEnabled {
|
||||||
|
fmt.Printf("Enabled: %t (Stage %d)\n", serviceEnabled, serviceStage)
|
||||||
|
} else {
|
||||||
|
fmt.Printf("Enabled: %t\n", serviceEnabled)
|
||||||
|
}
|
||||||
|
if serviceState == "running" {
|
||||||
|
fmt.Printf("Process ID: %d\n", processID)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func listAllServices() {
|
||||||
|
// Get flags
|
||||||
|
printJson, _ := currentFlagSet.GetBool("json")
|
||||||
|
|
||||||
|
type ServiceCommandJsonStruct struct {
|
||||||
|
Command string `json:"command"`
|
||||||
|
}
|
||||||
|
serviceCommandJson := ServiceCommandJsonStruct{
|
||||||
|
Command: "list",
|
||||||
|
}
|
||||||
|
|
||||||
|
// Encode struct to json string
|
||||||
|
jsonData, err := json.Marshal(serviceCommandJson)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Could not encode JSON data! Error: %s\n", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = conn.Write(jsonData)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Could not write JSON data to socket! Error: %s\n", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Read data from the connection.
|
||||||
|
data, err := readAllConn(conn)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Could not read data from socket! Error: %s\n", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Print json data if flag is set
|
||||||
|
if printJson {
|
||||||
|
fmt.Println(string(data))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Decoode JSON data
|
||||||
|
var returnedJsonData map[string]any
|
||||||
|
err = json.Unmarshal(data, &returnedJsonData)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Could not decode JSON data from connection!")
|
||||||
|
}
|
||||||
|
|
||||||
|
if err, ok := returnedJsonData["error"]; ok {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, serviceMap := range returnedJsonData["services"].([]any) {
|
||||||
|
serviceName := serviceMap.(map[string]any)["name"].(string)
|
||||||
|
serviceDescription := serviceMap.(map[string]any)["description"].(string)
|
||||||
|
serviceState := serviceMap.(map[string]any)["state"].(string)
|
||||||
|
serviceEnabled := serviceMap.(map[string]any)["is_enabled"].(bool)
|
||||||
|
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("Description: %s\n", serviceDescription)
|
||||||
|
fmt.Printf("State: %s\n", serviceState)
|
||||||
|
if serviceEnabled {
|
||||||
|
fmt.Printf("Enabled: %t (Stage %d)\n", serviceEnabled, serviceStage)
|
||||||
|
} else {
|
||||||
|
fmt.Printf("Enabled: %t\n", serviceEnabled)
|
||||||
|
}
|
||||||
|
if serviceState == "running" {
|
||||||
|
fmt.Printf("Process ID: %d\n", processID)
|
||||||
|
}
|
||||||
|
fmt.Println()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func reloadAllServices() {
|
||||||
|
// Get flags
|
||||||
|
printJson, _ := currentFlagSet.GetBool("json")
|
||||||
|
|
||||||
|
type ServiceCommandJsonStruct struct {
|
||||||
|
Command string `json:"command"`
|
||||||
|
Service string `json:"service"`
|
||||||
|
}
|
||||||
|
serviceCommandJson := ServiceCommandJsonStruct{
|
||||||
|
Command: "reload",
|
||||||
|
}
|
||||||
|
|
||||||
|
// Encode struct to json string
|
||||||
|
jsonData, err := json.Marshal(serviceCommandJson)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Could not encode JSON data! Error: %s\n", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = conn.Write(jsonData)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Could not write JSON data to socket! Error: %s\n", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Read data from the connection.
|
||||||
|
data, err := readAllConn(conn)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Could not read data from socket! Error: %s\n", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Print json data if flag is set
|
||||||
|
if printJson {
|
||||||
|
fmt.Println(string(data))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Decoode JSON data
|
||||||
|
var returnedJsonData map[string]any
|
||||||
|
err = json.Unmarshal(data, &returnedJsonData)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Could not decode JSON data from connection!")
|
||||||
|
}
|
||||||
|
|
||||||
|
if err, ok := returnedJsonData["error"]; ok {
|
||||||
|
log.Fatal(err)
|
||||||
|
} else if msg, ok := returnedJsonData["success"]; ok {
|
||||||
|
fmt.Println(msg)
|
||||||
|
} else {
|
||||||
|
log.Fatal("Connection returned empty string!")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func printSvUsage() {
|
||||||
|
fmt.Printf("Usage: ectl %s <subcommand> [options] [service]\n", os.Args[1])
|
||||||
|
fmt.Println("Description: Manage system services")
|
||||||
|
fmt.Println("Sucommands:")
|
||||||
|
fmt.Println(" start Start service")
|
||||||
|
fmt.Println(" stop Stop service")
|
||||||
|
fmt.Println(" restart Restart service")
|
||||||
|
fmt.Println(" enable Enable service")
|
||||||
|
fmt.Println(" disable Disable service")
|
||||||
|
fmt.Println(" status Show service status")
|
||||||
|
fmt.Println(" list List services")
|
||||||
|
fmt.Println(" reload Reload services")
|
||||||
|
}
|
||||||
|
|
||||||
|
func setupFlagsAndHelp(flagset *flag.FlagSet, usage, desc string, args []string) {
|
||||||
|
flagset.Usage = func() {
|
||||||
|
fmt.Println("Usage: " + usage)
|
||||||
|
fmt.Println("Description: " + desc)
|
||||||
|
fmt.Println("Options:")
|
||||||
|
if !flagset.HasFlags() {
|
||||||
|
fmt.Println(" No flags defined")
|
||||||
|
}
|
||||||
|
flagset.PrintDefaults()
|
||||||
|
}
|
||||||
|
flagset.Parse(args)
|
||||||
|
}
|
||||||
|
|
||||||
|
func dialSocket() {
|
||||||
|
if _, err := os.Stat(path.Join(runstatedir, "esvm/esvm.sock")); err != nil {
|
||||||
|
log.Fatalf("Could not find esvm.sock! Error: %s\n", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
var err error
|
||||||
|
conn, err = net.Dial("unix", path.Join(runstatedir, "esvm/esvm.sock"))
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Failed to connect to esvm.sock! Error: %s\n", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := conn.SetDeadline(time.Now().Add(30 * time.Second)); err != nil {
|
||||||
|
log.Fatalf("Failed to set write deadline! Error: %s\n", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func readAllConn(conn net.Conn) ([]byte, error) {
|
||||||
|
var buf bytes.Buffer
|
||||||
|
|
||||||
|
for {
|
||||||
|
dataChunk := make([]byte, 1024)
|
||||||
|
|
||||||
|
n, err := conn.Read(dataChunk)
|
||||||
|
if err != nil && err != io.EOF {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
buf.Write(dataChunk[:n])
|
||||||
|
|
||||||
|
if n < 1024 {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return buf.Bytes(), nil
|
||||||
|
}
|
||||||
@@ -0,0 +1,88 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
"path"
|
||||||
|
"slices"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"gopkg.in/yaml.v3"
|
||||||
|
)
|
||||||
|
|
||||||
|
func isServiceEnabled(service string) (bool, int) {
|
||||||
|
for stage, services := range readEnabledServices() {
|
||||||
|
if slices.Contains(services, service) {
|
||||||
|
return true, stage
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false, 0
|
||||||
|
}
|
||||||
|
|
||||||
|
func setServiceEnabled(service string, stage int) error {
|
||||||
|
// Get current service enabled status
|
||||||
|
_, s := isServiceEnabled(service)
|
||||||
|
|
||||||
|
// Return if service is already in correct state
|
||||||
|
if s == stage {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
EnabledServices := readEnabledServices()
|
||||||
|
|
||||||
|
// Remove service from current stage
|
||||||
|
EnabledServices[s] = slices.DeleteFunc(EnabledServices[s], func(name string) bool {
|
||||||
|
return name == service
|
||||||
|
})
|
||||||
|
if len(EnabledServices[s]) == 0 {
|
||||||
|
delete(EnabledServices, s)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add service to stage
|
||||||
|
if stage != 0 {
|
||||||
|
EnabledServices[stage] = append(EnabledServices[stage], service)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Save enabled services to file
|
||||||
|
data, err := yaml.Marshal(EnabledServices)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
err = os.WriteFile(path.Join(sysconfdir, "esvm/enabled-services.yml"), data, 0644)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func readEnabledServices() (EnabledServices map[int][]string) {
|
||||||
|
EnabledServices = make(map[int][]string)
|
||||||
|
|
||||||
|
data, err := os.ReadFile(path.Join(sysconfdir, "esvm/enabled-services.yml"))
|
||||||
|
if err != nil {
|
||||||
|
return EnabledServices
|
||||||
|
}
|
||||||
|
|
||||||
|
err = yaml.Unmarshal(data, &EnabledServices)
|
||||||
|
if err != nil {
|
||||||
|
// Assume old plain text format
|
||||||
|
for _, service := range strings.Split(strings.TrimSpace(string(data)), "\n") {
|
||||||
|
EnabledServices[3] = append(EnabledServices[3], service)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update enabled-services.yml file
|
||||||
|
data, err := yaml.Marshal(EnabledServices)
|
||||||
|
if err != nil {
|
||||||
|
return EnabledServices
|
||||||
|
}
|
||||||
|
err = os.WriteFile(path.Join(sysconfdir, "esvm/enabled-services.yml"), data, 0644)
|
||||||
|
if err != nil {
|
||||||
|
return EnabledServices
|
||||||
|
}
|
||||||
|
|
||||||
|
return EnabledServices
|
||||||
|
}
|
||||||
|
|
||||||
|
return EnabledServices
|
||||||
|
}
|
||||||
+4
-1
@@ -2,4 +2,7 @@ module enit
|
|||||||
|
|
||||||
go 1.23.4
|
go 1.23.4
|
||||||
|
|
||||||
require golang.org/x/sys v0.31.0
|
require (
|
||||||
|
github.com/mitchellh/go-ps v1.0.0
|
||||||
|
golang.org/x/sys v0.31.0
|
||||||
|
)
|
||||||
|
|||||||
@@ -1,2 +1,4 @@
|
|||||||
|
github.com/mitchellh/go-ps v1.0.0 h1:i6ampVEEF4wQFF+bkYfwYgY+F/uYJDktmvLPf7qIgjc=
|
||||||
|
github.com/mitchellh/go-ps v1.0.0/go.mod h1:J4lOc8z8yJs6vUwklHw2XEIiT4z4C40KtWVN3nvg8Pg=
|
||||||
golang.org/x/sys v0.31.0 h1:ioabZlmFYtWhL+TRYpcnNlLwhyxaM9kWTDEmfnprqik=
|
golang.org/x/sys v0.31.0 h1:ioabZlmFYtWhL+TRYpcnNlLwhyxaM9kWTDEmfnprqik=
|
||||||
golang.org/x/sys v0.31.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
|
golang.org/x/sys v0.31.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
|
||||||
|
|||||||
+68
-16
@@ -1,6 +1,7 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bufio"
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
@@ -12,6 +13,8 @@ import (
|
|||||||
"syscall"
|
"syscall"
|
||||||
"time"
|
"time"
|
||||||
"unsafe"
|
"unsafe"
|
||||||
|
|
||||||
|
"github.com/mitchellh/go-ps"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Build-time variables
|
// Build-time variables
|
||||||
@@ -78,42 +81,42 @@ func mountVirtualFilesystems() {
|
|||||||
|
|
||||||
// Mount /proc
|
// Mount /proc
|
||||||
if err := mount("proc", "/proc", "proc", commonOptions+",nodev,noexec", false); err != nil {
|
if err := mount("proc", "/proc", "proc", commonOptions+",nodev,noexec", false); err != nil {
|
||||||
panic(err)
|
printErrorAndReboot("Error: could not mount /proc: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Mount /sys
|
// Mount /sys
|
||||||
if err := mount("sys", "/sys", "sysfs", commonOptions+",nodev,noexec", false); err != nil {
|
if err := mount("sys", "/sys", "sysfs", commonOptions+",nodev,noexec", false); err != nil {
|
||||||
panic(err)
|
printErrorAndReboot("Error: could not mount /sys: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Mount /dev
|
// Mount /dev
|
||||||
if err := mount("dev", "/dev", "devtmpfs", commonOptions+",mode=755,inode64", false); err != nil {
|
if err := mount("dev", "/dev", "devtmpfs", commonOptions+",mode=755,inode64", false); err != nil {
|
||||||
panic(err)
|
printErrorAndReboot("Error: could not mount /dev: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Mount /run
|
// Mount /run
|
||||||
if err := mount("run", "/run", "tmpfs", commonOptions+",nodev,mode=755,inode64", false); err != nil {
|
if err := mount("run", "/run", "tmpfs", commonOptions+",nodev,mode=755,inode64", false); err != nil {
|
||||||
panic(err)
|
printErrorAndReboot("Error: could not mount /run: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Mount /dev/pts
|
// Mount /dev/pts
|
||||||
if err := mount("devpts", "/dev/pts", "devpts", commonOptions+",gid=5,mode=620,ptmxmode=000", true); err != nil {
|
if err := mount("devpts", "/dev/pts", "devpts", commonOptions+",gid=5,mode=620,ptmxmode=000", true); err != nil {
|
||||||
panic(err)
|
printErrorAndReboot("Error: could not mount /dev/pts: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Mount /dev/shm
|
// Mount /dev/shm
|
||||||
if err := mount("shm", "/dev/shm", "tmpfs", commonOptions+",nodev,inode64", true); err != nil {
|
if err := mount("shm", "/dev/shm", "tmpfs", commonOptions+",nodev,inode64", true); err != nil {
|
||||||
panic(err)
|
printErrorAndReboot("Error: could not mount /dev/shm: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Mount securityfs
|
// Mount securityfs
|
||||||
if err := mount("securityfs", "/sys/kernel/security", "securityfs", commonOptions, false); err != nil {
|
if err := mount("securityfs", "/sys/kernel/security", "securityfs", commonOptions, false); err != nil {
|
||||||
panic(err)
|
printErrorAndReboot("Error: could not mount /sys/kernel/security: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Mount cgroups v2
|
// Mount cgroups v2
|
||||||
if err := mount("cgroup2", "/sys/fs/cgroup", "cgroup2", commonOptions+",noexec,nsdelegate,memory_recursiveprot", false); err != nil {
|
if err := mount("cgroup2", "/sys/fs/cgroup", "cgroup2", commonOptions+",noexec,nsdelegate,memory_recursiveprot", false); err != nil {
|
||||||
panic(err)
|
printErrorAndReboot("Error: could not mount /sys/fs/cgroup: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Println("Done.")
|
fmt.Println("Done.")
|
||||||
@@ -122,9 +125,8 @@ func mountVirtualFilesystems() {
|
|||||||
func mountFilesystems() {
|
func mountFilesystems() {
|
||||||
fmt.Print("Mounting fstab entries... ")
|
fmt.Print("Mounting fstab entries... ")
|
||||||
|
|
||||||
if err := mountFstabEntries(); err != nil {
|
if err, line := mountFstabEntries(); err != nil {
|
||||||
log.Println("Could not mount fstab entries!")
|
printErrorAndReboot("Error: could not mount fstab entry on line %d: %s", line, err)
|
||||||
panic(err)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Println("Done.")
|
fmt.Println("Done.")
|
||||||
@@ -138,8 +140,7 @@ func startServiceManager() {
|
|||||||
cmd.Stderr = os.Stderr
|
cmd.Stderr = os.Stderr
|
||||||
err := cmd.Start()
|
err := cmd.Start()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("Could not initialize service manager!")
|
printErrorAndReboot("Error: could not initialize service manager: %s", err)
|
||||||
panic(err)
|
|
||||||
}
|
}
|
||||||
serviceManagerPid = cmd.Process.Pid
|
serviceManagerPid = cmd.Process.Pid
|
||||||
|
|
||||||
@@ -174,7 +175,7 @@ func stopServiceManager() {
|
|||||||
case <-exited:
|
case <-exited:
|
||||||
fmt.Println("Done.")
|
fmt.Println("Done.")
|
||||||
return
|
return
|
||||||
case <-time.After(60 * time.Second):
|
case <-time.After(300 * time.Second):
|
||||||
log.Println("Could not stop service manager!")
|
log.Println("Could not stop service manager!")
|
||||||
syscall.Kill(serviceManagerPid, syscall.SIGKILL)
|
syscall.Kill(serviceManagerPid, syscall.SIGKILL)
|
||||||
return
|
return
|
||||||
@@ -185,6 +186,42 @@ func stopServiceManager() {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func killProcesses() {
|
||||||
|
fmt.Print("Killing processes... ")
|
||||||
|
|
||||||
|
// Send sigterm to all processes
|
||||||
|
processes, err := ps.Processes()
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
for _, process := range processes {
|
||||||
|
sid, _, _ := syscall.Syscall(syscall.SYS_GETSID, uintptr(process.Pid()), 0, 0)
|
||||||
|
if process.Pid() == 1 || sid == 1 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
syscall.Kill(process.Pid(), syscall.SIGTERM)
|
||||||
|
}
|
||||||
|
|
||||||
|
time.Sleep(1 * time.Second)
|
||||||
|
|
||||||
|
// Send sigkill to remaining processes
|
||||||
|
processes, err = ps.Processes()
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
for _, process := range processes {
|
||||||
|
sid, _, _ := syscall.Syscall(syscall.SYS_GETSID, uintptr(process.Pid()), 0, 0)
|
||||||
|
if process.Pid() == 1 || sid == 1 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
syscall.Kill(process.Pid(), syscall.SIGKILL)
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println("Done.")
|
||||||
|
}
|
||||||
|
|
||||||
func setHostname() {
|
func setHostname() {
|
||||||
fmt.Print("Setting hostname... ")
|
fmt.Print("Setting hostname... ")
|
||||||
|
|
||||||
@@ -233,9 +270,13 @@ func shutdownSystem() {
|
|||||||
fmt.Println("Shutting down...")
|
fmt.Println("Shutting down...")
|
||||||
|
|
||||||
stopServiceManager()
|
stopServiceManager()
|
||||||
|
killProcesses()
|
||||||
|
unmountFilesystems()
|
||||||
|
remountRootReadonly()
|
||||||
|
|
||||||
fmt.Println("Syncing disks...")
|
fmt.Print("Syncing disks... ")
|
||||||
syscall.Sync()
|
syscall.Sync()
|
||||||
|
fmt.Println("Done.")
|
||||||
|
|
||||||
fmt.Println("Sending shutdown syscall...")
|
fmt.Println("Sending shutdown syscall...")
|
||||||
err := syscall.Reboot(syscall.LINUX_REBOOT_CMD_POWER_OFF)
|
err := syscall.Reboot(syscall.LINUX_REBOOT_CMD_POWER_OFF)
|
||||||
@@ -248,9 +289,13 @@ func rebootSystem() {
|
|||||||
fmt.Println("Rebooting...")
|
fmt.Println("Rebooting...")
|
||||||
|
|
||||||
stopServiceManager()
|
stopServiceManager()
|
||||||
|
killProcesses()
|
||||||
|
unmountFilesystems()
|
||||||
|
remountRootReadonly()
|
||||||
|
|
||||||
fmt.Println("Syncing disks...")
|
fmt.Print("Syncing disks... ")
|
||||||
syscall.Sync()
|
syscall.Sync()
|
||||||
|
fmt.Println("Done.")
|
||||||
|
|
||||||
fmt.Println("Sending reboot syscall...")
|
fmt.Println("Sending reboot syscall...")
|
||||||
err := syscall.Reboot(syscall.LINUX_REBOOT_CMD_RESTART)
|
err := syscall.Reboot(syscall.LINUX_REBOOT_CMD_RESTART)
|
||||||
@@ -258,3 +303,10 @@ func rebootSystem() {
|
|||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func printErrorAndReboot(format string, v ...any) {
|
||||||
|
log.Printf(format, v...)
|
||||||
|
fmt.Println("Press 'Enter' to reboot...")
|
||||||
|
bufio.NewReader(os.Stdin).ReadBytes('\n')
|
||||||
|
rebootSystem()
|
||||||
|
}
|
||||||
|
|||||||
+199
-18
@@ -1,10 +1,14 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"slices"
|
"slices"
|
||||||
"strings"
|
"strings"
|
||||||
|
"syscall"
|
||||||
|
"time"
|
||||||
"unsafe"
|
"unsafe"
|
||||||
|
|
||||||
"golang.org/x/sys/unix"
|
"golang.org/x/sys/unix"
|
||||||
@@ -28,12 +32,14 @@ var flagsEquivalence = map[string]uintptr{
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Split string flags to mount flags and mount data
|
// Split string flags to mount flags and mount data
|
||||||
func convertMountOptions(options string) (flags []uintptr, data string) {
|
func convertMountOptions(options string) (flags []uintptr, data string, extra []string) {
|
||||||
for _, flag := range strings.Split(options, ",") {
|
for _, flag := range strings.Split(options, ",") {
|
||||||
if unixFlag, ok := flagsEquivalence[flag]; ok {
|
if unixFlag, ok := flagsEquivalence[flag]; ok {
|
||||||
flags = append(flags, unixFlag)
|
flags = append(flags, unixFlag)
|
||||||
} else {
|
} else {
|
||||||
if data == "" {
|
if flag == "noauto" || flag == "nofail" {
|
||||||
|
extra = append(extra, flag)
|
||||||
|
} else if data == "" {
|
||||||
data = flag
|
data = flag
|
||||||
} else {
|
} else {
|
||||||
data += "," + flag
|
data += "," + flag
|
||||||
@@ -41,7 +47,7 @@ func convertMountOptions(options string) (flags []uintptr, data string) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return flags, data
|
return flags, data, extra
|
||||||
}
|
}
|
||||||
|
|
||||||
// Combine a unix flag slice or array into a single uintptr
|
// Combine a unix flag slice or array into a single uintptr
|
||||||
@@ -84,7 +90,7 @@ func isMountpoint(mountpoint string) bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func mount(source, target, fstype string, options string, mkdir bool) error {
|
func mount(source, target, fstype string, options string, mkdir bool) error {
|
||||||
flags, data := convertMountOptions(options)
|
flags, data, _ := convertMountOptions(options)
|
||||||
|
|
||||||
if isMountpoint(target) && !slices.Contains(flags, unix.MS_REMOUNT) {
|
if isMountpoint(target) && !slices.Contains(flags, unix.MS_REMOUNT) {
|
||||||
flags = append(flags, unix.MS_REMOUNT)
|
flags = append(flags, unix.MS_REMOUNT)
|
||||||
@@ -104,34 +110,68 @@ func mount(source, target, fstype string, options string, mkdir bool) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func mountFstabEntries() error {
|
func mountFstabEntries() (error, int) {
|
||||||
if _, err := os.Stat("/etc/fstab"); os.IsNotExist(err) {
|
if _, err := os.Stat("/etc/fstab"); os.IsNotExist(err) {
|
||||||
return nil
|
return nil, 0
|
||||||
} else if err != nil {
|
} else if err != nil {
|
||||||
return err
|
return err, 0
|
||||||
}
|
}
|
||||||
|
|
||||||
bytes, err := os.ReadFile("/etc/fstab")
|
bytes, err := os.ReadFile("/etc/fstab")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err, 0
|
||||||
}
|
}
|
||||||
|
|
||||||
swapPriority := -2
|
swapPriority := -2
|
||||||
|
|
||||||
for _, line := range strings.Split(string(bytes), "\n") {
|
for i, line := range strings.Split(string(bytes), "\n") {
|
||||||
line = strings.TrimSpace(line)
|
line = strings.TrimSpace(line)
|
||||||
if strings.HasPrefix(line, "#") || line == "" {
|
if strings.HasPrefix(line, "#") || line == "" {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
source := strings.Split(line, " ")[0]
|
// Get fields from line
|
||||||
target := strings.Split(line, " ")[1]
|
fields := []string{}
|
||||||
fstype := strings.Split(line, " ")[2]
|
sb := &strings.Builder{}
|
||||||
options := strings.Split(line, " ")[3]
|
quoted := false
|
||||||
|
for _, r := range line {
|
||||||
|
if r == '"' {
|
||||||
|
quoted = !quoted
|
||||||
|
} else if !quoted && r == ' ' {
|
||||||
|
str := sb.String()
|
||||||
|
if len(strings.TrimSpace(str)) > 0 {
|
||||||
|
fields = append(fields, sb.String())
|
||||||
|
}
|
||||||
|
sb.Reset()
|
||||||
|
} else {
|
||||||
|
sb.WriteRune(r)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if sb.Len() > 0 {
|
||||||
|
fields = append(fields, sb.String())
|
||||||
|
}
|
||||||
|
if len(fields) < 4 {
|
||||||
|
return fmt.Errorf("Not enough fields"), i + 1
|
||||||
|
}
|
||||||
|
source := fields[0]
|
||||||
|
target := fields[1]
|
||||||
|
fstype := fields[2]
|
||||||
|
options := fields[3]
|
||||||
|
|
||||||
flags, data := convertMountOptions(options)
|
// Replace device prefixes
|
||||||
|
if cutSource, ok := strings.CutPrefix(source, "LABEL="); ok {
|
||||||
|
source = "/dev/disk/by-label/" + strings.ReplaceAll(cutSource, " ", "\\x20")
|
||||||
|
} else if cutSource, ok := strings.CutPrefix(source, "UUID="); ok {
|
||||||
|
source = "/dev/disk/by-uuid/" + cutSource
|
||||||
|
} else if cutSource, ok := strings.CutPrefix(source, "PARTLABEL="); ok {
|
||||||
|
source = "/dev/disk/by-partlabel/" + cutSource
|
||||||
|
} else if cutSource, ok := strings.CutPrefix(source, "PARTUUID="); ok {
|
||||||
|
source = "/dev/disk/by-partuuid/" + cutSource
|
||||||
|
}
|
||||||
|
|
||||||
if slices.Contains(strings.Split(data, ","), "noauto") {
|
flags, data, extra := convertMountOptions(options)
|
||||||
|
|
||||||
|
if slices.Contains(extra, "noauto") {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -142,7 +182,11 @@ func mountFstabEntries() error {
|
|||||||
_, _, err := unix.Syscall(unix.SYS_SWAPON, uintptr(unsafe.Pointer(&b[0])), uintptr((swapPriority<<SwapFlagPrioShift)&SwapFlagPrioMask), 0)
|
_, _, err := unix.Syscall(unix.SYS_SWAPON, uintptr(unsafe.Pointer(&b[0])), uintptr((swapPriority<<SwapFlagPrioShift)&SwapFlagPrioMask), 0)
|
||||||
swapPriority--
|
swapPriority--
|
||||||
if err != 0 {
|
if err != 0 {
|
||||||
return fmt.Errorf("swapon syscall returned none-zero error code: %d", err)
|
if slices.Contains(extra, "nofail") {
|
||||||
|
fmt.Printf("Warning: could not mount fstab entry on line %d: swapon syscall returned non-zero exit code: %d\n", i+1, err)
|
||||||
|
} else {
|
||||||
|
return fmt.Errorf("swapon syscall returned non-zero exit code: %d", err), i + 1
|
||||||
|
}
|
||||||
}
|
}
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
@@ -152,9 +196,146 @@ func mountFstabEntries() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if err := unix.Mount(source, target, fstype, combineUnixFlags(flags), data); err != nil {
|
if err := unix.Mount(source, target, fstype, combineUnixFlags(flags), data); err != nil {
|
||||||
return err
|
if slices.Contains(extra, "nofail") {
|
||||||
|
log.Printf("Warning: could not mount fstab entry on line %d: %s\n", i+1, err)
|
||||||
|
} else {
|
||||||
|
return err, i + 1
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil, 0
|
||||||
|
}
|
||||||
|
|
||||||
|
func unmountFilesystems() {
|
||||||
|
// Disable all swap memory
|
||||||
|
data, err := os.ReadFile("/proc/swaps")
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
for i, entry := range strings.Split(string(data), "\n") {
|
||||||
|
if i == 0 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
entry = strings.TrimSpace(entry)
|
||||||
|
if len(entry) == 0 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
mountpoint := strings.Fields(entry)[0]
|
||||||
|
|
||||||
|
// Unmount swap at mountpoint
|
||||||
|
fmt.Printf("Disabling swap at %s... ", mountpoint)
|
||||||
|
b := append([]byte(mountpoint), 0)
|
||||||
|
_, _, err := unix.Syscall(unix.SYS_SWAPOFF, uintptr(unsafe.Pointer(&b[0])), 0, 0)
|
||||||
|
if err == 0 {
|
||||||
|
fmt.Println("Done.")
|
||||||
|
} else {
|
||||||
|
fmt.Printf("Error: %s\n", err.Error())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
data, err = os.ReadFile("/proc/self/mountinfo")
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Unmount filesystems
|
||||||
|
entries := strings.Split(string(data), "\n")
|
||||||
|
slices.Reverse(entries)
|
||||||
|
for _, entry := range entries {
|
||||||
|
entry = strings.TrimSpace(entry)
|
||||||
|
if len(entry) == 0 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get entry fields
|
||||||
|
fields := strings.Fields(entry)
|
||||||
|
mountpoint := fields[4]
|
||||||
|
filesystem := ""
|
||||||
|
for i := 6; i < len(fields); i++ {
|
||||||
|
if fields[i] == "-" {
|
||||||
|
filesystem = fields[i+1]
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Skip root filesystem
|
||||||
|
if mountpoint == "/" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// Skip root and ignored filesystems
|
||||||
|
ignoredFilesystems := []string{
|
||||||
|
"devtmpfs",
|
||||||
|
"proc",
|
||||||
|
"sysfs",
|
||||||
|
"tmpfs",
|
||||||
|
}
|
||||||
|
|
||||||
|
if slices.Contains(ignoredFilesystems, filesystem) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// Unmount filesystem at mountpoint
|
||||||
|
fmt.Printf("Unmounting %s...", mountpoint)
|
||||||
|
err := unix.Unmount(mountpoint, 0)
|
||||||
|
if errors.Is(err, syscall.EBUSY) {
|
||||||
|
fmt.Println(" Busy.")
|
||||||
|
time.Sleep(1 * time.Second)
|
||||||
|
} else if err != nil {
|
||||||
|
fmt.Printf(" Error: %s\n", err.Error())
|
||||||
|
} else {
|
||||||
|
fmt.Println(" Done.")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func remountRootReadonly() {
|
||||||
|
fmt.Print("Remounting root as read-only...")
|
||||||
|
|
||||||
|
data, err := os.ReadFile("/proc/self/mountinfo")
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
filesystem := ""
|
||||||
|
source := ""
|
||||||
|
fsData := ""
|
||||||
|
|
||||||
|
// Get root filesystems
|
||||||
|
entries := strings.Split(string(data), "\n")
|
||||||
|
slices.Reverse(entries)
|
||||||
|
for _, entry := range entries {
|
||||||
|
entry = strings.TrimSpace(entry)
|
||||||
|
if len(entry) == 0 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get entry fields
|
||||||
|
fields := strings.Fields(entry)
|
||||||
|
mountpoint := fields[4]
|
||||||
|
for i := 6; i < len(fields); i++ {
|
||||||
|
if fields[i] == "-" {
|
||||||
|
filesystem = fields[i+1]
|
||||||
|
source = fields[i+2]
|
||||||
|
fsData = fields[i+3]
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if mountpoint == "/" {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
err = unix.Mount(source, "/", filesystem, syscall.MS_RDONLY|syscall.MS_REMOUNT, fsData)
|
||||||
|
if errors.Is(err, syscall.EBUSY) {
|
||||||
|
fmt.Println(" Busy.")
|
||||||
|
} else if err != nil {
|
||||||
|
fmt.Printf(" Error: %s\n", err.Error())
|
||||||
|
} else {
|
||||||
|
fmt.Println(" Done.")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+22
-63
@@ -1,7 +1,6 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"crypto/sha256"
|
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
@@ -15,8 +14,6 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"syscall"
|
"syscall"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"gopkg.in/yaml.v3"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Build-time variables
|
// Build-time variables
|
||||||
@@ -136,64 +133,8 @@ func Init() {
|
|||||||
// Read and initialize service files
|
// Read and initialize service files
|
||||||
for _, entry := range dirEntries {
|
for _, entry := range dirEntries {
|
||||||
if !entry.IsDir() && strings.HasSuffix(entry.Name(), ".esv") {
|
if !entry.IsDir() && strings.HasSuffix(entry.Name(), ".esv") {
|
||||||
logger.Printf("Initializing service (%s)...\n", entry.Name())
|
filepath := path.Join(serviceConfigDir, "services", entry.Name())
|
||||||
bytes, err := os.ReadFile(path.Join(serviceConfigDir, "services", entry.Name()))
|
LoadService(filepath)
|
||||||
if err != nil {
|
|
||||||
logger.Printf("Error: Could not read service file (%s)", path.Join(serviceConfigDir, "services", entry.Name()))
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
service := EnitService{
|
|
||||||
Name: "",
|
|
||||||
Description: "",
|
|
||||||
Type: "",
|
|
||||||
StartCmd: "",
|
|
||||||
ExitMethod: "",
|
|
||||||
StopCmd: "",
|
|
||||||
Restart: "",
|
|
||||||
Setpgid: true,
|
|
||||||
CrashOnSafeExit: true,
|
|
||||||
LogOutput: true,
|
|
||||||
Filepath: path.Join(serviceConfigDir, "services", entry.Name()),
|
|
||||||
filepathChecksum: sha256.Sum256(bytes),
|
|
||||||
restartCount: 0,
|
|
||||||
stopChannel: make(chan bool),
|
|
||||||
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()))
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, sv := range Services {
|
|
||||||
if sv.Name == service.Name {
|
|
||||||
logger.Printf("Error: service with name (%s) has already been initialized", service.Name)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
switch service.Type {
|
|
||||||
case "simple", "background":
|
|
||||||
default:
|
|
||||||
logger.Printf("Error: unknown service type (%s)", service.Type)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
switch service.ExitMethod {
|
|
||||||
case "stop_command", "kill":
|
|
||||||
default:
|
|
||||||
logger.Printf("Error: unknown exit method (%s)\n", service.ExitMethod)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
switch service.Restart {
|
|
||||||
case "true", "always":
|
|
||||||
default:
|
|
||||||
service.Restart = "false"
|
|
||||||
}
|
|
||||||
|
|
||||||
Services = append(Services, &service)
|
|
||||||
|
|
||||||
logger.Printf("Service (%s) has been initialized!\n", service.Name)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -231,8 +172,26 @@ func Init() {
|
|||||||
func Reload() {
|
func Reload() {
|
||||||
logger.Println("Reloading all ESVM services...")
|
logger.Println("Reloading all ESVM services...")
|
||||||
|
|
||||||
for _, service := range Services {
|
dirEntries, err := os.ReadDir(path.Join(serviceConfigDir, "services"))
|
||||||
service.ReloadService()
|
if err != nil {
|
||||||
|
logger.Fatalf("Error: Could not initialize ESVM: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Read and load service files
|
||||||
|
servicesToRemove := slices.Clone(Services)
|
||||||
|
for _, entry := range dirEntries {
|
||||||
|
if !entry.IsDir() && strings.HasSuffix(entry.Name(), ".esv") {
|
||||||
|
filepath := path.Join(serviceConfigDir, "services", entry.Name())
|
||||||
|
LoadService(filepath)
|
||||||
|
servicesToRemove = slices.DeleteFunc(servicesToRemove, func(sv *EnitService) bool {
|
||||||
|
return sv.Filepath == filepath
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Reload services that had their esv file removed
|
||||||
|
for _, service := range servicesToRemove {
|
||||||
|
LoadService(service.Filepath)
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.Println("All ESVM services have been reloaded!")
|
logger.Println("All ESVM services have been reloaded!")
|
||||||
|
|||||||
+132
-107
@@ -6,8 +6,10 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
|
"os/user"
|
||||||
"path"
|
"path"
|
||||||
"slices"
|
"slices"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"syscall"
|
"syscall"
|
||||||
"time"
|
"time"
|
||||||
@@ -42,9 +44,9 @@ type EnitService struct {
|
|||||||
Description string `yaml:"description,omitempty"`
|
Description string `yaml:"description,omitempty"`
|
||||||
Type string `yaml:"type"`
|
Type string `yaml:"type"`
|
||||||
StartCmd string `yaml:"start_cmd"`
|
StartCmd string `yaml:"start_cmd"`
|
||||||
ExitMethod string `yaml:"exit_method"`
|
|
||||||
CrashOnSafeExit bool `yaml:"crash_on_safe_exit"`
|
CrashOnSafeExit bool `yaml:"crash_on_safe_exit"`
|
||||||
StopCmd string `yaml:"stop_cmd,omitempty"`
|
StopCmd string `yaml:"stop_cmd,omitempty"`
|
||||||
|
User string `yaml:"user,omitempty"`
|
||||||
Restart string `yaml:"restart,omitempty"`
|
Restart string `yaml:"restart,omitempty"`
|
||||||
ReadyFd int `yaml:"ready_fd"`
|
ReadyFd int `yaml:"ready_fd"`
|
||||||
Setpgid bool `yaml:"setpgid"`
|
Setpgid bool `yaml:"setpgid"`
|
||||||
@@ -101,9 +103,18 @@ func (service *EnitService) GetLogFile() (file *os.File, err error) {
|
|||||||
return file, nil
|
return file, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (service *EnitService) ReloadService() {
|
func LoadService(filepath string) {
|
||||||
bytes, err := os.ReadFile(service.Filepath)
|
bytes, err := os.ReadFile(filepath)
|
||||||
checksum := sha256.Sum256(bytes)
|
checksum := sha256.Sum256(bytes)
|
||||||
|
|
||||||
|
var serviceToReload *EnitService
|
||||||
|
|
||||||
|
// Check if service is already loaded
|
||||||
|
for _, service := range Services {
|
||||||
|
if service.Filepath != filepath {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
if slices.Equal(checksum[:], service.filepathChecksum[:]) {
|
if slices.Equal(checksum[:], service.filepathChecksum[:]) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -114,17 +125,29 @@ func (service *EnitService) ReloadService() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
service.shouldReload = false
|
service.shouldReload = false
|
||||||
|
serviceToReload = service
|
||||||
|
|
||||||
logger.Printf("Reloading service (%s)...\n", service.Filepath)
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
if serviceToReload == nil {
|
||||||
|
logger.Printf("Loading service (%s)...\n", filepath)
|
||||||
|
} else {
|
||||||
|
logger.Printf("Reloading service (%s)...\n", filepath)
|
||||||
|
}
|
||||||
|
|
||||||
if os.IsNotExist(err) {
|
if os.IsNotExist(err) {
|
||||||
Services = slices.DeleteFunc(Services, func(sv *EnitService) bool {
|
Services = slices.DeleteFunc(Services, func(sv *EnitService) bool {
|
||||||
return sv == service
|
if sv.Filepath == filepath {
|
||||||
|
logger.Printf("Service (%s) has been removed\n", sv.Name)
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return false
|
||||||
})
|
})
|
||||||
logger.Printf("Service (%s) has been removed\n", service.Name)
|
|
||||||
return
|
return
|
||||||
} else if err != nil {
|
} else if err != nil {
|
||||||
logger.Printf("Error: Could not read service file (%s)", service.Filepath)
|
logger.Printf("Error: Could not read service file (%s)", filepath)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -133,26 +156,32 @@ func (service *EnitService) ReloadService() {
|
|||||||
Description: "",
|
Description: "",
|
||||||
Type: "",
|
Type: "",
|
||||||
StartCmd: "",
|
StartCmd: "",
|
||||||
ExitMethod: "",
|
|
||||||
StopCmd: "",
|
StopCmd: "",
|
||||||
|
User: "",
|
||||||
Restart: "",
|
Restart: "",
|
||||||
Setpgid: true,
|
Setpgid: true,
|
||||||
CrashOnSafeExit: true,
|
CrashOnSafeExit: true,
|
||||||
LogOutput: true,
|
LogOutput: true,
|
||||||
Filepath: service.Filepath,
|
Filepath: filepath,
|
||||||
filepathChecksum: checksum,
|
filepathChecksum: sha256.Sum256(bytes),
|
||||||
restartCount: service.restartCount,
|
restartCount: 0,
|
||||||
stopChannel: service.stopChannel,
|
stopChannel: make(chan bool),
|
||||||
state: service.state,
|
state: EnitServiceUnloaded,
|
||||||
|
}
|
||||||
|
if serviceToReload != nil {
|
||||||
|
newService.restartCount = serviceToReload.restartCount
|
||||||
|
newService.stopChannel = serviceToReload.stopChannel
|
||||||
|
newService.state = serviceToReload.state
|
||||||
}
|
}
|
||||||
if err := yaml.Unmarshal(bytes, &newService); err != nil {
|
if err := yaml.Unmarshal(bytes, &newService); err != nil {
|
||||||
logger.Printf("Error: could not read service file %s", service.Filepath)
|
logger.Printf("Error: could not read service file %s", filepath)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, sv := range Services {
|
for _, sv := range Services {
|
||||||
if sv.Name == newService.Name && sv != service {
|
if sv.Name == newService.Name && sv != serviceToReload {
|
||||||
logger.Printf("Error: service with name (%s) has already been initialized", service.Name)
|
logger.Printf("Error: service with name (%s) has already been loaded", newService.Name)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -163,13 +192,6 @@ func (service *EnitService) ReloadService() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
switch newService.ExitMethod {
|
|
||||||
case "stop_command", "kill":
|
|
||||||
default:
|
|
||||||
logger.Printf("Error: unknown exit method (%s)\n", newService.ExitMethod)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
switch newService.Restart {
|
switch newService.Restart {
|
||||||
case "true", "always":
|
case "true", "always":
|
||||||
default:
|
default:
|
||||||
@@ -177,12 +199,15 @@ func (service *EnitService) ReloadService() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for i, sv := range Services {
|
for i, sv := range Services {
|
||||||
if sv == service {
|
if sv == serviceToReload {
|
||||||
Services[i] = &newService
|
Services[i] = &newService
|
||||||
|
logger.Printf("Service (%s) has been reloaded!\n", newService.Name)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.Printf("Service (%s) has been reloaded!\n", newService.Name)
|
Services = append(Services, &newService)
|
||||||
|
logger.Printf("Service (%s) has been loaded!\n", newService.Name)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (service *EnitService) StartService() (err error) {
|
func (service *EnitService) StartService() (err error) {
|
||||||
@@ -206,10 +231,38 @@ func (service *EnitService) StartService() (err error) {
|
|||||||
|
|
||||||
cmd := exec.Command("/bin/sh", "-c", "exec "+service.StartCmd)
|
cmd := exec.Command("/bin/sh", "-c", "exec "+service.StartCmd)
|
||||||
cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: service.Setpgid, Pgid: 0}
|
cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: service.Setpgid, Pgid: 0}
|
||||||
|
|
||||||
|
// Setup service log file
|
||||||
if logFile != nil {
|
if logFile != nil {
|
||||||
cmd.Stdout = logFile
|
cmd.Stdout = logFile
|
||||||
cmd.Stderr = logFile
|
cmd.Stderr = logFile
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Setup command credentials
|
||||||
|
if service.User != "" && service.User != "root" {
|
||||||
|
// Lookup user in /etc/passwd
|
||||||
|
u, err := user.Lookup(service.User)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get user id and group id
|
||||||
|
uid, err := strconv.Atoi(u.Uid)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
gid, err := strconv.Atoi(u.Gid)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
cmd.SysProcAttr.Credential = &syscall.Credential{
|
||||||
|
Uid: uint32(uid),
|
||||||
|
Gid: uint32(gid),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Setup command pipes
|
||||||
var pipeReader, pipeWriter *os.File
|
var pipeReader, pipeWriter *os.File
|
||||||
if service.ReadyFd > 2 {
|
if service.ReadyFd > 2 {
|
||||||
pipeReader, pipeWriter, err = os.Pipe()
|
pipeReader, pipeWriter, err = os.Pipe()
|
||||||
@@ -237,6 +290,7 @@ func (service *EnitService) StartService() (err error) {
|
|||||||
}
|
}
|
||||||
cmd.ExtraFiles = append(cmd.ExtraFiles, pipeWriter)
|
cmd.ExtraFiles = append(cmd.ExtraFiles, pipeWriter)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := cmd.Start(); err != nil {
|
if err := cmd.Start(); err != nil {
|
||||||
// Close log file if not nil
|
// Close log file if not nil
|
||||||
if logFile != nil {
|
if logFile != nil {
|
||||||
@@ -246,6 +300,7 @@ func (service *EnitService) StartService() (err error) {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pid := cmd.Process.Pid
|
||||||
service.processID = cmd.Process.Pid
|
service.processID = cmd.Process.Pid
|
||||||
service.state = EnitServiceStarting
|
service.state = EnitServiceStarting
|
||||||
|
|
||||||
@@ -260,7 +315,7 @@ func (service *EnitService) StartService() (err error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Kill process and children
|
// Kill process and children
|
||||||
syscall.Kill(-service.processID, syscall.SIGKILL)
|
syscall.Kill(-pid, syscall.SIGKILL)
|
||||||
|
|
||||||
service.processID = 0
|
service.processID = 0
|
||||||
service.state = EnitServiceCrashed
|
service.state = EnitServiceCrashed
|
||||||
@@ -284,23 +339,18 @@ func (service *EnitService) StartService() (err error) {
|
|||||||
service.restartCount = 0
|
service.restartCount = 0
|
||||||
default:
|
default:
|
||||||
// Kill remaining child processes
|
// Kill remaining child processes
|
||||||
syscall.Kill(-service.processID, syscall.SIGKILL)
|
syscall.Kill(-pid, syscall.SIGKILL)
|
||||||
|
|
||||||
if service.Type == "simple" && err == nil {
|
if service.Type == "simple" && err == nil {
|
||||||
service.restartCount = 0
|
service.restartCount = 0
|
||||||
if service.ExitMethod != "stop_command" {
|
if strings.TrimSpace(service.StopCmd) == "" {
|
||||||
service.state = EnitServiceCompleted
|
service.state = EnitServiceCompleted
|
||||||
|
|
||||||
// Reload service if needed
|
// Reload service if needed
|
||||||
if service.shouldReload {
|
if service.shouldReload {
|
||||||
service.ReloadService()
|
LoadService(service.Filepath)
|
||||||
if GetServiceByName(service.Name) == nil {
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
service.state = EnitServiceRunning
|
|
||||||
}
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if !service.CrashOnSafeExit {
|
if !service.CrashOnSafeExit {
|
||||||
@@ -313,7 +363,7 @@ func (service *EnitService) StartService() (err error) {
|
|||||||
|
|
||||||
// Reload service if needed
|
// Reload service if needed
|
||||||
if service.shouldReload {
|
if service.shouldReload {
|
||||||
service.ReloadService()
|
LoadService(service.Filepath)
|
||||||
if GetServiceByName(service.Name) == nil {
|
if GetServiceByName(service.Name) == nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -352,14 +402,16 @@ func (service *EnitService) StopService() error {
|
|||||||
newServiceStatus := EnitServiceCrashed
|
newServiceStatus := EnitServiceCrashed
|
||||||
defer func() {
|
defer func() {
|
||||||
// Kill remaining child processes
|
// Kill remaining child processes
|
||||||
|
if pid != 0 {
|
||||||
syscall.Kill(-pid, syscall.SIGKILL)
|
syscall.Kill(-pid, syscall.SIGKILL)
|
||||||
|
}
|
||||||
|
|
||||||
service.state = newServiceStatus
|
service.state = newServiceStatus
|
||||||
service.processID = 0
|
service.processID = 0
|
||||||
|
|
||||||
// Reload service if needed
|
// Reload service if needed
|
||||||
if service.shouldReload {
|
if service.shouldReload {
|
||||||
service.ReloadService()
|
LoadService(service.Filepath)
|
||||||
if GetServiceByName(service.Name) == nil {
|
if GetServiceByName(service.Name) == nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -367,7 +419,7 @@ func (service *EnitService) StopService() error {
|
|||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
if service.ExitMethod == "kill" {
|
if strings.TrimSpace(service.StopCmd) == "" {
|
||||||
if err := service.GetProcess().Signal(syscall.Signal(0)); err != nil {
|
if err := service.GetProcess().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)
|
||||||
@@ -381,12 +433,46 @@ func (service *EnitService) StopService() error {
|
|||||||
service.GetProcess().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 {
|
||||||
|
go func() { service.stopChannel <- true }()
|
||||||
|
|
||||||
|
cmd := exec.Command("/bin/sh", "-c", service.StopCmd)
|
||||||
|
|
||||||
|
// Setup command credentials
|
||||||
|
if service.User != "" && service.User != "root" {
|
||||||
|
// Lookup user in /etc/passwd
|
||||||
|
u, err := user.Lookup(service.User)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get user id and group id
|
||||||
|
uid, err := strconv.Atoi(u.Uid)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
gid, err := strconv.Atoi(u.Gid)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
cmd.SysProcAttr.Credential = &syscall.Credential{
|
||||||
|
Uid: uint32(uid),
|
||||||
|
Gid: uint32(gid),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := cmd.Run(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if service.Type == "background" {
|
||||||
// Check if the process has stopped gracefully, otherwise send sigkill on timeout
|
// Check if the process has stopped gracefully, otherwise send sigkill on timeout
|
||||||
exited := make(chan bool)
|
exited := make(chan bool)
|
||||||
go func() {
|
go func() {
|
||||||
for {
|
for {
|
||||||
if err := service.GetProcess().Signal(syscall.Signal(0)); err != nil {
|
if err := syscall.Kill(pid, syscall.Signal(0)); err != nil {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -395,15 +481,10 @@ func (service *EnitService) StopService() error {
|
|||||||
|
|
||||||
select {
|
select {
|
||||||
case <-exited:
|
case <-exited:
|
||||||
case <-time.After(5 * time.Second):
|
case <-time.After(15 * time.Second):
|
||||||
service.GetProcess().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 {
|
|
||||||
cmd := exec.Command("/bin/sh", "-c", service.StopCmd)
|
|
||||||
if err := cmd.Run(); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
newServiceStatus = EnitServiceStopped
|
newServiceStatus = EnitServiceStopped
|
||||||
@@ -417,6 +498,12 @@ func (service *EnitService) RestartService() error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Get service from list in case of a reload
|
||||||
|
if GetServiceByName(service.Name) == nil {
|
||||||
|
return fmt.Errorf("service was removed")
|
||||||
|
}
|
||||||
|
service = GetServiceByName(service.Name)
|
||||||
|
|
||||||
if err := service.StartService(); err != nil {
|
if err := service.StartService(); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -424,78 +511,16 @@ func (service *EnitService) RestartService() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (service *EnitService) isEnabled() (bool, int) {
|
|
||||||
for stage, services := range ReadEnabledServices() {
|
|
||||||
if slices.Contains(services, service.Name) {
|
|
||||||
return true, stage
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return false, 0
|
|
||||||
}
|
|
||||||
|
|
||||||
func (service *EnitService) SetEnabled(stage int) error {
|
|
||||||
// Get current service enabled status
|
|
||||||
_, s := service.isEnabled()
|
|
||||||
|
|
||||||
// Return if service is already in correct state
|
|
||||||
if s == stage {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
EnabledServices := ReadEnabledServices()
|
|
||||||
|
|
||||||
// Remove service from current stage
|
|
||||||
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)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
err = os.WriteFile(path.Join(serviceConfigDir, "enabled_services"), data, 0644)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func ReadEnabledServices() (EnabledServices map[int][]string) {
|
func ReadEnabledServices() (EnabledServices map[int][]string) {
|
||||||
EnabledServices = make(map[int][]string)
|
EnabledServices = make(map[int][]string)
|
||||||
|
|
||||||
data, err := os.ReadFile(path.Join(serviceConfigDir, "enabled_services"))
|
data, err := os.ReadFile(path.Join(serviceConfigDir, "enabled-services.yml"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return EnabledServices
|
return EnabledServices
|
||||||
}
|
}
|
||||||
|
|
||||||
err = yaml.Unmarshal(data, &EnabledServices)
|
err = yaml.Unmarshal(data, &EnabledServices)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// Assume old plain text format
|
|
||||||
for _, service := range strings.Split(strings.TrimSpace(string(data)), "\n") {
|
|
||||||
EnabledServices[3] = append(EnabledServices[3], service)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Update enabled_services file
|
|
||||||
data, err := yaml.Marshal(EnabledServices)
|
|
||||||
if err != nil {
|
|
||||||
return EnabledServices
|
|
||||||
}
|
|
||||||
err = os.WriteFile(path.Join(serviceConfigDir, "enabled_services"), data, 0644)
|
|
||||||
if err != nil {
|
|
||||||
return EnabledServices
|
|
||||||
}
|
|
||||||
|
|
||||||
return EnabledServices
|
return EnabledServices
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -22,7 +22,6 @@ func initSocket() (socket net.Listener, err error) {
|
|||||||
commandHandlers["start"] = handleStartServiceCommand
|
commandHandlers["start"] = handleStartServiceCommand
|
||||||
commandHandlers["stop"] = handleStopServiceCommand
|
commandHandlers["stop"] = handleStopServiceCommand
|
||||||
commandHandlers["restart"] = handleRestartServiceCommand
|
commandHandlers["restart"] = handleRestartServiceCommand
|
||||||
commandHandlers["set_enabled"] = handleSetEnabledServiceCommand
|
|
||||||
commandHandlers["status"] = handleStatusServiceCommand
|
commandHandlers["status"] = handleStatusServiceCommand
|
||||||
commandHandlers["list"] = handleListServicesCommand
|
commandHandlers["list"] = handleListServicesCommand
|
||||||
|
|
||||||
@@ -151,65 +150,6 @@ func handleRestartServiceCommand(conn net.Conn, jsonData map[string]any) {
|
|||||||
conn.Write(wrapSuccessMsgInJson(fmt.Sprintf("Service (%s) has restarted sucessfully", serviceName.(string))))
|
conn.Write(wrapSuccessMsgInJson(fmt.Sprintf("Service (%s) has restarted sucessfully", serviceName.(string))))
|
||||||
}
|
}
|
||||||
|
|
||||||
func handleSetEnabledServiceCommand(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
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get service stage from json json data
|
|
||||||
_serviceStage, ok := jsonData["stage"]
|
|
||||||
if !ok {
|
|
||||||
conn.Write(wrapErrorInJson(fmt.Errorf("'stage' field missing")))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
serviceStage, ok := _serviceStage.(float64)
|
|
||||||
if !ok {
|
|
||||||
conn.Write(wrapErrorInJson(fmt.Errorf("'stage' field is not a number")))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// Ensure service exists
|
|
||||||
service := GetServiceByName(serviceName.(string))
|
|
||||||
if service == nil {
|
|
||||||
conn.Write(wrapErrorInJson(fmt.Errorf("Service (%s) not found", serviceName.(string))))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get current service enabled status
|
|
||||||
_, s := service.isEnabled()
|
|
||||||
|
|
||||||
// Return if service is already in correct state
|
|
||||||
if s == int(serviceStage) {
|
|
||||||
if serviceStage == 0 {
|
|
||||||
conn.Write(wrapSuccessMsgInJson(fmt.Sprintf("Service (%s) is already disabled", serviceName.(string))))
|
|
||||||
} else {
|
|
||||||
conn.Write(wrapSuccessMsgInJson(fmt.Sprintf("Service (%s) is already enabled", serviceName.(string))))
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// Enable service
|
|
||||||
err := service.SetEnabled(int(serviceStage))
|
|
||||||
if err != nil {
|
|
||||||
if serviceStage == 0 {
|
|
||||||
conn.Write(wrapErrorInJson(fmt.Errorf("Could not disable service! Error: %s", err)))
|
|
||||||
} else {
|
|
||||||
conn.Write(wrapErrorInJson(fmt.Errorf("Could not enable service! Error: %s", err)))
|
|
||||||
}
|
|
||||||
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) {
|
func handleStatusServiceCommand(conn net.Conn, jsonData map[string]any) {
|
||||||
// Get service name from json data
|
// Get service name from json data
|
||||||
serviceName, ok := jsonData["service"]
|
serviceName, ok := jsonData["service"]
|
||||||
@@ -230,7 +170,6 @@ func handleStatusServiceCommand(conn net.Conn, jsonData map[string]any) {
|
|||||||
statusMap["description"] = service.Description
|
statusMap["description"] = service.Description
|
||||||
statusMap["state"] = EnitServiceStateNames[service.state]
|
statusMap["state"] = EnitServiceStateNames[service.state]
|
||||||
statusMap["process_id"] = service.processID
|
statusMap["process_id"] = service.processID
|
||||||
statusMap["is_enabled"], statusMap["stage"] = service.isEnabled()
|
|
||||||
|
|
||||||
// Encode map to json string
|
// Encode map to json string
|
||||||
newJsonData, err := json.Marshal(statusMap)
|
newJsonData, err := json.Marshal(statusMap)
|
||||||
@@ -253,7 +192,6 @@ func handleListServicesCommand(conn net.Conn, _ map[string]any) {
|
|||||||
statusMap["description"] = service.Description
|
statusMap["description"] = service.Description
|
||||||
statusMap["state"] = EnitServiceStateNames[service.state]
|
statusMap["state"] = EnitServiceStateNames[service.state]
|
||||||
statusMap["process_id"] = service.processID
|
statusMap["process_id"] = service.processID
|
||||||
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