2019-07-01 08:44:48 +00:00
|
|
|
//go:generate go run -mod=vendor github.com/UnnoTed/fileb0x b0x.toml
|
|
|
|
|
|
|
|
package installer
|
|
|
|
|
|
|
|
import (
|
2019-07-02 06:02:09 +00:00
|
|
|
"fmt"
|
2019-07-01 08:44:48 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
|
2019-07-03 05:51:30 +00:00
|
|
|
"git.rootprojects.org/root/go-serviceman/service"
|
|
|
|
)
|
2019-07-01 08:44:48 +00:00
|
|
|
|
|
|
|
// Install will do a best-effort attempt to install a start-on-startup
|
|
|
|
// user or system service via systemd, launchd, or reg.exe
|
2019-07-03 05:51:30 +00:00
|
|
|
func Install(c *service.Service) error {
|
2019-07-01 08:44:48 +00:00
|
|
|
if "" == c.Exec {
|
|
|
|
c.Exec = c.Name
|
|
|
|
}
|
|
|
|
|
|
|
|
if !c.System {
|
|
|
|
home, err := os.UserHomeDir()
|
|
|
|
if nil != err {
|
2019-07-02 06:02:09 +00:00
|
|
|
fmt.Fprintf(os.Stderr, "Unrecoverable Error: %s", err)
|
|
|
|
os.Exit(4)
|
2019-07-01 08:44:48 +00:00
|
|
|
return err
|
2019-07-02 06:02:09 +00:00
|
|
|
} else {
|
2019-07-03 05:51:30 +00:00
|
|
|
c.Home = home
|
2019-07-01 08:44:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
err := install(c)
|
|
|
|
if nil != err {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-07-02 06:25:16 +00:00
|
|
|
err = os.MkdirAll(c.Logdir, 0755)
|
2019-07-01 08:44:48 +00:00
|
|
|
if nil != err {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns true if we suspect that the current user (or process) will be able
|
|
|
|
// to write to system folders, bind to privileged ports, and otherwise
|
|
|
|
// successfully run a system service.
|
|
|
|
func IsPrivileged() bool {
|
|
|
|
return isPrivileged()
|
|
|
|
}
|
|
|
|
|
|
|
|
func WhereIs(exec string) (string, error) {
|
2019-07-03 05:51:30 +00:00
|
|
|
// TODO use exec.LookPath instead
|
2019-07-01 08:44:48 +00:00
|
|
|
exec = filepath.ToSlash(exec)
|
|
|
|
if strings.Contains(exec, "/") {
|
2019-07-02 06:02:09 +00:00
|
|
|
// it's a path (so we don't allow filenames with slashes)
|
|
|
|
stat, err := os.Stat(exec)
|
|
|
|
if nil != err {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
if stat.IsDir() {
|
|
|
|
return "", fmt.Errorf("'%s' is not an executable file", exec)
|
|
|
|
}
|
|
|
|
return filepath.Abs(exec)
|
2019-07-01 08:44:48 +00:00
|
|
|
}
|
|
|
|
return whereIs(exec)
|
|
|
|
}
|