forked from mesosphere-backup/etcd-mesos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.go
256 lines (233 loc) · 8.44 KB
/
app.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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package main
import (
"flag"
"fmt"
"io/ioutil"
"net"
"net/http"
"os"
"github.com/gogo/protobuf/proto"
log "github.com/golang/glog"
"github.com/mesos/mesos-go/auth"
"github.com/mesos/mesos-go/auth/sasl"
"github.com/mesos/mesos-go/auth/sasl/mech"
mesos "github.com/mesos/mesos-go/mesosproto"
"github.com/mesos/mesos-go/scheduler"
"github.com/samuel/go-zookeeper/zk"
"golang.org/x/net/context"
"github.com/mesosphere/etcd-mesos/rpc"
etcdscheduler "github.com/mesosphere/etcd-mesos/scheduler"
)
func parseIP(address string) net.IP {
addr, err := net.LookupIP(address)
if err != nil {
log.Fatal(err)
}
if len(addr) < 1 {
log.Fatalf("failed to parse IP from address '%v'", address)
}
return addr[0]
}
func main() {
frameworkName :=
flag.String("framework-name", "etcd", "Unique name of this etcd cluster")
master :=
flag.String("master", "127.0.0.1:5050", "Master address <ip:port>")
zkFrameworkPersist :=
flag.String("zk-framework-persist", "", "Zookeeper URI of the form zk://host1:port1,host2:port2/chroot/path")
taskCount :=
flag.Int("cluster-size", 5, "Total task count to run")
adminPort :=
flag.Int("admin-port", 23400, "Binding port for admin interface")
reseedTimeout :=
flag.Int("reseed-timeout", 240, "Seconds of etcd livelock to wait for before attempting a cluster re-seed")
autoReseed :=
flag.Bool("auto-reseed", true, "Perform automatic cluster reseed when the "+
"cluster has been livelocked for -reseed-timeout seconds")
artifactPort :=
flag.Int("artifact-port", 12300, "Binding port for artifact server")
sandboxDisk :=
flag.Float64("sandbox-disk-limit", 4096, "Max disk usage for the etcd mesos sandbox in MB")
sandboxCpu :=
flag.Float64("sandbox-cpu-limit", 4, "Max cpu usage for the etcd mesos sandbox in MB")
sandboxMem :=
flag.Float64("sandbox-mem-limit", 2048, "Max memory usage for the etcd mesos sandbox in MB")
executorPath :=
flag.String("executor-bin", "./bin/etcd-mesos-executor", "Path to executor binary")
etcdPath :=
flag.String("etcd-bin", "./bin/etcd", "Path to etcd binary")
etcdctlPath :=
flag.String("etcdctl-bin", "./bin/etcdctl", "Path to etcdctl binary")
address :=
flag.String("address", "", "Binding address for scheduler and artifact server")
driverPort :=
flag.Int("driver-port", 0, "Binding port for scheduler driver")
mesosAuthPrincipal :=
flag.String("mesos-authentication-principal", "", "Mesos authentication principal")
mesosAuthSecretFile :=
flag.String("mesos-authentication-secret-file", "", "Mesos authentication secret file")
authProvider :=
flag.String("mesos-authentication-provider", sasl.ProviderName,
fmt.Sprintf("Authentication provider to use, default is SASL that supports mechanisms: %+v", mech.ListSupported()))
singleInstancePerSlave :=
flag.Bool("single-instance-per-slave", true, "Only allow one etcd instance to be started per slave")
failoverTimeoutSeconds :=
flag.Float64("failover-timeout-seconds", 60*60*24*7, "Mesos framework failover timeout in seconds")
weburi := flag.String("framework-weburi", "", "A URI that points to a web-based interface for interacting with the framework.")
flag.Parse()
if *zkFrameworkPersist == "" {
log.Fatal("No value provided for -zk-framework-persist !")
}
if !*singleInstancePerSlave {
log.Warning("-single-instance-per-slave=false is dangerous because it may lead to " +
"multiple etcd instances in the same cluster on a single node, amplifying " +
"the cost of a single node being lost, livelock, and data loss.")
}
if *address == "" {
hostname, err := os.Hostname()
if err == nil {
*address = hostname
} else {
log.Errorf("Could not set default binding to hostname. Defaulting to 127.0.0.1")
*address = "127.0.0.1"
}
}
if *weburi == "" {
*weburi = fmt.Sprintf("http://%s:%d/", *address, *adminPort)
}
executorUris := []*mesos.CommandInfo_URI{}
execUri, err := etcdscheduler.ServeExecutorArtifact(*executorPath, *address, *artifactPort)
if err != nil {
log.Errorf("Could not stat executor binary: %v", err)
return
}
executorUris = append(executorUris, &mesos.CommandInfo_URI{
Value: execUri,
Executable: proto.Bool(true),
})
etcdUri, err := etcdscheduler.ServeExecutorArtifact(*etcdPath, *address, *artifactPort)
if err != nil {
log.Errorf("Could not stat etcd binary: %v", err)
return
}
executorUris = append(executorUris, &mesos.CommandInfo_URI{
Value: etcdUri,
Executable: proto.Bool(true),
})
etcdctlUri, err := etcdscheduler.ServeExecutorArtifact(*etcdctlPath, *address, *artifactPort)
if err != nil {
log.Errorf("Could not stat etcd binary: %v", err)
return
}
executorUris = append(executorUris, &mesos.CommandInfo_URI{
Value: etcdctlUri,
Executable: proto.Bool(true),
})
go http.ListenAndServe(fmt.Sprintf("%s:%d", *address, *artifactPort), nil)
log.V(2).Info("Serving executor artifacts...")
bindingAddress := parseIP(*address)
// chillFactor is the number of seconds that are slept for to allow for
// convergence across the cluster during mutations.
chillFactor := 10
etcdScheduler := etcdscheduler.NewEtcdScheduler(
*taskCount,
chillFactor,
*reseedTimeout,
*autoReseed,
executorUris,
*singleInstancePerSlave,
*sandboxDisk,
*sandboxCpu,
*sandboxMem,
)
etcdScheduler.ExecutorPath = *executorPath
etcdScheduler.Master = *master
etcdScheduler.FrameworkName = *frameworkName
etcdScheduler.ZkConnect = *zkFrameworkPersist
fwinfo := &mesos.FrameworkInfo{
User: proto.String(""), // Mesos-go will fill in user.
Name: proto.String(*frameworkName),
Checkpoint: proto.Bool(true),
FailoverTimeout: proto.Float64(*failoverTimeoutSeconds),
WebuiUrl: proto.String(*weburi),
}
cred := (*mesos.Credential)(nil)
if *mesosAuthPrincipal != "" {
fwinfo.Principal = proto.String(*mesosAuthPrincipal)
secret, err := ioutil.ReadFile(*mesosAuthSecretFile)
if err != nil {
log.Fatal(err)
}
cred = &mesos.Credential{
Principal: proto.String(*mesosAuthPrincipal),
Secret: secret,
}
}
zkServers, zkChroot, err := rpc.ParseZKURI(*zkFrameworkPersist)
etcdScheduler.ZkServers = zkServers
etcdScheduler.ZkChroot = zkChroot
if err != nil && *zkFrameworkPersist != "" {
log.Fatalf("Error parsing zookeeper URI of %s: %s", *zkFrameworkPersist, err)
} else if *zkFrameworkPersist != "" {
previous, err := rpc.GetPreviousFrameworkID(
zkServers,
zkChroot,
etcdScheduler.FrameworkName,
)
if err != nil && err != zk.ErrNoNode {
log.Fatalf("Could not retrieve previous framework ID: %s", err)
} else if err == zk.ErrNoNode {
log.Info("No previous persisted framework ID exists in zookeeper.")
} else {
log.Infof("Found stored framework ID in Zookeeper, "+
"attempting to re-use: %s", previous)
fwinfo.Id = &mesos.FrameworkID{
Value: proto.String(previous),
}
}
}
config := scheduler.DriverConfig{
Scheduler: etcdScheduler,
Framework: fwinfo,
Master: etcdScheduler.Master,
Credential: cred,
BindingAddress: bindingAddress,
BindingPort: uint16(*driverPort),
WithAuthContext: func(ctx context.Context) context.Context {
ctx = auth.WithLoginProvider(ctx, *authProvider)
ctx = sasl.WithBindingAddress(ctx, bindingAddress)
return ctx
},
}
driver, err := scheduler.NewMesosSchedulerDriver(config)
if err != nil {
log.Errorln("Unable to create a SchedulerDriver ", err.Error())
}
go etcdScheduler.SerialLauncher(driver)
go etcdScheduler.PeriodicReconciler(driver)
go etcdScheduler.PeriodicHealthChecker()
go etcdScheduler.PeriodicLaunchRequestor()
go etcdScheduler.AdminHTTP(*adminPort, driver)
if stat, err := driver.Run(); err != nil {
log.Infof("Framework stopped with status %s and error: %s",
stat.String(),
err.Error())
}
}