-
Notifications
You must be signed in to change notification settings - Fork 0
/
autostart.go
96 lines (81 loc) · 2.26 KB
/
autostart.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
package main
import (
"fmt"
"path/filepath"
"runtime"
)
func autoStart() error {
path, err := getMyPath()
if err != nil {
return err
}
switch runtime.GOOS {
case "windows":
result, err := execute("nssm.exe", path, "install", "utmstack", "utmstack-windows.exe", "run")
if err {
return fmt.Errorf("%s", result)
}
result, err = execute("nssm.exe", path, "set", "utmstack", "AppDirectory", path)
if err {
return fmt.Errorf("%s", result)
}
result, err = execute("nssm.exe", path, "set", "utmstack", "DisplayName", "UTMStack Agent")
if err {
return fmt.Errorf("%s", result)
}
result, err = execute("nssm.exe", path, "set", "utmstack", "AppExit", "Default", "Restart")
if err {
return fmt.Errorf("%s", result)
}
result, err = execute("nssm.exe", path, "set", "utmstack", "Start", "SERVICE_AUTO_START")
if err {
return fmt.Errorf("%s", result)
}
result, err = execute("nssm.exe", path, "set", "utmstack", "ObjectName", "LocalSystem")
if err {
return fmt.Errorf("%s", result)
}
result, err = execute("nssm.exe", path, "start", "utmstack")
if err {
return fmt.Errorf("%s", result)
}
case "linux":
type bash struct {
Path string
}
scriptFile := filepath.Join("/", "usr", "local", "bin", "utmstack-agent.sh")
scriptTemplateFile := filepath.Join(path, "templates", "utmstack-agent-bash.sh")
err := generateFromTemplate(bash{Path: path}, scriptTemplateFile, scriptFile)
if err != nil {
return err
}
result, errB := execute("chmod", path, "755", filepath.Join("/", "usr", "local", "bin", "utmstack-agent.sh"))
if errB {
return fmt.Errorf("%s", result)
}
incidentServiceConfig := `[Unit]
Description=UTMStack Agent
After=network.target
StartLimitIntervalSec=0
[Service]
Type=simple
Restart=always
RestartSec=60
ExecStart=/usr/local/bin/utmstack-agent.sh
[Install]
WantedBy=multi-user.target`
err = writeToFile("/etc/systemd/system/utmstack.service", incidentServiceConfig)
if err != nil {
return err
}
result, errB = execute("systemctl", filepath.Join(path, "beats"), "enable", "utmstack")
if errB {
return fmt.Errorf("%s", result)
}
result, errB = execute("systemctl", filepath.Join(path, "beats"), "restart", "utmstack")
if errB {
return fmt.Errorf("%s", result)
}
}
return nil
}