forked from hashicorp/nomad
-
Notifications
You must be signed in to change notification settings - Fork 0
/
job_dispatch.go
174 lines (143 loc) · 4.33 KB
/
job_dispatch.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package command
import (
"fmt"
"io/ioutil"
"os"
"strings"
"github.com/hashicorp/nomad/api/contexts"
flaghelper "github.com/hashicorp/nomad/helper/flag-helpers"
"github.com/posener/complete"
)
type JobDispatchCommand struct {
Meta
}
func (c *JobDispatchCommand) Help() string {
helpText := `
Usage: nomad job dispatch [options] <parameterized job> [input source]
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
times.
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.
General Options:
` + generalOptionsUsage() + `
Dispatch Options:
-meta <key>=<value>
Meta takes a key/value pair separated by "=". The metadata key will be
merged into the job's metadata. The job may define a default value for the
key which is overridden 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.
-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.
`
return strings.TrimSpace(helpText)
}
func (c *JobDispatchCommand) Synopsis() string {
return "Dispatch an instance of a parameterized job"
}
func (c *JobDispatchCommand) AutocompleteFlags() complete.Flags {
return mergeAutocompleteFlags(c.Meta.AutocompleteFlags(FlagSetClient),
complete.Flags{
"-meta": complete.PredictAnything,
"-detach": complete.PredictNothing,
"-verbose": complete.PredictNothing,
})
}
func (c *JobDispatchCommand) AutocompleteArgs() complete.Predictor {
return complete.PredictFunc(func(a complete.Args) []string {
client, err := c.Meta.Client()
if err != nil {
return nil
}
resp, _, err := client.Search().PrefixSearch(a.Last, contexts.Jobs, nil)
if err != nil {
return []string{}
}
return resp.Matches[contexts.Jobs]
})
}
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
length := shortId
if verbose {
length = fullId
}
// Check that we got exactly one node
args = flags.Args()
if l := len(args); l < 1 || l > 2 {
c.Ui.Error(c.Help())
return 1
}
job := args[0]
var payload []byte
var readErr error
// Read the input
if len(args) == 2 {
switch args[1] {
case "-":
payload, readErr = ioutil.ReadAll(os.Stdin)
default:
payload, readErr = ioutil.ReadFile(args[1])
}
if readErr != nil {
c.Ui.Error(fmt.Sprintf("Error reading input data: %v", readErr))
return 1
}
}
// 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)
if err != nil {
c.Ui.Error(fmt.Sprintf("Failed to dispatch job: %s", err))
return 1
}
// See if an evaluation was created. If the job is periodic there will be no
// eval.
evalCreated := resp.EvalID != ""
basic := []string{
fmt.Sprintf("Dispatched Job ID|%s", resp.DispatchedJobID),
}
if evalCreated {
basic = append(basic, fmt.Sprintf("Evaluation ID|%s", limit(resp.EvalID, length)))
}
c.Ui.Output(formatKV(basic))
// Nothing to do
if detach || !evalCreated {
return 0
}
c.Ui.Output("")
mon := newMonitor(c.Ui, client, length)
return mon.monitor(resp.EvalID, false)
}