github.com/huiliang/nomad@v0.2.1-0.20151124023127-7a8b664699ff/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.
    68  # Create a job with ID and Name 'example'
    69  job "example" {
    70  	# Run the job in the global region, which is the default.
    71  	# region = "global"
    72  
    73  	# Specify the datacenters within the region this job can run in.
    74  	datacenters = ["dc1"]
    75  
    76  	# Service type jobs optimize for long-lived services. This is
    77  	# the default but we can change to batch for short-lived tasks.
    78  	# type = "service"
    79  
    80  	# Priority controls our access to resources and scheduling priority.
    81  	# This can be 1 to 100, inclusively, and defaults to 50.
    82  	# priority = 50
    83  
    84  	# Restrict our job to only linux. We can specify multiple
    85  	# constraints as needed.
    86  	constraint {
    87  		attribute = "$attr.kernel.name"
    88  		value = "linux"
    89  	}
    90  
    91  	# Configure the job to do rolling updates
    92  	update {
    93  		# Stagger updates every 10 seconds
    94  		stagger = "10s"
    95  
    96  		# Update a single task at a time
    97  		max_parallel = 1
    98  	}
    99  
   100  	# Create a 'cache' group. Each task in the group will be
   101  	# scheduled onto the same machine.
   102  	group "cache" {
   103  		# Control the number of instances of this groups.
   104  		# Defaults to 1
   105  		# count = 1
   106  
   107  		# Restart Policy - This block defines the restart policy for TaskGroups,
   108  		# the attempts value defines the number of restarts Nomad will do if Tasks
   109  		# in this TaskGroup fails in a rolling window of interval duration
   110  		# The delay value makes Nomad wait for that duration to restart after a Task
   111  		# fails or crashes.
   112  		restart {
   113  			interval = "5m"
   114  			attempts = 10
   115  			delay = "25s"
   116  		}
   117  
   118  		# Define a task to run
   119  		task "redis" {
   120  			# Use Docker to run the task.
   121  			driver = "docker"
   122  
   123  			# Configure Docker driver with the image
   124  			config {
   125  				image = "redis:latest"
   126  				port_map {
   127  					db = 6379
   128  				}
   129  			}
   130  
   131  			service {
   132  				# name = redis
   133  				tags = ["global", "cache"]
   134  				port = "db"
   135  				check {
   136  					name = "alive"
   137  					type = "tcp"
   138  					interval = "10s"
   139  					timeout = "2s"
   140  				}
   141  			}
   142  
   143  			# We must specify the resources required for
   144  			# this task to ensure it runs on a machine with
   145  			# enough capacity.
   146  			resources {
   147  				cpu = 500 # 500 Mhz
   148  				memory = 256 # 256MB
   149  				network {
   150  					mbits = 10
   151  					port "db" {
   152  					}
   153  				}
   154  			}
   155  		}
   156  	}
   157  }
   158  `)