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:
@@ -94,6 +94,8 @@ const (
|
||||
optionUpstartScript = "UpstartScript"
|
||||
optionLaunchdConfig = "LaunchdConfig"
|
||||
optionOpenRCScript = "OpenRCScript"
|
||||
|
||||
optionLogDirectory = "LogDirectory"
|
||||
)
|
||||
|
||||
// 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.
|
||||
// - SuccessExitStatus string () - The list of exit status that shall be considered as successful,
|
||||
// in addition to the default ones.
|
||||
// - LogDirectory string(/var/log) - The path to the log files directory
|
||||
//
|
||||
// * Linux (systemd)
|
||||
// - LimitNOFILE int (-1) - Maximum open files (ulimit -n)
|
||||
// (https://serverfault.com/questions/628610/increasing-nproc-for-processes-launched-by-systemd-on-centos-7)
|
||||
|
||||
+25
-15
@@ -20,7 +20,10 @@ import (
|
||||
|
||||
const maxPathSize = 32 * 1024
|
||||
|
||||
const version = "darwin-launchd"
|
||||
const (
|
||||
version = "darwin-launchd"
|
||||
defaultDarwinLogDirectory = "/var/log"
|
||||
)
|
||||
|
||||
type darwinSystem struct{}
|
||||
|
||||
@@ -109,16 +112,26 @@ func (s *darwinLaunchdService) getServiceFilePath() (string, error) {
|
||||
return "/Library/LaunchDaemons/" + s.Name + ".plist", nil
|
||||
}
|
||||
|
||||
func (s *darwinLaunchdService) getLogPath(logType string) (string, error) {
|
||||
if s.userService {
|
||||
homeDir, err := s.getHomeDir()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return fmt.Sprintf("%s/.%s.%s.log", homeDir, s.Name, logType), nil
|
||||
func (s *darwinLaunchdService) logDir() (string, error) {
|
||||
if customDir := s.Option.string(optionLogDirectory, ""); customDir != "" {
|
||||
return customDir, 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 {
|
||||
@@ -135,9 +148,8 @@ func (s *darwinLaunchdService) template() *template.Template {
|
||||
|
||||
if 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 {
|
||||
@@ -169,9 +181,7 @@ func (s *darwinLaunchdService) Install() error {
|
||||
return err
|
||||
}
|
||||
|
||||
stdOutPath, _ := s.getLogPath("out")
|
||||
stdErrPath, _ := s.getLogPath("err")
|
||||
|
||||
stdOutPath, stdErrPath, _ := s.getLogPaths()
|
||||
var to = &struct {
|
||||
*Config
|
||||
Path string
|
||||
|
||||
@@ -60,9 +60,8 @@ func (s *openrc) template() *template.Template {
|
||||
|
||||
if 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) {
|
||||
@@ -113,10 +112,12 @@ func (s *openrc) Install() error {
|
||||
|
||||
var to = &struct {
|
||||
*Config
|
||||
Path string
|
||||
Path string
|
||||
LogDirectory string
|
||||
}{
|
||||
s.Config,
|
||||
path,
|
||||
s.Option.string(optionLogDirectory, defaultLogDirectory),
|
||||
}
|
||||
|
||||
err = s.template().Execute(f, to)
|
||||
@@ -227,7 +228,7 @@ command={{.Path|cmdEscape}}
|
||||
command_args="{{range .Arguments}}{{.}} {{end}}"
|
||||
{{- end }}
|
||||
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 }}
|
||||
depend() {
|
||||
|
||||
@@ -128,9 +128,8 @@ func (s *systemd) template() *template.Template {
|
||||
|
||||
if 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 {
|
||||
@@ -168,6 +167,7 @@ func (s *systemd) Install() error {
|
||||
Restart string
|
||||
SuccessExitStatus string
|
||||
LogOutput bool
|
||||
LogDirectory string
|
||||
}{
|
||||
s.Config,
|
||||
path,
|
||||
@@ -178,6 +178,7 @@ func (s *systemd) Install() error {
|
||||
s.Option.string(optionRestart, "always"),
|
||||
s.Option.string(optionSuccessExitStatus, ""),
|
||||
s.Option.bool(optionLogOutput, optionLogOutputDefault),
|
||||
s.Option.string(optionLogDirectory, defaultLogDirectory),
|
||||
}
|
||||
|
||||
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 .PIDFile}}PIDFile={{.PIDFile|cmd}}{{end}}
|
||||
{{if and .LogOutput .HasOutputFileSupport -}}
|
||||
StandardOutput=file:/var/log/{{.Name}}.out
|
||||
StandardError=file:/var/log/{{.Name}}.err
|
||||
StandardOutput=file:{{.LogDirectory}}/{{.Name}}.out
|
||||
StandardError=file:{{.LogDirectory}}/{{.Name}}.err
|
||||
{{- end}}
|
||||
{{if gt .LimitNOFILE -1 }}LimitNOFILE={{.LimitNOFILE}}{{end}}
|
||||
{{if .Restart}}Restart={{.Restart}}{{end}}
|
||||
|
||||
@@ -58,9 +58,8 @@ func (s *sysv) template() *template.Template {
|
||||
|
||||
if 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 {
|
||||
@@ -86,10 +85,12 @@ func (s *sysv) Install() error {
|
||||
|
||||
var to = &struct {
|
||||
*Config
|
||||
Path string
|
||||
Path string
|
||||
LogDirectory string
|
||||
}{
|
||||
s.Config,
|
||||
path,
|
||||
s.Option.string(optionLogDirectory, defaultLogDirectory),
|
||||
}
|
||||
|
||||
err = s.template().Execute(f, to)
|
||||
@@ -203,8 +204,8 @@ cmd="{{.Path}}{{range .Arguments}} {{.|cmd}}{{end}}"
|
||||
|
||||
name=$(basename $(readlink -f $0))
|
||||
pid_file="/var/run/$name.pid"
|
||||
stdout_log="/var/log/$name.log"
|
||||
stderr_log="/var/log/$name.err"
|
||||
stdout_log="{{.LogDirectory}}/$name.log"
|
||||
stderr_log="{{.LogDirectory}}/$name.err"
|
||||
|
||||
[ -e /etc/sysconfig/$name ] && . /etc/sysconfig/$name
|
||||
|
||||
|
||||
@@ -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
|
||||
@@ -16,6 +17,8 @@ import (
|
||||
"syscall"
|
||||
)
|
||||
|
||||
const defaultLogDirectory = "/var/log"
|
||||
|
||||
func newSysLogger(name string, errs chan<- error) (Logger, error) {
|
||||
w, err := syslog.New(syslog.LOG_INFO, name)
|
||||
if err != nil {
|
||||
|
||||
@@ -152,12 +152,14 @@ func (s *upstart) Install() error {
|
||||
HasKillStanza bool
|
||||
HasSetUIDStanza bool
|
||||
LogOutput bool
|
||||
LogDirectory string
|
||||
}{
|
||||
s.Config,
|
||||
path,
|
||||
s.hasKillStanza(),
|
||||
s.hasSetUIDStanza(),
|
||||
s.Option.bool(optionLogOutput, optionLogOutputDefault),
|
||||
s.Option.string(optionLogDirectory, defaultLogDirectory),
|
||||
}
|
||||
|
||||
return s.template().Execute(f, to)
|
||||
@@ -254,8 +256,8 @@ end script
|
||||
# Start
|
||||
script
|
||||
{{if .LogOutput}}
|
||||
stdout_log="/var/log/{{.Name}}.out"
|
||||
stderr_log="/var/log/{{.Name}}.err"
|
||||
stdout_log="{{.LogDirectory}}/{{.Name}}.out"
|
||||
stderr_log="{{.LogDirectory}}/{{.Name}}.err"
|
||||
{{end}}
|
||||
|
||||
if [ -f "/etc/sysconfig/{{.Name}}" ]; then
|
||||
|
||||
Reference in New Issue
Block a user