From dca2676ec1c3ac0b352ae01c91683b51765ef4ce Mon Sep 17 00:00:00 2001 From: chinhnc Date: Tue, 4 Aug 2020 09:06:44 +0700 Subject: [PATCH 1/3] update executablePath In cases where input path is ./binary then the service file will be invalid as the path is not absolute --- helper.go | 33 +++++++++++++++++++++++++++++---- 1 file changed, 29 insertions(+), 4 deletions(-) diff --git a/helper.go b/helper.go index e4bd4c1..55096e2 100644 --- a/helper.go +++ b/helper.go @@ -10,6 +10,8 @@ import ( "errors" "os" "os/exec" + "path" + "path/filepath" "strconv" "strings" ) @@ -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 From bebebe0a615ff329d97281bfe01054e90b760892 Mon Sep 17 00:00:00 2001 From: chinhnc Date: Tue, 4 Aug 2020 09:07:50 +0700 Subject: [PATCH 2/3] Fix installation failed Installation fails when executable path is not absolute --- daemon_linux_systemd.go | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/daemon_linux_systemd.go b/daemon_linux_systemd.go index d6e6cca..97be39d 100644 --- a/daemon_linux_systemd.go +++ b/daemon_linux_systemd.go @@ -7,6 +7,7 @@ package daemon import ( "os" "os/exec" + "path" "regexp" "strings" "text/template" @@ -85,9 +86,10 @@ 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, @@ -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 } @@ -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 From 47e84bbe7ce438c31edcdb758836875cdff08b11 Mon Sep 17 00:00:00 2001 From: chinhnc Date: Tue, 4 Aug 2020 09:09:16 +0700 Subject: [PATCH 3/3] Fix typo --- daemon_linux_systemd.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/daemon_linux_systemd.go b/daemon_linux_systemd.go index 97be39d..9046951 100644 --- a/daemon_linux_systemd.go +++ b/daemon_linux_systemd.go @@ -73,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 } @@ -92,7 +92,7 @@ func (linux *systemDRecord) Install(args ...string) (string, error) { path.Base(linux.name), linux.description, strings.Join(linux.dependencies, " "), - execPatch, + execPath, strings.Join(args, " "), }, ); err != nil {