serviceman/manager/install_darwin.go

174 lines
3.4 KiB
Go
Raw Normal View History

2019-07-04 07:36:35 +00:00
package manager
2019-07-01 08:44:48 +00:00
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"text/template"
2019-07-04 07:36:35 +00:00
"git.rootprojects.org/root/go-serviceman/manager/static"
2019-07-03 05:51:30 +00:00
"git.rootprojects.org/root/go-serviceman/service"
2019-07-01 08:44:48 +00:00
)
2019-07-05 18:48:58 +00:00
const (
srvExt = ".plist"
srvSysPath = "/Library/LaunchDaemons"
srvUserPath = "Library/LaunchAgents"
)
var srvLen int
func init() {
srvLen = len(srvExt)
}
func start(conf *service.Service) error {
system := conf.System
home := conf.Home
rdns := conf.ReverseDNS
service, err := getService(system, home, rdns)
2019-07-05 18:48:58 +00:00
if nil != err {
return err
}
cmds := []Runnable{
Runnable{
Exec: "launchctl",
Args: []string{"unload", "-w", service},
Must: false,
},
Runnable{
2019-07-07 08:14:25 +00:00
Exec: "launchctl",
Args: []string{"load", "-w", service},
Must: true,
Badwords: []string{"No such file or directory", "service already loaded"},
2019-07-05 18:48:58 +00:00
},
}
2019-07-07 06:48:25 +00:00
cmds = adjustPrivs(system, cmds)
typ := "USER"
if system {
typ = "SYSTEM"
}
fmt.Printf("Starting launchd %s service...\n\n", typ)
for i := range cmds {
exe := cmds[i]
fmt.Println("\t" + exe.String())
err := exe.Run()
if nil != err {
return err
}
}
fmt.Println()
return nil
}
func stop(conf *service.Service) error {
system := conf.System
home := conf.Home
rdns := conf.ReverseDNS
service, err := getService(system, home, rdns)
if nil != err {
return err
}
cmds := []Runnable{
Runnable{
Exec: "launchctl",
Args: []string{"unload", service},
Must: false,
Badwords: []string{"No such file or directory", "Cound not find specified service"},
},
}
cmds = adjustPrivs(system, cmds)
fmt.Println()
typ := "USER"
if system {
typ = "SYSTEM"
}
fmt.Printf("Stopping launchd %s service...\n", typ)
2019-07-05 18:48:58 +00:00
for i := range cmds {
exe := cmds[i]
fmt.Println("\t" + exe.String())
2019-07-05 18:48:58 +00:00
err := exe.Run()
if nil != err {
return err
}
}
fmt.Println()
return nil
}
func Render(c *service.Service) ([]byte, error) {
// Create service file from template
b, err := static.ReadFile("dist/Library/LaunchDaemons/_rdns_.plist.tmpl")
if err != nil {
return nil, err
}
s := string(b)
rw := &bytes.Buffer{}
// not sure what the template name does, but whatever
tmpl, err := template.New("service").Parse(s)
if err != nil {
return nil, err
}
err = tmpl.Execute(rw, c)
if nil != err {
return nil, err
}
return rw.Bytes(), nil
}
func install(c *service.Service) (string, error) {
2019-07-01 08:44:48 +00:00
// Darwin-specific config options
if c.PrivilegedPorts {
if !c.System {
return "", fmt.Errorf("You must use root-owned LaunchDaemons (not user-owned LaunchAgents) to use priveleged ports on OS X")
2019-07-01 08:44:48 +00:00
}
}
2019-07-05 18:48:58 +00:00
plistDir := srvSysPath
2019-07-01 08:44:48 +00:00
if !c.System {
2019-07-05 18:48:58 +00:00
plistDir = filepath.Join(c.Home, srvUserPath)
2019-07-01 08:44:48 +00:00
}
// Check paths first
2019-07-02 06:25:16 +00:00
err := os.MkdirAll(filepath.Dir(plistDir), 0755)
2019-07-01 08:44:48 +00:00
if nil != err {
return "", err
2019-07-01 08:44:48 +00:00
}
b, err := Render(c)
2019-07-01 08:44:48 +00:00
if nil != err {
return "", err
2019-07-01 08:44:48 +00:00
}
// Write the file out
// TODO rdns
2019-07-02 06:02:09 +00:00
plistName := c.ReverseDNS + ".plist"
2019-07-01 08:44:48 +00:00
plistPath := filepath.Join(plistDir, plistName)
if err := ioutil.WriteFile(plistPath, b, 0644); err != nil {
return "", fmt.Errorf("Error writing %s: %v", plistPath, err)
2019-07-01 08:44:48 +00:00
}
2019-07-05 18:48:58 +00:00
// TODO --no-start
err = start(c)
2019-07-05 18:48:58 +00:00
if nil != err {
fmt.Printf("If things don't go well you should be able to get additional logging from launchctl:\n")
fmt.Printf("\tsudo launchctl log level debug\n")
fmt.Printf("\ttail -f /var/log/system.log\n")
return "", err
2019-07-02 06:25:16 +00:00
}
2019-07-01 08:44:48 +00:00
return "launchd", nil
2019-07-01 08:44:48 +00:00
}