Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(systemd): imp systemd config #320

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions example/simple/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,26 @@ func main() {
Name: "GoServiceExampleSimple",
DisplayName: "Go Service Example",
Description: "This is an example Go service.",
ExecStartPres: []string{
"/usr/bin/ls /tmp/",
},
}

prg := &program{}
s, err := service.New(prg, svcConfig)
if err != nil {
log.Fatal(err)
return
}
logger, err = s.Logger(nil)
if err != nil {
log.Fatal(err)
return
}
err = s.Install()
if err != nil {
logger.Error(err)
return
}
err = s.Run()
if err != nil {
Expand Down
Binary file added example/simple/simple
Binary file not shown.
29 changes: 24 additions & 5 deletions service.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,20 @@ const (
optionLimitNOFILE = "LimitNOFILE"
optionLimitNOFILEDefault = -1 // -1 = don't set in configuration
optionRestart = "Restart"
optionRestartSec = "RestartSec"
optionRestartSecDefault = 30

optionSuccessExitStatus = "SuccessExitStatus"

optionSystemdScript = "SystemdScript"
optionSysvScript = "SysvScript"
optionUpstartScript = "UpstartScript"
optionLaunchdConfig = "LaunchdConfig"
optionOpenRCScript = "OpenRCScript"
optionSystemdScript = "SystemdScript"
optionSysvScript = "SysvScript"
optionUpstartScript = "UpstartScript"
optionLaunchdConfig = "LaunchdConfig"
optionOpenRCScript = "OpenRCScript"
optionType = "Type"
optionKillMode = "KillMode"
optionDelegate = "Delegate"
optionDelegateDefault = false
)

// Status represents service status as an byte value
Expand Down Expand Up @@ -128,6 +134,19 @@ type Config struct {
// the generated service config file, will not check their correctness.
Dependencies []string

// Optional
// Not yet fully implemented on Linux or OS X:
// 1. Support linux-systemd dependencies, just put each full line as the
// default
// /etc/sysconfig/%N
// /etc/default/%N
// /etc/systemd/system/{{ .Name }}.service.env
// you can config Other Configuration paths
EnvironmentFiles []string

// Optional pre exec start
ExecStartPres []string

// The following fields are not supported on Windows.
WorkingDirectory string // Initial working directory.
ChRoot string
Expand Down
3 changes: 2 additions & 1 deletion service_aix.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
//+build aix
//go:build aix
// +build aix

// Copyright 2015 Daniel Theophanes.
// Use of this source code is governed by a zlib-style
Expand Down
3 changes: 2 additions & 1 deletion service_go1.8.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
//+build go1.8
//go:build go1.8
// +build go1.8

package service

Expand Down
1 change: 1 addition & 0 deletions service_nosu_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Use of this source code is governed by a zlib-style
// license that can be found in the LICENSE file.

//go:build !su
// +build !su

package service_test
Expand Down
1 change: 1 addition & 0 deletions service_su_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// license that can be found in the LICENSE file.

// This needs to be run as root/admin hence the reason there is a build tag
//go:build su
// +build su

package service_test
Expand Down
24 changes: 22 additions & 2 deletions service_systemd_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,12 @@ func (s *systemd) Install() error {
PIDFile string
LimitNOFILE int
Restart string
RestartSec int
SuccessExitStatus string
LogOutput bool
Type string
KillMode string
Delegate bool
}{
s.Config,
path,
Expand All @@ -176,8 +180,12 @@ func (s *systemd) Install() error {
s.Option.string(optionPIDFile, ""),
s.Option.int(optionLimitNOFILE, optionLimitNOFILEDefault),
s.Option.string(optionRestart, "always"),
s.Option.int(optionRestartSec, optionRestartSecDefault),
s.Option.string(optionSuccessExitStatus, ""),
s.Option.bool(optionLogOutput, optionLogOutputDefault),
s.Option.string(optionType, ""),
s.Option.string(optionKillMode, ""),
s.Option.bool(optionDelegate, optionDelegateDefault),
}

err = s.template().Execute(f, to)
Expand Down Expand Up @@ -293,8 +301,18 @@ ConditionFileIsExecutable={{.Path|cmdEscape}}
{{$dep}} {{end}}

[Service]
{{if .Type}}Type={{.Type}}{{end}}
{{if .KillMode}}Type={{.KillMode}}{{end}}
{{if .Delegate}}Delegate=yes{{end}}
EnvironmentFile=-/etc/sysconfig/%N
EnvironmentFile=-/etc/default/%N
EnvironmentFile=-/etc/systemd/system/{{.Name}}.service.env
{{range $i, $envfile := .EnvironmentFiles}}
EnvironmentFile=-{{$envfile}} {{- end}}
StartLimitInterval=5
StartLimitBurst=10
{{range $i, $pre := .ExecStartPres}}
ExecStartPre=-{{$pre}} {{- end}}
ExecStart={{.Path|cmdEscape}}{{range .Arguments}} {{.|cmd}}{{end}}
{{if .ChRoot}}RootDirectory={{.ChRoot|cmd}}{{end}}
{{if .WorkingDirectory}}WorkingDirectory={{.WorkingDirectory|cmdEscape}}{{end}}
Expand All @@ -306,10 +324,12 @@ StandardOutput=file:/var/log/{{.Name}}.out
StandardError=file:/var/log/{{.Name}}.err
{{- end}}
{{if gt .LimitNOFILE -1 }}LimitNOFILE={{.LimitNOFILE}}{{end}}
LimitNPROC=infinity
LimitCORE=infinity
TasksMax=infinity
{{if .Restart}}Restart={{.Restart}}{{end}}
{{if .RestartSec}}RestartSec={{.RestartSec}}{{end}}
{{if .SuccessExitStatus}}SuccessExitStatus={{.SuccessExitStatus}}{{end}}
RestartSec=120
EnvironmentFile=-/etc/sysconfig/{{.Name}}

[Install]
WantedBy=multi-user.target
Expand Down
1 change: 1 addition & 0 deletions service_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Use of this source code is governed by a zlib-style
// license that can be found in the LICENSE file.

//go:build linux || darwin || solaris || aix || freebsd
// +build linux darwin solaris aix freebsd

package service
Expand Down
1 change: 1 addition & 0 deletions servicetest_unix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Use of this source code is governed by a zlib-style
// license that can be found in the LICENSE file.

//go:build darwin || dragonfly || freebsd || linux || nacl || netbsd || openbsd || solaris
// +build darwin dragonfly freebsd linux nacl netbsd openbsd solaris

package service_test
Expand Down