2019-07-04 07:36:35 +00:00
|
|
|
package manager
|
2019-07-01 08:44:48 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"text/template"
|
|
|
|
|
2019-07-04 07:36:35 +00:00
|
|
|
"git.rootprojects.org/root/go-serviceman/manager/static"
|
2019-07-03 05:51:30 +00:00
|
|
|
"git.rootprojects.org/root/go-serviceman/service"
|
2019-07-01 08:44:48 +00:00
|
|
|
)
|
|
|
|
|
2019-07-05 18:48:58 +00:00
|
|
|
var (
|
2019-07-07 06:48:25 +00:00
|
|
|
srvLen int
|
|
|
|
srvExt = ".service"
|
|
|
|
srvSysPath = "/etc/systemd/system"
|
|
|
|
|
|
|
|
// Not sure which of these it's supposed to be...
|
|
|
|
// * ~/.local/share/systemd/user/watchdog.service
|
|
|
|
// * ~/.config/systemd/user/watchdog.service
|
|
|
|
// https://wiki.archlinux.org/index.php/Systemd/User
|
|
|
|
// This seems to work on Ubuntu
|
|
|
|
srvUserPath = ".config/systemd/user"
|
2019-07-05 18:48:58 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
srvLen = len(srvExt)
|
|
|
|
}
|
|
|
|
|
2019-07-07 06:48:25 +00:00
|
|
|
func start(system bool, home string, name string) error {
|
|
|
|
sys, user, err := getMatchingSrvs(home, name)
|
|
|
|
if nil != err {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var service string
|
|
|
|
if system {
|
|
|
|
service, err = getOneSysSrv(sys, user, name)
|
|
|
|
if nil != err {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
service = filepath.Join(srvSysPath, service)
|
|
|
|
} else {
|
|
|
|
service, err = getOneUserSrv(home, sys, user, name)
|
|
|
|
if nil != err {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
service = filepath.Join(home, srvUserPath, service)
|
|
|
|
}
|
|
|
|
|
|
|
|
var cmds []Runnable
|
|
|
|
if system {
|
|
|
|
cmds = []Runnable{
|
|
|
|
Runnable{
|
|
|
|
Exec: "systemctl",
|
|
|
|
Args: []string{"daemon-reload"},
|
|
|
|
Must: false,
|
|
|
|
},
|
|
|
|
Runnable{
|
|
|
|
Exec: "systemctl",
|
|
|
|
Args: []string{"stop", name + ".service"},
|
|
|
|
Must: false,
|
|
|
|
},
|
|
|
|
Runnable{
|
|
|
|
Exec: "systemctl",
|
|
|
|
Args: []string{"start", name + ".service"},
|
|
|
|
Badwords: []string{"not found", "failed"},
|
|
|
|
Must: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
cmds = []Runnable{
|
|
|
|
Runnable{
|
|
|
|
Exec: "systemctl",
|
|
|
|
Args: []string{"--user", "daemon-reload"},
|
|
|
|
Must: false,
|
|
|
|
},
|
|
|
|
Runnable{
|
|
|
|
Exec: "systemctl",
|
|
|
|
Args: []string{"stop", "--user", name + ".service"},
|
|
|
|
Must: false,
|
|
|
|
},
|
|
|
|
Runnable{
|
|
|
|
Exec: "systemctl",
|
|
|
|
Args: []string{"start", "--user", name + ".service"},
|
|
|
|
Badwords: []string{"not found", "failed"},
|
|
|
|
Must: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cmds = adjustPrivs(system, cmds)
|
|
|
|
|
|
|
|
fmt.Println()
|
2019-07-07 06:55:01 +00:00
|
|
|
fmt.Println("Starting systemd service unit...")
|
2019-07-07 06:48:25 +00:00
|
|
|
for i := range cmds {
|
|
|
|
exe := cmds[i]
|
2019-07-07 06:55:01 +00:00
|
|
|
fmt.Println("\t" + exe.String())
|
2019-07-07 06:48:25 +00:00
|
|
|
err := exe.Run()
|
|
|
|
if nil != err {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fmt.Println()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-07-03 05:51:30 +00:00
|
|
|
func install(c *service.Service) error {
|
2019-07-01 08:44:48 +00:00
|
|
|
// Linux-specific config options
|
|
|
|
if c.System {
|
|
|
|
if "" == c.User {
|
|
|
|
c.User = "root"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if "" == c.Group {
|
|
|
|
c.Group = c.User
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check paths first
|
2019-07-07 06:48:25 +00:00
|
|
|
serviceDir := srvSysPath
|
2019-07-01 08:44:48 +00:00
|
|
|
if !c.System {
|
2019-07-05 18:48:58 +00:00
|
|
|
serviceDir = filepath.Join(c.Home, srvUserPath)
|
2019-07-07 06:48:25 +00:00
|
|
|
err := os.MkdirAll(serviceDir, 0755)
|
2019-07-02 06:25:16 +00:00
|
|
|
if nil != err {
|
|
|
|
return err
|
|
|
|
}
|
2019-07-01 08:44:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create service file from template
|
|
|
|
b, err := static.ReadFile("dist/etc/systemd/system/_name_.service.tmpl")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
s := string(b)
|
|
|
|
rw := &bytes.Buffer{}
|
|
|
|
// not sure what the template name does, but whatever
|
|
|
|
tmpl, err := template.New("service").Parse(s)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = tmpl.Execute(rw, c)
|
|
|
|
if nil != err {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write the file out
|
2019-07-07 06:48:25 +00:00
|
|
|
serviceName := c.Name + ".service"
|
2019-07-01 08:44:48 +00:00
|
|
|
servicePath := filepath.Join(serviceDir, serviceName)
|
|
|
|
if err := ioutil.WriteFile(servicePath, rw.Bytes(), 0644); err != nil {
|
2019-07-07 06:48:25 +00:00
|
|
|
return fmt.Errorf("Error writing %s: %v", servicePath, err)
|
2019-07-01 08:44:48 +00:00
|
|
|
}
|
|
|
|
|
2019-07-07 06:48:25 +00:00
|
|
|
// TODO --no-start
|
|
|
|
err = start(c.System, c.Home, c.Name)
|
|
|
|
if nil != err {
|
|
|
|
sudo := ""
|
|
|
|
// --user-unit rather than --user --unit for older systemd
|
|
|
|
unit := "--user-unit"
|
|
|
|
if c.System {
|
|
|
|
sudo = "sudo "
|
|
|
|
unit = "--unit"
|
|
|
|
}
|
|
|
|
fmt.Printf("If things don't go well you should be able to get additional logging from journalctl:\n")
|
|
|
|
fmt.Printf("\t%sjournalctl -xe %s %s.service\n", sudo, unit, c.Name)
|
|
|
|
return err
|
2019-07-01 08:44:48 +00:00
|
|
|
}
|
2019-07-07 06:48:25 +00:00
|
|
|
|
|
|
|
fmt.Printf("Added and started '%s' as a systemd service.\n", c.Name)
|
2019-07-01 08:44:48 +00:00
|
|
|
return nil
|
|
|
|
}
|