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

systemd environment file: lookup for both /etc/default (debian like) and /etc/sysconfig (centos) #350

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
25 changes: 24 additions & 1 deletion service_systemd_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import (
"text/template"
)

const defaultEnvDir = "/etc/sysconfig"

func isSystemd() bool {
if _, err := os.Stat("/run/systemd/system"); err == nil {
return true
Expand Down Expand Up @@ -89,6 +91,25 @@ func (s *systemd) configPath() (cp string, err error) {
return
}

func (s *systemd) envFilePath() (ep string) {
dirs := []string{"/etc/default", "/etc/sysconfig"}
// first lookup for existing file for backward compatibility
for _, dir := range dirs {
ep = filepath.Join(dir, s.Name)
if _, err := os.Stat(ep); err == nil {
return
}
}
// if not exists, lookup system default
for _, v := range dirs {
if _, err := os.Stat(v); err == nil {
ep = filepath.Join(v, s.Name)
return
}
}
return filepath.Join(defaultEnvDir, s.Name)
}

func (s *systemd) unitName() string {
return s.Config.Name + ".service"
}
Expand Down Expand Up @@ -172,6 +193,7 @@ func (s *systemd) Install() error {
SuccessExitStatus string
LogOutput bool
LogDirectory string
EnvFile string
}{
s.Config,
path,
Expand All @@ -183,6 +205,7 @@ func (s *systemd) Install() error {
s.Option.string(optionSuccessExitStatus, ""),
s.Option.bool(optionLogOutput, optionLogOutputDefault),
s.Option.string(optionLogDirectory, defaultLogDirectory),
s.envFilePath(),
}

err = s.template().Execute(f, to)
Expand Down Expand Up @@ -321,7 +344,7 @@ StandardError=file:{{.LogDirectory}}/{{.Name}}.err
{{if .Restart}}Restart={{.Restart}}{{end}}
{{if .SuccessExitStatus}}SuccessExitStatus={{.SuccessExitStatus}}{{end}}
RestartSec=120
EnvironmentFile=-/etc/sysconfig/{{.Name}}
EnvironmentFile=-{{.EnvFile}}

{{range $k, $v := .EnvVars -}}
Environment={{$k}}={{$v}}
Expand Down