Skip to content

Feature set exec path #67

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

Open
wants to merge 5 commits into
base: develop
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
4 changes: 2 additions & 2 deletions daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,6 @@ type Executable interface {
// name: name of the service
//
// description: any explanation, what is the service, its purpose
func New(name, description string, dependencies ...string) (Daemon, error) {
return newDaemon(strings.Join(strings.Fields(name), "_"), description, dependencies)
func New(name, description, execStartPath string, dependencies ...string) (Daemon, error) {
return newDaemon(strings.Join(strings.Fields(name), "_"), description, execStartPath, dependencies)
}
30 changes: 19 additions & 11 deletions daemon_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@ import (

// darwinRecord - standard record (struct) for darwin version of daemon package
type darwinRecord struct {
name string
description string
dependencies []string
name string
description string
execStartPath string
dependencies []string
}

func newDaemon(name, description string, dependencies []string) (Daemon, error) {
func newDaemon(name, description, execStartPath string, dependencies []string) (Daemon, error) {

return &darwinRecord{name, description, dependencies}, nil
return &darwinRecord{name, description, execStartPath,dependencies}, nil
}

// Standard service path for system daemons
Expand Down Expand Up @@ -66,6 +67,7 @@ func (darwin *darwinRecord) checkRunning() (string, bool) {
func (darwin *darwinRecord) Install(args ...string) (string, error) {
installAction := "Install " + darwin.description + ":"

var err error
if ok, err := checkPrivileges(); !ok {
return installAction + failed, err
}
Expand All @@ -76,16 +78,22 @@ func (darwin *darwinRecord) Install(args ...string) (string, error) {
return installAction + failed, ErrAlreadyInstalled
}

file, err := os.Create(srvPath)
if err != nil {
return installAction + failed, err
if darwin.execStartPath == "" {
darwin.execStartPath, err = executablePath(darwin.name)
if err != nil {
return installAction + failed, err
}
}
defer file.Close()

execPatch, err := executablePath(darwin.name)
if stat, err := os.Stat(darwin.execStartPath); os.IsNotExist(err) || stat.IsDir() {
return installAction + failed, ErrIncorrectExecStartPath
}

file, err := os.Create(srvPath)
if err != nil {
return installAction + failed, err
}
defer file.Close()

templ, err := template.New("propertyList").Parse(propertyList)
if err != nil {
Expand All @@ -97,7 +105,7 @@ func (darwin *darwinRecord) Install(args ...string) (string, error) {
&struct {
Name, Path string
Args []string
}{darwin.name, execPatch, args},
}{darwin.name, darwin.execStartPath, args},
); err != nil {
return installAction + failed, err
}
Expand Down
29 changes: 18 additions & 11 deletions daemon_freebsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ import (

// systemVRecord - standard record (struct) for linux systemV version of daemon package
type bsdRecord struct {
name string
description string
dependencies []string
name string
description string
execStartPath string
dependencies []string
}

// Standard service path for systemV daemons
Expand Down Expand Up @@ -66,8 +67,8 @@ func (bsd *bsdRecord) getCmd(cmd string) string {
}

// Get the daemon properly
func newDaemon(name, description string, dependencies []string) (Daemon, error) {
return &bsdRecord{name, description, dependencies}, nil
func newDaemon(name, description, execStartPath string, dependencies []string) (Daemon, error) {
return &bsdRecord{name, description, execStartPath,dependencies}, nil
}

func execPath() (name string, err error) {
Expand Down Expand Up @@ -114,16 +115,22 @@ func (bsd *bsdRecord) Install(args ...string) (string, error) {
return installAction + failed, ErrAlreadyInstalled
}

file, err := os.Create(srvPath)
if err != nil {
return installAction + failed, err
if bsd.execStartPath == "" {
bsd.execStartPath, err = executablePath(bsd.name)
if err != nil {
return installAction + failed, err
}
}
defer file.Close()

execPatch, err := executablePath(bsd.name)
if stat, err := os.Stat(bsd.execStartPath); os.IsNotExist(err) || stat.IsDir() {
return installAction + failed, ErrIncorrectExecStartPath
}

file, err := os.Create(srvPath)
if err != nil {
return installAction + failed, err
}
defer file.Close()

templ, err := template.New("bsdConfig").Parse(bsdConfig)
if err != nil {
Expand All @@ -134,7 +141,7 @@ func (bsd *bsdRecord) Install(args ...string) (string, error) {
file,
&struct {
Name, Description, Path, Args string
}{bsd.name, bsd.description, execPatch, strings.Join(args, " ")},
}{bsd.name, bsd.description, bsd.execStartPath, strings.Join(args, " ")},
); err != nil {
return installAction + failed, err
}
Expand Down
8 changes: 4 additions & 4 deletions daemon_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ import (
)

// Get the daemon properly
func newDaemon(name, description string, dependencies []string) (Daemon, error) {
func newDaemon(name, description, execStartPath string, dependencies []string) (Daemon, error) {
// newer subsystem must be checked first
if _, err := os.Stat("/run/systemd/system"); err == nil {
return &systemDRecord{name, description, dependencies}, nil
return &systemDRecord{name, description, execStartPath, dependencies}, nil
}
if _, err := os.Stat("/sbin/initctl"); err == nil {
return &upstartRecord{name, description, dependencies}, nil
return &upstartRecord{name, description, execStartPath, dependencies}, nil
}
return &systemVRecord{name, description, dependencies}, nil
return &systemVRecord{name, description, execStartPath, dependencies}, nil
}

// Get executable path
Expand Down
26 changes: 17 additions & 9 deletions daemon_linux_systemd.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ import (

// systemDRecord - standard record (struct) for linux systemD version of daemon package
type systemDRecord struct {
name string
description string
dependencies []string
name string
description string
execStartPath string
dependencies []string
}

// Standard service path for systemD daemons
Expand Down Expand Up @@ -55,6 +56,7 @@ func (linux *systemDRecord) checkRunning() (string, bool) {
func (linux *systemDRecord) Install(args ...string) (string, error) {
installAction := "Install " + linux.description + ":"

var err error
if ok, err := checkPrivileges(); !ok {
return installAction + failed, err
}
Expand All @@ -65,16 +67,22 @@ func (linux *systemDRecord) Install(args ...string) (string, error) {
return installAction + failed, ErrAlreadyInstalled
}

file, err := os.Create(srvPath)
if err != nil {
return installAction + failed, err
if linux.execStartPath == "" {
linux.execStartPath, err = executablePath(linux.name)
if err != nil {
return installAction + failed, err
}
}
defer file.Close()

execPatch, err := executablePath(linux.name)
if stat, err := os.Stat(linux.execStartPath); os.IsNotExist(err) || stat.IsDir() {
return installAction + failed, ErrIncorrectExecStartPath
}

file, err := os.Create(srvPath)
if err != nil {
return installAction + failed, err
}
defer file.Close()

templ, err := template.New("systemDConfig").Parse(systemDConfig)
if err != nil {
Expand All @@ -89,7 +97,7 @@ func (linux *systemDRecord) Install(args ...string) (string, error) {
linux.name,
linux.description,
strings.Join(linux.dependencies, " "),
execPatch,
linux.execStartPath,
strings.Join(args, " "),
},
); err != nil {
Expand Down
26 changes: 17 additions & 9 deletions daemon_linux_systemv.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ import (

// systemVRecord - standard record (struct) for linux systemV version of daemon package
type systemVRecord struct {
name string
description string
dependencies []string
name string
description string
execStartPath string
dependencies []string
}

// Standard service path for systemV daemons
Expand Down Expand Up @@ -55,6 +56,7 @@ func (linux *systemVRecord) checkRunning() (string, bool) {
func (linux *systemVRecord) Install(args ...string) (string, error) {
installAction := "Install " + linux.description + ":"

var err error
if ok, err := checkPrivileges(); !ok {
return installAction + failed, err
}
Expand All @@ -65,16 +67,22 @@ func (linux *systemVRecord) Install(args ...string) (string, error) {
return installAction + failed, ErrAlreadyInstalled
}

file, err := os.Create(srvPath)
if err != nil {
return installAction + failed, err
if linux.execStartPath == "" {
linux.execStartPath, err = executablePath(linux.name)
if err != nil {
return installAction + failed, err
}
}
defer file.Close()

execPatch, err := executablePath(linux.name)
if stat, err := os.Stat(linux.execStartPath); os.IsNotExist(err) || stat.IsDir() {
return installAction + failed, ErrIncorrectExecStartPath
}

file, err := os.Create(srvPath)
if err != nil {
return installAction + failed, err
}
defer file.Close()

templ, err := template.New("systemVConfig").Parse(systemVConfig)
if err != nil {
Expand All @@ -85,7 +93,7 @@ func (linux *systemVRecord) Install(args ...string) (string, error) {
file,
&struct {
Name, Description, Path, Args string
}{linux.name, linux.description, execPatch, strings.Join(args, " ")},
}{linux.name, linux.description, linux.execStartPath, strings.Join(args, " ")},
); err != nil {
return installAction + failed, err
}
Expand Down
26 changes: 17 additions & 9 deletions daemon_linux_upstart.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ import (

// upstartRecord - standard record (struct) for linux upstart version of daemon package
type upstartRecord struct {
name string
description string
dependencies []string
name string
description string
execStartPath string
dependencies []string
}

// Standard service path for systemV daemons
Expand Down Expand Up @@ -55,6 +56,7 @@ func (linux *upstartRecord) checkRunning() (string, bool) {
func (linux *upstartRecord) Install(args ...string) (string, error) {
installAction := "Install " + linux.description + ":"

var err error
if ok, err := checkPrivileges(); !ok {
return installAction + failed, err
}
Expand All @@ -65,16 +67,22 @@ func (linux *upstartRecord) Install(args ...string) (string, error) {
return installAction + failed, ErrAlreadyInstalled
}

file, err := os.Create(srvPath)
if err != nil {
return installAction + failed, err
if linux.execStartPath == "" {
linux.execStartPath, err = executablePath(linux.name)
if err != nil {
return installAction + failed, err
}
}
defer file.Close()

execPatch, err := executablePath(linux.name)
if stat, err := os.Stat(linux.execStartPath); os.IsNotExist(err) || stat.IsDir() {
return installAction + failed, ErrIncorrectExecStartPath
}

file, err := os.Create(srvPath)
if err != nil {
return installAction + failed, err
}
defer file.Close()

templ, err := template.New("upstatConfig").Parse(upstatConfig)
if err != nil {
Expand All @@ -85,7 +93,7 @@ func (linux *upstartRecord) Install(args ...string) (string, error) {
file,
&struct {
Name, Description, Path, Args string
}{linux.name, linux.description, execPatch, strings.Join(args, " ")},
}{linux.name, linux.description, linux.execStartPath, strings.Join(args, " ")},
); err != nil {
return installAction + failed, err
}
Expand Down
19 changes: 11 additions & 8 deletions daemon_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,29 +14,32 @@ import (
"time"
"unicode/utf16"
"unsafe"

"golang.org/x/sys/windows/registry"
"golang.org/x/sys/windows/svc"
"golang.org/x/sys/windows/svc/mgr"
)

// windowsRecord - standard record (struct) for windows version of daemon package
type windowsRecord struct {
name string
description string
dependencies []string
name string
description string
execStartPath string
dependencies []string
}

func newDaemon(name, description string, dependencies []string) (Daemon, error) {
func newDaemon(name, description, execStartPath string, dependencies []string) (Daemon, error) {

return &windowsRecord{name, description, dependencies}, nil
return &windowsRecord{name, description, execStartPath, dependencies}, nil
}

// Install the service
func (windows *windowsRecord) Install(args ...string) (string, error) {
installAction := "Install " + windows.description + ":"

execp, err := execPath()
var err error
if windows.execStartPath == "" {
windows.execStartPath, err = execPath()
}

if err != nil {
return installAction + failed, err
Expand All @@ -54,7 +57,7 @@ func (windows *windowsRecord) Install(args ...string) (string, error) {
return installAction + failed, err
}

s, err = m.CreateService(windows.name, execp, mgr.Config{
s, err = m.CreateService(windows.name, windows.execStartPath, mgr.Config{
DisplayName: windows.name,
Description: windows.description,
StartType: mgr.StartAutomatic,
Expand Down
3 changes: 3 additions & 0 deletions helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ var (

// ErrAlreadyStopped appears if try to stop already stopped service
ErrAlreadyStopped = errors.New("Service has already been stopped")

// ErrIncorrectExecStartPath appears if try to path folder or incorrect exec path start for service
ErrIncorrectExecStartPath = errors.New("Incorrect exec start path")
)

// ExecPath tries to get executable path
Expand Down