Provide custom log directory for posix (#311)

When running a service with posix, provide a custom path to the
service output
This commit is contained in:
tomfeigin
2022-04-28 15:56:17 +03:00
committed by GitHub
parent bc65850ac5
commit abe98a9274
7 changed files with 52 additions and 30 deletions
+4
View File
@@ -94,6 +94,8 @@ const (
optionUpstartScript = "UpstartScript" optionUpstartScript = "UpstartScript"
optionLaunchdConfig = "LaunchdConfig" optionLaunchdConfig = "LaunchdConfig"
optionOpenRCScript = "OpenRCScript" optionOpenRCScript = "OpenRCScript"
optionLogDirectory = "LogDirectory"
) )
// Status represents service status as an byte value // Status represents service status as an byte value
@@ -186,6 +188,8 @@ func New(i Interface, c *Config) (Service, error) {
// - Restart string (always) - How shall service be restarted. // - Restart string (always) - How shall service be restarted.
// - SuccessExitStatus string () - The list of exit status that shall be considered as successful, // - SuccessExitStatus string () - The list of exit status that shall be considered as successful,
// in addition to the default ones. // in addition to the default ones.
// - LogDirectory string(/var/log) - The path to the log files directory
//
// * Linux (systemd) // * Linux (systemd)
// - LimitNOFILE int (-1) - Maximum open files (ulimit -n) // - LimitNOFILE int (-1) - Maximum open files (ulimit -n)
// (https://serverfault.com/questions/628610/increasing-nproc-for-processes-launched-by-systemd-on-centos-7) // (https://serverfault.com/questions/628610/increasing-nproc-for-processes-launched-by-systemd-on-centos-7)
+25 -15
View File
@@ -20,7 +20,10 @@ import (
const maxPathSize = 32 * 1024 const maxPathSize = 32 * 1024
const version = "darwin-launchd" const (
version = "darwin-launchd"
defaultDarwinLogDirectory = "/var/log"
)
type darwinSystem struct{} type darwinSystem struct{}
@@ -109,16 +112,26 @@ func (s *darwinLaunchdService) getServiceFilePath() (string, error) {
return "/Library/LaunchDaemons/" + s.Name + ".plist", nil return "/Library/LaunchDaemons/" + s.Name + ".plist", nil
} }
func (s *darwinLaunchdService) getLogPath(logType string) (string, error) { func (s *darwinLaunchdService) logDir() (string, error) {
if s.userService { if customDir := s.Option.string(optionLogDirectory, ""); customDir != "" {
homeDir, err := s.getHomeDir() return customDir, nil
if err != nil {
return "", err
}
return fmt.Sprintf("%s/.%s.%s.log", homeDir, s.Name, logType), nil
} }
return fmt.Sprintf("%s/%s.%s.log", "/var/log", s.Name, logType), nil if !s.userService {
return defaultDarwinLogDirectory, nil
}
return s.getHomeDir()
}
func (s *darwinLaunchdService) getLogPaths() (string, string, error) {
logDir, err := s.logDir()
if err != nil {
return "", "", err
}
return s.getLogPath(logDir, "out"), s.getLogPath(logDir, "err"), nil
}
func (s *darwinLaunchdService) getLogPath(logDir, logType string) string {
return fmt.Sprintf("%s/%s.%s.log", logDir, s.Name, logType)
} }
func (s *darwinLaunchdService) template() *template.Template { func (s *darwinLaunchdService) template() *template.Template {
@@ -135,9 +148,8 @@ func (s *darwinLaunchdService) template() *template.Template {
if customConfig != "" { if customConfig != "" {
return template.Must(template.New("").Funcs(functions).Parse(customConfig)) return template.Must(template.New("").Funcs(functions).Parse(customConfig))
} else {
return template.Must(template.New("").Funcs(functions).Parse(launchdConfig))
} }
return template.Must(template.New("").Funcs(functions).Parse(launchdConfig))
} }
func (s *darwinLaunchdService) Install() error { func (s *darwinLaunchdService) Install() error {
@@ -169,9 +181,7 @@ func (s *darwinLaunchdService) Install() error {
return err return err
} }
stdOutPath, _ := s.getLogPath("out") stdOutPath, stdErrPath, _ := s.getLogPaths()
stdErrPath, _ := s.getLogPath("err")
var to = &struct { var to = &struct {
*Config *Config
Path string Path string
+5 -4
View File
@@ -60,9 +60,8 @@ func (s *openrc) template() *template.Template {
if customScript != "" { if customScript != "" {
return template.Must(template.New("").Funcs(tf).Parse(customScript)) return template.Must(template.New("").Funcs(tf).Parse(customScript))
} else {
return template.Must(template.New("").Funcs(tf).Parse(openRCScript))
} }
return template.Must(template.New("").Funcs(tf).Parse(openRCScript))
} }
func newOpenRCService(i Interface, platform string, c *Config) (Service, error) { func newOpenRCService(i Interface, platform string, c *Config) (Service, error) {
@@ -113,10 +112,12 @@ func (s *openrc) Install() error {
var to = &struct { var to = &struct {
*Config *Config
Path string Path string
LogDirectory string
}{ }{
s.Config, s.Config,
path, path,
s.Option.string(optionLogDirectory, defaultLogDirectory),
} }
err = s.template().Execute(f, to) err = s.template().Execute(f, to)
@@ -227,7 +228,7 @@ command={{.Path|cmdEscape}}
command_args="{{range .Arguments}}{{.}} {{end}}" command_args="{{range .Arguments}}{{.}} {{end}}"
{{- end }} {{- end }}
name=$(basename $(readlink -f $command)) name=$(basename $(readlink -f $command))
supervise_daemon_args="--stdout /var/log/${name}.log --stderr /var/log/${name}.err" supervise_daemon_args="--stdout {{.LogDirectory}}/${name}.log --stderr {{.LogDirectory}}/${name}.err"
{{- if .Dependencies }} {{- if .Dependencies }}
depend() { depend() {
+5 -4
View File
@@ -128,9 +128,8 @@ func (s *systemd) template() *template.Template {
if customScript != "" { if customScript != "" {
return template.Must(template.New("").Funcs(tf).Parse(customScript)) return template.Must(template.New("").Funcs(tf).Parse(customScript))
} else {
return template.Must(template.New("").Funcs(tf).Parse(systemdScript))
} }
return template.Must(template.New("").Funcs(tf).Parse(systemdScript))
} }
func (s *systemd) isUserService() bool { func (s *systemd) isUserService() bool {
@@ -168,6 +167,7 @@ func (s *systemd) Install() error {
Restart string Restart string
SuccessExitStatus string SuccessExitStatus string
LogOutput bool LogOutput bool
LogDirectory string
}{ }{
s.Config, s.Config,
path, path,
@@ -178,6 +178,7 @@ func (s *systemd) Install() error {
s.Option.string(optionRestart, "always"), s.Option.string(optionRestart, "always"),
s.Option.string(optionSuccessExitStatus, ""), s.Option.string(optionSuccessExitStatus, ""),
s.Option.bool(optionLogOutput, optionLogOutputDefault), s.Option.bool(optionLogOutput, optionLogOutputDefault),
s.Option.string(optionLogDirectory, defaultLogDirectory),
} }
err = s.template().Execute(f, to) err = s.template().Execute(f, to)
@@ -309,8 +310,8 @@ ExecStart={{.Path|cmdEscape}}{{range .Arguments}} {{.|cmd}}{{end}}
{{if .ReloadSignal}}ExecReload=/bin/kill -{{.ReloadSignal}} "$MAINPID"{{end}} {{if .ReloadSignal}}ExecReload=/bin/kill -{{.ReloadSignal}} "$MAINPID"{{end}}
{{if .PIDFile}}PIDFile={{.PIDFile|cmd}}{{end}} {{if .PIDFile}}PIDFile={{.PIDFile|cmd}}{{end}}
{{if and .LogOutput .HasOutputFileSupport -}} {{if and .LogOutput .HasOutputFileSupport -}}
StandardOutput=file:/var/log/{{.Name}}.out StandardOutput=file:{{.LogDirectory}}/{{.Name}}.out
StandardError=file:/var/log/{{.Name}}.err StandardError=file:{{.LogDirectory}}/{{.Name}}.err
{{- end}} {{- end}}
{{if gt .LimitNOFILE -1 }}LimitNOFILE={{.LimitNOFILE}}{{end}} {{if gt .LimitNOFILE -1 }}LimitNOFILE={{.LimitNOFILE}}{{end}}
{{if .Restart}}Restart={{.Restart}}{{end}} {{if .Restart}}Restart={{.Restart}}{{end}}
+6 -5
View File
@@ -58,9 +58,8 @@ func (s *sysv) template() *template.Template {
if customScript != "" { if customScript != "" {
return template.Must(template.New("").Funcs(tf).Parse(customScript)) return template.Must(template.New("").Funcs(tf).Parse(customScript))
} else {
return template.Must(template.New("").Funcs(tf).Parse(sysvScript))
} }
return template.Must(template.New("").Funcs(tf).Parse(sysvScript))
} }
func (s *sysv) Install() error { func (s *sysv) Install() error {
@@ -86,10 +85,12 @@ func (s *sysv) Install() error {
var to = &struct { var to = &struct {
*Config *Config
Path string Path string
LogDirectory string
}{ }{
s.Config, s.Config,
path, path,
s.Option.string(optionLogDirectory, defaultLogDirectory),
} }
err = s.template().Execute(f, to) err = s.template().Execute(f, to)
@@ -203,8 +204,8 @@ cmd="{{.Path}}{{range .Arguments}} {{.|cmd}}{{end}}"
name=$(basename $(readlink -f $0)) name=$(basename $(readlink -f $0))
pid_file="/var/run/$name.pid" pid_file="/var/run/$name.pid"
stdout_log="/var/log/$name.log" stdout_log="{{.LogDirectory}}/$name.log"
stderr_log="/var/log/$name.err" stderr_log="{{.LogDirectory}}/$name.err"
[ -e /etc/sysconfig/$name ] && . /etc/sysconfig/$name [ -e /etc/sysconfig/$name ] && . /etc/sysconfig/$name
+3
View File
@@ -2,6 +2,7 @@
// Use of this source code is governed by a zlib-style // Use of this source code is governed by a zlib-style
// license that can be found in the LICENSE file. // license that can be found in the LICENSE file.
//go:build linux || darwin || solaris || aix || freebsd
// +build linux darwin solaris aix freebsd // +build linux darwin solaris aix freebsd
package service package service
@@ -16,6 +17,8 @@ import (
"syscall" "syscall"
) )
const defaultLogDirectory = "/var/log"
func newSysLogger(name string, errs chan<- error) (Logger, error) { func newSysLogger(name string, errs chan<- error) (Logger, error) {
w, err := syslog.New(syslog.LOG_INFO, name) w, err := syslog.New(syslog.LOG_INFO, name)
if err != nil { if err != nil {
+4 -2
View File
@@ -152,12 +152,14 @@ func (s *upstart) Install() error {
HasKillStanza bool HasKillStanza bool
HasSetUIDStanza bool HasSetUIDStanza bool
LogOutput bool LogOutput bool
LogDirectory string
}{ }{
s.Config, s.Config,
path, path,
s.hasKillStanza(), s.hasKillStanza(),
s.hasSetUIDStanza(), s.hasSetUIDStanza(),
s.Option.bool(optionLogOutput, optionLogOutputDefault), s.Option.bool(optionLogOutput, optionLogOutputDefault),
s.Option.string(optionLogDirectory, defaultLogDirectory),
} }
return s.template().Execute(f, to) return s.template().Execute(f, to)
@@ -254,8 +256,8 @@ end script
# Start # Start
script script
{{if .LogOutput}} {{if .LogOutput}}
stdout_log="/var/log/{{.Name}}.out" stdout_log="{{.LogDirectory}}/{{.Name}}.out"
stderr_log="/var/log/{{.Name}}.err" stderr_log="{{.LogDirectory}}/{{.Name}}.err"
{{end}} {{end}}
if [ -f "/etc/sysconfig/{{.Name}}" ]; then if [ -f "/etc/sysconfig/{{.Name}}" ]; then