-
Notifications
You must be signed in to change notification settings - Fork 0
/
service_maker.go
192 lines (167 loc) · 4.7 KB
/
service_maker.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package servicemaker
import (
"bytes"
"fmt"
"io/fs"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"runtime"
"text/template"
)
type ServiceMaker struct {
User string
UserGroups []string
ServicePath string
ServiceDescription string
ExecDir string
ExecName string
SkipConfig bool
ExampleConfig string
}
func (sm *ServiceMaker) systemdServiceContent() (out []byte, err error) {
tmpl, err := template.New("service").Parse(`[Unit]
Description={{.ServiceDescription}}
After=network.target
StartLimitIntervalSec=0
[Service]
Type=simple
Restart=always
RestartSec=5
User={{.User}}
WorkingDirectory={{.ExecDir}}
ExecStart={{.ExecDir}}/{{.ExecName}}
[Install]
WantedBy=multi-user.target`)
if err != nil {
return
}
var buff bytes.Buffer
err = tmpl.Execute(&buff, sm)
if err != nil {
return
}
out = buff.Bytes()
return
}
func (sm *ServiceMaker) linuxCheckAndUpdateUser() error {
var outErr bytes.Buffer
userCheckCommand := exec.Command("id", sm.User)
if userCheckCommand.Run() != nil {
userCreateCommand := exec.Command("useradd", "-r", "-s", "/bin/false", sm.User)
userCreateCommand.Stderr = &outErr
err := userCreateCommand.Run()
if err != nil {
return fmt.Errorf("error creating user %s:\n%v\n%s\n", sm.User, err, outErr.String())
}
}
for _, groupName := range sm.UserGroups {
groupAppendCommand := exec.Command("usermod", "-a", "-G", groupName, sm.User)
groupAppendCommand.Stderr = &outErr
err := groupAppendCommand.Run()
if err != nil {
return fmt.Errorf("error appending dialout group for user %s:\n%v\n%s\n", sm.User, err, outErr.String())
}
}
return nil
}
func (sm *ServiceMaker) linuxCopyFiles() error {
fsys := os.DirFS(sm.ExecDir)
serviceDir, err := fs.Stat(fsys, ".")
if err != nil {
if os.IsNotExist(err) {
err = os.Mkdir(sm.ExecDir, os.FileMode(0755))
if err != nil {
return fmt.Errorf("error creating directory (%s) for executable: \n%v", sm.ExecDir, err)
}
serviceDir, err = fs.Stat(fsys, ".")
if err != nil {
return fmt.Errorf("error getting directory: %v", err)
}
} else {
return fmt.Errorf("error when opening exec dir %s: \n%v", sm.ExecDir, err)
}
}
if !serviceDir.IsDir() {
return fmt.Errorf("path %s is not Dir, but it needs to be!", serviceDir.Name())
}
exePath, err := os.Executable()
if err != nil {
return fmt.Errorf("Error checking executable path: \n%v", err)
}
copyCommand := exec.Command("cp", exePath, filepath.Join(sm.ExecDir, sm.ExecName))
var stdErr bytes.Buffer
copyCommand.Stderr = &stdErr
err = copyCommand.Run()
if err != nil {
return fmt.Errorf("error copying excecutable:\n%v\n%s\n", err, stdErr.String())
}
if !sm.SkipConfig {
_, err = fs.Stat(fsys, "config.json")
if err != nil {
if os.IsNotExist(err) {
localFs := os.DirFS(".")
localConfig, err := fs.ReadFile(localFs, "config.json")
if err == nil {
err = ioutil.WriteFile(filepath.Join(sm.ExecDir, "config.json"), localConfig, os.FileMode(0644))
} else {
err = ioutil.WriteFile(filepath.Join(sm.ExecDir, "config.json"), []byte(sm.ExampleConfig), os.FileMode(0644))
}
if err != nil {
return fmt.Errorf("error writing config.json to %s:\n%v\n", sm.ExecDir, err)
}
}
}
}
ownCommand := exec.Command("chown", "-R", sm.User, sm.ExecDir)
ownCommand.Stderr = &stdErr
err = ownCommand.Run()
if err != nil {
return fmt.Errorf("error running chown on %s:\n%v\n%s\n", sm.ExecDir, err, stdErr.String())
}
return nil
}
func (sm *ServiceMaker) linuxInstallService() error {
if !(isSystemd()) {
return fmt.Errorf("Systemd not found but required, check if installed.")
}
err := sm.linuxCheckAndUpdateUser()
if err != nil {
return err
}
err = sm.linuxCopyFiles()
if err != nil {
return err
}
configInBytes, err := sm.systemdServiceContent()
if err != nil {
return fmt.Errorf("error parsing content of service file: %v", err)
}
err = ioutil.WriteFile(sm.ServicePath, configInBytes, os.FileMode(0644))
if err != nil {
return fmt.Errorf("error creating file %s: 'n%v", sm.ServicePath, err)
}
enableCommand := exec.Command("systemctl", "enable", filepath.Base(sm.ServicePath))
var outErr bytes.Buffer
enableCommand.Stderr = &outErr
err = enableCommand.Run()
if err != nil {
return fmt.Errorf("error enabling systemd service %s:\n%v\n%s\n", sm.ServicePath, err, outErr.String())
}
return nil
}
func (sm *ServiceMaker) InstallService() error {
switch runtime.GOOS {
case "linux":
return sm.linuxInstallService()
default:
return fmt.Errorf("Error ServiceMaker InstallService failed: unsupported operating system.")
}
}
func isSystemd() bool {
if _, err := os.Stat("/run/systemd/system"); err == nil {
return true
}
return false
}