mirror of
https://git.sdf.org/rudi/headless9.git
synced 2026-03-22 05:17:27 +00:00
Split daemon from control mode
This commit is contained in:
127
main.go
127
main.go
@ -2,55 +2,142 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
"fmt"
|
"flag"
|
||||||
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
controlFile = "/adm/services"
|
startup []string
|
||||||
services = make(map[string]string)
|
daemon = false
|
||||||
|
serviceFile = "/adm/services"
|
||||||
|
services = make(map[string]string)
|
||||||
|
controlSocket = "/adm/headless9/ctl/headless9.ctl"
|
||||||
|
logPath = "/adm/headless9/log/"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
log.Println("Starting headless9")
|
flag.Parse()
|
||||||
|
if flag.NArg() == 0 {
|
||||||
|
log.Println("Starting headless9")
|
||||||
|
daemon = true
|
||||||
|
runDaemon()
|
||||||
|
}
|
||||||
|
if flag.NArg() != 2 {
|
||||||
|
fmt.Println("Command structure: \"headless9 $verb $service\"")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
verb := flag.Args()[1]
|
||||||
|
service := flag.Args()[2]
|
||||||
|
|
||||||
|
f := getCtl()
|
||||||
|
_, err := fmt.Fprintf(f, "%+v %+v", verb, service)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("Unable to write to Control Socket. Please check the file %+v and that it's permissions are 700", controlSocket)
|
||||||
|
}
|
||||||
|
f.Close()
|
||||||
|
err = watchFile(controlSocket)
|
||||||
|
if err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
}
|
||||||
|
fmt.Println(strings.Join(sysTail(10, controlSocket), "\n"))
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func getCtl() *os.File {
|
||||||
|
f, err := os.OpenFile(controlSocket, os.O_TRUNC, 0700)
|
||||||
|
if err != nil {
|
||||||
|
if daemon {
|
||||||
|
log.Printf("Unable to open Control Socket. Please check the file %+v and that it's permissions are 700", controlSocket)
|
||||||
|
} else {
|
||||||
|
fmt.Printf("Unable to open Control Socket. Please check the file %+v and that it's permissions are 700", controlSocket)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
err = f.Truncate(0)
|
||||||
|
if err != nil {
|
||||||
|
if daemon {
|
||||||
|
log.Printf("Unable to clear Control Socket. Please check the file %+v and that it's permissions are 700", controlSocket)
|
||||||
|
} else {
|
||||||
|
fmt.Printf("Unable to clear Control Socket. Please check the file %+v and that it's permissions are 700", controlSocket)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_, err = f.Seek(0, 0)
|
||||||
|
if err != nil {
|
||||||
|
if daemon {
|
||||||
|
log.Printf("Unable to rewind Control Socket. Please check the file %+v and that it's permissions are 700", controlSocket)
|
||||||
|
} else {
|
||||||
|
fmt.Printf("Unable to rewind Control Socket. Please check the file %+v and that it's permissions are 700", controlSocket)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return f
|
||||||
|
}
|
||||||
|
|
||||||
|
func runDaemon() {
|
||||||
|
go headlessControls()
|
||||||
for {
|
for {
|
||||||
log.Printf("Refreshing controlFile %+v", controlFile)
|
log.Printf("Refreshing controlFile %+v", serviceFile)
|
||||||
startup, err := readLines(controlFile)
|
startup, err := readLines(serviceFile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalln(err)
|
log.Fatalln(err)
|
||||||
}
|
}
|
||||||
for _, svc := range startup {
|
for _, svc := range startup {
|
||||||
running := false
|
running := false
|
||||||
|
svcArgs := strings.Split(svc, " ")
|
||||||
for runningProc := range services {
|
for runningProc := range services {
|
||||||
if svc == runningProc {
|
if svcArgs[0] == runningProc {
|
||||||
running = true
|
running = true
|
||||||
log.Printf("%+v exists as PID %+v", svc, services[svc])
|
log.Printf("%+v exists as PID %+v", svcArgs[0], services[svcArgs[0]])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if !running {
|
if !running {
|
||||||
log.Printf("Svc not detected, starting: %+v", svc)
|
log.Printf("Svc not detected, starting: %+v", svcArgs[0])
|
||||||
go execCommand(svc)
|
go execCommand(svcArgs[0], svcArgs[1:]...)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
err = watchFile(controlFile)
|
err = watchFile(serviceFile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func headlessControls() {
|
||||||
|
for {
|
||||||
|
err := watchFile(controlSocket)
|
||||||
|
if err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
}
|
||||||
|
pendingCommands, err := readLines(controlSocket)
|
||||||
|
if err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
} else {
|
||||||
|
for _, cmd := range pendingCommands {
|
||||||
|
log.Printf("Processing command: %+v", cmd)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
func execCommand(cmd string, arg ...string) {
|
func execCommand(cmd string, arg ...string) {
|
||||||
defer PanicSafe()
|
defer PanicSafe()
|
||||||
|
if len(cmd) < 2 {
|
||||||
|
log.Printf("Invalid command `%v`, skipping. Args: { %+v }", cmd, arg)
|
||||||
|
return
|
||||||
|
}
|
||||||
proc := exec.Command(cmd, arg...)
|
proc := exec.Command(cmd, arg...)
|
||||||
// open the out file for writing
|
// open the out file for writing
|
||||||
outfile, err := os.Create(fmt.Sprintf("/adm/log/%+v.log", cmd))
|
outfile, err := os.Create(fmt.Sprintf("%+v/%+v.log", logPath, cmd))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
infile, err := os.Create(fmt.Sprintf("/adm/log/%+v.ctl", cmd))
|
infile, err := os.Create(fmt.Sprintf("%+v/%+v.ctl", logPath, cmd))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
@ -61,7 +148,8 @@ func execCommand(cmd string, arg ...string) {
|
|||||||
proc.Stdin = infile
|
proc.Stdin = infile
|
||||||
err = proc.Start()
|
err = proc.Start()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
log.Printf("Error starting service %+v, see log for more info.", cmd)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
services[cmd] = fmt.Sprint(proc.Process.Pid)
|
services[cmd] = fmt.Sprint(proc.Process.Pid)
|
||||||
proc.Wait()
|
proc.Wait()
|
||||||
@ -106,11 +194,20 @@ func watchFile(filePath string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// PanicSafe is a deferrable function to recover from a panic operation.
|
// PanicSafe is a deferrable function to recover from a panic operation.
|
||||||
func PanicSafe(a ...interface{}) {
|
func PanicSafe(a ...interface{}) {
|
||||||
if r := recover(); r != nil {
|
if r := recover(); r != nil {
|
||||||
log.Printf("Panic detected: %+v", r)
|
log.Printf("Panic detected: %+v", r)
|
||||||
log.Printf("Optional panic data: %+v", a...)
|
log.Printf("Optional panic data: %+v", a...)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// count for number of lines, path of file
|
||||||
|
func sysTail(count int, path string) []string {
|
||||||
|
c := exec.Command("tail", fmt.Sprintf("-%d", count+1), path)
|
||||||
|
output, _ := c.Output()
|
||||||
|
//log.Printf("SysTail call output: %+v\nEND", string(output))
|
||||||
|
lines := strings.Split(string(output), "\n")
|
||||||
|
|
||||||
|
return lines[:len(lines)-1]
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user