Compare commits

...

8 Commits

  1. 11
      .drone.yml
  2. 37
      README.md
  3. 44
      main.go
  4. 8
      strings.go

11
.drone.yml

@ -0,0 +1,11 @@ @@ -0,0 +1,11 @@
kind: pipeline
type: docker
name: default
steps:
- name: Build
image: golang
commands:
- go get ./...
- go vet -v
- go build

37
README.md

@ -1,3 +1,38 @@ @@ -1,3 +1,38 @@
# headless9
background services in plan9
background services in plan9
## Badges
[![GPLv3 License](https://img.shields.io/badge/License-LGPL%20v3-green.svg)](https://opensource.org/licenses/)
[![Build Status](https://drone.nightmare.haus/api/badges/rudi/headless9/status.svg)](https://drone.nightmare.haus/api/badges/rudi/headless9)
[![Go Report Card](https://goreportcard.com/badge/git.sdf.org/rudi/headless9)](https://goreportcard.com/badge/git.sdf.org/rudi/headless9)
[![Documentation](https://godoc.org/git.sdf.org/rudi/headless9?status.svg)](https://godoc.org/git.sdf.org/rudi/headless9)
## Deployment
Ensure you have already installed Go, then run the following to set up the directory for headless9 admin and usage.
```bash
mkdir -p /adm/headless9/ctl
mkdir -p /adm/headless9/log
touch /adm/services
go install git.sdf.org/rudi/headles9@latest
cp go/bin/headless9 /bin/headless9
```
## Features
- Headless services from plan9 console
- Logging and control for the running services
## Usage/Examples
Command Line Tool:
```bash
headless9 {start|stop|restart|status} plan9HttpApp
```
Service File (default /adm/services):
```
plan9HttpApp arg1 arg2 ...argN
```

44
main.go

@ -2,6 +2,7 @@ package main @@ -2,6 +2,7 @@ package main
import (
"bufio"
"errors"
"flag"
"fmt"
"log"
@ -30,8 +31,8 @@ func main() { @@ -30,8 +31,8 @@ func main() {
fmt.Println(CMD_SYNTAX)
return
}
verb := flag.Args()[1]
service := flag.Args()[2]
verb := flag.Args()[0]
service := flag.Args()[1]
f := getCtl()
_, err := fmt.Fprintf(f, "%+v %+v", verb, service)
@ -48,7 +49,7 @@ func main() { @@ -48,7 +49,7 @@ func main() {
}
func getCtl() *os.File {
f, err := os.OpenFile(controlSocket, os.O_TRUNC, 0700)
f, err := os.OpenFile(controlSocket, os.O_TRUNC, 0660)
if err != nil {
if daemon {
log.Printf(CTL_NOT_OPEN, controlSocket)
@ -81,7 +82,15 @@ func runDaemon() { @@ -81,7 +82,15 @@ func runDaemon() {
log.Printf(DAEMON_FILE_REFRESH, serviceFile)
startup, err := readLines(serviceFile)
if err != nil {
log.Fatalln(err)
if errors.Is(err, os.ErrNotExist) {
test, err := os.Create(serviceFile)
if err != nil {
log.Fatalln(err)
}
test.Close()
} else {
log.Fatalln(err)
}
}
for _, svc := range startup {
running := false
@ -134,22 +143,17 @@ func processCommand(cmd string) error { @@ -134,22 +143,17 @@ func processCommand(cmd string) error {
svc := parts[1]
start := time.Now()
if verb == "restart" || verb == "stop" {
for testSvc := range services {
if svc == testSvc {
f, err := os.OpenFile(fmt.Sprintf("/proc/%d/ctl", services[svc].ProcessHandle.Pid), os.O_WRONLY, 0600)
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))
}
}
f, err := os.OpenFile(fmt.Sprintf("/proc/%d/ctl", services[svc].ProcessHandle.Pid), os.O_WRONLY, 0660)
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" {

8
strings.go

@ -3,10 +3,10 @@ package main @@ -3,10 +3,10 @@ package main
var (
DAEMON_START = "Starting headless9"
CMD_SYNTAX = "Command structure: \"headless9 $verb $service\""
CTL_UNABLE_WRITE = "Unable to write to Control Socket. Please check the file %+v and that it's permissions are 700"
CTL_NOT_OPEN = "Unable to open 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_UNABLE_WRITE = "Unable to write to Control Socket. Please check the file %+v and that it's permissions are 660"
CTL_NOT_OPEN = "Unable to open Control Socket. Please check the file %+v and that its permissions are 660"
CTL_NOT_CLEAR = "Unable to clear Control Socket. Please check the file %+v and that its permissions are 660"
CTL_NOT_REWOUND = "Unable to rewind Control Socket. Please check the file %+v and that its permissions are 660"
DAEMON_FILE_REFRESH = "Refreshing controlFile %+v"
DAEMON_SVC_INVALID = "Invalid command `%v`, skipping. Args: { %+v }"
DAEMON_SVC_DISABLED = "%+v is disabled, skipping!"

Loading…
Cancel
Save