github.com/diptanu/nomad@v0.5.7-0.20170516172507-d72e86cbe3d9/command/init.go (about) 1 package command 2 3 import ( 4 "fmt" 5 "io/ioutil" 6 "os" 7 "strings" 8 ) 9 10 const ( 11 // DefaultInitName is the default name we use when 12 // initializing the example file 13 DefaultInitName = "example.nomad" 14 ) 15 16 // InitCommand generates a new job template that you can customize to your 17 // liking, like vagrant init 18 type InitCommand struct { 19 Meta 20 } 21 22 func (c *InitCommand) Help() string { 23 helpText := ` 24 Usage: nomad init 25 26 Creates an example job file that can be used as a starting 27 point to customize further. 28 ` 29 return strings.TrimSpace(helpText) 30 } 31 32 func (c *InitCommand) Synopsis() string { 33 return "Create an example job file" 34 } 35 36 func (c *InitCommand) Run(args []string) int { 37 // Check for misuse 38 if len(args) != 0 { 39 c.Ui.Error(c.Help()) 40 return 1 41 } 42 43 // Check if the file already exists 44 _, err := os.Stat(DefaultInitName) 45 if err != nil && !os.IsNotExist(err) { 46 c.Ui.Error(fmt.Sprintf("Failed to stat '%s': %v", DefaultInitName, err)) 47 return 1 48 } 49 if !os.IsNotExist(err) { 50 c.Ui.Error(fmt.Sprintf("Job '%s' already exists", DefaultInitName)) 51 return 1 52 } 53 54 // Write out the example 55 err = ioutil.WriteFile(DefaultInitName, []byte(defaultJob), 0660) 56 if err != nil { 57 c.Ui.Error(fmt.Sprintf("Failed to write '%s': %v", DefaultInitName, err)) 58 return 1 59 } 60 61 // Success 62 c.Ui.Output(fmt.Sprintf("Example job file written to %s", DefaultInitName)) 63 return 0 64 } 65 66 var defaultJob = strings.TrimSpace(` 67 # There can only be a single job definition per file. This job is named 68 # "example" so it will create a job with the ID and Name "example". 69 70 # The "job" stanza is the top-most configuration option in the job 71 # specification. A job is a declarative specification of tasks that Nomad 72 # should run. Jobs have a globally unique name, one or many task groups, which 73 # are themselves collections of one or many tasks. 74 # 75 # For more information and examples on the "job" stanza, please see 76 # the online documentation at: 77 # 78 # https://www.nomadproject.io/docs/job-specification/job.html 79 # 80 job "example" { 81 # The "region" parameter specifies the region in which to execute the job. If 82 # omitted, this inherits the default region name of "global". 83 # region = "global" 84 85 # The "datacenters" parameter specifies the list of datacenters which should 86 # be considered when placing this task. This must be provided. 87 datacenters = ["dc1"] 88 89 # The "type" parameter controls the type of job, which impacts the scheduler's 90 # decision on placement. This configuration is optional and defaults to 91 # "service". For a full list of job types and their differences, please see 92 # the online documentation. 93 # 94 # For more information, please see the online documentation at: 95 # 96 # https://www.nomadproject.io/docs/jobspec/schedulers.html 97 # 98 type = "service" 99 100 # The "constraint" stanza defines additional constraints for placing this job, 101 # in addition to any resource or driver constraints. This stanza may be placed 102 # at the "job", "group", or "task" level, and supports variable interpolation. 103 # 104 # For more information and examples on the "constraint" stanza, please see 105 # the online documentation at: 106 # 107 # https://www.nomadproject.io/docs/job-specification/constraint.html 108 # 109 # constraint { 110 # attribute = "${attr.kernel.name}" 111 # value = "linux" 112 # } 113 114 # The "update" stanza specifies the job update strategy. The update strategy 115 # is used to control things like rolling upgrades. If omitted, rolling 116 # updates are disabled. 117 # 118 # For more information and examples on the "update" stanza, please see 119 # the online documentation at: 120 # 121 # https://www.nomadproject.io/docs/job-specification/update.html 122 # 123 update { 124 # The "stagger" parameter specifies to do rolling updates of this job every 125 # 10 seconds. 126 stagger = "10s" 127 128 # The "max_parallel" parameter specifies the maximum number of updates to 129 # perform in parallel. In this case, this specifies to update a single task 130 # at a time. 131 max_parallel = 1 132 } 133 134 # The "group" stanza defines a series of tasks that should be co-located on 135 # the same Nomad client. Any task within a group will be placed on the same 136 # client. 137 # 138 # For more information and examples on the "group" stanza, please see 139 # the online documentation at: 140 # 141 # https://www.nomadproject.io/docs/job-specification/group.html 142 # 143 group "cache" { 144 # The "count" parameter specifies the number of the task groups that should 145 # be running under this group. This value must be non-negative and defaults 146 # to 1. 147 count = 1 148 149 # The "restart" stanza configures a group's behavior on task failure. If 150 # left unspecified, a default restart policy is used based on the job type. 151 # 152 # For more information and examples on the "restart" stanza, please see 153 # the online documentation at: 154 # 155 # https://www.nomadproject.io/docs/job-specification/restart.html 156 # 157 restart { 158 # The number of attempts to run the job within the specified interval. 159 attempts = 10 160 interval = "5m" 161 162 # The "delay" parameter specifies the duration to wait before restarting 163 # a task after it has failed. 164 delay = "25s" 165 166 # The "mode" parameter controls what happens when a task has restarted 167 # "attempts" times within the interval. "delay" mode delays the next 168 # restart until the next interval. "fail" mode does not restart the task 169 # if "attempts" has been hit within the interval. 170 mode = "delay" 171 } 172 173 # The "ephemeral_disk" stanza instructs Nomad to utilize an ephemeral disk 174 # instead of a hard disk requirement. Clients using this stanza should 175 # not specify disk requirements in the resources stanza of the task. All 176 # tasks in this group will share the same ephemeral disk. 177 # 178 # For more information and examples on the "ephemeral_disk" stanza, please 179 # see the online documentation at: 180 # 181 # https://www.nomadproject.io/docs/job-specification/ephemeral_disk.html 182 # 183 ephemeral_disk { 184 # When sticky is true and the task group is updated, the scheduler 185 # will prefer to place the updated allocation on the same node and 186 # will migrate the data. This is useful for tasks that store data 187 # that should persist across allocation updates. 188 # sticky = true 189 # 190 # Setting migrate to true results in the allocation directory of a 191 # sticky allocation directory to be migrated. 192 # migrate = true 193 194 # The "size" parameter specifies the size in MB of shared ephemeral disk 195 # between tasks in the group. 196 size = 300 197 } 198 199 # The "task" stanza creates an individual unit of work, such as a Docker 200 # container, web application, or batch processing. 201 # 202 # For more information and examples on the "task" stanza, please see 203 # the online documentation at: 204 # 205 # https://www.nomadproject.io/docs/job-specification/task.html 206 # 207 task "redis" { 208 # The "driver" parameter specifies the task driver that should be used to 209 # run the task. 210 driver = "docker" 211 212 # The "config" stanza specifies the driver configuration, which is passed 213 # directly to the driver to start the task. The details of configurations 214 # are specific to each driver, so please see specific driver 215 # documentation for more information. 216 config { 217 image = "redis:3.2" 218 port_map { 219 db = 6379 220 } 221 } 222 223 # The "artifact" stanza instructs Nomad to download an artifact from a 224 # remote source prior to starting the task. This provides a convenient 225 # mechanism for downloading configuration files or data needed to run the 226 # task. It is possible to specify the "artifact" stanza multiple times to 227 # download multiple artifacts. 228 # 229 # For more information and examples on the "artifact" stanza, please see 230 # the online documentation at: 231 # 232 # https://www.nomadproject.io/docs/job-specification/artifact.html 233 # 234 # artifact { 235 # source = "http://foo.com/artifact.tar.gz" 236 # options { 237 # checksum = "md5:c4aa853ad2215426eb7d70a21922e794" 238 # } 239 # } 240 241 # The "logs" stana instructs the Nomad client on how many log files and 242 # the maximum size of those logs files to retain. Logging is enabled by 243 # default, but the "logs" stanza allows for finer-grained control over 244 # the log rotation and storage configuration. 245 # 246 # For more information and examples on the "logs" stanza, please see 247 # the online documentation at: 248 # 249 # https://www.nomadproject.io/docs/job-specification/logs.html 250 # 251 # logs { 252 # max_files = 10 253 # max_file_size = 15 254 # } 255 256 # The "resources" stanza describes the requirements a task needs to 257 # execute. Resource requirements include memory, network, cpu, and more. 258 # This ensures the task will execute on a machine that contains enough 259 # resource capacity. 260 # 261 # For more information and examples on the "resources" stanza, please see 262 # the online documentation at: 263 # 264 # https://www.nomadproject.io/docs/job-specification/resources.html 265 # 266 resources { 267 cpu = 500 # 500 MHz 268 memory = 256 # 256MB 269 network { 270 mbits = 10 271 port "db" {} 272 } 273 } 274 275 # The "service" stanza instructs Nomad to register this task as a service 276 # in the service discovery engine, which is currently Consul. This will 277 # make the service addressable after Nomad has placed it on a host and 278 # port. 279 # 280 # For more information and examples on the "service" stanza, please see 281 # the online documentation at: 282 # 283 # https://www.nomadproject.io/docs/job-specification/service.html 284 # 285 service { 286 name = "global-redis-check" 287 tags = ["global", "cache"] 288 port = "db" 289 check { 290 name = "alive" 291 type = "tcp" 292 interval = "10s" 293 timeout = "2s" 294 } 295 } 296 297 # The "template" stanza instructs Nomad to manage a template, such as 298 # a configuration file or script. This template can optionally pull data 299 # from Consul or Vault to populate runtime configuration data. 300 # 301 # For more information and examples on the "template" stanza, please see 302 # the online documentation at: 303 # 304 # https://www.nomadproject.io/docs/job-specification/template.html 305 # 306 # template { 307 # data = "---\nkey: {{ key \"service/my-key\" }}" 308 # destination = "local/file.yml" 309 # change_mode = "signal" 310 # change_signal = "SIGHUP" 311 # } 312 313 # The "vault" stanza instructs the Nomad client to acquire a token from 314 # a HashiCorp Vault server. The Nomad servers must be configured and 315 # authorized to communicate with Vault. By default, Nomad will inject 316 # The token into the job via an environment variable and make the token 317 # available to the "template" stanza. The Nomad client handles the renewal 318 # and revocation of the Vault token. 319 # 320 # For more information and examples on the "vault" stanza, please see 321 # the online documentation at: 322 # 323 # https://www.nomadproject.io/docs/job-specification/vault.html 324 # 325 # vault { 326 # policies = ["cdn", "frontend"] 327 # change_mode = "signal" 328 # change_signal = "SIGHUP" 329 # } 330 331 # Controls the timeout between signalling a task it will be killed 332 # and killing the task. If not set a default is used. 333 # kill_timeout = "20s" 334 } 335 } 336 } 337 `)