-
Notifications
You must be signed in to change notification settings - Fork 109
/
main.go
237 lines (209 loc) · 6.19 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
package main
import (
"embed"
"encoding/json"
"fmt"
"os"
"os/exec"
"os/signal"
"syscall"
"time"
"github.com/housepower/ckman/common"
"github.com/housepower/ckman/repository"
"github.com/housepower/ckman/service/cron"
"github.com/housepower/ckman/service/runner"
"github.com/housepower/ckman/config"
"github.com/housepower/ckman/log"
_ "github.com/housepower/ckman/repository/dm8"
_ "github.com/housepower/ckman/repository/local"
_ "github.com/housepower/ckman/repository/mysql"
_ "github.com/housepower/ckman/repository/postgres"
"github.com/housepower/ckman/server"
"github.com/housepower/ckman/service/nacos"
"github.com/housepower/ckman/service/zookeeper"
"github.com/patrickmn/go-cache"
"github.com/spf13/cobra"
"gopkg.in/sevlyar/go-daemon.v0"
)
const (
MARK_NAME = "_GO_CKMAN_RELOAD"
MARK_VALUE = "1"
)
var (
Version = ""
BuildTimeStamp = ""
GitCommitHash = ""
Daemon = false
ConfigFilePath = ""
LogFilePath = ""
PidFilePath = ""
EncryptPassword = ""
)
//go:embed static/dist
var fs embed.FS
// @title CKMAN API
// @version 2.0
// @securityDefinitions.apikey ApiKeyAuth
// @in header
// @name token
// @BasePath /
func main() {
InitCmd()
if err := config.ParseConfigFile(ConfigFilePath, Version); err != nil {
fmt.Printf("Parse config file %s fail: %v\n", ConfigFilePath, err)
os.Exit(1)
}
var conf config.CKManConfig
_ = common.DeepCopyByGob(&conf, &config.GlobalConfig)
err := common.Gsypt.Unmarshal(&config.GlobalConfig)
if err != nil {
fmt.Printf("gsypt config file %s fail: %v\n", ConfigFilePath, err)
os.Exit(1)
}
log.InitLogger(LogFilePath, &config.GlobalConfig.Log)
cntxt := &daemon.Context{
PidFileName: PidFilePath,
PidFilePerm: 0644,
LogFilePerm: 0640,
WorkDir: "./",
Umask: 027,
}
if Daemon && os.Getenv(MARK_NAME) != MARK_VALUE {
d, err := cntxt.Reborn()
if err != nil {
log.Logger.Fatal(err)
}
if d != nil {
return
}
defer cntxt.Release()
}
log.Logger.Info("ckman starting...")
log.Logger.Infof("version: %v", Version)
log.Logger.Infof("build time: %v", BuildTimeStamp)
log.Logger.Infof("git commit hash: %v", GitCommitHash)
//dump config to log must ensure the password not be decode
DumpConfig(conf)
if config.GlobalConfig.Server.Ip == "" {
config.GlobalConfig.Server.Ip = common.GetOutboundIP().String()
}
signalCh := make(chan os.Signal, 1)
defer common.Pool.Close()
err = repository.InitPersistent()
if err != nil {
log.Logger.Fatalf("init persistent failed:%v", err)
}
nacosClient, err := nacos.InitNacosClient(&config.GlobalConfig.Nacos, LogFilePath)
if err != nil {
log.Logger.Fatalf("Failed to init nacos client, %v", err)
}
err = nacosClient.Start(config.GlobalConfig.Server.Ip, config.GlobalConfig.Server.Port)
if err != nil {
log.Logger.Fatalf("Failed to start nacos client, %v", err)
}
zookeeper.ZkServiceCache = cache.New(time.Hour, time.Minute)
zookeeper.ZkServiceCache.OnEvicted(func(key string, value interface{}) {
value.(*zookeeper.ZkService).Conn.Close()
})
runnerServ := runner.NewRunnerService(config.GlobalConfig.Server.Ip, config.GlobalConfig.Server)
runnerServ.Start()
defer runnerServ.Stop()
cronSvr := cron.NewCronService(config.GlobalConfig.Cron)
if err = cronSvr.Start(); err != nil {
log.Logger.Fatalf("Failed to start cron service, %v", err)
}
defer cronSvr.Stop()
// start http server
svr := server.NewApiServer(&config.GlobalConfig, signalCh, fs)
if err := svr.Start(); err != nil {
log.Logger.Fatalf("start http server fail: %v", err)
}
defer svr.Stop()
log.Logger.Infof("start http server %s:%d success", config.GlobalConfig.Server.Ip, config.GlobalConfig.Server.Port)
//block here, waiting for terminal signal
handleSignal(signalCh)
}
func handleSignal(ch chan os.Signal) {
signal.Notify(ch, syscall.SIGTERM, syscall.SIGINT, syscall.SIGHUP)
sig := <-ch
log.Logger.Infof("receive signal: %v", sig)
log.Logger.Warn("ckman exiting...")
switch sig {
case syscall.SIGINT, syscall.SIGTERM:
_ = termHandler()
case syscall.SIGHUP:
_ = termHandler()
_ = reloadHandler()
}
signal.Stop(ch)
}
func termHandler() error {
var hosts []string
common.ConnectPool.Range(func(k, v interface{}) bool {
hosts = append(hosts, k.(string))
return true
})
common.CloseConns(hosts)
return nil
}
func reloadHandler() error {
env := os.Environ()
mark := fmt.Sprintf("%s=%s", MARK_NAME, MARK_VALUE)
env = append(env, mark)
cmd := exec.Command(os.Args[0], os.Args[1:]...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Env = env
return cmd.Start()
}
var VersionCmd = &cobra.Command{
Use: "version",
Short: "Print version info",
Long: "Print version info",
Run: func(cmd *cobra.Command, args []string) {
fmt.Printf("version: %v\n", Version)
fmt.Printf("build time: %v\n", BuildTimeStamp)
fmt.Printf("git commit hash: %v\n", GitCommitHash)
os.Exit(0)
},
}
func InitCmd() {
var rootCmd = &cobra.Command{
Use: "ckman",
}
rootCmd.PersistentFlags().StringVarP(&ConfigFilePath, "conf", "c", "conf/ckman.hjson", "Task file path")
rootCmd.PersistentFlags().StringVarP(&LogFilePath, "log", "l", "logs/ckman.log", "Log file path")
rootCmd.PersistentFlags().StringVarP(&PidFilePath, "pid", "p", "run/ckman.pid", "Pid file path")
rootCmd.PersistentFlags().StringVarP(&EncryptPassword, "encrypt", "e", "", "encrypt password")
rootCmd.PersistentFlags().BoolVarP(&Daemon, "daemon", "d", false, "Run as daemon")
rootCmd.AddCommand(VersionCmd)
rootCmd.SetUsageFunc(func(cmd *cobra.Command) error {
return nil
})
rootCmd.SetHelpCommand(&cobra.Command{
Use: "help",
Short: "Help about any command",
Long: "Help about any command",
Run: func(cmd *cobra.Command, args []string) {
rootCmd.SetUsageFunc(nil)
_ = rootCmd.Help()
os.Exit(0)
},
})
_ = rootCmd.Execute()
if EncryptPassword != "" {
fmt.Println(common.AesEncryptECB(EncryptPassword))
os.Exit(0)
}
fmt.Println("ckman is used to manager and monitor clickhouse")
fmt.Printf("ckman-%v is running...\n", Version)
fmt.Printf("See more information in %s\n", LogFilePath)
}
func DumpConfig(conf config.CKManConfig) {
data, err := json.MarshalIndent(conf, "", " ")
if err != nil {
log.Logger.Errorf("marshal error: %v", err)
return
}
log.Logger.Infof("%v", string(data))
}