forked from ochinchina/supervisord
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
191 lines (175 loc) · 4.86 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
package main
import (
"bufio"
"fmt"
"os"
"os/signal"
"path/filepath"
"runtime"
"strings"
"syscall"
"unicode"
"github.com/jessevdk/go-flags"
"github.com/ochinchina/go-ini"
"github.com/ochinchina/supervisord/config"
"github.com/ochinchina/supervisord/logger"
log "github.com/sirupsen/logrus"
)
// 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() {
nullLogger := logger.NewNullLogger(logger.NewNullLogEventEmitter())
log.SetOutput(nullLogger)
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 {
if len(options.Configuration) <= 0 {
options.Configuration, _ = findSupervisordConf()
}
s := NewSupervisor(options.Configuration)
initSignals(s)
if _, _, _, sErr := s.Reload(); sErr != nil {
panic(sErr)
}
s.WaitForExit()
}
}
// Get the supervisord log file
func getSupervisordLogFile(configFile string) string {
configFileDir := filepath.Dir(configFile)
env := config.NewStringExpression("here", configFileDir)
myini := ini.NewIni()
myini.LoadFile(configFile)
cwd, err := os.Getwd()
if err != nil {
cwd = "."
}
logFile := myini.GetValueWithDefault("supervisord", "logfile", filepath.Join(cwd, "supervisord.log"))
logFile, err = env.Eval(logFile)
if err == nil {
return logFile
} else {
return filepath.Join(".", "supervisord.log")
}
}
func main() {
ReapZombie()
// when execute `supervisord` without sub-command, it should start the server
parser.Command.SubcommandsOptional = true
parser.CommandHandler = func(command flags.Commander, args []string) error {
if command == nil {
log.SetOutput(os.Stdout)
if options.Daemon {
logFile := getSupervisordLogFile(options.Configuration)
Daemonize(logFile, runServer)
} else {
runServer()
}
os.Exit(0)
}
return command.Execute(args)
}
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)
default:
_, _ = fmt.Fprintf(os.Stderr, "error when parsing command: %s\n", err)
os.Exit(1)
}
}
}
}