-
Notifications
You must be signed in to change notification settings - Fork 2
/
process.go
281 lines (238 loc) · 6.84 KB
/
process.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
package process
import (
"bytes"
"fmt"
log "github.com/cihub/seelog"
"github.com/hailo-platform/H2O/platform/util"
dao "github.com/hailo-platform/H2O/provisioning-service/dao"
"math/rand"
"os"
"os/exec"
"path"
"runtime"
"strconv"
"strings"
"text/template"
"time"
)
const (
osName = runtime.GOOS
defaultUser = "hailosvc"
defaultGroup = "hailosvc"
)
var (
initCtl = newInitCtler()
exeDir = "/opt/hailo/bin" // where we store downloaded executable files
)
type initCtler interface {
List(string) ([]string, error)
Install(string, uint64, uint64, uint64) error
Start(string, uint64, uint64, uint64) error
Stop(string, uint64) error
Restart(string, uint64) error
Uninstall(string, uint64) error
}
type config struct {
Directory string
Extension string
}
type platform struct {
InitCmd string
Config config
}
func combineNameVersion(serviceName string, serviceVersion uint64) string {
return serviceName + "-" + strconv.Itoa(int(serviceVersion))
}
func getConfDir() string {
if dir := os.Getenv("HAILO_INIT_DIR"); dir != "" {
return dir
}
return defaultConfDir
}
func getConfPath(serviceName string, serviceVersion uint64, conf config) string {
return path.Join(conf.Directory, combineNameVersion(serviceName, serviceVersion)+conf.Extension)
}
func getEnvironment() map[string]string {
env := make(map[string]string)
for _, val := range os.Environ() {
vals := strings.SplitN(val, "=", 2)
env[vals[0]] = vals[1]
}
return env
}
// getExePath() returns a string containing the path for this provisioned
// service's executable, once downloaded to the local filesystem.
func getExePath(serviceName string, serviceVersion uint64) string {
return path.Join(exeDir, combineNameVersion(serviceName, serviceVersion))
}
// Returns an interface capable of executing the local OS init cmd.
func newInitCtler() initCtler {
return newPlatform()
}
// run executes any given command
func run(cmdName string, args ...string) error {
cmd := exec.Command(cmdName, args...)
var stdout, stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr
if err := cmd.Run(); err != nil {
return fmt.Errorf("%v; stdout: %q, stderr: %q", err, stdout.Bytes(), stderr.Bytes())
}
return nil
}
// convenience function which wraps getExePath()
func ExePath(ps *dao.ProvisionedService) string {
return getExePath(ps.ServiceName, ps.ServiceVersion)
}
func Install(serviceName string, serviceVersion, noFileSoftLimit, noFileHardLimit uint64) error {
return initCtl.Install(serviceName, serviceVersion, noFileSoftLimit, noFileHardLimit)
}
func Uninstall(serviceName string, serviceVersion uint64) error {
return initCtl.Uninstall(serviceName, serviceVersion)
}
func Start(serviceName string, serviceVersion, noFileSoftLimit, noFileHardLimit uint64) error {
return initCtl.Start(serviceName, serviceVersion, noFileSoftLimit, noFileHardLimit)
}
func Stop(serviceName string, serviceVersion uint64) error {
return initCtl.Stop(serviceName, serviceVersion)
}
func RestartAZ(azName string) error {
thisAzName, err := util.GetAwsAZName()
if err != nil {
log.Errorf("Unable to determine this az name. %s", err)
return err
}
if azName != thisAzName {
// skip
return nil
}
provisionedServices, err := dao.CachedServices(getMachineClass())
if err != nil {
return fmt.Errorf("Error restarting AZ. Could not retrieve list of provisioned services. %s", err)
}
var lastErr error
for _, p := range provisionedServices {
time.Sleep(time.Duration(rand.Int63n(5)) * time.Second) // jitter 5 second to reduce thundering herd
if thisErr := initCtl.Restart(p.ServiceName, p.ServiceVersion); thisErr != nil {
log.Errorf("Error restarting service %s", thisErr)
lastErr = thisErr
}
}
log.Critical("Restart AZ finished. Committing suicide.")
os.Exit(0)
return lastErr
}
func Restart(serviceName string, serviceVersion uint64, azName string) error {
thisAzName, err := util.GetAwsAZName()
if err != nil {
log.Errorf("Unable to determine this az name")
return err
}
if len(azName) > 0 && thisAzName != azName {
// skip
return nil
}
// add some random jitter 0-60 seconds
time.Sleep(time.Duration(rand.Int63n(60)) * time.Second)
return initCtl.Restart(serviceName, serviceVersion)
}
func ListRunning(matching string) ([]string, error) {
return initCtl.List(matching)
}
func CachedCountRunningInstances(serviceName string, serviceVersion uint64, processes []string) int {
instance := combineNameVersion(serviceName, serviceVersion)
running := 0
for _, process := range processes {
if instance == process {
running++
}
}
return running
}
func CountRunningInstances(serviceName string, serviceVersion uint64) (int, error) {
processes, err := ListRunning(combineNameVersion(serviceName, serviceVersion))
if err != nil {
return -1, err
}
return len(processes), nil
}
func install(serviceName string, serviceVersion, noFileSoftLimit, noFileHardLimit uint64, conf config, tmpl *template.Template) error {
cmdName := combineNameVersion(serviceName, serviceVersion)
exePath := getExePath(serviceName, serviceVersion)
confPath := getConfPath(serviceName, serviceVersion, conf)
if _, err := os.Stat(exePath); os.IsNotExist(err) {
return fmt.Errorf("Unable to install, binary does not exist: %v", exePath)
}
if err := os.MkdirAll(conf.Directory, 0777); err != nil {
return err
}
_, err := os.Stat(confPath)
if err == nil {
log.Info("Deleting existing init file: ", confPath)
os.Remove(confPath)
}
if err := os.MkdirAll(exeDir, 0777); err != nil {
return err
}
file, err := os.Create(confPath)
if err != nil {
return err
}
defer file.Close()
user := os.Getenv("HAILO_INIT_RUNASUSER")
group := os.Getenv("HAILO_INIT_RUNASGROUP")
if user == "" {
user = defaultUser
log.Info("HAILO_INIT_RUNASUSER was not set - defaulting to " + user)
}
if group == "" {
group = defaultGroup
log.Info("HAILO_INIT_RUNASGROUP was not set - defaulting to " + group)
}
if noFileSoftLimit < 1024 {
noFileSoftLimit = 1024
}
if noFileHardLimit < 1024 {
noFileHardLimit = 4096
}
params := &struct {
Description string
Author string
ProcessName string
GeneratedAt string
RunAsUser string
RunAsGroup string
NoFileSoftLimit string
NoFileHardLimit string
Environment map[string]string
}{
cmdName,
"com.hailocab.service.provisioning.create",
exePath,
time.Now().String(),
user,
group,
strconv.Itoa(int(noFileSoftLimit)),
strconv.Itoa(int(noFileHardLimit)),
getEnvironment(),
}
err = tmpl.Execute(file, params)
if err != nil {
return err
}
return nil
}
func uninstall(serviceName string, serviceVersion uint64, conf config) error {
confPath := getConfPath(serviceName, serviceVersion, conf)
if err := os.Remove(confPath); err != nil {
return err
}
return nil
}
func getMachineClass() string {
myClass := os.Getenv("H2O_MACHINE_CLASS")
if len(myClass) == 0 {
myClass = "default"
}
return myClass
}