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(system bool, home string, name string) error {
|
|
|
|
sys, user, err := getMatchingSrvs(home, name)
|
|
|
|
if nil != err {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var service string
|
|
|
|
if system {
|
|
|
|
service, err = getOneSysSrv(sys, user, name)
|
|
|
|
if nil != err {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
service = filepath.Join(srvSysPath, service)
|
|
|
|
} else {
|
|
|
|
service, err = getOneUserSrv(home, sys, user, name)
|
|
|
|
if nil != err {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
service = filepath.Join(home, srvUserPath, service)
|
|
|
|
}
|
|
|
|
|
|
|
|
cmds := []Runnable{
|
|
|
|
Runnable{
|
|
|
|
Exec: "launchctl",
|
|
|
|
Args: []string{"unload", "-w", service},
|
|
|
|
Must: false,
|
|
|
|
},
|
|
|
|
Runnable{
|
|
|
|
Exec: "launchctl",
|
|
|
|
Args: []string{"load", "-w", service},
|
|
|
|
Must: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2019-07-07 06:48:25 +00:00
|
|
|
cmds = adjustPrivs(system, cmds)
|
|
|
|
|
2019-07-05 18:48:58 +00:00
|
|
|
fmt.Println()
|
|
|
|
for i := range cmds {
|
|
|
|
exe := cmds[i]
|
|
|
|
fmt.Println(exe.String())
|
|
|
|
err := exe.Run()
|
|
|
|
if nil != err {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fmt.Println()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-07-03 05:51:30 +00:00
|
|
|
func install(c *service.Service) 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-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
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create service file from template
|
|
|
|
b, err := static.ReadFile("dist/Library/LaunchDaemons/_rdns_.plist.tmpl")
|
|
|
|
if err != nil {
|
|
|
|
return 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 err
|
|
|
|
}
|
|
|
|
err = tmpl.Execute(rw, c)
|
|
|
|
if nil != err {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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, rw.Bytes(), 0644); err != nil {
|
2019-07-07 06:48:25 +00:00
|
|
|
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.System, c.Home, c.ReverseDNS)
|
|
|
|
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
|
|
|
|
2019-07-05 18:48:58 +00:00
|
|
|
fmt.Printf("Added and started '%s' as a launchctl service.\n", c.Name)
|
2019-07-01 08:44:48 +00:00
|
|
|
return nil
|
|
|
|
}
|