This repository has been archived by the owner on Jul 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 95
/
main.go
173 lines (148 loc) · 5.26 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
package main
import (
"fmt"
"net/http"
"os"
"strings"
"time"
"github.com/mantl/mesos-consul/config"
"github.com/mantl/mesos-consul/consul"
"github.com/mantl/mesos-consul/mesos"
flag "github.com/ogier/pflag"
log "github.com/sirupsen/logrus"
)
const Name = "mesos-consul"
const Version = "0.4.0"
func main() {
c, err := parseFlags(os.Args[1:])
if err != nil {
log.Fatal(err)
}
if c.Healthcheck {
go StartHealthcheckService(c)
}
log.Info("Using zookeeper: ", c.Zk)
leader := mesos.New(c)
ticker := time.NewTicker(c.Refresh)
leader.Refresh()
for _ = range ticker.C {
leader.Refresh()
}
}
func StartHealthcheckService(c *config.Config) {
http.HandleFunc("/health", HealthHandler)
log.Fatal(http.ListenAndServe(fmt.Sprintf("%s:%s", c.HealthcheckIp, c.HealthcheckPort), nil))
}
func HealthHandler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "OK")
}
func parseFlags(args []string) (*config.Config, error) {
var doHelp bool
var doVersion bool
var c = config.DefaultConfig()
flags := flag.NewFlagSet("mesos-consul", flag.ContinueOnError)
flags.Usage = func() {
fmt.Println(Help())
}
flags.BoolVar(&doHelp, "help", false, "")
flags.BoolVar(&doVersion, "version", false, "")
flags.StringVar(&c.LogLevel, "log-level", "WARN", "")
flags.DurationVar(&c.Refresh, "refresh", time.Minute, "")
flags.StringVar(&c.Zk, "zk", "zk://127.0.0.1:2181/mesos", "")
flags.StringVar(&c.Separator, "group-separator", "", "")
flags.StringVar(&c.MesosIpOrder, "mesos-ip-order", "netinfo,mesos,host", "")
flags.BoolVar(&c.Healthcheck, "healthcheck", false, "")
flags.StringVar(&c.HealthcheckIp, "healthcheck-ip", "", "")
flags.StringVar(&c.HealthcheckPort, "healthcheck-port", "24476", "")
flags.Var((funcVar)(func(s string) error {
c.TaskWhiteList = append(c.TaskWhiteList, s)
return nil
}), "whitelist", "")
flags.Var((funcVar)(func(s string) error {
c.TaskBlackList = append(c.TaskBlackList, s)
return nil
}), "blacklist", "")
flags.Var((funcVar)(func(s string) error {
c.FwWhiteList = append(c.FwWhiteList, s)
return nil
}), "fw-whitelist", "")
flags.Var((funcVar)(func(s string) error {
c.FwBlackList = append(c.FwBlackList, s)
return nil
}), "fw-blacklist", "")
flags.Var((funcVar)(func(s string) error {
c.TaskTag = append(c.TaskTag, s)
return nil
}), "task-tag", "")
flags.StringVar(&c.ServiceName, "service-name", "mesos", "")
flags.StringVar(&c.ServiceTags, "service-tags", "", "")
flags.StringVar(&c.ServiceIdPrefix, "service-id-prefix", "mesos-consul", "")
flags.StringVar(&c.ServicePortLabel, "service-port-label", "", "")
consul.AddCmdFlags(flags)
if err := flags.Parse(args); err != nil {
return nil, err
}
args = flags.Args()
if len(args) > 0 {
return nil, fmt.Errorf("extra argument(s): %q", args)
}
if doVersion {
fmt.Printf("%s v%s\n", Name, Version)
os.Exit(0)
}
if doHelp {
flags.Usage()
os.Exit(0)
}
l, err := log.ParseLevel(strings.ToLower(c.LogLevel))
if err != nil {
log.SetLevel(log.WarnLevel)
log.Warnf("Invalid log level '%v'. Setting to WARN", c.LogLevel)
} else {
log.SetLevel(l)
}
return c, nil
}
func Help() string {
helpText := `
Usage: mesos-consul [options]
Options:
--version Print mesos-consul version
--log-level=<log_level> Set the Logging level to one of [ "DEBUG", "INFO", "WARN", "ERROR" ]
(default "WARN")
--refresh=<time> Set the Mesos refresh rate (default 1m)
--zk=<address> Zookeeper path to Mesos (default zk://127.0.0.1:2181/mesos)
--group-separator=<separator> Choose the group separator. Will replace _ in task names (default is empty)
--healthcheck Enables a http endpoint for health checks. When this
flag is enabled, serves a service health status on 127.0.0.1:24476 (default not enabled)
--healthcheck-ip=<ip> Health check interface ip
--healthcheck-port=<port> Health check service port (default 24476)
--mesos-ip-order Comma separated list to control the order in
which mesos-consul searches for the task IP
address. Valid options are 'netinfo', 'mesos', 'docker' and 'host'
(default netinfo,mesos,host)
--heartbeats-before-remove Number of times that registration needs to fail before removing
task from Consul. (default: 1)
--whitelist=<regex> Only register services matching the provided regex.
Can be specified multiple times
--blacklist=<regex> Do not register services matching the provided regex.
Can be specified multiple times
--fw-whitelist=<regex> Only register services from frameworks matching the provided
regex.
Can be specified multiple times
--fw-blacklist=<regex> Do not register services from frameworks matching the provided
regex.
Can be specified multiple times
--task-tag=<pattern:tag> Tag tasks whose name contains 'pattern' substring (case-insensitive) with given tag.
Can be specified multiple times
--service-name=<name> Service name of the Mesos hosts. (default: mesos)
--service-tags=<tag>,... Comma delimited list of tags to add to the mesos hosts
Hosts are registered as
(leader|master|follower).<tag>.mesos.service.conul
` + consul.Help()
return strings.TrimSpace(helpText)
}
type funcVar func(s string) error
func (f funcVar) Set(s string) error { return f(s) }
func (f funcVar) String() string { return "" }
func (f funcVar) IsBoolFlag() bool { return false }