Add start/stop/restart ability to command
This commit is contained in:
parent
1598980f0e
commit
3e39875b78
63
main.go
63
main.go
|
@ -8,14 +8,14 @@ import (
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"strings"
|
"strings"
|
||||||
|
"syscall"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
startup []string
|
|
||||||
daemon = false
|
daemon = false
|
||||||
serviceFile = "/adm/services"
|
serviceFile = "/adm/services"
|
||||||
services = make(map[string]string)
|
services = make(map[string]Service)
|
||||||
controlSocket = "/adm/headless9/ctl/headless9.ctl"
|
controlSocket = "/adm/headless9/ctl/headless9.ctl"
|
||||||
logPath = "/adm/headless9/log/"
|
logPath = "/adm/headless9/log/"
|
||||||
)
|
)
|
||||||
|
@ -116,16 +116,65 @@ func headlessControls() {
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
} else {
|
} else {
|
||||||
for _, cmd := range pendingCommands {
|
for _, cmd := range pendingCommands {
|
||||||
log.Printf(DAEMON_PROCESSING_CTL, cmd)
|
log.Printf(DAEMON_CTL_PROCESSING, cmd)
|
||||||
|
start := time.Now()
|
||||||
|
err = processCommand(cmd)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf(DAEMON_CTL_FAILED, err, time.Since(start))
|
||||||
|
} else {
|
||||||
|
log.Printf(DAEMON_CTL_PROCESSED, cmd, time.Since(start))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func processCommand(cmd string) error {
|
||||||
|
parts := strings.Split(cmd, " ")
|
||||||
|
verb := parts[0]
|
||||||
|
svc := parts[1]
|
||||||
|
start := time.Now()
|
||||||
|
if verb == "restart" || verb == "stop" {
|
||||||
|
for testSvc := range services {
|
||||||
|
if svc == testSvc {
|
||||||
|
err := syscall.Kill(services[svc].ProcessHandle.Pid, 9)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf(DAEMON_CTL_FAILED, err, time.Since(start))
|
||||||
|
} else {
|
||||||
|
delete(services, svc)
|
||||||
|
log.Printf(DAEMON_CTL_PROCESSED, svc, time.Since(start))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if verb == "restart" || verb == "start" {
|
||||||
|
startup, err := readLines(serviceFile)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalln(err)
|
||||||
|
}
|
||||||
|
for _, testSvc := range startup {
|
||||||
|
svcArgs := strings.Split(testSvc, " ")
|
||||||
|
if svc != testSvc {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Printf(DAEMON_SVC_MISSING, svcArgs[0])
|
||||||
|
go execCommand(svcArgs[0], svcArgs[1:]...)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func execCommand(cmd string, arg ...string) {
|
func execCommand(cmd string, arg ...string) {
|
||||||
defer PanicSafe()
|
defer PanicSafe()
|
||||||
if len(cmd) < 2 {
|
if len(cmd) < 2 {
|
||||||
log.Printf(DAEMON_CMD_INVALID, cmd, arg)
|
log.Printf(DAEMON_SVC_INVALID, cmd, arg)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if strings.HasPrefix(cmd, "#") {
|
||||||
|
log.Printf(DAEMON_SVC_DISABLED, cmd)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
proc := exec.Command(cmd, arg...)
|
proc := exec.Command(cmd, arg...)
|
||||||
|
@ -148,7 +197,11 @@ func execCommand(cmd string, arg ...string) {
|
||||||
log.Printf(DAEMON_SVC_FAIL, cmd)
|
log.Printf(DAEMON_SVC_FAIL, cmd)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
services[cmd] = fmt.Sprint(proc.Process.Pid)
|
services[cmd] = Service{
|
||||||
|
ProcessHandle: proc.Process,
|
||||||
|
StartupArgs: arg,
|
||||||
|
StartTime: time.Now(),
|
||||||
|
}
|
||||||
proc.Wait()
|
proc.Wait()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -8,9 +8,12 @@ var (
|
||||||
CTL_NOT_CLEAR = "Unable to clear Control Socket. Please check the file %+v and that it's permissions are 700"
|
CTL_NOT_CLEAR = "Unable to clear Control Socket. Please check the file %+v and that it's permissions are 700"
|
||||||
CTL_NOT_REWOUND = "Unable to rewind Control Socket. Please check the file %+v and that it's permissions are 700"
|
CTL_NOT_REWOUND = "Unable to rewind Control Socket. Please check the file %+v and that it's permissions are 700"
|
||||||
DAEMON_FILE_REFRESH = "Refreshing controlFile %+v"
|
DAEMON_FILE_REFRESH = "Refreshing controlFile %+v"
|
||||||
|
DAEMON_SVC_INVALID = "Invalid command `%v`, skipping. Args: { %+v }"
|
||||||
|
DAEMON_SVC_DISABLED = "%+v is disabled, skipping!"
|
||||||
DAEMON_SVC_EXISTS = "%+v exists as PID %+v"
|
DAEMON_SVC_EXISTS = "%+v exists as PID %+v"
|
||||||
DAEMON_SVC_MISSING = "Svc not detected, starting: %+v"
|
|
||||||
DAEMON_PROCESSING_CTL = "Processing command: %+v"
|
|
||||||
DAEMON_CMD_INVALID = "Invalid command `%v`, skipping. Args: { %+v }"
|
|
||||||
DAEMON_SVC_FAIL = "Error starting service %+v, see log for more info."
|
DAEMON_SVC_FAIL = "Error starting service %+v, see log for more info."
|
||||||
|
DAEMON_SVC_MISSING = "Svc not detected, starting: %+v"
|
||||||
|
DAEMON_CTL_PROCESSING = "Processing command: `%+v`"
|
||||||
|
DAEMON_CTL_PROCESSED = "Processed command `%+v` in %+v"
|
||||||
|
DAEMON_CTL_FAILED = "Processing command `%+v` failed after %+v"
|
||||||
)
|
)
|
||||||
|
|
Loading…
Reference in New Issue