support systemd user services (#185)
This commit is contained in:
@@ -10,6 +10,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
|
"path/filepath"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
@@ -66,15 +67,21 @@ func (s *systemd) Platform() string {
|
|||||||
return s.platform
|
return s.platform
|
||||||
}
|
}
|
||||||
|
|
||||||
// Systemd services should be supported, but are not currently.
|
|
||||||
var errNoUserServiceSystemd = errors.New("User services are not supported on systemd.")
|
|
||||||
|
|
||||||
func (s *systemd) configPath() (cp string, err error) {
|
func (s *systemd) configPath() (cp string, err error) {
|
||||||
if s.Option.bool(optionUserService, optionUserServiceDefault) {
|
if !s.Option.bool(optionUserService, optionUserServiceDefault) {
|
||||||
err = errNoUserServiceSystemd
|
cp = "/etc/systemd/system/" + s.Config.Name + ".service"
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
cp = "/etc/systemd/system/" + s.Config.Name + ".service"
|
homeDir, err := os.UserHomeDir()
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
systemdUserDir := filepath.Join(homeDir, ".config/systemd/user")
|
||||||
|
err = os.MkdirAll(systemdUserDir, os.ModePerm)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
cp = filepath.Join(systemdUserDir, s.Config.Name + ".service")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -168,15 +175,30 @@ func (s *systemd) Install() error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if s.Option.bool(optionUserService, optionUserServiceDefault) {
|
||||||
|
err = run("systemctl", "enable", "--user", s.Name+".service")
|
||||||
|
} else {
|
||||||
err = run("systemctl", "enable", s.Name+".service")
|
err = run("systemctl", "enable", s.Name+".service")
|
||||||
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return run("systemctl", "daemon-reload")
|
|
||||||
|
if s.Option.bool(optionUserService, optionUserServiceDefault) {
|
||||||
|
err = run("systemctl", "daemon-reload", "--user")
|
||||||
|
} else {
|
||||||
|
err = run("systemctl", "daemon-reload")
|
||||||
|
}
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *systemd) Uninstall() error {
|
func (s *systemd) Uninstall() error {
|
||||||
err := run("systemctl", "disable", s.Name+".service")
|
var err error
|
||||||
|
if s.Option.bool(optionUserService, optionUserServiceDefault) {
|
||||||
|
err = run("systemctl", "disable", "--user", s.Name+".service")
|
||||||
|
} else {
|
||||||
|
err = run("systemctl", "disable", s.Name+".service")
|
||||||
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -40,6 +40,29 @@ func TestRunInterrupt(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Should always run, without asking for any permission
|
||||||
|
func TestUserRunInterrupt(t *testing.T) {
|
||||||
|
p := &program{}
|
||||||
|
options := make(service.KeyValue)
|
||||||
|
options["UserService"] = true
|
||||||
|
sc := &service.Config{
|
||||||
|
Name: "go_user_service_test",
|
||||||
|
Option: options,
|
||||||
|
}
|
||||||
|
s, err := service.New(p, sc)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("New err: %s", err)
|
||||||
|
}
|
||||||
|
err = s.Install()
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("Install err: %s", err)
|
||||||
|
}
|
||||||
|
err = s.Uninstall()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Uninstall err: %s", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
type program struct {
|
type program struct {
|
||||||
numStopped int
|
numStopped int
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user