forked from rexray/rexray
-
Notifications
You must be signed in to change notification settings - Fork 0
/
installer.go
432 lines (389 loc) · 10.2 KB
/
installer.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
// +build !client
package cli
import (
"bytes"
"fmt"
"os"
"os/exec"
"path"
"runtime"
"strings"
"text/template"
"github.com/akutz/gotil"
apitypes "github.com/codedellemc/rexray/libstorage/api/types"
"github.com/codedellemc/rexray/util"
log "github.com/sirupsen/logrus"
)
func init() {
installFunc = install
uninstallFunc = uninstall
}
// init system types
const (
Unknown = iota
SystemD
UpdateRcD
ChkConfig
)
func install(ctx apitypes.Context) {
checkOpPerms("installed")
switch runtime.GOOS {
case "linux":
switch getInitSystemType() {
case SystemD:
installSystemD(ctx)
case UpdateRcD:
installUpdateRcd()
case ChkConfig:
installChkConfig()
}
}
}
func isRpmInstall(pkgName *string) bool {
exePath := util.BinFilePath
cmd := exec.Command("rpm", "-qf", exePath)
output, err := cmd.CombinedOutput()
soutput := string(output)
if err != nil {
log.WithFields(log.Fields{
"exePath": exePath,
"output": soutput,
"error": err,
}).Debug("error checking if rpm install")
return false
}
log.WithField("output", soutput).Debug("rpm install query result")
*pkgName = gotil.Trim(soutput)
log.WithFields(log.Fields{
"exePath": exePath,
"pkgName": *pkgName,
}).Debug("is rpm install success")
return true
}
func isDebInstall(pkgName *string) bool {
exePath := util.BinFilePath
cmd := exec.Command("dpkg-query", "-S", exePath)
output, err := cmd.CombinedOutput()
soutput := string(output)
if err != nil {
log.WithFields(log.Fields{
"exePath": exePath,
"output": soutput,
"error": err,
}).Debug("error checking if deb install")
return false
}
log.WithField("output", soutput).Debug("deb install query result")
*pkgName = strings.Split(gotil.Trim(soutput), ":")[0]
log.WithFields(log.Fields{
"exePath": exePath,
"pkgName": *pkgName,
}).Debug("is deb install success")
return true
}
func uninstallRpm(pkgName string) bool {
output, err := exec.Command("rpm", "-e", pkgName).CombinedOutput()
if err != nil {
log.WithFields(log.Fields{
"pkgName": pkgName,
"output": string(output),
"error": err,
}).Error("error uninstalling rpm")
}
return true
}
func uninstallDeb(pkgName string) bool {
output, err := exec.Command("dpkg", "-r", pkgName).CombinedOutput()
if err != nil {
log.WithFields(log.Fields{
"pkgName": pkgName,
"output": string(output),
"error": err,
}).Error("error uninstalling deb")
}
return true
}
func uninstall(ctx apitypes.Context, pkgManager bool) {
checkOpPerms("uninstalled")
binFilePath := util.BinFilePath
// if the uninstall command was executed manually we should check to see
// if this file is owned by a package manager and remove it that way if so
if !pkgManager {
log.WithField("binFilePath", binFilePath).Debug(
"is this a managed file?")
var pkgName string
if isRpmInstall(&pkgName) {
uninstallRpm(pkgName)
return
} else if isDebInstall(&pkgName) {
uninstallDeb(pkgName)
return
}
}
func() {
defer func() {
recover()
}()
serviceStop(ctx)
}()
switch getInitSystemType() {
case SystemD:
uninstallSystemD()
case UpdateRcD:
uninstallUpdateRcd()
case ChkConfig:
uninstallChkConfig()
}
if !pkgManager {
os.Remove(binFilePath)
if util.IsPrefixed(ctx) {
os.RemoveAll(util.GetPrefix(ctx))
}
}
}
func getInitSystemCmd() string {
switch getInitSystemType() {
case SystemD:
return "systemd"
case UpdateRcD:
return "update-rc.d"
case ChkConfig:
return "chkconfig"
default:
return "unknown"
}
}
func getInitSystemType() int {
if gotil.FileExistsInPath("systemctl") {
return SystemD
}
if gotil.FileExistsInPath("update-rc.d") {
return UpdateRcD
}
if gotil.FileExistsInPath("chkconfig") {
return ChkConfig
}
return Unknown
}
func installSystemD(ctx apitypes.Context) {
if err := createUnitFile(ctx); err != nil {
fmt.Fprintf(os.Stderr, "error: install failed: %v\n", err)
os.Exit(1)
}
if err := createEnvFile(ctx); err != nil {
fmt.Fprintf(os.Stderr, "error: install failed: %v\n", err)
os.Exit(1)
}
cmd := exec.Command("systemctl", "enable", "-q", util.UnitFileName)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
fmt.Fprintf(os.Stderr, "error: install failed: %v\n", err)
os.Exit(1)
}
fmt.Print("REX-Ray is now installed. Before starting it please check ")
fmt.Print("http://github.com/codedellemc/rexray for instructions on how to ")
fmt.Print("configure it.\n\n Once configured the REX-Ray service can be ")
fmt.Print("started with the command 'sudo systemctl start rexray'.\n\n")
}
func uninstallSystemD() {
// a link created by systemd as docker should "want" rexray as a service.
// the uninstaller will fail
os.Remove(path.Join("/etc/systemd/system/docker.service.wants",
util.UnitFileName))
cmd := exec.Command("systemctl", "disable", "-q", util.UnitFileName)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
fmt.Fprintf(os.Stderr, "error: uninstall failed: %v\n", err)
os.Exit(1)
}
os.Remove(util.UnitFilePath)
}
func installUpdateRcd() {
if err := createInitFile(); err != nil {
fmt.Fprintf(os.Stderr, "error: install failed: %v\n", err)
os.Exit(1)
}
cmd := exec.Command("update-rc.d", util.InitFileName, "defaults")
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
fmt.Fprintf(os.Stderr, "error: install failed: %v\n", err)
os.Exit(1)
}
fmt.Print("REX-Ray is now installed. Before starting it please check ")
fmt.Print("http://github.com/codedellemc/rexray for instructions on how to ")
fmt.Print("configure it.\n\n Once configured the REX-Ray service can be ")
fmt.Print("started with the command ")
fmt.Printf("'sudo %s start'.\n\n", util.InitFilePath)
}
func uninstallUpdateRcd() {
os.Remove(util.InitFilePath)
cmd := exec.Command("update-rc.d", util.InitFileName, "remove")
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
fmt.Fprintf(os.Stderr, "error: uninstall failed: %v\n", err)
os.Exit(1)
}
}
func installChkConfig() {
if err := createInitFile(); err != nil {
fmt.Fprintf(os.Stderr, "error: install failed: %v\n", err)
os.Exit(1)
}
cmd := exec.Command("chkconfig", util.InitFileName, "on")
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
fmt.Fprintf(os.Stderr, "error: install failed: %v\n", err)
os.Exit(1)
}
fmt.Print("REX-Ray is now installed. Before starting it please check ")
fmt.Print("http://github.com/codedellemc/rexray for instructions on how to ")
fmt.Print("configure it.\n\n Once configured the REX-Ray service can be ")
fmt.Print("started with the command ")
fmt.Printf("'sudo %s start'.\n\n", util.InitFilePath)
}
func uninstallChkConfig() {
cmd := exec.Command("chkconfig", "--del", util.InitFileName)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
fmt.Fprintf(os.Stderr, "error: uninstall failed: %v\n", err)
os.Exit(1)
}
os.Remove(util.InitFilePath)
}
func createEnvFile(ctx apitypes.Context) error {
envFilePath := util.EnvFilePath(ctx)
f, err := os.OpenFile(envFilePath, os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
return fmt.Errorf("open env file failed: %s: %v", envFilePath, err)
}
defer f.Close()
if util.IsPrefixed(ctx) {
f.WriteString("REXRAY_HOME=")
f.WriteString(util.GetPrefix(ctx))
}
return nil
}
func createUnitFile(ctx apitypes.Context) error {
data := struct {
BinFileName string
BinFilePath string
EnvFilePath string
}{
util.BinFileName,
util.BinFilePath,
util.EnvFilePath(ctx),
}
tmpl, err := template.New("UnitFile").Parse(unitFileTemplate)
if err != nil {
return fmt.Errorf("create unit file template failed: %v", err)
}
var buf bytes.Buffer
if err := tmpl.Execute(&buf, data); err != nil {
return fmt.Errorf("exec unit file template failed: %v", err)
}
text := buf.String()
f, err := os.OpenFile(util.UnitFilePath, os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
return fmt.Errorf(
"create unit file failed: %s: %v", util.UnitFilePath, err)
}
defer f.Close()
if _, err := f.WriteString(text); err != nil {
return fmt.Errorf(
"write unit file failed: %s: %v", util.UnitFilePath, err)
}
return nil
}
const unitFileTemplate = `[Unit]
Description={{.BinFileName}}
Wants=scini.service
Before=docker.service
After=scini.service
[Service]
EnvironmentFile={{.EnvFilePath}}
ExecStart={{.BinFilePath}} start
ExecReload=/bin/kill -HUP $MAINPID
KillMode=process
[Install]
WantedBy=docker.service
`
func createInitFile() error {
data := struct {
BinFileName string
BinFilePath string
}{
util.BinFileName,
util.BinFilePath,
}
tmpl, err := template.New("InitScript").Parse(initScriptTemplate)
if err != nil {
return fmt.Errorf("create init file template failed: %v", err)
}
var buf bytes.Buffer
if err := tmpl.Execute(&buf, data); err != nil {
return fmt.Errorf("exec unit file template failed: %v", err)
}
text := buf.String()
// wrapped in a function to defer the close to ensure file is written to
// disk before subsequent chmod below
if err := func() error {
f, err := os.OpenFile(util.InitFilePath, os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
return fmt.Errorf(
"create init file failed: %s: %v", util.InitFilePath, err)
}
defer f.Close()
if _, err := f.WriteString(text); err != nil {
return fmt.Errorf(
"write init file failed: %s: %v", util.InitFilePath, err)
}
return nil
}(); err != nil {
return err
}
if err := os.Chmod(util.InitFilePath, 0755); err != nil {
return fmt.Errorf(
"chmod init file failed: %s: %v", util.InitFilePath, err)
}
return nil
}
const initScriptTemplate = `### BEGIN INIT INFO
# Provides: {{.BinFileName}}
# Required-Start: $remote_fs $syslog
# Required-Stop: $remote_fs $syslog
# Should-Start: scini
# X-Start-Before: docker
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start daemon at boot time
# Description: Enable service provided by daemon.
### END INIT INFO
case "$1" in
start)
{{.BinFilePath}} start
;;
stop)
{{.BinFilePath}} stop
;;
status)
{{.BinFilePath}} status
;;
restart)
{{.BinFilePath}} restart
;;
reload)
{{.BinFilePath}} reload
;;
force-reload)
{{.BinFilePath}} force-reload
;;
*)
echo "Usage: $0 {start|stop|status|restart|reload|force-reload}"
esac
`