github.com/diptanu/nomad@v0.5.7-0.20170516172507-d72e86cbe3d9/command/job_dispatch.go (about)

     1  package command
     2  
     3  import (
     4  	"fmt"
     5  	"io/ioutil"
     6  	"os"
     7  	"strings"
     8  
     9  	flaghelper "github.com/hashicorp/nomad/helper/flag-helpers"
    10  )
    11  
    12  type JobDispatchCommand struct {
    13  	Meta
    14  }
    15  
    16  func (c *JobDispatchCommand) Help() string {
    17  	helpText := `
    18  Usage: nomad job dispatch [options] <parameterized job> [input source]
    19  
    20  Dispatch creates an instance of a parameterized job. A data payload to the
    21  dispatched instance can be provided via stdin by using "-" or by specifiying a
    22  path to a file. Metadata can be supplied by using the meta flag one or more
    23  times. 
    24  
    25  Upon successful creation, the dispatched job ID will be printed and the
    26  triggered evaluation will be monitored. This can be disabled by supplying the
    27  detach flag.
    28  
    29  General Options:
    30  
    31    ` + generalOptionsUsage() + `
    32  
    33  Dispatch Options:
    34  
    35    -meta <key>=<value>
    36      Meta takes a key/value pair seperated by "=". The metadata key will be
    37      merged into the job's metadata. The job may define a default value for the
    38      key which is overriden when dispatching. The flag can be provided more than
    39      once to inject multiple metadata key/value pairs. Arbitrary keys are not
    40      allowed. The parameterized job must allow the key to be merged.
    41      
    42    -detach
    43      Return immediately instead of entering monitor mode. After job dispatch,
    44      the evaluation ID will be printed to the screen, which can be used to
    45      examine the evaluation using the eval-status command.
    46  
    47    -verbose
    48      Display full information.
    49  `
    50  	return strings.TrimSpace(helpText)
    51  }
    52  
    53  func (c *JobDispatchCommand) Synopsis() string {
    54  	return "Dispatch an instance of a parameterized job"
    55  }
    56  
    57  func (c *JobDispatchCommand) Run(args []string) int {
    58  	var detach, verbose bool
    59  	var meta []string
    60  
    61  	flags := c.Meta.FlagSet("job dispatch", FlagSetClient)
    62  	flags.Usage = func() { c.Ui.Output(c.Help()) }
    63  	flags.BoolVar(&detach, "detach", false, "")
    64  	flags.BoolVar(&verbose, "verbose", false, "")
    65  	flags.Var((*flaghelper.StringFlag)(&meta), "meta", "")
    66  
    67  	if err := flags.Parse(args); err != nil {
    68  		return 1
    69  	}
    70  
    71  	// Truncate the id unless full length is requested
    72  	length := shortId
    73  	if verbose {
    74  		length = fullId
    75  	}
    76  
    77  	// Check that we got exactly one node
    78  	args = flags.Args()
    79  	if l := len(args); l < 1 || l > 2 {
    80  		c.Ui.Error(c.Help())
    81  		return 1
    82  	}
    83  
    84  	job := args[0]
    85  	var payload []byte
    86  	var readErr error
    87  
    88  	// Read the input
    89  	if len(args) == 2 {
    90  		switch args[1] {
    91  		case "-":
    92  			payload, readErr = ioutil.ReadAll(os.Stdin)
    93  		default:
    94  			payload, readErr = ioutil.ReadFile(args[1])
    95  		}
    96  		if readErr != nil {
    97  			c.Ui.Error(fmt.Sprintf("Error reading input data: %v", readErr))
    98  			return 1
    99  		}
   100  	}
   101  
   102  	// Build the meta
   103  	metaMap := make(map[string]string, len(meta))
   104  	for _, m := range meta {
   105  		split := strings.SplitN(m, "=", 2)
   106  		if len(split) != 2 {
   107  			c.Ui.Error(fmt.Sprintf("Error parsing meta value: %v", m))
   108  			return 1
   109  		}
   110  
   111  		metaMap[split[0]] = split[1]
   112  	}
   113  
   114  	// Get the HTTP client
   115  	client, err := c.Meta.Client()
   116  	if err != nil {
   117  		c.Ui.Error(fmt.Sprintf("Error initializing client: %s", err))
   118  		return 1
   119  	}
   120  
   121  	// Dispatch the job
   122  	resp, _, err := client.Jobs().Dispatch(job, metaMap, payload, nil)
   123  	if err != nil {
   124  		c.Ui.Error(fmt.Sprintf("Failed to dispatch job: %s", err))
   125  		return 1
   126  	}
   127  
   128  	// See if an evaluation was created. If the job is periodic there will be no
   129  	// eval.
   130  	evalCreated := resp.EvalID != ""
   131  
   132  	basic := []string{
   133  		fmt.Sprintf("Dispatched Job ID|%s", resp.DispatchedJobID),
   134  	}
   135  	if evalCreated {
   136  		basic = append(basic, fmt.Sprintf("Evaluation ID|%s", limit(resp.EvalID, length)))
   137  	}
   138  	c.Ui.Output(formatKV(basic))
   139  
   140  	// Nothing to do
   141  	if detach || !evalCreated {
   142  		return 0
   143  	}
   144  
   145  	c.Ui.Output("")
   146  	mon := newMonitor(c.Ui, client, length)
   147  	return mon.monitor(resp.EvalID, false)
   148  }