headless9/main.go

266 lines
5.4 KiB
Go
Raw Normal View History

2022-07-25 14:55:20 -04:00
package main
import (
"bufio"
2022-07-31 11:09:02 -04:00
"flag"
2022-07-31 11:19:24 -04:00
"fmt"
2022-07-25 14:55:20 -04:00
"log"
"os"
"os/exec"
2022-07-31 11:09:02 -04:00
"strings"
2022-07-25 14:55:20 -04:00
"time"
)
var (
2022-07-31 11:19:24 -04:00
daemon = false
2022-07-31 11:09:02 -04:00
serviceFile = "/adm/services"
services = make(map[string]Service)
2022-07-31 11:09:02 -04:00
controlSocket = "/adm/headless9/ctl/headless9.ctl"
2022-07-31 11:19:24 -04:00
logPath = "/adm/headless9/log/"
2022-07-25 14:55:20 -04:00
)
func main() {
2022-07-31 11:09:02 -04:00
flag.Parse()
if flag.NArg() == 0 {
2022-07-31 11:16:42 -04:00
log.Println(DAEMON_START)
2022-07-31 11:09:02 -04:00
daemon = true
runDaemon()
}
if flag.NArg() != 2 {
2022-07-31 11:16:42 -04:00
fmt.Println(CMD_SYNTAX)
2022-07-31 11:09:02 -04:00
return
}
2022-10-23 00:13:37 -04:00
verb := flag.Args()[0]
service := flag.Args()[1]
2022-07-31 11:09:02 -04:00
f := getCtl()
_, err := fmt.Fprintf(f, "%+v %+v", verb, service)
if err != nil {
2022-07-31 11:16:42 -04:00
fmt.Printf(CTL_UNABLE_WRITE, controlSocket)
2022-07-31 11:09:02 -04:00
}
f.Close()
err = watchFile(controlSocket)
if err != nil {
log.Println(err)
}
fmt.Println(strings.Join(sysTail(10, controlSocket), "\n"))
2022-07-31 11:19:24 -04:00
2022-07-31 11:09:02 -04:00
}
func getCtl() *os.File {
2022-10-23 00:13:03 -04:00
f, err := os.OpenFile(controlSocket, os.O_TRUNC, 0660)
2022-07-31 11:09:02 -04:00
if err != nil {
if daemon {
2022-07-31 11:16:42 -04:00
log.Printf(CTL_NOT_OPEN, controlSocket)
2022-07-31 11:09:02 -04:00
} else {
2022-07-31 11:16:42 -04:00
fmt.Printf(CTL_NOT_OPEN, controlSocket)
2022-07-31 11:09:02 -04:00
}
}
err = f.Truncate(0)
if err != nil {
if daemon {
2022-07-31 11:16:42 -04:00
log.Printf(CTL_NOT_CLEAR, controlSocket)
2022-07-31 11:09:02 -04:00
} else {
2022-07-31 11:16:42 -04:00
fmt.Printf(CTL_NOT_CLEAR, controlSocket)
2022-07-31 11:09:02 -04:00
}
}
_, err = f.Seek(0, 0)
if err != nil {
if daemon {
2022-07-31 11:16:42 -04:00
log.Printf(CTL_NOT_REWOUND, controlSocket)
2022-07-31 11:09:02 -04:00
} else {
2022-07-31 11:16:42 -04:00
fmt.Printf(CTL_NOT_REWOUND, controlSocket)
2022-07-31 11:09:02 -04:00
}
}
return f
}
func runDaemon() {
go headlessControls()
2022-07-25 14:55:20 -04:00
for {
2022-07-31 11:16:42 -04:00
log.Printf(DAEMON_FILE_REFRESH, serviceFile)
2022-07-31 11:09:02 -04:00
startup, err := readLines(serviceFile)
2022-07-25 14:55:20 -04:00
if err != nil {
log.Fatalln(err)
}
for _, svc := range startup {
running := false
2022-07-31 11:09:02 -04:00
svcArgs := strings.Split(svc, " ")
2022-07-25 14:55:20 -04:00
for runningProc := range services {
2022-07-31 11:09:02 -04:00
if svcArgs[0] == runningProc {
2022-07-25 14:55:20 -04:00
running = true
2022-07-31 11:16:42 -04:00
log.Printf(DAEMON_SVC_EXISTS, svcArgs[0], services[svcArgs[0]])
2022-07-25 14:55:20 -04:00
}
}
if !running {
2022-07-31 11:16:42 -04:00
log.Printf(DAEMON_SVC_MISSING, svcArgs[0])
2022-07-31 11:09:02 -04:00
go execCommand(svcArgs[0], svcArgs[1:]...)
2022-07-25 14:55:20 -04:00
}
}
2022-07-31 11:09:02 -04:00
err = watchFile(serviceFile)
2022-07-25 14:55:20 -04:00
if err != nil {
log.Println(err)
}
}
}
2022-07-31 11:09:02 -04:00
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(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" {
2022-10-23 00:13:03 -04:00
f, err := os.OpenFile(fmt.Sprintf("/proc/%d/ctl", services[svc].ProcessHandle.Pid), os.O_WRONLY, 0660)
2022-10-22 23:22:50 -04:00
if err != nil {
log.Printf("")
}
defer f.Close()
_, err = f.WriteString("kill")
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
2022-07-31 11:09:02 -04:00
}
log.Printf(DAEMON_SVC_MISSING, svcArgs[0])
go execCommand(svcArgs[0], svcArgs[1:]...)
2022-07-31 11:09:02 -04:00
}
}
return nil
2022-07-31 11:09:02 -04:00
}
2022-07-25 14:55:20 -04:00
func execCommand(cmd string, arg ...string) {
defer PanicSafe()
2022-07-31 11:09:02 -04:00
if len(cmd) < 2 {
log.Printf(DAEMON_SVC_INVALID, cmd, arg)
return
}
if strings.HasPrefix(cmd, "#") {
log.Printf(DAEMON_SVC_DISABLED, cmd)
2022-07-31 11:09:02 -04:00
return
}
2022-07-25 14:55:20 -04:00
proc := exec.Command(cmd, arg...)
// open the out file for writing
2022-07-31 11:09:02 -04:00
outfile, err := os.Create(fmt.Sprintf("%+v/%+v.log", logPath, cmd))
2022-07-25 14:55:20 -04:00
if err != nil {
panic(err)
}
2022-07-31 11:09:02 -04:00
infile, err := os.Create(fmt.Sprintf("%+v/%+v.ctl", logPath, cmd))
2022-07-25 14:55:20 -04:00
if err != nil {
panic(err)
}
defer outfile.Close()
defer infile.Close()
proc.Stderr = outfile
proc.Stdout = outfile
proc.Stdin = infile
err = proc.Start()
if err != nil {
2022-07-31 11:16:42 -04:00
log.Printf(DAEMON_SVC_FAIL, cmd)
2022-07-31 11:09:02 -04:00
return
2022-07-25 14:55:20 -04:00
}
services[cmd] = Service{
ProcessHandle: proc.Process,
StartupArgs: arg,
StartTime: time.Now(),
}
2022-07-25 14:55:20 -04:00
proc.Wait()
}
// readLines reads a whole file into memory
// and returns a slice of its lines.
func readLines(path string) ([]string, error) {
file, err := os.Open(path)
if err != nil {
return nil, err
}
defer file.Close()
var lines []string
scanner := bufio.NewScanner(file)
for scanner.Scan() {
lines = append(lines, scanner.Text())
}
return lines, scanner.Err()
}
func watchFile(filePath string) error {
initialStat, err := os.Stat(filePath)
if err != nil {
return err
}
for {
stat, err := os.Stat(filePath)
if err != nil {
return err
}
if stat.Size() != initialStat.Size() || stat.ModTime() != initialStat.ModTime() {
break
}
time.Sleep(250 * time.Millisecond)
}
return nil
}
// PanicSafe is a deferrable function to recover from a panic operation.
func PanicSafe(a ...interface{}) {
if r := recover(); r != nil {
log.Printf("Panic detected: %+v", r)
log.Printf("Optional panic data: %+v", a...)
}
2022-07-31 11:09:02 -04:00
}
// 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]
2022-07-31 11:19:24 -04:00
}