Skip to content

Commit

Permalink
update upstart script
Browse files Browse the repository at this point in the history
  • Loading branch information
SteelPhase authored and kardianos committed Aug 20, 2018
1 parent 615a14e commit 1166804
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 17 deletions.
21 changes: 18 additions & 3 deletions service.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,26 @@ const (
optionUserServiceDefault = false
optionSessionCreate = "SessionCreate"
optionSessionCreateDefault = false
optionLogOutput = "LogOutput"
optionLogOutputDefault = false

optionRunWait = "RunWait"
optionReloadSignal = "ReloadSignal"
optionPIDFile = "PIDFile"
)

// Status represents service status as an interger value
type Status int

// Status of service represented as an integer
const (
StatusNotImplemented Status = iota
StatusUnknown
StatusError
StatusRunning
StatusStopped
)

// Config provides the setup for a Service. The Name field is required.
type Config struct {
Name string // Required name of the service. No spaces suggested.
Expand Down Expand Up @@ -108,9 +122,10 @@ type Config struct {
// - 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.
// - PIDFile string () [/run/prog.pid] - Location of the PID file.
// - 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.
Option KeyValue
}

Expand Down
67 changes: 53 additions & 14 deletions service_upstart_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,32 +68,54 @@ func (s *upstart) configPath() (cp string, err error) {

func (s *upstart) hasKillStanza() bool {
defaultValue := true
version := s.getUpstartVersion()
if version == nil {
return defaultValue
}

maxVersion := []int{0, 6, 5}
if versionAtMost(version, maxVersion) {
return false
}

return defaultValue
}

func (s *upstart) hasSetUIDStanza() bool {
defaultValue := true
version := s.getUpstartVersion()
if version == nil {
return defaultValue
}

maxVersion := []int{1, 4, 0}
if versionAtMost(version, maxVersion) {
return false
}

return defaultValue
}

func (s *upstart) getUpstartVersion() []int {
out, err := exec.Command("/sbin/init", "--version").Output()
if err != nil {
return defaultValue
return nil
}

re := regexp.MustCompile(`init \(upstart (\d+.\d+.\d+)\)`)
matches := re.FindStringSubmatch(string(out))
if len(matches) != 2 {
return defaultValue
return nil
}

version := make([]int, 3)
for idx, vStr := range strings.Split(matches[1], ".") {
version[idx], err = strconv.Atoi(vStr)
if err != nil {
return defaultValue
return nil
}
}

maxVersion := []int{0, 6, 5}
if versionAtMost(version, maxVersion) {
return false
}

return defaultValue
return version
}

func versionAtMost(version, max []int) bool {
Expand Down Expand Up @@ -133,12 +155,16 @@ func (s *upstart) Install() error {

var to = &struct {
*Config
Path string
HasKillStanza bool
Path string
HasKillStanza bool
HasSetUIDStanza bool
LogOutput bool
}{
s.Config,
path,
s.hasKillStanza(),
s.hasSetUIDStanza(),
s.Option.bool(optionLogOutput, optionLogOutputDefault),
}

return s.template().Execute(f, to)
Expand Down Expand Up @@ -209,7 +235,7 @@ const upstartScript = `# {{.Description}}
start on filesystem or runlevel [2345]
stop on runlevel [!2345]
{{if .UserName}}setuid {{.UserName}}{{end}}
{{if and .UserName .HasSetUIDStanza}}setuid {{.UserName}}{{end}}
respawn
respawn limit 10 5
Expand All @@ -222,5 +248,18 @@ pre-start script
end script
# Start
exec {{.Path}}{{range .Arguments}} {{.|cmd}}{{end}}
script
{{if .LogOutput}}
stdout_log="/var/log/{{.Name}}.out"
stderr_log="/var/log/{{.Name}}.err"
{{end}}
if [ -f "/etc/sysconfig/{{.Name}}" ]; then
set -a
source /etc/sysconfig/{{.Name}}
set +a
fi
exec {{if and .UserName (not .HasSetUIDStanza)}}sudo -E -u {{.UserName}} {{end}}{{.Path}}{{range .Arguments}} {{.|cmd}}{{end}}{{if .LogOutput}} >> $stdout_log 2>> $stderr_log{{end}}
end script
`

0 comments on commit 1166804

Please sign in to comment.