forked from ochinchina/supervisord
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
155 lines (142 loc) · 3.77 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
package main
import (
"bufio"
"fmt"
"github.com/jessevdk/go-flags"
log "github.com/sirupsen/logrus"
"os"
"os/signal"
"path/filepath"
"runtime"
"strings"
"syscall"
"unicode"
)
// Options the command line options
type Options struct {
Configuration string `short:"c" long:"configuration" description:"the configuration file"`
Daemon bool `short:"d" long:"daemon" description:"run as daemon"`
EnvFile string `long:"env-file" description:"the environment file"`
}
func init() {
log.SetOutput(os.Stdout)
if runtime.GOOS == "windows" {
log.SetFormatter(&log.TextFormatter{DisableColors: true, FullTimestamp: true})
} else {
log.SetFormatter(&log.TextFormatter{DisableColors: false, FullTimestamp: true})
}
log.SetLevel(log.DebugLevel)
}
func initSignals(s *Supervisor) {
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
go func() {
sig := <-sigs
log.WithFields(log.Fields{"signal": sig}).Info("receive a signal to stop all process & exit")
s.procMgr.StopAllProcesses()
os.Exit(-1)
}()
}
var options Options
var parser = flags.NewParser(&options, flags.Default & ^flags.PrintErrors)
func loadEnvFile() {
if len(options.EnvFile) <= 0 {
return
}
//try to open the environment file
f, err := os.Open(options.EnvFile)
if err != nil {
log.WithFields(log.Fields{"file": options.EnvFile}).Error("Fail to open environment file")
return
}
defer f.Close()
reader := bufio.NewReader(f)
for {
//for each line
line, err := reader.ReadString('\n')
if err != nil {
break
}
//if line starts with '#', it is a comment line, ignore it
line = strings.TrimSpace(line)
if len(line) > 0 && line[0] == '#' {
continue
}
//if environment variable is exported with "export"
if strings.HasPrefix(line, "export") && len(line) > len("export") && unicode.IsSpace(rune(line[len("export")])) {
line = strings.TrimSpace(line[len("export"):])
}
//split the environment variable with "="
pos := strings.Index(line, "=")
if pos != -1 {
k := strings.TrimSpace(line[0:pos])
v := strings.TrimSpace(line[pos+1:])
//if key and value are not empty, put it into the environment
if len(k) > 0 && len(v) > 0 {
os.Setenv(k, v)
}
}
}
}
// find the supervisord.conf in following order:
//
// 1. $CWD/supervisord.conf
// 2. $CWD/etc/supervisord.conf
// 3. /etc/supervisord.conf
// 4. /etc/supervisor/supervisord.conf (since Supervisor 3.3.0)
// 5. ../etc/supervisord.conf (Relative to the executable)
// 6. ../supervisord.conf (Relative to the executable)
func findSupervisordConf() (string, error) {
possibleSupervisordConf := []string{options.Configuration,
"./supervisord.conf",
"./etc/supervisord.conf",
"/etc/supervisord.conf",
"/etc/supervisor/supervisord.conf",
"../etc/supervisord.conf",
"../supervisord.conf"}
for _, file := range possibleSupervisordConf {
if _, err := os.Stat(file); err == nil {
absFile, err := filepath.Abs(file)
if err == nil {
return absFile, nil
}
return file, nil
}
}
return "", fmt.Errorf("fail to find supervisord.conf")
}
func runServer() {
// infinite loop for handling Restart ('reload' command)
loadEnvFile()
for true {
options.Configuration, _ = findSupervisordConf()
s := NewSupervisor(options.Configuration)
initSignals(s)
if _, _, _, sErr := s.Reload(); sErr != nil {
panic(sErr)
}
s.WaitForExit()
}
}
func main() {
ReapZombie()
if _, err := parser.Parse(); err != nil {
flagsErr, ok := err.(*flags.Error)
if ok {
switch flagsErr.Type {
case flags.ErrHelp:
fmt.Fprintln(os.Stdout, err)
os.Exit(0)
case flags.ErrCommandRequired:
if options.Daemon {
Deamonize(runServer)
} else {
runServer()
}
default:
fmt.Fprintf(os.Stderr, "error when parsing command: %s\n", err)
os.Exit(1)
}
}
}
}