-
Notifications
You must be signed in to change notification settings - Fork 1
/
config.go
129 lines (112 loc) · 4.92 KB
/
config.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
package main
import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"os"
"github.com/gogo/protobuf/proto"
log "github.com/golang/glog"
"github.com/mesos/mesos-go/auth/sasl"
"github.com/mesos/mesos-go/auth/sasl/mech"
mesos "github.com/mesos/mesos-go/mesosproto"
)
// See example-config.json for an example of this config.
type Docker struct {
Image string `json:"image"`
Cmd string `json:"cmd"`
ForcePullImage bool `json:"force_pull"`
Env map[string]string `json:"env"`
EnvString string `json:"env_string,omitempty"`
Cpus float64 `json:"cpus"`
Mem float64 `json:"mem"`
}
type Runonce struct {
Address string `json:"address"`
BindingPort uint `json:"port"`
AuthProvider string `json:"authentication_provider"`
Master string `json:"master"`
TaskCount int `json:"task_count"`
MesosRunasUser string `json:"user"`
MesosRole string `json:"role"`
AuthPrincipal string `json:"principal"`
AuthSecretFile string `json:"secret_file"`
}
type Task struct {
Id string `json:"id"`
Name string `json:"name"`
Docker `json:"docker"`
}
type Config struct {
Runonce `json:"runonce"`
Task `json:"task"`
}
// Required interface for flag.Var
func (config *Config) Set(runonceConfig string) error {
_, err := os.Stat(runonceConfig)
if err != nil {
log.Exit("missing config file: ", err.Error())
}
configData, err := ioutil.ReadFile(runonceConfig)
if err != nil {
log.Exit("failed to read config file: ", err.Error())
}
return json.Unmarshal([]byte(configData), config)
}
// Required interface for flag.Var. This specifies the "default" that is returned when doing -h
func (config *Config) String() string {
return ""
}
// Used to allow passing in of string of JSON or defining in your config.json. This behaves differently
// than the other options in that -env-vars will always override what is defined in config.json
func (c *Config) EnvVars() *mesos.Environment {
var envVars map[string]string
if config.Task.Docker.EnvString != "" {
var dEnvVars Docker
err := json.Unmarshal([]byte(config.Task.Docker.EnvString), &dEnvVars)
if err != nil {
log.Exitf("JSON error: %#v with unparsable env vars: %+v", err, config.Task.EnvString)
}
envVars = dEnvVars.Env
} else if len(config.Task.Docker.Env) != 0 {
envVars = config.Task.Env
}
var variables []*mesos.Environment_Variable
for key, value := range envVars {
variables = append(variables, &mesos.Environment_Variable{Name: proto.String(key), Value: proto.String(value)})
}
return &mesos.Environment{Variables: variables}
}
// Responsible for loading up our config.json && || all the command lines switches. The way this is setup
// will be order specific on the command line. Only exception to this rule is env-vars (see EnvVars()).
//
// E.g.
//
// # address will override config.json if it is defined in config.json.
// mesos-runonce -config=config.json -address=address
//
// # config.json will override address if it is defined in config.json.
// mesos-runonce -address=address -config=config.json
func loadConfig() *Config {
cfg := new(Config)
flag.BoolVar(&cfg.Task.Docker.ForcePullImage, "force-pull", false, "Boolean for forcing pull of image before run.")
flag.Float64Var(&cfg.Task.Docker.Cpus, "cpus", 1.0, "How many CPUs to use.")
flag.Float64Var(&cfg.Task.Docker.Mem, "mem", 10, "How much memory to use.")
flag.IntVar(&cfg.TaskCount, "task-count", 1, "Total task count to run.")
flag.StringVar(&cfg.Runonce.Address, "address", "127.0.0.1", "Address for mesos to callback on.")
flag.StringVar(&cfg.Runonce.AuthPrincipal, "principal", "", "Mesos authentication principal.")
flag.StringVar(&cfg.Runonce.AuthProvider, "authentication-provider", sasl.ProviderName, fmt.Sprintf("Authentication provider to use, default is SASL that supports mechanisms: %+v", mech.ListSupported()))
flag.StringVar(&cfg.Runonce.AuthSecretFile, "secret-file", "", "Mesos authentication secret file.")
flag.StringVar(&cfg.Runonce.Master, "master", "127.0.0.1:5050", "Master address <ip:port>")
flag.StringVar(&cfg.Runonce.MesosRunasUser, "user", "root", "Mesos user to run tasks as.")
flag.StringVar(&cfg.Runonce.MesosRole, "role", "", "Mesos role to run tasks with.")
flag.StringVar(&cfg.Task.Docker.Cmd, "docker-cmd", "", "Docker command to run.")
flag.StringVar(&cfg.Task.Docker.EnvString, "env-vars", "", "Docker env vars for the container. E.g. -env-vars='{\"env\":{\"FOO\":\"bar\"}}'")
flag.StringVar(&cfg.Task.Docker.Image, "docker-image", "", "Docker image to run.")
flag.StringVar(&cfg.Task.Id, "task-id", "", "Mesos task id to identify the task.")
flag.StringVar(&cfg.Task.Name, "task-name", "", "Mesos task name to label the task.")
flag.UintVar(&cfg.Runonce.BindingPort, "port", 0, "Port for address to use for mesos to callback.")
flag.Var(cfg, "config", "Runonce config of JSON. See spec in config.go for details.")
flag.Parse()
return cfg
}