-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
171 lines (134 loc) · 3.12 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
package main
import (
"context"
"flag"
"fmt"
"io/ioutil"
"os"
"os/signal"
"path/filepath"
"strings"
"sync"
"syscall"
"github.com/sirupsen/logrus"
"gopkg.in/yaml.v2"
"github.com/heraldgo/heraldd/util"
)
var log *logrus.Logger
var cfg struct {
LogLevel string `yaml:"log_level"`
LogTimeFormat string `yaml:"time_format"`
LogOutput string `yaml:"log_output"`
WorkDir string `yaml:"work_dir"`
Secret string `yaml:"secret"`
UnixSocket string `yaml:"unix_socket"`
Host string `yaml:"host"`
Port int `yaml:"port"`
}
func loadConfigFile(configFile string) error {
buffer, err := ioutil.ReadFile(configFile)
if err != nil {
return err
}
err = yaml.Unmarshal(buffer, &cfg)
if err != nil {
return err
}
return nil
}
func setupLog(logFile **os.File) {
level := logrus.InfoLevel
timeFormat := "2006-01-02 15:04:05.000 -0700 MST"
levelLogrusTemp, err := logrus.ParseLevel(cfg.LogLevel)
if err == nil {
level = levelLogrusTemp
}
if cfg.LogTimeFormat != "" {
timeFormat = cfg.LogTimeFormat
}
log.SetLevel(level)
log.SetFormatter(&util.SimpleFormatter{
TimeFormat: timeFormat,
})
if cfg.LogOutput != "" {
logDir := filepath.Dir(cfg.LogOutput)
if logDir != "" {
os.MkdirAll(logDir, 0755)
if err != nil {
log.Errorf(`Create log directory "%s" failed: %s`, logDir, err)
return
}
}
f, err := os.OpenFile(cfg.LogOutput, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
log.Errorf(`[HeraldRunner] Create log file "%s" error: %s`, cfg.LogOutput, err)
} else {
log.SetOutput(f)
*logFile = f
}
}
}
func newRunner() *runner {
s := &runner{}
s.ServerHeader = "herald-runner"
s.UnixSocket = cfg.UnixSocket
s.Host = cfg.Host
s.Port = cfg.Port
s.secret = cfg.Secret
s.exeGit.WorkDir = cfg.WorkDir
if s.Port == 0 && s.UnixSocket == "" {
s.Host = "127.0.0.1"
s.Port = 8124
}
s.SetLogger(&util.PrefixLogger{
Logger: log,
Prefix: "[HeraldRunner]",
})
return s
}
func printVersion() {
fmt.Printf("Herald Runner %s\n", Version)
}
func main() {
flagVersion := flag.Bool("version", false, "Print Herald Runner version")
flagConfigFile := flag.String("config", "config.yml", "Configuration file path")
flag.Parse()
if *flagVersion {
printVersion()
return
}
log = logrus.New()
err := loadConfigFile(*flagConfigFile)
if err != nil {
log.Errorf(`[HeraldRunner] Load config file "%s" error: %s`, *flagConfigFile, err)
return
}
var logFile *os.File
defer func() {
if logFile != nil {
logFile.Close()
}
}()
setupLog(&logFile)
log.Infoln(strings.Repeat("=", 80))
log.Infoln("[HeraldRunner] Herald runner version:", Version)
log.Infoln("[HeraldRunner] Initialize...")
ctx := context.Background()
ctx, cancel := context.WithCancel(ctx)
s := newRunner()
log.Infoln("[HeraldRunner] Start...")
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
s.Run(ctx)
}()
quit := make(chan os.Signal)
signal.Notify(quit, os.Interrupt, syscall.SIGTERM)
<-quit
log.Infoln("[HeraldRunner] Shutdown...")
cancel()
wg.Wait()
log.Infoln("[HeraldRunner] Exiting...")
log.Infoln(strings.Repeat("-", 80))
}