update upstart script
This commit is contained in:
committed by
Daniel Theophanes
parent
615a14ed75
commit
1166804cbc
+18
-3
@@ -75,12 +75,26 @@ const (
|
|||||||
optionUserServiceDefault = false
|
optionUserServiceDefault = false
|
||||||
optionSessionCreate = "SessionCreate"
|
optionSessionCreate = "SessionCreate"
|
||||||
optionSessionCreateDefault = false
|
optionSessionCreateDefault = false
|
||||||
|
optionLogOutput = "LogOutput"
|
||||||
|
optionLogOutputDefault = false
|
||||||
|
|
||||||
optionRunWait = "RunWait"
|
optionRunWait = "RunWait"
|
||||||
optionReloadSignal = "ReloadSignal"
|
optionReloadSignal = "ReloadSignal"
|
||||||
optionPIDFile = "PIDFile"
|
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.
|
// Config provides the setup for a Service. The Name field is required.
|
||||||
type Config struct {
|
type Config struct {
|
||||||
Name string // Required name of the service. No spaces suggested.
|
Name string // Required name of the service. No spaces suggested.
|
||||||
@@ -108,9 +122,10 @@ type Config struct {
|
|||||||
// - UserService bool (false) - Install as a current user service.
|
// - UserService bool (false) - Install as a current user service.
|
||||||
// - SessionCreate bool (false) - Create a full user session.
|
// - SessionCreate bool (false) - Create a full user session.
|
||||||
// * POSIX
|
// * POSIX
|
||||||
// - RunWait func() (wait for SIGNAL) - Do not install signal but wait for this function to return.
|
// - RunWait func() (wait for SIGNAL) - Do not install signal but wait for this function to return.
|
||||||
// - ReloadSignal string () [USR1, ...] - Signal to send on reaload.
|
// - ReloadSignal string () [USR1, ...] - Signal to send on reaload.
|
||||||
// - PIDFile string () [/run/prog.pid] - Location of the PID file.
|
// - PIDFile string () [/run/prog.pid] - Location of the PID file.
|
||||||
|
// - LogOutput bool (false) - Redirect StdErr & StdOut to files.
|
||||||
Option KeyValue
|
Option KeyValue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+60
-21
@@ -68,26 +68,11 @@ func (s *upstart) configPath() (cp string, err error) {
|
|||||||
|
|
||||||
func (s *upstart) hasKillStanza() bool {
|
func (s *upstart) hasKillStanza() bool {
|
||||||
defaultValue := true
|
defaultValue := true
|
||||||
|
version := s.getUpstartVersion()
|
||||||
out, err := exec.Command("/sbin/init", "--version").Output()
|
if version == nil {
|
||||||
if err != nil {
|
|
||||||
return defaultValue
|
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}
|
maxVersion := []int{0, 6, 5}
|
||||||
if versionAtMost(version, maxVersion) {
|
if versionAtMost(version, maxVersion) {
|
||||||
return false
|
return false
|
||||||
@@ -96,6 +81,43 @@ func (s *upstart) hasKillStanza() bool {
|
|||||||
return defaultValue
|
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 nil
|
||||||
|
}
|
||||||
|
|
||||||
|
re := regexp.MustCompile(`init \(upstart (\d+.\d+.\d+)\)`)
|
||||||
|
matches := re.FindStringSubmatch(string(out))
|
||||||
|
if len(matches) != 2 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
version := make([]int, 3)
|
||||||
|
for idx, vStr := range strings.Split(matches[1], ".") {
|
||||||
|
version[idx], err = strconv.Atoi(vStr)
|
||||||
|
if err != nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return version
|
||||||
|
}
|
||||||
|
|
||||||
func versionAtMost(version, max []int) bool {
|
func versionAtMost(version, max []int) bool {
|
||||||
for idx, m := range max {
|
for idx, m := range max {
|
||||||
v := version[idx]
|
v := version[idx]
|
||||||
@@ -133,12 +155,16 @@ func (s *upstart) Install() error {
|
|||||||
|
|
||||||
var to = &struct {
|
var to = &struct {
|
||||||
*Config
|
*Config
|
||||||
Path string
|
Path string
|
||||||
HasKillStanza bool
|
HasKillStanza bool
|
||||||
|
HasSetUIDStanza bool
|
||||||
|
LogOutput bool
|
||||||
}{
|
}{
|
||||||
s.Config,
|
s.Config,
|
||||||
path,
|
path,
|
||||||
s.hasKillStanza(),
|
s.hasKillStanza(),
|
||||||
|
s.hasSetUIDStanza(),
|
||||||
|
s.Option.bool(optionLogOutput, optionLogOutputDefault),
|
||||||
}
|
}
|
||||||
|
|
||||||
return s.template().Execute(f, to)
|
return s.template().Execute(f, to)
|
||||||
@@ -209,7 +235,7 @@ const upstartScript = `# {{.Description}}
|
|||||||
start on filesystem or runlevel [2345]
|
start on filesystem or runlevel [2345]
|
||||||
stop on runlevel [!2345]
|
stop on runlevel [!2345]
|
||||||
|
|
||||||
{{if .UserName}}setuid {{.UserName}}{{end}}
|
{{if and .UserName .HasSetUIDStanza}}setuid {{.UserName}}{{end}}
|
||||||
|
|
||||||
respawn
|
respawn
|
||||||
respawn limit 10 5
|
respawn limit 10 5
|
||||||
@@ -222,5 +248,18 @@ pre-start script
|
|||||||
end script
|
end script
|
||||||
|
|
||||||
# Start
|
# 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
|
||||||
`
|
`
|
||||||
|
|||||||
Reference in New Issue
Block a user