BI-1472: Fix upstart service on 0.6.5 and earlier

This commit is contained in:
Ryan Chipman
2018-02-16 12:56:57 -05:00
committed by Daniel Theophanes
parent 89346fbade
commit 1dd765d045
+47 -2
View File
@@ -10,6 +10,8 @@ import (
"os" "os"
"os/exec" "os/exec"
"os/signal" "os/signal"
"regexp"
"strconv"
"strings" "strings"
"text/template" "text/template"
"time" "time"
@@ -63,6 +65,47 @@ func (s *upstart) configPath() (cp string, err error) {
cp = "/etc/init/" + s.Config.Name + ".conf" cp = "/etc/init/" + s.Config.Name + ".conf"
return return
} }
func (s *upstart) hasKillStanza() bool {
defaultValue := true
out, err := exec.Command("/sbin/init", "--version").Output()
if err != nil {
return defaultValue
}
re := regexp.MustCompile(`init \(upstart (\d+.\d+.\d+)\)`)
matches := re.FindStringSubmatch(string(out))
if len(matches) != 2 {
return defaultValue
}
version := make([]int, 3)
for idx, vStr := range strings.Split(matches[1], ".") {
version[idx], err = strconv.Atoi(vStr)
if err != nil {
return defaultValue
}
}
maxVersion := []int{0, 6, 5}
if versionAtMost(version, maxVersion) {
return false
}
return defaultValue
}
func versionAtMost(version, max []int) bool {
for idx, m := range max {
v := version[idx]
if v > m {
return false
}
}
return true
}
func (s *upstart) template() *template.Template { func (s *upstart) template() *template.Template {
return template.Must(template.New("").Funcs(tf).Parse(upstartScript)) return template.Must(template.New("").Funcs(tf).Parse(upstartScript))
} }
@@ -90,10 +133,12 @@ func (s *upstart) Install() error {
var to = &struct { var to = &struct {
*Config *Config
Path string Path string
HasKillStanza bool
}{ }{
s.Config, s.Config,
path, path,
s.hasKillStanza(),
} }
return s.template().Execute(f, to) return s.template().Execute(f, to)
@@ -158,7 +203,7 @@ const upstartScript = `# {{.Description}}
{{if .DisplayName}}description "{{.DisplayName}}"{{end}} {{if .DisplayName}}description "{{.DisplayName}}"{{end}}
kill signal INT {{if .HasKillStanza}}kill signal INT{{end}}
{{if .ChRoot}}chroot {{.ChRoot}}{{end}} {{if .ChRoot}}chroot {{.ChRoot}}{{end}}
{{if .WorkingDirectory}}chdir {{.WorkingDirectory}}{{end}} {{if .WorkingDirectory}}chdir {{.WorkingDirectory}}{{end}}
start on filesystem or runlevel [2345] start on filesystem or runlevel [2345]