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

Fix systemd install failures #95

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
14 changes: 8 additions & 6 deletions daemon_linux_systemd.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package daemon
import (
"os"
"os/exec"
"path"
"regexp"
"strings"
"text/template"
Expand Down Expand Up @@ -72,7 +73,7 @@ func (linux *systemDRecord) Install(args ...string) (string, error) {
}
defer file.Close()

execPatch, err := executablePath(linux.name)
execPath, err := executablePath(linux.name)
if err != nil {
return installAction + failed, err
}
Expand All @@ -85,12 +86,13 @@ func (linux *systemDRecord) Install(args ...string) (string, error) {
if err := templ.Execute(
file,
&struct {
Name, Description, Dependencies, Path, Args string
Name, BaseName, Description, Dependencies, Path, Args string
}{
linux.name,
path.Base(linux.name),
linux.description,
strings.Join(linux.dependencies, " "),
execPatch,
execPath,
strings.Join(args, " "),
},
); err != nil {
Expand All @@ -101,7 +103,7 @@ func (linux *systemDRecord) Install(args ...string) (string, error) {
return installAction + failed, err
}

if err := exec.Command("systemctl", "enable", linux.name+".service").Run(); err != nil {
if err := exec.Command("systemctl", "enable", path.Base(linux.name)+".service").Run(); err != nil {
return installAction + failed, err
}

Expand Down Expand Up @@ -217,8 +219,8 @@ Requires={{.Dependencies}}
After={{.Dependencies}}

[Service]
PIDFile=/var/run/{{.Name}}.pid
ExecStartPre=/bin/rm -f /var/run/{{.Name}}.pid
PIDFile=/var/run/{{.BaseName}}.pid
ExecStartPre=/bin/rm -f /var/run/{{.BaseName}}.pid
ExecStart={{.Path}} {{.Args}}
Restart=on-failure

Expand Down
33 changes: 29 additions & 4 deletions helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"errors"
"os"
"os/exec"
"path"
"path/filepath"
"strconv"
"strings"
)
Expand Down Expand Up @@ -47,12 +49,35 @@ func ExecPath() (string, error) {

// Lookup path for executable file
func executablePath(name string) (string, error) {
if path, err := exec.LookPath(name); err == nil {
if _, err := os.Stat(path); err == nil {
return path, nil
if p, err := exec.LookPath(name); err == nil {
if _, err := os.Stat(p); err == nil {
cwd, _ := os.Getwd()
if strings.HasPrefix(p, "./") {
p = path.Join(cwd, path.Base(p))
}
return p, nil
}
}
return os.Executable()

return findBinaryPath()
}

func findBinaryPath() (string, error) {
ctlExePath, err := os.Executable()
if err != nil {
return "./", err
}

dir, err := filepath.EvalSymlinks(ctlExePath)
if err != nil {
return "./", err
}

cwd, _ := os.Getwd()
if strings.HasPrefix(dir, "./") {
dir = path.Join(cwd, path.Base(dir))
}
return filepath.Abs(filepath.Dir(dir))
}

// Check root rights to use system service
Expand Down