open-nomad/command/job_dispatch.go

142 lines
3.4 KiB
Go
Raw Normal View History

2016-12-05 05:22:13 +00:00
package command
import (
"fmt"
"io/ioutil"
"os"
"strings"
flaghelper "github.com/hashicorp/nomad/helper/flag-helpers"
)
type JobDispatchCommand struct {
Meta
}
func (c *JobDispatchCommand) Help() string {
helpText := `
Usage: nomad job dispatch [options] <parameterized job> [input source]
2016-12-05 05:22:13 +00:00
Dispatch creates an instance of a parameterized job. A data payload to the
2016-12-05 23:10:23 +00:00
dispatched instance can be provided via stdin by using "-" or by specifiying a
path to a file. Metadata can be supplied by using the meta flag one or more
times.
2017-02-21 16:58:33 +00:00
Upon successful creation, the dispatched job ID will be printed and the
2016-12-05 23:10:23 +00:00
triggered evaluation will be monitored. This can be disabled by supplying the
detach flag.
2016-12-05 05:22:13 +00:00
General Options:
` + generalOptionsUsage() + `
Dispatch Options:
2017-01-25 21:48:33 +00:00
-meta <key>=<value>
Meta takes a key/value pair seperated by "=". The metadata key will be
merged into the job's metadata. The job may define a default value for the
key which is overriden when dispatching. The flag can be provided more than
once to inject multiple metadata key/value pairs. Arbitrary keys are not
allowed. The parameterized job must allow the key to be merged.
2017-01-25 21:48:33 +00:00
2016-12-05 23:10:23 +00:00
-detach
Return immediately instead of entering monitor mode. After job dispatch,
the evaluation ID will be printed to the screen, which can be used to
examine the evaluation using the eval-status command.
-verbose
Display full information.
2016-12-05 05:22:13 +00:00
`
return strings.TrimSpace(helpText)
}
func (c *JobDispatchCommand) Synopsis() string {
2017-01-26 21:07:50 +00:00
return "Dispatch an instance of a parameterized job"
2016-12-05 05:22:13 +00:00
}
func (c *JobDispatchCommand) Run(args []string) int {
var detach, verbose bool
var meta []string
flags := c.Meta.FlagSet("job dispatch", FlagSetClient)
flags.Usage = func() { c.Ui.Output(c.Help()) }
flags.BoolVar(&detach, "detach", false, "")
flags.BoolVar(&verbose, "verbose", false, "")
flags.Var((*flaghelper.StringFlag)(&meta), "meta", "")
if err := flags.Parse(args); err != nil {
return 1
}
// Truncate the id unless full length is requested
2016-12-05 23:10:23 +00:00
length := shortId
if verbose {
length = fullId
}
2016-12-05 05:22:13 +00:00
// Check that we got exactly one node
args = flags.Args()
2016-12-05 23:10:23 +00:00
if l := len(args); l < 1 || l > 2 {
2016-12-05 05:22:13 +00:00
c.Ui.Error(c.Help())
return 1
}
job := args[0]
2016-12-14 20:50:08 +00:00
var payload []byte
2016-12-05 05:22:13 +00:00
var readErr error
2016-12-05 23:10:23 +00:00
// Read the input
if len(args) == 2 {
switch args[1] {
case "-":
2016-12-14 20:50:08 +00:00
payload, readErr = ioutil.ReadAll(os.Stdin)
2016-12-05 23:10:23 +00:00
default:
2016-12-14 20:50:08 +00:00
payload, readErr = ioutil.ReadFile(args[1])
2016-12-05 23:10:23 +00:00
}
if readErr != nil {
c.Ui.Error(fmt.Sprintf("Error reading input data: %v", readErr))
return 1
}
2016-12-05 05:22:13 +00:00
}
// Build the meta
metaMap := make(map[string]string, len(meta))
for _, m := range meta {
split := strings.SplitN(m, "=", 2)
if len(split) != 2 {
c.Ui.Error(fmt.Sprintf("Error parsing meta value: %v", m))
return 1
}
metaMap[split[0]] = split[1]
}
// Get the HTTP client
client, err := c.Meta.Client()
if err != nil {
c.Ui.Error(fmt.Sprintf("Error initializing client: %s", err))
return 1
}
// Dispatch the job
resp, _, err := client.Jobs().Dispatch(job, metaMap, payload, nil)
2016-12-05 05:22:13 +00:00
if err != nil {
c.Ui.Error(fmt.Sprintf("Failed to dispatch job: %s", err))
return 1
}
basic := []string{
fmt.Sprintf("Dispatched Job ID|%s", resp.DispatchedJobID),
2016-12-05 23:10:23 +00:00
fmt.Sprintf("Evaluation ID|%s", limit(resp.EvalID, length)),
2016-12-05 05:22:13 +00:00
}
c.Ui.Output(formatKV(basic))
2016-12-05 23:10:23 +00:00
if detach {
return 0
}
c.Ui.Output("")
mon := newMonitor(c.Ui, client, length)
return mon.monitor(resp.EvalID, false)
2016-12-05 05:22:13 +00:00
}