serviceman/installer/install_darwin.go

72 lines
1.6 KiB
Go
Raw Normal View History

2019-07-01 08:44:48 +00:00
package installer
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"text/template"
"git.rootprojects.org/root/go-serviceman/installer/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-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")
}
}
plistDir := "/Library/LaunchDaemons/"
if !c.System {
2019-07-03 05:51:30 +00:00
plistDir = filepath.Join(c.Home, "Library/LaunchAgents")
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-02 06:25:16 +00:00
2019-07-01 08:44:48 +00:00
return fmt.Errorf("ioutil.WriteFile error: %v", err)
}
fmt.Printf("Installed. To start '%s' run the following:\n", c.Name)
// TODO template config file
2019-07-03 05:51:30 +00:00
if "" != c.Home {
plistPath = strings.Replace(plistPath, c.Home, "~", 1)
2019-07-02 06:25:16 +00:00
}
sudo := ""
if c.System {
sudo = "sudo "
}
fmt.Printf("\t%slaunchctl load -w %s\n", sudo, plistPath)
2019-07-01 08:44:48 +00:00
return nil
}