service: update darwin to use some extra options.
This commit is contained in:
+58
-22
@@ -6,6 +6,7 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
|
"os/user"
|
||||||
"text/template"
|
"text/template"
|
||||||
|
|
||||||
"bitbucket.org/kardianos/osext"
|
"bitbucket.org/kardianos/osext"
|
||||||
@@ -15,9 +16,7 @@ const maxPathSize = 32 * 1024
|
|||||||
|
|
||||||
func newService(c *Config) (s *darwinLaunchdService, err error) {
|
func newService(c *Config) (s *darwinLaunchdService, err error) {
|
||||||
s = &darwinLaunchdService{
|
s = &darwinLaunchdService{
|
||||||
name: c.Name,
|
Config: c,
|
||||||
displayName: c.DisplayName,
|
|
||||||
description: c.Description,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
s.logger, err = syslog.New(syslog.LOG_INFO, c.Name)
|
s.logger, err = syslog.New(syslog.LOG_INFO, c.Name)
|
||||||
@@ -29,17 +28,36 @@ func newService(c *Config) (s *darwinLaunchdService, err error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type darwinLaunchdService struct {
|
type darwinLaunchdService struct {
|
||||||
name, displayName, description string
|
*Config
|
||||||
logger *syslog.Writer
|
logger *syslog.Writer
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *darwinLaunchdService) getServiceFilePath() string {
|
func (s *darwinLaunchdService) getServiceFilePath() (string, error) {
|
||||||
return "/Library/LaunchDaemons/" + s.name + ".plist"
|
if s.UserService {
|
||||||
|
u, err := user.Current()
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
return u.HomeDir + "/Library/LaunchAgents/" + s.Name + ".plist", nil
|
||||||
|
}
|
||||||
|
return "/Library/LaunchDaemons/" + s.Name + ".plist", nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *darwinLaunchdService) kvBool(name string, def bool) bool {
|
||||||
|
if v, found := s.KV["KeepAlive"]; found {
|
||||||
|
if castValue, is := v.(bool); is {
|
||||||
|
return castValue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return def
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *darwinLaunchdService) Install() error {
|
func (s *darwinLaunchdService) Install() error {
|
||||||
var confPath = s.getServiceFilePath()
|
confPath, err := s.getServiceFilePath()
|
||||||
_, err := os.Stat(confPath)
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
_, err = os.Stat(confPath)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
return fmt.Errorf("Init already exists: %s", confPath)
|
return fmt.Errorf("Init already exists: %s", confPath)
|
||||||
}
|
}
|
||||||
@@ -56,35 +74,52 @@ func (s *darwinLaunchdService) Install() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var to = &struct {
|
var to = &struct {
|
||||||
Name string
|
*Config
|
||||||
Display string
|
Path string
|
||||||
Description string
|
|
||||||
Path string
|
KeepAlive, RunAtLoad bool
|
||||||
}{
|
}{
|
||||||
s.name,
|
Config: s.Config,
|
||||||
s.displayName,
|
Path: path,
|
||||||
s.description,
|
KeepAlive: s.kvBool("KeepAlive", true),
|
||||||
path,
|
RunAtLoad: s.kvBool("RunAtLoad", false),
|
||||||
}
|
}
|
||||||
|
|
||||||
t := template.Must(template.New("launchdConfig").Parse(launchdConfig))
|
functions := template.FuncMap{
|
||||||
|
"bool": func(v bool) string {
|
||||||
|
if v {
|
||||||
|
return "true"
|
||||||
|
}
|
||||||
|
return "false"
|
||||||
|
},
|
||||||
|
}
|
||||||
|
t := template.Must(template.New("launchdConfig").Funcs(functions).Parse(launchdConfig))
|
||||||
return t.Execute(f, to)
|
return t.Execute(f, to)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *darwinLaunchdService) Remove() error {
|
func (s *darwinLaunchdService) Remove() error {
|
||||||
s.Stop()
|
s.Stop()
|
||||||
|
|
||||||
confPath := s.getServiceFilePath()
|
confPath, err := s.getServiceFilePath()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
return os.Remove(confPath)
|
return os.Remove(confPath)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *darwinLaunchdService) Start() error {
|
func (s *darwinLaunchdService) Start() error {
|
||||||
confPath := s.getServiceFilePath()
|
confPath, err := s.getServiceFilePath()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
cmd := exec.Command("launchctl", "load", confPath)
|
cmd := exec.Command("launchctl", "load", confPath)
|
||||||
return cmd.Run()
|
return cmd.Run()
|
||||||
}
|
}
|
||||||
func (s *darwinLaunchdService) Stop() error {
|
func (s *darwinLaunchdService) Stop() error {
|
||||||
confPath := s.getServiceFilePath()
|
confPath, err := s.getServiceFilePath()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
cmd := exec.Command("launchctl", "unload", confPath)
|
cmd := exec.Command("launchctl", "unload", confPath)
|
||||||
return cmd.Run()
|
return cmd.Run()
|
||||||
}
|
}
|
||||||
@@ -127,7 +162,8 @@ var launchdConfig = `<?xml version='1.0' encoding='UTF-8'?>
|
|||||||
<array>
|
<array>
|
||||||
<string>{{.Path}}</string>
|
<string>{{.Path}}</string>
|
||||||
</array>
|
</array>
|
||||||
<key>KeepAlive</key><true/>
|
<key>KeepAlive</key><{{bool .KeepAlive}}/>
|
||||||
|
<key>RunAtLoad</key><{{bool .RunAtLoad}}/>
|
||||||
<key>Disabled</key><false/>
|
<key>Disabled</key><false/>
|
||||||
</dict>
|
</dict>
|
||||||
</plist>
|
</plist>
|
||||||
|
|||||||
Reference in New Issue
Block a user