forked from CodisLabs/codis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
155 lines (135 loc) · 3.13 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
// Copyright 2014 Wandoujia Inc. All Rights Reserved.
// Licensed under the MIT (MIT-LICENSE.txt) license.
package main
import (
"flag"
"fmt"
"net/http"
_ "net/http/pprof"
"os"
"os/signal"
"strings"
"syscall"
"github.com/c4pt0r/cfg"
"github.com/docopt/docopt-go"
"github.com/wandoulabs/codis/pkg/utils/errors"
"github.com/wandoulabs/codis/pkg/utils/log"
)
// global objects
var (
globalEnv Env
livingNode string
createdDashboardNode bool
)
type Command struct {
Run func(cmd *Command, args []string)
Usage string
Short string
Long string
Flag flag.FlagSet
Ctx interface{}
}
var usage = `usage: codis-config [-c <config_file>] [-L <log_file>] [--log-level=<loglevel>]
<command> [<args>...]
options:
-c set config file
-L set output log file, default is stdout
--log-level=<loglevel> set log level: info, warn, error, debug [default: info]
commands:
server
slot
dashboard
action
proxy
`
func init() {
log.SetLevel(log.LEVEL_INFO)
}
func setLogLevel(level string) {
var lv = log.LEVEL_INFO
switch strings.ToLower(level) {
case "error":
lv = log.LEVEL_ERROR
case "warn", "warning":
lv = log.LEVEL_WARN
case "debug":
lv = log.LEVEL_DEBUG
case "info":
fallthrough
default:
lv = log.LEVEL_INFO
}
log.SetLevel(lv)
log.Infof("set log level to %s", lv)
}
func runCommand(cmd string, args []string) (err error) {
argv := make([]string, 1)
argv[0] = cmd
argv = append(argv, args...)
switch cmd {
case "action":
return errors.Trace(cmdAction(argv))
case "dashboard":
return errors.Trace(cmdDashboard(argv))
case "server":
return errors.Trace(cmdServer(argv))
case "proxy":
return errors.Trace(cmdProxy(argv))
case "slot":
return errors.Trace(cmdSlot(argv))
}
return errors.Errorf("%s is not a valid command. See 'codis-config -h'", cmd)
}
func main() {
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
signal.Notify(c, syscall.SIGTERM)
go func() {
<-c
if createdDashboardNode {
releaseDashboardNode()
}
log.Panicf("ctrl-c or SIGTERM found, exit")
}()
args, err := docopt.Parse(usage, nil, true, "codis config v0.1", true)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
// set output log file
if s, ok := args["-L"].(string); ok && s != "" {
f, err := os.OpenFile(s, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0666)
if err != nil {
log.PanicErrorf(err, "open log file failed: %s", s)
} else {
defer f.Close()
log.StdLog = log.New(f, "")
}
}
log.SetLevel(log.LEVEL_INFO)
log.SetFlags(log.Flags() | log.Lshortfile)
// set log level
if s, ok := args["--log-level"].(string); ok && s != "" {
setLogLevel(s)
}
// set config file
var configFile string
if args["-c"] != nil {
configFile = args["-c"].(string)
} else {
configFile = "config.ini"
}
config := cfg.NewCfg(configFile)
if err := config.Load(); err != nil {
log.PanicErrorf(err, "load config file error")
}
// load global vars
globalEnv = LoadCodisEnv(config)
cmd := args["<command>"].(string)
cmdArgs := args["<args>"].([]string)
go http.ListenAndServe(":10086", nil)
err = runCommand(cmd, cmdArgs)
if err != nil {
log.PanicErrorf(err, "run sub-command failed")
}
}