generated from brevdev/seed
-
Notifications
You must be signed in to change notification settings - Fork 9
/
linux.go
138 lines (122 loc) · 3.18 KB
/
linux.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package autostartconf
import (
"os"
"path"
breverrors "github.com/brevdev/brev-cli/pkg/errors"
)
type LinuxSystemdConfigurer struct {
Store AutoStartStore
ExecString string
WantedBy string
After string
ServiceName string
Type string
User string
ServiceType string
}
const (
systemDConfigDir = "/etc/systemd/system/"
)
func (lsc LinuxSystemdConfigurer) ValueConfigFile() string {
// [Unit]
// Description=brevmon
// After=network.target
// [Service]
// User=root
// Type=exec
// ExecStart=/usr/local/bin/brevmon
// ExecReload=/usr/local/bin/brevmon
// Restart=always
// [Install]
// WantedBy=default.target
unit := `[Unit]
Description=` + lsc.ServiceName + `
After=` + lsc.After + `
[Service]`
if lsc.User != "" {
unit += `
User=` + lsc.User
}
unit += `Type=` + lsc.Type + `
ExecStart=` + lsc.ExecString + `
ExecReload=` + lsc.ExecString + `
Restart=always
[Install]
WantedBy=` + lsc.WantedBy
return unit
}
func (lsc *LinuxSystemdConfigurer) WithFlags(flags []string) *LinuxSystemdConfigurer {
for _, flag := range flags {
lsc.ExecString += " " + flag
}
return lsc
}
func (lsc LinuxSystemdConfigurer) getDestConfigFile() string {
return path.Join(systemDConfigDir, lsc.ServiceName)
}
func (lsc LinuxSystemdConfigurer) UnInstall() error {
exists, err := lsc.Store.FileExists(lsc.getDestConfigFile())
if err != nil {
return breverrors.WrapAndTrace(err)
}
if exists {
errother := lsc.Store.Remove(lsc.getDestConfigFile())
if errother != nil {
return breverrors.WrapAndTrace(errother)
}
}
err = execCommands([][]string{
{"systemctl", "disable", lsc.ServiceName},
{"systemctl", "stop", lsc.ServiceName},
})
if err != nil {
return breverrors.WrapAndTrace(err)
}
return nil
}
func (lsc LinuxSystemdConfigurer) Install() error {
_ = lsc.UnInstall() // best effort
err := lsc.Store.CopyBin(targetBin)
if err != nil {
return breverrors.WrapAndTrace(err)
}
err = lsc.Store.WriteString(lsc.getDestConfigFile(), lsc.ValueConfigFile())
if err != nil {
return breverrors.WrapAndTrace(err)
}
if ShouldSymlink() {
errother := lsc.CreateForcedSymlink()
if errother != nil {
return breverrors.WrapAndTrace(errother)
}
} else {
errother := execCommands([][]string{
{"systemctl", "enable", lsc.ServiceName},
{"systemctl", "start", lsc.ServiceName},
{"systemctl", "daemon-reload"},
})
if errother != nil {
return breverrors.WrapAndTrace(errother)
}
}
return nil
}
// CreateForcedSymlink aims to be the equivalent operation as running
// ln -sf /lib/systemd/system/huproxy.service /etc/systemd/system/default.target.wants/huproxy.service
// which overwrite's an existing symbolic link to point to a different file
// which we need to do in the workspace docker image because systemd isn't running
// at build time.
func (lsc LinuxSystemdConfigurer) CreateForcedSymlink() error {
symlinkTarget := path.Join("/etc/systemd/system/default.target.wants/", lsc.ServiceName)
err := os.Symlink(lsc.getDestConfigFile(), symlinkTarget)
if err != nil {
return breverrors.WrapAndTrace(err)
}
return nil
}
func ShouldSymlink() bool {
if os.Getenv("SHOULD_SYMLINK") != "" {
return os.Getenv("SHOULD_SYMLINK") == "1"
}
return false
}