| 1 | package app |
| 2 | |
| 3 | import ( |
| 4 | "flag" |
| 5 | "time" |
| 6 | |
| 7 | "github.com/mesos/mesos-go/api/v1/cmd" |
| 8 | "github.com/mesos/mesos-go/api/v1/lib/encoding/codecs" |
| 9 | ) |
| 10 | |
| 11 | type Config struct { |
| 12 | user string |
| 13 | name string |
| 14 | role string |
| 15 | url string |
| 16 | codec codec |
| 17 | timeout time.Duration |
| 18 | failoverTimeout time.Duration |
| 19 | checkpoint bool |
| 20 | principal string |
| 21 | hostname string |
| 22 | labels Labels |
| 23 | server server |
| 24 | executor string |
| 25 | tasks int |
| 26 | verbose bool |
| 27 | taskCPU float64 |
| 28 | taskMemory float64 |
| 29 | execCPU float64 |
| 30 | execMemory float64 |
| 31 | reviveBurst int |
| 32 | reviveWait time.Duration |
| 33 | metrics metrics |
| 34 | resourceTypeMetrics bool |
| 35 | maxRefuseSeconds time.Duration |
| 36 | jobRestartDelay time.Duration |
| 37 | summaryMetrics bool |
| 38 | execImage string |
| 39 | compression bool |
| 40 | credentials credentials |
| 41 | authMode string |
| 42 | gpuClusterCompat bool |
| 43 | } |
| 44 | |
| 45 | func (cfg *Config) AddFlags(fs *flag.FlagSet) { |
| 46 | fs.StringVar(&cfg.user, "user", cfg.user, "Framework user to register with the Mesos master") |
| 47 | fs.StringVar(&cfg.name, "name", cfg.name, "Framework name to register with the Mesos master") |
| 48 | fs.StringVar(&cfg.role, "role", cfg.role, "Framework role to register with the Mesos master") |
| 49 | fs.Var(&cfg.codec, "codec", "Codec to encode/decode scheduler API communications [protobuf, json]") |
| 50 | fs.StringVar(&cfg.url, "url", cfg.url, "Mesos scheduler API URL") |
| 51 | fs.DurationVar(&cfg.timeout, "timeout", cfg.timeout, "Mesos scheduler API connection timeout") |
| 52 | fs.DurationVar(&cfg.failoverTimeout, "failoverTimeout", cfg.failoverTimeout, "Framework failover timeout (recover from scheduler failure)") |
| 53 | fs.BoolVar(&cfg.checkpoint, "checkpoint", cfg.checkpoint, "Enable/disable agent checkpointing for framework tasks (recover from agent failure)") |
| 54 | fs.StringVar(&cfg.principal, "principal", cfg.principal, "Framework principal with which to authenticate") |
| 55 | fs.StringVar(&cfg.hostname, "hostname", cfg.hostname, "Framework hostname that is advertised to the master") |
| 56 | fs.Var(&cfg.labels, "label", "Framework label, may be specified multiple times") |
| 57 | fs.StringVar(&cfg.server.address, "server.address", cfg.server.address, "IP of artifact server") |
| 58 | fs.IntVar(&cfg.server.port, "server.port", cfg.server.port, "Port of artifact server") |
| 59 | fs.StringVar(&cfg.executor, "executor", cfg.executor, "Full path to executor binary") |
| 60 | fs.IntVar(&cfg.tasks, "tasks", cfg.tasks, "Number of tasks to spawn") |
| 61 | fs.BoolVar(&cfg.verbose, "verbose", cfg.verbose, "Verbose logging") |
| 62 | fs.Float64Var(&cfg.taskCPU, "cpu", cfg.taskCPU, "CPU resources to consume per-task") |
| 63 | fs.Float64Var(&cfg.taskMemory, "memory", cfg.taskMemory, "Memory resources (MB) to consume per-task") |
| 64 | fs.Float64Var(&cfg.execCPU, "exec.cpu", cfg.execCPU, "CPU resources to consume per-executor") |
| 65 | fs.Float64Var(&cfg.execMemory, "exec.memory", cfg.execMemory, "Memory resources (MB) to consume per-executor") |
| 66 | fs.IntVar(&cfg.reviveBurst, "revive.burst", cfg.reviveBurst, "Number of revive messages that may be sent in a burst within revive-wait period") |
| 67 | fs.DurationVar(&cfg.reviveWait, "revive.wait", cfg.reviveWait, "Wait this long to fully recharge revive-burst quota") |
| 68 | fs.IntVar(&cfg.metrics.port, "metrics.port", cfg.metrics.port, "Port of metrics server (listens on server.address)") |
| 69 | fs.StringVar(&cfg.metrics.path, "metrics.path", cfg.metrics.path, "URI path to metrics endpoint") |
| 70 | fs.BoolVar(&cfg.resourceTypeMetrics, "resourceTypeMetrics", cfg.resourceTypeMetrics, "Collect scalar resource metrics per-type") |
| 71 | fs.DurationVar(&cfg.maxRefuseSeconds, "maxRefuseSeconds", cfg.maxRefuseSeconds, "Max length of time to refuse future offers") |
| 72 | fs.DurationVar(&cfg.jobRestartDelay, "jobRestartDelay", cfg.jobRestartDelay, "Duration between job (internal service) restarts between failures") |
| 73 | fs.BoolVar(&cfg.summaryMetrics, "summaryMetrics", cfg.summaryMetrics, "Collect summary metrics for tasks launched per-offer-cycle, offer processing time, etc.") |
| 74 | fs.StringVar(&cfg.execImage, "exec.image", cfg.execImage, "Name of the docker image to run the executor") |
| 75 | fs.BoolVar(&cfg.compression, "compression", cfg.compression, "When true attempt to use compression for HTTP streams.") |
| 76 | fs.StringVar(&cfg.credentials.username, "credentials.username", cfg.credentials.username, "Username for Mesos authentication") |
| 77 | fs.StringVar(&cfg.credentials.password, "credentials.passwordFile", cfg.credentials.password, "Path to file that contains the password for Mesos authentication") |
| 78 | fs.StringVar(&cfg.authMode, "authmode", cfg.authMode, "Method to use for Mesos authentication; specify '"+AuthModeBasic+"' for simple HTTP authentication") |
| 79 | fs.BoolVar(&cfg.gpuClusterCompat, "gpuClusterCompat", cfg.gpuClusterCompat, "When true the framework will receive offers from agents w/ GPU resources.") |
| 80 | } |
| 81 | |
| 82 | const AuthModeBasic = "basic" |
| 83 | |
| 84 | func NewConfig() Config { |
| 85 | return Config{ |
| 86 | user: env("FRAMEWORK_USER", "root"), |
| 87 | name: env("FRAMEWORK_NAME", "example"), |
| 88 | url: env("MESOS_MASTER_HTTP", "http://:5050/api/v1/scheduler"), |
| 89 | codec: codec{Codec: codecs.ByMediaType[codecs.MediaTypeProtobuf]}, |
| 90 | timeout: envDuration("MESOS_CONNECT_TIMEOUT", "20s"), |
| 91 | failoverTimeout: envDuration("SCHEDULER_FAILOVER_TIMEOUT", "1000h"), |
| 92 | checkpoint: true, |
| 93 | server: server{address: env("LIBPROCESS_IP", "127.0.0.1")}, |
| 94 | tasks: envInt("NUM_TASKS", "5"), |
| 95 | taskCPU: envFloat("TASK_CPU", "1"), |
| 96 | taskMemory: envFloat("TASK_MEMORY", "64"), |
| 97 | execCPU: envFloat("EXEC_CPU", "0.01"), |
| 98 | execMemory: envFloat("EXEC_MEMORY", "64"), |
| 99 | reviveBurst: envInt("REVIVE_BURST", "3"), |
| 100 | reviveWait: envDuration("REVIVE_WAIT", "1s"), |
| 101 | maxRefuseSeconds: envDuration("MAX_REFUSE_SECONDS", "5s"), |
| 102 | jobRestartDelay: envDuration("JOB_RESTART_DELAY", "5s"), |
| 103 | execImage: env("EXEC_IMAGE", cmd.DockerImageTag), |
| 104 | executor: env("EXEC_BINARY", "/opt/example-executor"), |
| 105 | metrics: metrics{ |
| 106 | port: envInt("PORT0", "64009"), |
| 107 | path: env("METRICS_API_PATH", "/metrics"), |
| 108 | }, |
| 109 | credentials: credentials{ |
| 110 | username: env("AUTH_USER", ""), |
| 111 | password: env("AUTH_PASSWORD_FILE", ""), |
| 112 | }, |
| 113 | authMode: env("AUTH_MODE", ""), |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | type server struct { |
| 118 | address string |
| 119 | port int |
| 120 | } |
| 121 | |
| 122 | type metrics struct { |
| 123 | port int |
| 124 | path string |
| 125 | } |
| 126 | |
| 127 | type credentials struct { |
| 128 | username string |
| 129 | password string |
| 130 | } |