From 18c6e7250de35ff4791a4a39ff3d8cb115d20e23 Mon Sep 17 00:00:00 2001 From: Daniel Theophanes Date: Sat, 24 Jan 2015 15:29:42 -0800 Subject: [PATCH] service: enable more config params on systemd. --- service_darwin.go | 3 +++ service_linux.go | 20 +++++++++++++++----- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/service_darwin.go b/service_darwin.go index 700eb84..ff02184 100644 --- a/service_darwin.go +++ b/service_darwin.go @@ -199,6 +199,9 @@ var launchdConfig = ` {{html .}} {{end}} +{{if .UserName}}UserName{{html .UserName}}{{end}} +{{if .ChRoot}}RootDirectory{{html .ChRoot}}{{end}} +{{if .WorkingDirectory}}WorkingDirectory{{html .WorkingDirectory}}{{end}} KeepAlive<{{bool .KeepAlive}}/> RunAtLoad<{{bool .RunAtLoad}}/> Disabled diff --git a/service_linux.go b/service_linux.go index 00e86cb..cbb0bbb 100644 --- a/service_linux.go +++ b/service_linux.go @@ -10,6 +10,7 @@ import ( "os" "os/exec" "os/signal" + "strings" "text/template" "time" @@ -137,7 +138,7 @@ func (f initFlavor) ConfigPath(name string, c *Config) (cp string, err error) { return } -func (f initFlavor) GetTemplate() *template.Template { +func (f initFlavor) Template() *template.Template { var templ string switch f { case initSystemd: @@ -147,7 +148,7 @@ func (f initFlavor) GetTemplate() *template.Template { case initUpstart: templ = upstartScript } - return template.Must(template.New(f.String() + "Script").Parse(templ)) + return template.Must(template.New(f.String() + "Script").Funcs(tf).Parse(templ)) } var interactive = false @@ -196,7 +197,7 @@ func (s *linuxService) Install() error { path, } - err = flavor.GetTemplate().Execute(f, to) + err = flavor.Template().Execute(f, to) if err != nil { return err } @@ -298,6 +299,12 @@ func (s *linuxService) Restart() error { return s.Start() } +var tf = map[string]interface{}{ + "cmd": func(s string) string { + return `"` + strings.Replace(s, `"`, `\"`, -1) + `"` + }, +} + const systemVScript = `#!/bin/sh # For RedHat and cousins: # chkconfig: - 99 01 @@ -420,12 +427,15 @@ exec {{.Path}} const systemdScript = `[Unit] Description={{.Description}} -ConditionFileIsExecutable={{.Path}} +ConditionFileIsExecutable={{.Path|cmd}} [Service] StartLimitInterval=5 StartLimitBurst=10 -ExecStart={{.Path}} +ExecStart={{.Path|cmd}}{{range .Arguments}} {{.|cmd}}{{end}} +{{if .ChRoot}}RootDirectory={{.ChRoot|cmd}}{{end}} +{{if .WorkingDirectory}}WorkingDirectory={{.WorkingDirectory|cmd}}{{end}} +{{if .UserName}}User={{.UserName}}{{end}} Restart=always RestartSec=120