list() for Mac and Linux

This commit is contained in:
AJ ONeal 2019-08-05 05:53:32 -06:00
parent f03a0755af
commit 34ed9cc065
6 changed files with 132 additions and 14 deletions

View File

@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated for serviceman. Edit as you wish. -->
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<!-- Generated for serviceman. Edit as you wish. -->
<dict>
<key>Label</key>
<string>{{ .ReverseDNS }}</string>

View File

@ -108,6 +108,7 @@ func stop(conf *service.Service) error {
return nil
}
// Render will create a launchd .plist file using the simple internal template
func Render(c *service.Service) ([]byte, error) {
// Create service file from template
b, err := static.ReadFile("dist/Library/LaunchDaemons/_rdns_.plist.tmpl")
@ -142,7 +143,7 @@ func install(c *service.Service) (string, error) {
}
// Check paths first
err := os.MkdirAll(filepath.Dir(plistDir), 0755)
err := os.MkdirAll(plistDir, 0755)
if nil != err {
return "", err
}

View File

@ -159,7 +159,7 @@ func stop(conf *service.Service) error {
return nil
}
// Render will create a systemd .service file using a simple the internal template
// Render will create a systemd .service file using the simple internal template
func Render(c *service.Service) ([]byte, error) {
defaultUserGroup(c)

65
manager/install_nixes.go Normal file
View File

@ -0,0 +1,65 @@
// +build !windows
package manager
import (
"bytes"
"io/ioutil"
"os"
"path/filepath"
"strings"
"git.rootprojects.org/root/go-serviceman/service"
)
// this code is shared between Mac and Linux, but may diverge in the future
func list(c *service.Service) ([]string, []string, []error) {
confDir := srvSysPath
if !c.System {
confDir = filepath.Join(c.Home, srvUserPath)
}
// Enuser path exists
err := os.MkdirAll(confDir, 0755)
if nil != err {
return nil, nil, []error{err}
}
fis, err := ioutil.ReadDir(confDir)
if nil != err {
return nil, nil, []error{err}
}
managed := []string{}
others := []string{}
errs := []error{}
b := make([]byte, 256)
for i := range fis {
fi := fis[i]
if !strings.HasSuffix(strings.ToLower(fi.Name()), srvExt) || len(fi.Name()) <= srvLen {
continue
}
r, err := os.Open(filepath.Join(confDir, fi.Name()))
if nil != err {
errs = append(errs, err)
continue
}
n, err := r.Read(b)
if nil != err {
errs = append(errs, err)
continue
}
b = b[:n]
name := fi.Name()[:len(fi.Name())-srvLen]
if bytes.Contains(b, []byte("for serviceman.")) {
managed = append(managed, name)
} else {
others = append(others, name)
}
}
return managed, others, errs
}

File diff suppressed because one or more lines are too long

View File

@ -30,6 +30,7 @@ func usage() {
fmt.Println("\tserviceman <command> --help")
fmt.Println("\tserviceman add ./foo-app -- --foo-arg")
fmt.Println("\tserviceman run --config ./foo-app.json")
fmt.Println("\tserviceman list --all")
fmt.Println("\tserviceman start <name>")
fmt.Println("\tserviceman stop <name>")
}
@ -54,6 +55,8 @@ func main() {
start()
case "stop":
stop()
case "list":
list()
default:
fmt.Fprintf(os.Stderr, "Unknown argument %s\n", top)
usage()
@ -85,13 +88,6 @@ func add() {
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.BoolVar(&dryrun, "dryrun", false, "output the service file without modifying anything on disk")
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "Usage of %s:\n\n", os.Args[0])
flag.PrintDefaults()
fmt.Fprintf(os.Stderr, "Flags and arguments after \"--\" will be completely ignored by serviceman\n", os.Args[0])
}
flag.Parse()
flagargs := flag.Args()
@ -319,6 +315,62 @@ func add() {
fmt.Println()
}
func list() {
var verbose bool
forUser := false
forSystem := false
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")
flag.BoolVar(&verbose, "all", false, "show all services (even those not managed by serviceman)")
flag.Parse()
if forUser && forSystem {
fmt.Println("Pfff! You can't --user AND --system! What are you trying to pull?")
os.Exit(1)
return
}
conf := &service.Service{}
if forUser {
conf.System = false
} else if forSystem {
conf.System = true
} else {
conf.System = manager.IsPrivileged()
}
// Pretty much just for HomeDir
conf.NormalizeWithoutPath()
managed, others, errs := manager.List(conf)
for i := range errs {
fmt.Fprintf(os.Stderr, "possible error: %s\n", errs[i])
}
if len(errs) > 0 {
fmt.Fprintf(os.Stderr, "\n")
}
fmt.Println("serviceman-managed services:\n")
for i := range managed {
fmt.Println("\t" + managed[i])
}
if 0 == len(managed) {
fmt.Println("\t(none)")
}
fmt.Println("")
if verbose {
fmt.Println("other services:\n")
for i := range others {
fmt.Println("\t" + others[i])
}
if 0 == len(others) {
fmt.Println("\t(none)")
}
fmt.Println("")
}
}
func findExec(exe string, force bool) (string, error) {
// ex: node => /usr/local/bin/node
// ex: ./demo.js => /Users/aj/project/demo.js