| 1 |
package app |
| 2 |
|
| 3 |
import ( |
| 4 |
"errors" |
| 5 |
"fmt" |
| 6 |
"io/ioutil" |
| 7 |
"log" |
| 8 |
"math/rand" |
| 9 |
"net/http" |
| 10 |
"os" |
| 11 |
"time" |
| 12 |
|
| 13 |
proto "github.com/gogo/protobuf/proto" |
| 14 |
"github.com/mesos/mesos-go/api/v1/lib" |
| 15 |
"github.com/mesos/mesos-go/api/v1/lib/backoff" |
| 16 |
"github.com/mesos/mesos-go/api/v1/lib/httpcli" |
| 17 |
"github.com/mesos/mesos-go/api/v1/lib/httpcli/httpsched" |
| 18 |
"github.com/mesos/mesos-go/api/v1/lib/resources" |
| 19 |
"github.com/mesos/mesos-go/api/v1/lib/scheduler/calls" |
| 20 |
) |
| 21 |
|
| 22 |
func prepareExecutorInfo( |
| 23 |
execBinary, execImage string, |
| 24 |
server server, |
| 25 |
wantsResources mesos.Resources, |
| 26 |
jobRestartDelay time.Duration, |
| 27 |
metricsAPI *metricsAPI, |
| 28 |
) (*mesos.ExecutorInfo, error) { |
| 29 |
if execImage != "" { |
| 30 |
|
| 31 |
return &mesos.ExecutorInfo{ |
| 32 |
Type: mesos.ExecutorInfo_CUSTOM, |
| 33 |
ExecutorID: mesos.ExecutorID{Value: "default"}, |
| 34 |
Name: proto.String("Test Executor"), |
| 35 |
Command: &mesos.CommandInfo{ |
| 36 |
Shell: func() *bool { x := false; return &x }(), |
| 37 |
}, |
| 38 |
Container: &mesos.ContainerInfo{ |
| 39 |
Type: mesos.ContainerInfo_DOCKER.Enum(), |
| 40 |
Docker: &mesos.ContainerInfo_DockerInfo{ |
| 41 |
Image: execImage, |
| 42 |
ForcePullImage: func() *bool { x := true; return &x }(), |
| 43 |
Parameters: []mesos.Parameter{ |
| 44 |
{ |
| 45 |
Key: "entrypoint", |
| 46 |
Value: execBinary, |
| 47 |
}}}}, |
| 48 |
Resources: wantsResources, |
| 49 |
}, nil |
| 50 |
} else if execBinary != "" { |
| 51 |
log.Println("No executor image specified, will serve executor binary from built-in HTTP server") |
| 52 |
listener, iport, err := newListener(server) |
| 53 |
if err != nil { |
| 54 |
return nil, err |
| 55 |
} |
| 56 |
server.port = iport |
| 57 |
var ( |
| 58 |
mux = http.NewServeMux() |
| 59 |
executorUris = []mesos.CommandInfo_URI{} |
| 60 |
uri, executorCmd, err2 = serveExecutorArtifact(server, execBinary, mux) |
| 61 |
executorCommand = fmt.Sprintf("./%s", executorCmd) |
| 62 |
) |
| 63 |
if err2 != nil { |
| 64 |
return nil, err2 |
| 65 |
} |
| 66 |
wrapper := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 67 |
metricsAPI.artifactDownloads() |
| 68 |
mux.ServeHTTP(w, r) |
| 69 |
}) |
| 70 |
executorUris = append(executorUris, mesos.CommandInfo_URI{Value: uri, Executable: proto.Bool(true)}) |
| 71 |
|
| 72 |
go forever("artifact-server", jobRestartDelay, metricsAPI.jobStartCount, func() error { return http.Serve(listener, wrapper) }) |
| 73 |
log.Println("Serving executor artifacts...") |
| 74 |
|
| 75 |
|
| 76 |
return &mesos.ExecutorInfo{ |
| 77 |
Type: mesos.ExecutorInfo_CUSTOM, |
| 78 |
ExecutorID: mesos.ExecutorID{Value: "default"}, |
| 79 |
Name: proto.String("Test Executor"), |
| 80 |
Command: &mesos.CommandInfo{ |
| 81 |
Value: proto.String(executorCommand), |
| 82 |
URIs: executorUris, |
| 83 |
}, |
| 84 |
Resources: wantsResources, |
| 85 |
}, nil |
| 86 |
} |
| 87 |
return nil, errors.New("must specify an executor binary or image") |
| 88 |
} |
| 89 |
|
| 90 |
func buildWantsTaskResources(config Config) (r mesos.Resources) { |
| 91 |
r.Add( |
| 92 |
resources.NewCPUs(config.taskCPU).Resource, |
| 93 |
resources.NewMemory(config.taskMemory).Resource, |
| 94 |
) |
| 95 |
log.Println("wants-task-resources = " + r.String()) |
| 96 |
return |
| 97 |
} |
| 98 |
|
| 99 |
func buildWantsExecutorResources(config Config) (r mesos.Resources) { |
| 100 |
r.Add( |
| 101 |
resources.NewCPUs(config.execCPU).Resource, |
| 102 |
resources.NewMemory(config.execMemory).Resource, |
| 103 |
) |
| 104 |
log.Println("wants-executor-resources = " + r.String()) |
| 105 |
return |
| 106 |
} |
| 107 |
|
| 108 |
func buildHTTPSched(cfg Config, creds credentials) calls.Caller { |
| 109 |
var authConfigOpt httpcli.ConfigOpt |
| 110 |
|
| 111 |
if cfg.authMode == AuthModeBasic { |
| 112 |
log.Println("configuring HTTP Basic authentication") |
| 113 |
|
| 114 |
authConfigOpt = httpcli.BasicAuth(creds.username, creds.password) |
| 115 |
} |
| 116 |
cli := httpcli.New( |
| 117 |
httpcli.Endpoint(cfg.url), |
| 118 |
httpcli.Codec(cfg.codec.Codec), |
| 119 |
httpcli.Do(httpcli.With( |
| 120 |
authConfigOpt, |
| 121 |
httpcli.Timeout(cfg.timeout), |
| 122 |
)), |
| 123 |
) |
| 124 |
if cfg.compression { |
| 125 |
|
| 126 |
|
| 127 |
log.Println("compression enabled") |
| 128 |
cli.With(httpcli.RequestOptions(httpcli.Header("Accept-Encoding", "gzip"))) |
| 129 |
} |
| 130 |
return httpsched.NewCaller(cli) |
| 131 |
} |
| 132 |
|
| 133 |
func buildFrameworkInfo(cfg Config) *mesos.FrameworkInfo { |
| 134 |
failoverTimeout := cfg.failoverTimeout.Seconds() |
| 135 |
frameworkInfo := &mesos.FrameworkInfo{ |
| 136 |
User: cfg.user, |
| 137 |
Name: cfg.name, |
| 138 |
Checkpoint: &cfg.checkpoint, |
| 139 |
} |
| 140 |
if cfg.failoverTimeout > 0 { |
| 141 |
frameworkInfo.FailoverTimeout = &failoverTimeout |
| 142 |
} |
| 143 |
if cfg.role != "" { |
| 144 |
frameworkInfo.Role = &cfg.role |
| 145 |
} |
| 146 |
if cfg.principal != "" { |
| 147 |
frameworkInfo.Principal = &cfg.principal |
| 148 |
} |
| 149 |
if cfg.hostname != "" { |
| 150 |
frameworkInfo.Hostname = &cfg.hostname |
| 151 |
} |
| 152 |
if len(cfg.labels) > 0 { |
| 153 |
log.Println("using labels:", cfg.labels) |
| 154 |
frameworkInfo.Labels = &mesos.Labels{Labels: cfg.labels} |
| 155 |
} |
| 156 |
if cfg.gpuClusterCompat { |
| 157 |
frameworkInfo.Capabilities = append(frameworkInfo.Capabilities, |
| 158 |
mesos.FrameworkInfo_Capability{Type: mesos.FrameworkInfo_Capability_GPU_RESOURCES}, |
| 159 |
) |
| 160 |
} |
| 161 |
return frameworkInfo |
| 162 |
} |
| 163 |
|
| 164 |
func loadCredentials(userConfig credentials) (result credentials, err error) { |
| 165 |
result = userConfig |
| 166 |
if result.password != "" { |
| 167 |
|
| 168 |
_, err = os.Stat(result.password) |
| 169 |
if err != nil { |
| 170 |
return |
| 171 |
} |
| 172 |
var f *os.File |
| 173 |
f, err = os.Open(result.password) |
| 174 |
if err != nil { |
| 175 |
return |
| 176 |
} |
| 177 |
defer f.Close() |
| 178 |
var bytes []byte |
| 179 |
bytes, err = ioutil.ReadAll(f) |
| 180 |
if err != nil { |
| 181 |
return |
| 182 |
} |
| 183 |
result.password = string(bytes) |
| 184 |
} |
| 185 |
return |
| 186 |
} |
| 187 |
|
| 188 |
func newInternalState(cfg Config, shutdown func()) (*internalState, error) { |
| 189 |
metricsAPI := initMetrics(cfg) |
| 190 |
executorInfo, err := prepareExecutorInfo( |
| 191 |
cfg.executor, |
| 192 |
cfg.execImage, |
| 193 |
cfg.server, |
| 194 |
buildWantsExecutorResources(cfg), |
| 195 |
cfg.jobRestartDelay, |
| 196 |
metricsAPI, |
| 197 |
) |
| 198 |
if err != nil { |
| 199 |
return nil, err |
| 200 |
} |
| 201 |
creds, err := loadCredentials(cfg.credentials) |
| 202 |
if err != nil { |
| 203 |
return nil, err |
| 204 |
} |
| 205 |
state := &internalState{ |
| 206 |
config: cfg, |
| 207 |
totalTasks: cfg.tasks, |
| 208 |
reviveTokens: backoff.BurstNotifier(cfg.reviveBurst, cfg.reviveWait, cfg.reviveWait, nil), |
| 209 |
wantsTaskResources: buildWantsTaskResources(cfg), |
| 210 |
executor: executorInfo, |
| 211 |
metricsAPI: metricsAPI, |
| 212 |
cli: buildHTTPSched(cfg, creds), |
| 213 |
random: rand.New(rand.NewSource(time.Now().Unix())), |
| 214 |
shutdown: shutdown, |
| 215 |
} |
| 216 |
return state, nil |
| 217 |
} |
| 218 |
|
| 219 |
type internalState struct { |
| 220 |
tasksLaunched int |
| 221 |
tasksFinished int |
| 222 |
totalTasks int |
| 223 |
role string |
| 224 |
executor *mesos.ExecutorInfo |
| 225 |
cli calls.Caller |
| 226 |
config Config |
| 227 |
wantsTaskResources mesos.Resources |
| 228 |
reviveTokens <-chan struct{} |
| 229 |
metricsAPI *metricsAPI |
| 230 |
err error |
| 231 |
shutdown func() |
| 232 |
random *rand.Rand |
| 233 |
} |