go-serviceman/serviceman.go

212 lines
5.2 KiB
Go
Raw Normal View History

2019-07-01 08:44:48 +00:00
//go:generate go run -mod=vendor git.rootprojects.org/root/go-gitver
package main
import (
2019-07-03 05:51:30 +00:00
"encoding/json"
2019-07-01 08:44:48 +00:00
"flag"
"fmt"
2019-07-03 05:51:30 +00:00
"io/ioutil"
2019-07-01 08:44:48 +00:00
"os"
2019-07-03 05:51:30 +00:00
"os/exec"
2019-07-01 08:44:48 +00:00
"strings"
"time"
2019-07-04 07:36:35 +00:00
"git.rootprojects.org/root/go-serviceman/manager"
2019-07-03 05:51:30 +00:00
"git.rootprojects.org/root/go-serviceman/runner"
"git.rootprojects.org/root/go-serviceman/service"
2019-07-01 08:44:48 +00:00
)
var GitRev = "000000000"
var GitVersion = "v0.0.0"
var GitTimestamp = time.Now().Format(time.RFC3339)
2019-07-03 05:51:30 +00:00
func usage() {
2019-07-04 07:36:35 +00:00
fmt.Println("Usage: serviceman add ./foo-app -- --foo-arg")
2019-07-03 05:51:30 +00:00
fmt.Println("Usage: serviceman run --config ./foo-app.json")
}
2019-07-01 08:44:48 +00:00
func main() {
2019-07-03 05:51:30 +00:00
if len(os.Args) < 2 {
fmt.Fprintf(os.Stderr, "Too few arguments: %s\n", strings.Join(os.Args, " "))
usage()
os.Exit(1)
}
top := os.Args[1]
os.Args = append(os.Args[:1], os.Args[2:]...)
switch top {
2019-07-04 07:36:35 +00:00
case "version":
fmt.Println(GitVersion, GitTimestamp, GitRev)
case "add":
add()
2019-07-03 05:51:30 +00:00
case "run":
run()
default:
fmt.Fprintf(os.Stderr, "Unknown argument %s\n", top)
usage()
os.Exit(1)
}
}
2019-07-04 07:36:35 +00:00
func add() {
2019-07-03 05:51:30 +00:00
conf := &service.Service{
2019-07-01 08:44:48 +00:00
Restart: true,
}
args := []string{}
for i := range os.Args {
if "--" == os.Args[i] {
if len(os.Args) > i+1 {
args = os.Args[i+1:]
}
os.Args = os.Args[:i]
break
}
}
conf.Argv = args
2019-07-02 06:02:09 +00:00
force := false
2019-07-01 08:44:48 +00:00
forUser := false
forSystem := false
flag.StringVar(&conf.Title, "title", "", "a human-friendly name for the service")
flag.StringVar(&conf.Desc, "desc", "", "a human-friendly description of the service (ex: Foo App)")
flag.StringVar(&conf.Name, "name", "", "a computer-friendly name for the service (ex: foo-app)")
flag.StringVar(&conf.URL, "url", "", "the documentation on home page of the service")
2019-07-02 06:02:09 +00:00
//flag.StringVar(&conf.Workdir, "workdir", "", "the directory in which the service should be started")
2019-07-01 08:44:48 +00:00
flag.StringVar(&conf.ReverseDNS, "rdns", "", "a plist-friendly Reverse DNS name for launchctl (ex: com.example.foo-app)")
2019-07-04 07:36:35 +00:00
flag.BoolVar(&forSystem, "system", false, "attempt to add system service as an unprivileged/unelevated user")
flag.BoolVar(&forUser, "user", false, "add user space / user mode service even when admin/root/sudo/elevated")
2019-07-02 06:02:09 +00:00
flag.BoolVar(&force, "force", false, "if the interpreter or executable doesn't exist, or things don't make sense, try anyway")
2019-07-01 08:44:48 +00:00
flag.StringVar(&conf.User, "username", "", "run the service as this user")
flag.StringVar(&conf.Group, "groupname", "", "run the service as this group")
flag.BoolVar(&conf.PrivilegedPorts, "cap-net-bind", false, "this service should have access to privileged ports")
flag.Parse()
args = flag.Args()
if forUser && forSystem {
fmt.Println("Pfff! You can't --user AND --system! What are you trying to pull?")
os.Exit(1)
2019-07-02 06:02:09 +00:00
return
2019-07-01 08:44:48 +00:00
}
if forUser {
conf.System = false
} else if forSystem {
conf.System = true
} else {
2019-07-04 07:36:35 +00:00
conf.System = manager.IsPrivileged()
2019-07-01 08:44:48 +00:00
}
n := len(args)
if 0 == n {
2019-07-04 07:36:35 +00:00
fmt.Println("Usage: serviceman add ./foo-app -- --foo-arg")
2019-07-02 06:02:09 +00:00
os.Exit(2)
return
2019-07-01 08:44:48 +00:00
}
2019-07-04 07:36:35 +00:00
execpath, err := manager.WhereIs(args[0])
2019-07-01 08:44:48 +00:00
if nil != err {
2019-07-02 06:02:09 +00:00
fmt.Fprintf(os.Stderr, "Error: '%s' could not be found.\n", args[0])
if !force {
os.Exit(3)
return
}
} else {
args[0] = execpath
2019-07-01 08:44:48 +00:00
}
conf.Exec = args[0]
args = args[1:]
if n >= 2 {
conf.Interpreter = conf.Exec
conf.Exec = args[0]
conf.Argv = append(args[1:], conf.Argv...)
}
2019-07-03 05:51:30 +00:00
conf.Normalize(force)
//fmt.Printf("\n%#v\n\n", conf)
2019-07-03 05:51:30 +00:00
2019-07-04 07:36:35 +00:00
err = manager.Install(conf)
2019-07-03 05:51:30 +00:00
if nil != err {
fmt.Fprintf(os.Stderr, "%s\n", err)
2019-07-04 07:36:35 +00:00
fmt.Fprintf(os.Stderr, "Use 'sudo' to add service as a privileged system service.\n")
fmt.Fprintf(os.Stderr, "Use '--user' to add service as an user service.\n")
2019-07-01 08:44:48 +00:00
}
2019-07-03 05:51:30 +00:00
}
func run() {
var confpath string
var daemonize bool
flag.StringVar(&confpath, "config", "", "path to a config file to run")
flag.BoolVar(&daemonize, "daemon", false, "spawn a child process that lives in the background, and exit")
flag.Parse()
if "" == confpath {
fmt.Fprintf(os.Stderr, "%s", strings.Join(flag.Args(), " "))
fmt.Fprintf(os.Stderr, "--config /path/to/config.json is required\n")
usage()
os.Exit(1)
2019-07-01 08:44:48 +00:00
}
2019-07-03 05:51:30 +00:00
b, err := ioutil.ReadFile(confpath)
if nil != err {
fmt.Fprintf(os.Stderr, "Couldn't read config file: %s\n", err)
os.Exit(400)
2019-07-02 06:02:09 +00:00
}
2019-07-03 05:51:30 +00:00
s := &service.Service{}
err = json.Unmarshal(b, s)
if nil != err {
fmt.Fprintf(os.Stderr, "Couldn't JSON parse config file: %s\n", err)
os.Exit(400)
2019-07-02 06:02:09 +00:00
}
2019-07-03 05:51:30 +00:00
m := map[string]interface{}{}
err = json.Unmarshal(b, &m)
2019-07-02 06:02:09 +00:00
if nil != err {
2019-07-03 05:51:30 +00:00
fmt.Fprintf(os.Stderr, "Couldn't JSON parse config file: %s\n", err)
os.Exit(400)
}
2019-07-02 06:02:09 +00:00
2019-07-03 05:51:30 +00:00
// default Restart to true
if _, ok := m["restart"]; !ok {
s.Restart = true
2019-07-01 08:44:48 +00:00
}
2019-07-03 05:51:30 +00:00
if "" == s.Exec {
fmt.Fprintf(os.Stderr, "Missing exec\n")
os.Exit(400)
}
2019-07-01 08:44:48 +00:00
2019-07-03 05:51:30 +00:00
s.Normalize(false)
2019-07-03 08:11:50 +00:00
//fmt.Fprintf(os.Stdout, "Logdir: %s\n", s.Logdir)
err = os.MkdirAll(s.Logdir, 0755)
if nil != err {
fmt.Fprintf(os.Stderr, "%s\n", err)
return
}
2019-07-03 05:51:30 +00:00
if !daemonize {
2019-07-03 08:11:50 +00:00
//fmt.Fprintf(os.Stdout, "Running %s %s %s\n", s.Interpreter, s.Exec, strings.Join(s.Argv, " "))
2019-07-03 05:51:30 +00:00
runner.Run(s)
return
}
cmd := exec.Command(os.Args[0], "run", "--config", confpath)
// for debugging
/*
out, err := cmd.CombinedOutput()
if nil != err {
fmt.Println(err)
}
fmt.Println(string(out))
*/
err = cmd.Start()
2019-07-01 08:44:48 +00:00
if nil != err {
2019-07-02 06:25:16 +00:00
fmt.Fprintf(os.Stderr, "%s\n", err)
2019-07-03 05:51:30 +00:00
os.Exit(500)
2019-07-01 08:44:48 +00:00
}
}