add support for overriding service templates at runtime

This commit is contained in:
SteelPhase
2018-08-21 07:42:03 -04:00
committed by Daniel Theophanes
parent 994866c14a
commit e5178e50d2
5 changed files with 59 additions and 20 deletions
+16 -7
View File
@@ -81,6 +81,11 @@ const (
optionRunWait = "RunWait"
optionReloadSignal = "ReloadSignal"
optionPIDFile = "PIDFile"
optionSystemdScript = "SystemdScript"
optionSysvScript = "SysvScript"
optionUpstartScript = "UpstartScript"
optionLaunchdConfig = "LaunchdConfig"
)
// Config provides the setup for a Service. The Name field is required.
@@ -105,15 +110,19 @@ type Config struct {
// System specific options.
// * OS X
// - KeepAlive bool (true)
// - RunAtLoad bool (false)
// - UserService bool (false) - Install as a current user service.
// - SessionCreate bool (false) - Create a full user session.
// - LaunchdConfig string () - Use custom launchd config
// - KeepAlive bool (true)
// - RunAtLoad bool (false)
// - UserService bool (false) - Install as a current user service.
// - SessionCreate bool (false) - Create a full user session.
// * POSIX
// - RunWait func() (wait for SIGNAL) - Do not install signal but wait for this function to return.
// - ReloadSignal string () [USR1, ...] - Signal to send on reaload.
// - SystemdScript string () - Use custom systemd script
// - UpstartScript string () - Use custom upstart script
// - SysvScript string () - Use custom sysv script
// - RunWait func() (wait for SIGNAL) - Do not install signal but wait for this function to return.
// - ReloadSignal string () [USR1, ...] - Signal to send on reaload.
// - PIDFile string () [/run/prog.pid] - Location of the PID file.
// - LogOutput bool (false) - Redirect StdErr & StdOut to files.
// - LogOutput bool (false) - Redirect StdErr & StdOut to files.
Option KeyValue
}
+20 -10
View File
@@ -100,6 +100,25 @@ func (s *darwinLaunchdService) getServiceFilePath() (string, error) {
return "/Library/LaunchDaemons/" + s.Name + ".plist", nil
}
func (s *darwinLaunchdService) template() *template.Template {
functions := template.FuncMap{
"bool": func(v bool) string {
if v {
return "true"
}
return "false"
},
}
customConfig := s.Option.string(optionLaunchdConfig, "")
if customConfig != "" {
return template.Must(template.New("").Funcs(functions).Parse(customConfig))
} else {
return template.Must(template.New("").Funcs(functions).Parse(launchdConfig))
}
}
func (s *darwinLaunchdService) Install() error {
confPath, err := s.getServiceFilePath()
if err != nil {
@@ -143,16 +162,7 @@ func (s *darwinLaunchdService) Install() error {
SessionCreate: s.Option.bool(optionSessionCreate, optionSessionCreateDefault),
}
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 s.template().Execute(f, to)
}
func (s *darwinLaunchdService) Uninstall() error {
+8 -1
View File
@@ -52,8 +52,15 @@ func (s *systemd) configPath() (cp string, err error) {
cp = "/etc/systemd/system/" + s.Config.Name + ".service"
return
}
func (s *systemd) template() *template.Template {
return template.Must(template.New("").Funcs(tf).Parse(systemdScript))
customScript := s.Option.string(optionSystemdScript, "")
if customScript != "" {
return template.Must(template.New("").Funcs(tf).Parse(customScript))
} else {
return template.Must(template.New("").Funcs(tf).Parse(systemdScript))
}
}
func (s *systemd) Install() error {
+8 -1
View File
@@ -45,8 +45,15 @@ func (s *sysv) configPath() (cp string, err error) {
cp = "/etc/init.d/" + s.Config.Name
return
}
func (s *sysv) template() *template.Template {
return template.Must(template.New("").Funcs(tf).Parse(sysvScript))
customScript := s.Option.string(optionSysvScript, "")
if customScript != "" {
return template.Must(template.New("").Funcs(tf).Parse(customScript))
} else {
return template.Must(template.New("").Funcs(tf).Parse(sysvScript))
}
}
func (s *sysv) Install() error {
+7 -1
View File
@@ -111,7 +111,13 @@ func (s *upstart) getUpstartVersion() []int {
}
func (s *upstart) template() *template.Template {
return template.Must(template.New("").Funcs(tf).Parse(upstartScript))
customScript := s.Option.string(optionUpstartScript, "")
if customScript != "" {
return template.Must(template.New("").Funcs(tf).Parse(customScript))
} else {
return template.Must(template.New("").Funcs(tf).Parse(upstartScript))
}
}
func (s *upstart) Install() error {