generated from brevdev/seed
/
linux.go
94 lines (84 loc) · 2.36 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
package autostartconf
import (
"os"
"path"
breverrors "github.com/brevdev/brev-cli/pkg/errors"
)
type LinuxSystemdConfigurer struct {
Store AutoStartStore
ValueConfigFile string
ServiceName string
ServiceType string
}
const (
systemDConfigDir = "/etc/systemd/system/"
)
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
}