-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
388 lines (343 loc) · 11.5 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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
/**
* 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 (
"fmt"
"io"
"io/ioutil"
"net"
"os"
"strconv"
"strings"
"time"
"github.com/gogo/protobuf/proto"
log "github.com/golang/glog"
"github.com/mesos/mesos-go/auth"
"github.com/mesos/mesos-go/auth/sasl"
mesos "github.com/mesos/mesos-go/mesosproto"
util "github.com/mesos/mesos-go/mesosutil"
sched "github.com/mesos/mesos-go/scheduler"
mlog "github.com/yp-engineering/mesos-runonce/mesosutil"
"golang.org/x/net/context"
)
var eventCh = make(chan *mesos.TaskStatus)
var _frameworkId string
var exitStatus = 0
var config *Config
type MesosRunonceScheduler struct {
executor *mesos.ExecutorInfo
tasksLaunched int
tasksFinished int
totalTasks int
}
// ----------------------- Mesos interface ------------------------- //
func (sched *MesosRunonceScheduler) Registered(driver sched.SchedulerDriver, frameworkId *mesos.FrameworkID, masterInfo *mesos.MasterInfo) {
log.V(1).Infoln("Framework Registered with Master ", masterInfo)
_frameworkId = frameworkId.GetValue()
fmt.Println("Registered with master and given framework ID:", _frameworkId)
}
func (sched *MesosRunonceScheduler) Reregistered(driver sched.SchedulerDriver, masterInfo *mesos.MasterInfo) {
log.V(1).Infoln("Framework Re-Registered with Master ", masterInfo)
}
func (sched *MesosRunonceScheduler) Disconnected(sched.SchedulerDriver) {
log.Exitf("disconnected from master, aborting")
}
func (sched *MesosRunonceScheduler) OfferRescinded(_ sched.SchedulerDriver, oid *mesos.OfferID) {
log.V(1).Infof("offer rescinded: %v", oid)
}
func (sched *MesosRunonceScheduler) FrameworkMessage(_ sched.SchedulerDriver, eid *mesos.ExecutorID, sid *mesos.SlaveID, msg string) {
log.Errorf("framework message from executor %q slave %q: %q", eid, sid, msg)
}
func (sched *MesosRunonceScheduler) SlaveLost(_ sched.SchedulerDriver, sid *mesos.SlaveID) {
log.V(1).Infof("slave lost: %v", sid)
}
func (sched *MesosRunonceScheduler) ExecutorLost(_ sched.SchedulerDriver, eid *mesos.ExecutorID, sid *mesos.SlaveID, code int) {
log.Errorf("executor %q lost on slave %q code %d", eid, sid, code)
}
func (sched *MesosRunonceScheduler) Error(_ sched.SchedulerDriver, err string) {
log.Exitf("Scheduler received error: %v", err)
}
func (sched *MesosRunonceScheduler) ResourceOffers(driver sched.SchedulerDriver, offers []*mesos.Offer) {
if sched.tasksLaunched >= sched.totalTasks {
log.V(1).Info("decline all of the offers since all of our tasks are already launched")
ids := make([]*mesos.OfferID, len(offers))
for i, offer := range offers {
ids[i] = offer.Id
}
driver.LaunchTasks(ids, []*mesos.TaskInfo{}, &mesos.Filters{RefuseSeconds: proto.Float64(120)})
return
}
for _, offer := range offers {
cpuResources := util.FilterResources(offer.Resources, func(res *mesos.Resource) bool {
return res.GetName() == "cpus"
})
cpus := 0.0
for _, res := range cpuResources {
cpus += res.GetScalar().GetValue()
}
memResources := util.FilterResources(offer.Resources, func(res *mesos.Resource) bool {
return res.GetName() == "mem"
})
mems := 0.0
for _, res := range memResources {
mems += res.GetScalar().GetValue()
}
log.V(1).Infoln("Received Offer <", offer.Id.GetValue(), "> on host ", *offer.Hostname, "with cpus=", cpus, " mem=", mems)
remainingCpus := cpus
remainingMems := mems
dCpus := config.Task.Docker.Cpus
dMem := config.Task.Docker.Mem
var tasks []*mesos.TaskInfo
for sched.tasksLaunched < sched.totalTasks &&
dCpus <= remainingCpus &&
dMem <= remainingMems {
sched.tasksLaunched++
tID := strconv.Itoa(sched.tasksLaunched)
if config.Task.Id != "" {
tID = config.Task.Id
}
tName := "mesos-runonce-" + tID
if config.Task.Name != "" {
tName = config.Task.Name
}
taskId := &mesos.TaskID{
Value: proto.String(tID),
}
containerType := mesos.ContainerInfo_DOCKER
task := &mesos.TaskInfo{
Name: proto.String(tName),
TaskId: taskId,
SlaveId: offer.SlaveId,
Resources: []*mesos.Resource{
util.NewScalarResource("cpus", dCpus),
util.NewScalarResource("mem", dMem),
},
Command: &mesos.CommandInfo{
Shell: proto.Bool(false),
},
Container: &mesos.ContainerInfo{
Type: &containerType,
Docker: &mesos.ContainerInfo_DockerInfo{
Image: proto.String(config.Task.Docker.Image),
ForcePullImage: proto.Bool(config.Task.Docker.ForcePullImage),
},
},
}
fmt.Printf("Prepared task: [%s] with offer [%s] for launch on host [%s]\n", task.GetName(), offer.Id.GetValue(), *offer.Hostname)
if len(config.Task.Docker.Env) != 0 || config.Task.Docker.EnvString != "" {
task.Command.Environment = config.EnvVars()
}
// Allow arbitrary commands, else just use whatever the image defines in CMD
if config.Task.Docker.Cmd != "" {
task.Command.Value = proto.String(config.Task.Docker.Cmd)
task.Command.Shell = proto.Bool(true)
}
tasks = append(tasks, task)
remainingCpus -= dCpus
remainingMems -= dMem
}
log.V(1).Infoln("Launching ", len(tasks), "tasks for offer", offer.Id.GetValue())
driver.LaunchTasks([]*mesos.OfferID{offer.Id}, tasks, &mesos.Filters{RefuseSeconds: proto.Float64(5)})
}
}
func (sched *MesosRunonceScheduler) StatusUpdate(driver sched.SchedulerDriver, status *mesos.TaskStatus) {
log.V(1).Infoln("Status update: task", status.TaskId.GetValue(), " is in state ", status.State.Enum().String())
eventCh <- status
if status.GetState() == mesos.TaskState_TASK_FINISHED {
sched.tasksFinished++
}
if sched.tasksFinished >= sched.totalTasks {
log.V(1).Infoln("Total tasks completed, stopping framework.")
driver.Stop(false)
}
if status.GetState() == mesos.TaskState_TASK_LOST ||
status.GetState() == mesos.TaskState_TASK_KILLED ||
status.GetState() == mesos.TaskState_TASK_FAILED ||
status.GetState() == mesos.TaskState_TASK_ERROR {
exitStatus = 1
log.Warningf("mesos TaskStatus: %v", status)
driver.Stop(false)
log.Errorln(
"Aborting because task", status.TaskId.GetValue(),
"is in unexpected state", status.State.String(),
"with message.", status.GetMessage(),
)
}
}
// ----------------------- Helper methods ------------------------- //
func printLogs() {
timer := time.Tick(500 * time.Millisecond)
var (
readableStatus *mesos.TaskStatus
finished bool
oout, oerr int
)
for {
select {
case status := <-eventCh:
switch status.GetState() {
case mesos.TaskState_TASK_RUNNING,
mesos.TaskState_TASK_STARTING:
readableStatus = status
case mesos.TaskState_TASK_FAILED,
mesos.TaskState_TASK_LOST,
mesos.TaskState_TASK_KILLED:
readableStatus = status
finished = true
case mesos.TaskState_TASK_FINISHED:
finished = true
}
case <-timer:
if readableStatus != nil {
if finished {
time.Sleep(3 * time.Second)
}
x := printLog(readableStatus, oout, os.Stdout)
y := printLog(readableStatus, oerr, os.Stderr)
if finished && x == 0 && y == 0 {
log.V(1).Infof("framework terminating")
os.Exit(exitStatus)
}
oout += x
oerr += y
}
}
}
}
func printLog(status *mesos.TaskStatus, offset int, w io.Writer) int {
file := "stdout"
if w == os.Stderr {
file = "stderr"
}
data, err := mlog.FetchLogs(status, offset, file, _frameworkId)
if err != nil {
log.V(1).Infof("fetch logs err: %s\n", err)
return 0
}
if len(data) > 0 {
fmt.Fprint(w, string(data))
}
return len(data)
}
func prepareExecutorInfo() *mesos.ExecutorInfo {
// Create mesos scheduler driver.
containerType := mesos.ContainerInfo_DOCKER
return &mesos.ExecutorInfo{
ExecutorId: util.NewExecutorID("default"),
Name: proto.String("mesos-runonce-executor"),
Source: proto.String("mesos-runonce-executor"),
Container: &mesos.ContainerInfo{
Type: &containerType,
},
}
}
func parseIP(address string) net.IP {
addr, err := net.LookupIP(address)
if err != nil {
log.Exit(err)
}
if len(addr) < 1 {
log.Exitf("failed to parse IP from address '%v'", address)
}
return addr[0]
}
func newMesosRunonceScheduler(exec *mesos.ExecutorInfo) *MesosRunonceScheduler {
return &MesosRunonceScheduler{
executor: exec,
tasksLaunched: 0,
tasksFinished: 0,
totalTasks: config.Runonce.TaskCount,
}
}
func fwinfo() *mesos.FrameworkInfo {
fw := &mesos.FrameworkInfo{
User: proto.String(config.Runonce.MesosRunasUser),
Name: proto.String("mesos-runonce"),
}
if config.Runonce.MesosRole != "" {
fw.Role = proto.String(config.Runonce.MesosRole)
}
return fw
}
func cred(fwinfo *mesos.FrameworkInfo) *mesos.Credential {
cred := (*mesos.Credential)(nil)
mesosAuthPrincipal := config.Runonce.AuthPrincipal
if mesosAuthPrincipal != "" {
fwinfo.Principal = proto.String(mesosAuthPrincipal)
cred = &mesos.Credential{
Principal: proto.String(mesosAuthPrincipal),
}
mesosAuthSecretFile := config.Runonce.AuthSecretFile
if mesosAuthSecretFile != "" {
_, err := os.Stat(mesosAuthSecretFile)
if err != nil {
log.Exit("missing secret file: ", err.Error())
}
secret, err := ioutil.ReadFile(mesosAuthSecretFile)
if err != nil {
log.Exit("failed to read secret file: ", err.Error())
}
cred.Secret = proto.String(strings.TrimSuffix(string(secret), "\n"))
}
}
return cred
}
func driverConfig(fwinfo *mesos.FrameworkInfo) sched.DriverConfig {
// build command executor
exec := prepareExecutorInfo()
publishedAddress := parseIP(config.Runonce.Address)
dConfig := sched.DriverConfig{
Scheduler: newMesosRunonceScheduler(exec),
Framework: fwinfo,
Master: config.Runonce.Master,
Credential: cred(fwinfo),
PublishedAddress: publishedAddress,
WithAuthContext: func(ctx context.Context) context.Context {
ctx = auth.WithLoginProvider(ctx, config.Runonce.AuthProvider)
ctx = sasl.WithBindingAddress(ctx, publishedAddress)
return ctx
},
}
// Allow listening port to be configurable so we can run this inside of
// mesos if desired.
// NOTE only affects main PID, meaning the authentication step uses
// another PID which picks a random (32K range) port :(. Opened
// https://github.com/mesos/mesos-go/issues/229 to discuss.
if config.Runonce.BindingPort != 0 {
dConfig.BindingPort = uint16(config.Runonce.BindingPort)
}
return dConfig
}
// ----------------------- func main() ------------------------- //
func main() {
log.V(1).Infoln("Initializing the Example Scheduler...")
config = loadConfig()
// the framework
fwinfo := fwinfo()
driver, err := sched.NewMesosSchedulerDriver(driverConfig(fwinfo))
if err != nil {
log.Errorln("Unable to create a SchedulerDriver ", err.Error())
}
// Don't block on this call because we need printLogs to be the process blocker to ensure we retrieve all the logs.
go func() {
if stat, err := driver.Run(); err != nil {
log.V(1).Infof("Framework stopped with status %s and error: %s\n", stat.String(), err.Error())
}
}()
printLogs()
}