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-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
|
|
|
|
}
|
|
|
|
serviceDir := "/etc/systemd/system/"
|
|
|
|
|
|
|
|
// Check paths first
|
|
|
|
serviceName := c.Name + ".service"
|
|
|
|
if !c.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
|
2019-07-03 05:51:30 +00:00
|
|
|
serviceDir = filepath.Join(c.Home, ".local/share/systemd/user")
|
2019-07-02 06:25:16 +00:00
|
|
|
err := os.MkdirAll(filepath.Dir(serviceDir), 0755)
|
|
|
|
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
|
|
|
|
servicePath := filepath.Join(serviceDir, serviceName)
|
|
|
|
if err := ioutil.WriteFile(servicePath, rw.Bytes(), 0644); err != nil {
|
|
|
|
return fmt.Errorf("ioutil.WriteFile error: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO template this as well?
|
|
|
|
userspace := ""
|
|
|
|
sudo := "sudo "
|
|
|
|
if !c.System {
|
|
|
|
userspace = "--user "
|
|
|
|
sudo = ""
|
|
|
|
}
|
|
|
|
fmt.Printf("System service installed as '%s'.\n", servicePath)
|
|
|
|
fmt.Printf("Run the following to start '%s':\n", c.Name)
|
|
|
|
fmt.Printf("\t" + sudo + "systemctl " + userspace + "daemon-reload\n")
|
|
|
|
fmt.Printf("\t"+sudo+"systemctl "+userspace+"restart %s.service\n", c.Name)
|
|
|
|
fmt.Printf("\t"+sudo+"journalctl "+userspace+"-xefu %s\n", c.Name)
|
|
|
|
return nil
|
|
|
|
}
|