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)
|
|
|
|
}
|
|
|
|
|
2019-07-10 07:16:45 +00:00
|
|
|
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)
|
|
|
|
|
2019-07-05 18:48:58 +00:00
|
|
|
fmt.Println()
|
2019-07-10 07:16:45 +00:00
|
|
|
typ := "USER"
|
|
|
|
if system {
|
|
|
|
typ = "SYSTEM"
|
|
|
|
}
|
|
|
|
fmt.Printf("Starting launchd %s service...\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]
|
2019-07-07 06:55:01 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
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
|
2019-07-10 07:16:45 +00:00
|
|
|
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
|
|
|
|
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
|
|
|
|
}
|