forked from appsody/controller
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
707 lines (555 loc) · 17.5 KB
/
main.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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
package main
// Copyright © 2019 IBM Corporation and others.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import (
"flag"
"fmt"
"os"
"os/exec"
"regexp"
"strconv"
"strings"
"sync"
"syscall"
"time"
"github.com/radovskyb/watcher"
"k8s.io/klog"
)
var (
VERSION = "vlatest"
)
var appsodyWATCHIGNOREDIR []string
var appsodyWATCHDIRS []string // # regex of dirs/files to watch for changes. optional, default to mounts
var appsodyRUNWATCHACTION string //# command to run when files change, optional, in java only need to recompile not start server
var appsodyDEBUG string
var appsodyRUN string
var appsodyTEST string
var appsodyINSTALL string // Note this will be deprecated in a future release
var appsodyMOUNTS []string
var appsodyWATCHREGEX string
var appsodyPREP string
var appsodyWATCHINTERVAL time.Duration
var appsodyDEBUGWATCHACTION string
var appsodyTESTWATCHACTION string
var appsodyRUNKILL bool
var appsodyDEBUGKILL bool
var appsodyTESTKILL bool
var workDir string
var klogFlags *flag.FlagSet
var verbose bool
var vmode bool
type ProcessType int
const (
server ProcessType = 0
fileWatcher ProcessType = 1
)
type controllerManagedProcesses struct {
pids map[ProcessType]int
processes map[ProcessType]*os.Process
mu sync.RWMutex
}
var (
cmps *controllerManagedProcesses
once sync.Once
)
func appsodyControllerManagedProcesses() *controllerManagedProcesses {
once.Do(func() {
cmps = &controllerManagedProcesses{
pids: make(map[ProcessType]int),
processes: make(map[ProcessType]*os.Process),
}
})
return cmps
}
type envError struct {
environmentVar1 string
environmentVar2 string
environmentVar3 string
}
func (e envError) Error() string {
errorReturn := fmt.Sprintf("%v and %v and %v can not be empty.", e.environmentVar1, e.environmentVar2, e.environmentVar3)
return errorReturn
}
type volumesError struct {
environmentVar1 string
environmentVar2 string
}
func (e volumesError) Error() string {
errorReturn := fmt.Sprintf("%v and %v can not be empty. File watching is enabled.", e.environmentVar1, e.environmentVar2)
return errorReturn
}
type appsodylogger string
func (l appsodylogger) log(args ...interface{}) {
var dbg [1]interface{}
if l != "Info" {
dbg[0] = "[" + l + "] "
args = append(dbg[0:], args...)
}
if l == "Debug" {
if klog.V(2) {
// we don't want to pring out debug unless debug level is set
klog.InfoDepth(1, args...)
}
} else {
klog.InfoDepth(1, args...)
}
}
var (
// Info - informational logging
Info appsodylogger = "Info"
// Warning - warning logging
Warning appsodylogger = "Warning"
// Error - error logging
Error appsodylogger = "Error"
// Fatal - fatal errors
Fatal appsodylogger = "Fatal"
// Debug - debug
Debug appsodylogger = "Debug"
)
type mountError struct {
mountsString string
}
func (e mountError) Error() string {
return fmt.Sprintf("The Mount string has bad formatting: %v", e.mountsString)
}
func computeSigInt(tempSigInt string) bool {
var answer bool
if tempSigInt == "" || strings.Compare(strings.TrimSpace(strings.ToUpper(tempSigInt)), "TRUE") == 0 {
answer = true
} else {
answer = false
}
return answer
}
func setupEnvironmentVars() error {
Debug.log("setupEnvironmentVars ENTRY")
var err error
tmpWATCHIGNOREDIR := os.Getenv("APPSODY_WATCH_IGNORE_DIR")
Debug.log("APPSODY_WATCH_IGNORE_DIR ", tmpWATCHIGNOREDIR)
appsodyRUNKILL = computeSigInt(os.Getenv("APPSODY_RUN_KILL"))
Debug.log("APPSODY_RUN_KILL ", appsodyRUNKILL)
appsodyDEBUGKILL = computeSigInt(os.Getenv("APPSODY_DEBUG_KILL"))
Debug.log("APPSODY_DEBUG_KILL ", appsodyRUNKILL)
appsodyTESTKILL = computeSigInt(os.Getenv("APPSODY_TEST_KILL"))
Debug.log("APPSODY_DEBUG_KILL ", appsodyRUNKILL)
appsodyDEBUGWATCHACTION = os.Getenv("APPSODY_DEBUG_ON_CHANGE")
Debug.log("APPSODY_DEBUG_ON_CHANGE: " + appsodyDEBUGWATCHACTION)
appsodyTESTWATCHACTION = os.Getenv("APPSODY_TEST_ON_CHANGE")
Debug.log("APPSODY_TEST_ON_CHANGE: " + appsodyTESTWATCHACTION)
appsodyTEST = os.Getenv("APPSODY_TEST")
Debug.log("APPSODY_TEST: " + appsodyTEST)
appsodyWATCHREGEX = os.Getenv("APPSODY_WATCH_REGEX")
Debug.log("APPSODY_WATCH_REGEX: " + appsodyWATCHREGEX)
// if there is no watch expression default to watching for .go,.java,.js files
if appsodyWATCHREGEX == "" {
appsodyWATCHREGEX = "(^.*.java$)|(^.*.js$)|(^.*.go$)"
}
appsodyRUN = os.Getenv("APPSODY_RUN")
Debug.log("APPSODY_RUN: " + appsodyRUN)
tmpWatchDirs := os.Getenv("APPSODY_WATCH_DIR")
Debug.log("APPSODY_WATCH_DIR: " + tmpWatchDirs)
appsodyRUNWATCHACTION = os.Getenv("APPSODY_RUN_ON_CHANGE")
Debug.log("APPSODY_RUN_ON_CHANGE: " + appsodyRUNWATCHACTION)
appsodyINSTALL = os.Getenv("APPSODY_INSTALL") // Note this will be deprecated in a future release
Debug.log("APPSODY_INSTALL: " + appsodyINSTALL)
appsodyPREP = os.Getenv("APPSODY_PREP")
Debug.log("APPSODY_PREP: " + appsodyPREP)
if appsodyPREP == "" {
appsodyPREP = appsodyINSTALL
}
appsodyDEBUG = os.Getenv("APPSODY_DEBUG")
Debug.log("APPSODY_DEBUG: " + appsodyDEBUG)
tmpMountDirs := os.Getenv("APPSODY_MOUNTS")
Debug.log("APPSODY_MOUNTS: " + tmpMountDirs)
tempWatchInterval := os.Getenv("APPSODY_WATCH_INTERVAL")
Debug.log("APPSODY_WATCH_INTERVAL: " + tempWatchInterval + " seconds")
var value int
var atoiErr error
if tempWatchInterval != "" {
trimmedInterval := strings.TrimSpace(tempWatchInterval)
value, atoiErr = strconv.Atoi(trimmedInterval)
if atoiErr != nil {
Warning.log("Invalid watch interval, setting to default 2000: " + tempWatchInterval)
value = 2
}
} else {
// default to 2 seconds
value = 2
}
appsodyWATCHINTERVAL = time.Duration(int64(value) * int64(time.Second))
Debug.log("appsodyWATCHINTERVAL set to: ", appsodyWATCHINTERVAL)
fileWatchingOff := false
if appsodyRUNWATCHACTION == "" && appsodyDEBUGWATCHACTION == "" && appsodyTESTWATCHACTION == "" {
Debug.log("File watching is off.")
fileWatchingOff = true
}
if appsodyDEBUG == "" && appsodyRUN == "" && appsodyTEST == "" {
err = envError{"APPSODY_DEBUG", "APPSODY_RUN", "APPSODY_TEST"}
return err
} else if !fileWatchingOff && tmpMountDirs == "" && tmpWatchDirs == "" {
err = volumesError{"APPSODY_WATCH_DIR", "APPSODY_MOUNTS"}
return err
}
// split the watch dirs using ; separator
if tmpWatchDirs != "" {
appsodyWATCHDIRS = strings.Split(tmpWatchDirs, ";")
for i := 0; i < len(appsodyWATCHDIRS); i++ {
appsodyWATCHDIRS[i] = strings.TrimSpace(appsodyWATCHDIRS[i])
}
}
if tmpWATCHIGNOREDIR != "" {
appsodyWATCHIGNOREDIR = strings.Split(tmpWATCHIGNOREDIR, ";")
for i := 0; i < len(appsodyWATCHIGNOREDIR); i++ {
appsodyWATCHIGNOREDIR[i] = strings.TrimSpace(appsodyWATCHIGNOREDIR[i])
}
}
// split the mount dirs using ; separator
if tmpMountDirs != "" {
appsodyMOUNTS = strings.Split(tmpMountDirs, ";")
for i := 0; i < len(appsodyMOUNTS); i++ {
// check if there is a : separator
if strings.Contains(appsodyMOUNTS[i], ":") {
localDir := strings.Split(appsodyMOUNTS[i], ":")
//Windows may prepend the drive ID to the path so just take the last split
//ex. C:\whatever\path\:/linux/dir
appsodyMOUNTS[i] = strings.TrimSpace(localDir[len(localDir)-1])
} else {
err = mountError{tmpMountDirs}
break
}
}
}
Debug.log("setupEnvironmentVars EXIT")
return err
}
func killProcess(theProcessType ProcessType) error {
var processPid int
var err error
Debug.log("killProcess Entry")
if theProcessType == server {
processPid = cmps.pids[server]
} else {
processPid = cmps.pids[theProcessType]
}
Debug.log("Process pid is: ", processPid)
if processPid != 0 {
Debug.log("Killing pid: ", processPid)
err = syscall.Kill(-processPid, syscall.SIGINT)
cmps.processes[theProcessType] = nil
cmps.pids[theProcessType] = 0
if err != nil {
Error.log("Killing process returned an error SIGINT received error ", err)
}
}
return err
}
/*
runPrep
*/
func runPrep(commandString string) (*exec.Cmd, error) {
var err error
Info.log("Running Install: " + commandString)
cmd := exec.Command("/bin/bash", "-c", commandString)
Debug.log("Set workdir: " + workDir)
cmd.Dir = workDir
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
err = cmd.Run()
Debug.log("Install complete")
return cmd, err
}
/*
StartProcess
*/
func startProcess(commandString string, theProcessType ProcessType) (*exec.Cmd, error) {
var err error
Info.log("Running: " + commandString)
cmd := exec.Command("/bin/bash", "-c", commandString)
Debug.log("Set workdir: " + workDir)
cmd.Dir = workDir
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
err = cmd.Start()
cmps.processes[theProcessType] = cmd.Process
cmps.pids[theProcessType] = cmd.Process.Pid
Debug.log("New process created with pid ", strconv.Itoa(cmd.Process.Pid))
return cmd, err
}
func waitProcess(cmd *exec.Cmd, theProcessType ProcessType) error {
Debug.log("waitProcess ENTRY")
err := cmd.Wait()
if err != nil {
// do nothing as the kill causees and error condition
Info.log("Wait received error:", err)
}
Debug.log("waitProces EXIT")
return err
}
func unwatchDir(path string) bool {
unwatch := false
if appsodyWATCHIGNOREDIR != nil {
for i := 0; i < len(appsodyWATCHIGNOREDIR); i++ {
if strings.HasPrefix(path, appsodyWATCHIGNOREDIR[i]) {
unwatch = true
break
}
}
}
return unwatch
}
func runWatcher(fileChangeCommand string, dirs []string, killServer bool) error {
errorMessage := ""
var err error
Debug.log("Run watcher ENTRY: " + fileChangeCommand)
// Start the Watcher
// compile the regex prior to running watcher because panic leaves child processes if it occurs
r := regexp.MustCompile(appsodyWATCHREGEX)
w := watcher.New()
for d := 0; d < len(dirs); d++ {
// Watch each directory specified recursively for changes.
currentDir := dirs[d]
// Make sure the directory exists
_, err = os.Stat(currentDir)
if err != nil {
errorMessage = "Watched directory does not exist: " + currentDir
Warning.log(errorMessage, err)
}
if err = w.AddRecursive(currentDir); err != nil {
errorMessage = "Failed to add directory to recursive watch list: " + currentDir
Warning.log(errorMessage, err)
}
}
w.SetMaxEvents(1)
// Only files that match the regular expression during file listings
// will be watched. Currently we watch java, js, and go files.
// We may add an environment variable to add to this list
w.AddFilterHook(watcher.RegexFilterHook(r, false))
go func() {
for {
select {
case event := <-w.Event:
Debug.log("Full path is: " + event.Path)
if unwatchDir(event.Path) {
Debug.log("Path is not to be watched")
} else {
Debug.log("Matching for: " + event.Name())
if r.MatchString(event.Name()) {
Debug.log("About to restart watcher")
// Restart the watcher as a thread so we can do a wait to avoid zombie in ps -ef
if fileChangeCommand != "" {
Debug.log("kill server is: ", killServer)
go runCommands(fileChangeCommand, fileWatcher, killServer)
}
}
}
case err := <-w.Error:
Warning.log("An error occured in the watcher ", err)
case <-w.Closed:
Debug.log("Watcher is now closed")
return
}
}
}()
// Start the watching process - it'll check for changes every "n" ms.
Debug.log("Watch interval is: ", appsodyWATCHINTERVAL)
if err = w.Start(appsodyWATCHINTERVAL); err != nil {
errorMessage = "Could not start the watcher "
Error.log(errorMessage+" ", err)
}
defer w.Close()
// Close the watcher at function end
//defer w.Close()
Debug.log("Run watcher EXIT: ")
return err
}
/*
determine if we need to kill the server process
*/
func runCommands(commandString string, theProcessType ProcessType, killServer bool) {
var cmd *exec.Cmd
var err error
var mutexUnlocked bool
// Start a new watch action
Debug.log("ENTRY Running command: " + commandString)
cmps := appsodyControllerManagedProcesses()
// lock the mutex which protects the Process and the Pid String
cmps.mu.Lock()
Debug.log("Mutex Locked")
if theProcessType == server {
if appsodyPREP != "" {
_, err = runPrep(appsodyPREP)
}
if err != nil {
Error.log("FATAL error APPSODY_PREP command received an error. The controller is exiting: ", err)
os.Exit(1)
}
// keep going
cmd, err = startProcess(commandString, server)
Debug.log("Started Server process")
if err != nil {
Warning.log("ERROR start server (APPSODY_RUN) received error ", err)
}
cmps.mu.Unlock()
mutexUnlocked = true
Debug.log("mutex unlocked")
err = waitProcess(cmd, theProcessType)
if err != nil {
Info.log("Wait received error on server start ", err)
}
} else {
Debug.log("Inside watcher path")
// This is a watcher
if killServer {
Debug.log("killing server")
err = killProcess(server)
if err != nil {
// do nothing we continue after kill errors
Warning.log("killProcess received error ", err)
}
}
err = killProcess(fileWatcher)
if err != nil {
// do nothing we continue after kill errors
Warning.log("Watcher killProcess received error ", err)
}
commandToUse := commandString
processTypeToUse := fileWatcher
if !killServer {
// this path is only relevant for APPSODY_<RUN/DEBUG/TEST>KILL_SERVER=FALSE
// get the process of the current server (should not be nil ever) and send benign SIG 0 to the server proces
if cmps.processes[server] != nil && cmps.processes[server].Signal(syscall.Signal(0)) != nil {
// if there is no server process, an error is returned
Debug.log("The server process with pid:", cmps.processes[server].Pid, "was not found, and APPSODY_<action>_KILL is set to false. The server will be restarted.")
//start the server with the startCommand, not the watch action command
commandToUse = startCommand
processTypeToUse = server
}
}
cmd, err = startProcess(commandToUse, processTypeToUse)
if err != nil {
Warning.log("ERROR start process received error: ", err)
}
cmps.mu.Unlock()
mutexUnlocked = true
Debug.log("mutex unlocked")
err = waitProcess(cmd, processTypeToUse)
if err != nil {
// do nothing as the kill causees and error condition
Info.log("Wait received error ", err)
}
}
if !mutexUnlocked {
cmps.mu.Unlock()
}
Debug.log("runCommands EXIT")
}
var startCommand string
func main() {
var err error
var fileChangeCommand string
debugMode := false
testMode := false
var dirs []string
var stopWatchServerOnChange bool
errorMessage := ""
var errWorkDir error
mode := flag.String("mode", "run", "This is the mode the controller runs in: run, debug or test")
flag.BoolVar(&verbose, "verbose", false, "Turns on debug output and logging ")
flag.BoolVar(&vmode, "v", false, "Turns on debug output and logging ")
flag.Parse()
klogFlags = flag.NewFlagSet("klog", flag.ExitOnError)
klog.InitFlags(klogFlags)
if vmode || verbose {
// set debug mode
_ = klogFlags.Set("v", "4")
_ = klogFlags.Set("skip_headers", "false")
} else {
_ = klogFlags.Set("skip_headers", "true")
}
Debug.log("Controller main ENTRY " + VERSION)
if strings.Compare(*mode, "test") == 0 {
testMode = true
}
if strings.Compare(*mode, "debug") == 0 {
debugMode = true
}
workDir, errWorkDir = os.Getwd()
if errWorkDir != nil {
Fatal.log("Could not find the working dir ", errWorkDir)
os.Exit(1)
}
// Obtain the environment variables
err = setupEnvironmentVars()
if err != nil {
errorMessage = "Warning: setup did not find all environment variables "
Fatal.log(errorMessage, err)
os.Exit(1)
}
// Set the startCommand based upon whether debug Mode is enabled
if debugMode {
startCommand = appsodyDEBUG
} else if testMode {
startCommand = appsodyTEST
} else {
startCommand = appsodyRUN
}
if startCommand == "" {
Warning.log("Warning: startCommand (environment variable APPSODY_DEBUG,APPSODY_TEST or APPSODY_RUN) is unspecified")
}
Debug.log("startCommand: " + startCommand)
if debugMode {
//note this could be ""
fileChangeCommand = appsodyDEBUGWATCHACTION
} else if testMode {
fileChangeCommand = appsodyTESTWATCHACTION
} else {
fileChangeCommand = appsodyRUNWATCHACTION
}
Debug.log("File change command: " + fileChangeCommand)
if fileChangeCommand == "" {
Debug.log("fileChangeCommand environment variable APPSODY_WATCH_ACTION) is unspecified.")
Debug.log("Running sync: " + startCommand)
runCommands(startCommand, server, false)
} else {
Debug.log("Running " + startCommand)
go runCommands(startCommand, server, false)
}
// use the appropriate server on change setting
if debugMode {
stopWatchServerOnChange = appsodyDEBUGKILL
} else if testMode {
stopWatchServerOnChange = appsodyTESTKILL
} else {
stopWatchServerOnChange = appsodyRUNKILL
}
// Prefer the watch dirs be set to the APPSODY_WATCH_DIR value, but fall back to the APPSODY_MOUNTS if need be
if appsodyWATCHDIRS != nil {
dirs = appsodyWATCHDIRS
} else {
dirs = appsodyMOUNTS
}
if fileChangeCommand != "" {
err = runWatcher(fileChangeCommand, dirs, stopWatchServerOnChange)
} else {
Info.log("Watcher is not running.")
}
if err != nil {
errorMessage = "Error running watcher "
Fatal.log(errorMessage, err)
os.Exit(1)
}
Debug.log("Controller main EXIT")
}