From e5178e50d2cc5c5fb8524a0c6311be4d6fb03eae Mon Sep 17 00:00:00 2001 From: SteelPhase Date: Tue, 21 Aug 2018 07:42:03 -0400 Subject: [PATCH] add support for overriding service templates at runtime --- service.go | 23 ++++++++++++++++------- service_darwin.go | 30 ++++++++++++++++++++---------- service_systemd_linux.go | 9 ++++++++- service_sysv_linux.go | 9 ++++++++- service_upstart_linux.go | 8 +++++++- 5 files changed, 59 insertions(+), 20 deletions(-) diff --git a/service.go b/service.go index ec0d785..edda510 100644 --- a/service.go +++ b/service.go @@ -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 } diff --git a/service_darwin.go b/service_darwin.go index 0a28962..1bad9df 100644 --- a/service_darwin.go +++ b/service_darwin.go @@ -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 { diff --git a/service_systemd_linux.go b/service_systemd_linux.go index b7d63bc..bde52a6 100644 --- a/service_systemd_linux.go +++ b/service_systemd_linux.go @@ -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 { diff --git a/service_sysv_linux.go b/service_sysv_linux.go index d2a965b..0283cd8 100644 --- a/service_sysv_linux.go +++ b/service_sysv_linux.go @@ -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 { diff --git a/service_upstart_linux.go b/service_upstart_linux.go index 524d18c..c206dd6 100644 --- a/service_upstart_linux.go +++ b/service_upstart_linux.go @@ -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 {