2016-12-05 05:22:13 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
|
2021-10-22 16:39:05 +00:00
|
|
|
"github.com/hashicorp/nomad/api"
|
2020-12-14 16:07:27 +00:00
|
|
|
flaghelper "github.com/hashicorp/nomad/helper/flags"
|
2017-08-22 20:39:06 +00:00
|
|
|
"github.com/posener/complete"
|
2016-12-05 05:22:13 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type JobDispatchCommand struct {
|
|
|
|
Meta
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *JobDispatchCommand) Help() string {
|
|
|
|
helpText := `
|
2017-01-20 18:33:52 +00:00
|
|
|
Usage: nomad job dispatch [options] <parameterized job> [input source]
|
2016-12-05 05:22:13 +00:00
|
|
|
|
2017-10-13 21:36:02 +00:00
|
|
|
Dispatch creates an instance of a parameterized job. A data payload to the
|
|
|
|
dispatched instance can be provided via stdin by using "-" or by specifying a
|
|
|
|
path to a file. Metadata can be supplied by using the meta flag one or more
|
2018-04-18 16:02:11 +00:00
|
|
|
times.
|
2017-10-13 21:36:02 +00:00
|
|
|
|
2021-10-22 16:39:05 +00:00
|
|
|
An optional idempotency token can be used to prevent more than one instance
|
|
|
|
of the job to be dispatched. If an instance with the same token already
|
|
|
|
exists, the command returns without any action.
|
|
|
|
|
2017-10-13 21:36:02 +00:00
|
|
|
Upon successful creation, the dispatched job ID will be printed and the
|
|
|
|
triggered evaluation will be monitored. This can be disabled by supplying the
|
|
|
|
detach flag.
|
2016-12-05 05:22:13 +00:00
|
|
|
|
2020-11-19 21:38:08 +00:00
|
|
|
When ACLs are enabled, this command requires a token with the 'dispatch-job'
|
|
|
|
capability for the job's namespace.
|
|
|
|
|
2016-12-05 05:22:13 +00:00
|
|
|
General Options:
|
|
|
|
|
2020-11-19 16:15:23 +00:00
|
|
|
` + generalOptionsUsage(usageOptsDefault) + `
|
2016-12-05 05:22:13 +00:00
|
|
|
|
|
|
|
Dispatch Options:
|
|
|
|
|
2017-01-25 21:48:33 +00:00
|
|
|
-meta <key>=<value>
|
2017-08-07 21:13:05 +00:00
|
|
|
Meta takes a key/value pair separated by "=". The metadata key will be
|
2017-02-10 01:58:20 +00:00
|
|
|
merged into the job's metadata. The job may define a default value for the
|
2017-08-07 21:13:05 +00:00
|
|
|
key which is overridden when dispatching. The flag can be provided more than
|
2017-02-10 01:58:20 +00:00
|
|
|
once to inject multiple metadata key/value pairs. Arbitrary keys are not
|
|
|
|
allowed. The parameterized job must allow the key to be merged.
|
2018-04-18 16:02:11 +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.
|
|
|
|
|
2021-10-22 16:39:05 +00:00
|
|
|
-idempotency-token
|
|
|
|
Optional identifier used to prevent more than one instance of the job from
|
|
|
|
being dispatched.
|
|
|
|
|
2022-10-06 20:21:40 +00:00
|
|
|
-id-prefix-template
|
|
|
|
Optional prefix template for dispatched job IDs.
|
|
|
|
|
2016-12-05 23:10:23 +00:00
|
|
|
-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
|
|
|
}
|
|
|
|
|
2017-08-22 20:39:06 +00:00
|
|
|
func (c *JobDispatchCommand) AutocompleteFlags() complete.Flags {
|
2017-08-23 21:56:21 +00:00
|
|
|
return mergeAutocompleteFlags(c.Meta.AutocompleteFlags(FlagSetClient),
|
|
|
|
complete.Flags{
|
2021-10-22 16:39:05 +00:00
|
|
|
"-meta": complete.PredictAnything,
|
|
|
|
"-detach": complete.PredictNothing,
|
|
|
|
"-idempotency-token": complete.PredictAnything,
|
|
|
|
"-verbose": complete.PredictNothing,
|
2017-08-23 21:56:21 +00:00
|
|
|
})
|
2017-08-22 20:39:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *JobDispatchCommand) AutocompleteArgs() complete.Predictor {
|
|
|
|
return complete.PredictFunc(func(a complete.Args) []string {
|
2017-08-29 21:29:32 +00:00
|
|
|
client, err := c.Meta.Client()
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-10-13 00:01:54 +00:00
|
|
|
resp, _, err := client.Jobs().PrefixList(a.Last)
|
2017-08-22 20:39:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return []string{}
|
|
|
|
}
|
2021-10-13 00:01:54 +00:00
|
|
|
|
|
|
|
// filter by parameterized jobs
|
|
|
|
matches := make([]string, 0, len(resp))
|
|
|
|
for _, job := range resp {
|
|
|
|
if job.ParameterizedJob {
|
|
|
|
matches = append(matches, job.ID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return matches
|
|
|
|
|
2017-08-22 20:39:06 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-04-18 16:02:11 +00:00
|
|
|
func (c *JobDispatchCommand) Name() string { return "job dispatch" }
|
|
|
|
|
2016-12-05 05:22:13 +00:00
|
|
|
func (c *JobDispatchCommand) Run(args []string) int {
|
|
|
|
var detach, verbose bool
|
2021-10-22 16:39:05 +00:00
|
|
|
var idempotencyToken string
|
2016-12-05 05:22:13 +00:00
|
|
|
var meta []string
|
2022-10-06 20:21:40 +00:00
|
|
|
var idPrefixTemplate string
|
2016-12-05 05:22:13 +00:00
|
|
|
|
2018-04-18 16:02:11 +00:00
|
|
|
flags := c.Meta.FlagSet(c.Name(), FlagSetClient)
|
2016-12-05 05:22:13 +00:00
|
|
|
flags.Usage = func() { c.Ui.Output(c.Help()) }
|
|
|
|
flags.BoolVar(&detach, "detach", false, "")
|
|
|
|
flags.BoolVar(&verbose, "verbose", false, "")
|
2021-10-22 16:39:05 +00:00
|
|
|
flags.StringVar(&idempotencyToken, "idempotency-token", "", "")
|
2016-12-05 05:22:13 +00:00
|
|
|
flags.Var((*flaghelper.StringFlag)(&meta), "meta", "")
|
2022-10-06 20:21:40 +00:00
|
|
|
flags.StringVar(&idPrefixTemplate, "id-prefix-template", "", "")
|
2016-12-05 05:22:13 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
2018-04-19 01:21:43 +00:00
|
|
|
// Check that we got one or two arguments
|
2016-12-05 05:22:13 +00:00
|
|
|
args = flags.Args()
|
2018-04-19 01:21:43 +00:00
|
|
|
if l := len(args); l < 1 || l > 2 {
|
|
|
|
c.Ui.Error("This command takes one or two argument: <parameterized job> [input source]")
|
2018-04-18 16:02:11 +00:00
|
|
|
c.Ui.Error(commandErrorText(c))
|
2016-12-05 05:22:13 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2017-01-20 18:33:52 +00:00
|
|
|
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
|
2021-10-22 16:39:05 +00:00
|
|
|
w := &api.WriteOptions{
|
|
|
|
IdempotencyToken: idempotencyToken,
|
|
|
|
}
|
2022-10-06 20:21:40 +00:00
|
|
|
resp, _, err := client.Jobs().Dispatch(job, metaMap, payload, idPrefixTemplate, w)
|
2016-12-05 05:22:13 +00:00
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf("Failed to dispatch job: %s", err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2017-03-27 23:55:17 +00:00
|
|
|
// See if an evaluation was created. If the job is periodic there will be no
|
|
|
|
// eval.
|
|
|
|
evalCreated := resp.EvalID != ""
|
|
|
|
|
2016-12-05 05:22:13 +00:00
|
|
|
basic := []string{
|
|
|
|
fmt.Sprintf("Dispatched Job ID|%s", resp.DispatchedJobID),
|
2017-03-27 23:55:17 +00:00
|
|
|
}
|
|
|
|
if evalCreated {
|
|
|
|
basic = append(basic, 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
|
|
|
|
2017-03-27 23:55:17 +00:00
|
|
|
// Nothing to do
|
|
|
|
if detach || !evalCreated {
|
2016-12-05 23:10:23 +00:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Ui.Output("")
|
|
|
|
mon := newMonitor(c.Ui, client, length)
|
2020-11-02 18:24:49 +00:00
|
|
|
return mon.monitor(resp.EvalID)
|
2016-12-05 05:22:13 +00:00
|
|
|
}
|