-
Notifications
You must be signed in to change notification settings - Fork 0
/
systemd.go
72 lines (59 loc) · 1.8 KB
/
systemd.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package sdk
import (
"fmt"
"os"
"os/exec"
"os/user"
"path/filepath"
"github.com/tliron/commonlog"
"github.com/tliron/kutil/util"
)
const servicePrefix = "khutulun"
func CreateUserSystemdFile(name string, log commonlog.Logger) (*os.File, error) {
user_, err := user.Current()
if err != nil {
return nil, fmt.Errorf("current user: %w", err)
}
path := filepath.Join(user_.HomeDir, ".config", "systemd", "user", name)
if exists, err := util.DoesFileExist(path); err == nil {
if exists {
log.Infof("systemd unit already exists: %q", path)
//return nil
} else {
log.Infof("systemd unit: %q", path)
}
} else {
return nil, fmt.Errorf("file: %w", err)
}
err = os.MkdirAll(filepath.Dir(path), 0777)
if err != nil {
return nil, fmt.Errorf("mkdir: %w", err)
}
file, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0666)
if err != nil {
return nil, fmt.Errorf("create systemd unit file: %w", err)
}
return file, nil
}
func EnableUserSystemd(name string, log commonlog.Logger) error {
command := exec.Command("/usr/bin/systemctl", "--user", "daemon-reload")
if err := command.Run(); err != nil {
return fmt.Errorf("systemctl daemon-reload: %w", err)
}
log.Infof("systemctl enable %q", name)
command = exec.Command("/usr/bin/systemctl", "--user", "enable", name)
if err := command.Run(); err != nil {
return fmt.Errorf("systemctl enable: %w", err)
}
log.Infof("systemctl restart %q", name)
command = exec.Command("/usr/bin/systemctl", "--user", "--no-block", "restart", name)
if err := command.Run(); err != nil {
return fmt.Errorf("systemctl start: %w", err)
}
log.Info("loginctl enable-linger")
command = exec.Command("/usr/bin/loginctl", "enable-linger")
if err := command.Run(); err != nil {
return fmt.Errorf("loginctl enable-linger: %w", err)
}
return nil
}