2015-09-16 01:22:51 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/gob"
|
2016-03-26 01:24:51 +00:00
|
|
|
"encoding/json"
|
2015-09-16 01:22:51 +00:00
|
|
|
"fmt"
|
2016-08-17 23:49:05 +00:00
|
|
|
"os"
|
2016-06-08 23:48:02 +00:00
|
|
|
"regexp"
|
|
|
|
"strconv"
|
2015-09-16 01:22:51 +00:00
|
|
|
"strings"
|
2015-12-10 22:07:34 +00:00
|
|
|
"time"
|
2015-09-16 01:22:51 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/nomad/api"
|
2017-02-06 19:48:28 +00:00
|
|
|
"github.com/hashicorp/nomad/helper"
|
2015-09-16 01:22:51 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
|
|
|
)
|
|
|
|
|
2016-06-08 23:48:02 +00:00
|
|
|
var (
|
|
|
|
// enforceIndexRegex is a regular expression which extracts the enforcement error
|
|
|
|
enforceIndexRegex = regexp.MustCompile(`\((Enforcing job modify index.*)\)`)
|
|
|
|
)
|
|
|
|
|
2015-09-16 01:22:51 +00:00
|
|
|
type RunCommand struct {
|
|
|
|
Meta
|
2016-08-16 10:49:14 +00:00
|
|
|
JobGetter
|
2015-09-16 01:22:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *RunCommand) Help() string {
|
|
|
|
helpText := `
|
2016-06-21 17:50:18 +00:00
|
|
|
Usage: nomad run [options] <path>
|
2015-09-16 01:22:51 +00:00
|
|
|
|
2015-09-23 01:17:30 +00:00
|
|
|
Starts running a new job or updates an existing job using
|
2016-06-21 17:50:18 +00:00
|
|
|
the specification located at <path>. This is the main command
|
2015-09-23 01:17:30 +00:00
|
|
|
used to interact with Nomad.
|
2015-09-16 01:22:51 +00:00
|
|
|
|
2016-06-21 17:50:18 +00:00
|
|
|
If the supplied path is "-", the jobfile is read from stdin. Otherwise
|
2016-08-11 13:34:31 +00:00
|
|
|
it is read from the file at the supplied path or downloaded and
|
|
|
|
read from URL specified.
|
2016-06-21 17:50:18 +00:00
|
|
|
|
2015-09-18 04:09:34 +00:00
|
|
|
Upon successful job submission, this command will immediately
|
|
|
|
enter an interactive monitor. This is useful to watch Nomad's
|
|
|
|
internals make scheduling decisions and place the submitted work
|
|
|
|
onto nodes. The monitor will end once job placement is done. It
|
|
|
|
is safe to exit the monitor early using ctrl+c.
|
|
|
|
|
2015-09-21 19:19:34 +00:00
|
|
|
On successful job submission and scheduling, exit code 0 will be
|
|
|
|
returned. If there are job placement issues encountered
|
|
|
|
(unsatisfiable constraints, resource exhaustion, etc), then the
|
|
|
|
exit code will be 2. Any other errors, including client connection
|
|
|
|
issues or internal errors, are indicated by exit code 1.
|
|
|
|
|
2016-06-07 18:28:02 +00:00
|
|
|
If the job has specified the region, the -region flag and NOMAD_REGION
|
2016-10-11 19:31:40 +00:00
|
|
|
environment variable are overridden and the job's region is used.
|
2016-06-07 18:28:02 +00:00
|
|
|
|
2016-08-17 04:32:25 +00:00
|
|
|
The run command will set the vault_token of the job based on the following
|
|
|
|
precedence, going from highest to lowest: the -vault-token flag, the
|
|
|
|
$VAULT_TOKEN environment variable and finally the value in the job file.
|
|
|
|
|
2015-09-16 01:22:51 +00:00
|
|
|
General Options:
|
|
|
|
|
2015-09-16 20:58:33 +00:00
|
|
|
` + generalOptionsUsage() + `
|
|
|
|
|
|
|
|
Run Options:
|
|
|
|
|
2016-06-08 23:48:02 +00:00
|
|
|
-check-index
|
2016-10-11 19:31:40 +00:00
|
|
|
If set, the job is only registered or updated if the passed
|
2016-07-20 15:53:59 +00:00
|
|
|
job modify index matches the server side version. If a check-index value of
|
|
|
|
zero is passed, the job is only registered if it does not yet exist. If a
|
|
|
|
non-zero value is passed, it ensures that the job is being updated from a
|
|
|
|
known state. The use of this flag is most common in conjunction with plan
|
|
|
|
command.
|
2016-06-08 23:48:02 +00:00
|
|
|
|
2015-09-18 04:09:34 +00:00
|
|
|
-detach
|
2016-07-20 15:53:59 +00:00
|
|
|
Return immediately instead of entering monitor mode. After job submission,
|
|
|
|
the evaluation ID will be printed to the screen, which can be used to
|
|
|
|
examine the evaluation using the eval-status command.
|
2016-01-14 20:57:43 +00:00
|
|
|
|
2016-01-15 22:32:38 +00:00
|
|
|
-verbose
|
|
|
|
Display full information.
|
2016-03-26 01:24:51 +00:00
|
|
|
|
2016-08-17 04:32:25 +00:00
|
|
|
-vault-token
|
|
|
|
If set, the passed Vault token is stored in the job before sending to the
|
|
|
|
Nomad servers. This allows passing the Vault token without storing it in
|
|
|
|
the job file. This overrides the token found in $VAULT_TOKEN environment
|
|
|
|
variable and that found in the job.
|
|
|
|
|
2016-03-26 01:24:51 +00:00
|
|
|
-output
|
|
|
|
Output the JSON that would be submitted to the HTTP API without submitting
|
|
|
|
the job.
|
2015-09-16 20:58:33 +00:00
|
|
|
`
|
2015-09-16 01:22:51 +00:00
|
|
|
return strings.TrimSpace(helpText)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *RunCommand) Synopsis() string {
|
2015-09-23 01:17:55 +00:00
|
|
|
return "Run a new job or update an existing job"
|
2015-09-16 01:22:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *RunCommand) Run(args []string) int {
|
2016-08-10 14:30:19 +00:00
|
|
|
var detach, verbose, output bool
|
2016-08-17 04:32:25 +00:00
|
|
|
var checkIndexStr, vaultToken string
|
2015-09-16 20:58:33 +00:00
|
|
|
|
2015-09-16 01:22:51 +00:00
|
|
|
flags := c.Meta.FlagSet("run", FlagSetClient)
|
|
|
|
flags.Usage = func() { c.Ui.Output(c.Help()) }
|
2015-09-18 04:09:34 +00:00
|
|
|
flags.BoolVar(&detach, "detach", false, "")
|
2016-01-15 22:32:38 +00:00
|
|
|
flags.BoolVar(&verbose, "verbose", false, "")
|
2016-03-26 01:24:51 +00:00
|
|
|
flags.BoolVar(&output, "output", false, "")
|
2016-06-08 23:48:02 +00:00
|
|
|
flags.StringVar(&checkIndexStr, "check-index", "", "")
|
2016-08-17 04:32:25 +00:00
|
|
|
flags.StringVar(&vaultToken, "vault-token", "", "")
|
2015-09-16 20:58:33 +00:00
|
|
|
|
2015-09-16 01:22:51 +00:00
|
|
|
if err := flags.Parse(args); err != nil {
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2016-01-14 20:57:43 +00:00
|
|
|
// Truncate the id unless full length is requested
|
2016-01-15 22:32:38 +00:00
|
|
|
length := shortId
|
|
|
|
if verbose {
|
|
|
|
length = fullId
|
2016-01-14 20:57:43 +00:00
|
|
|
}
|
|
|
|
|
2016-07-21 10:40:47 +00:00
|
|
|
// Check that we got exactly one argument
|
2015-09-16 01:22:51 +00:00
|
|
|
args = flags.Args()
|
|
|
|
if len(args) != 1 {
|
|
|
|
c.Ui.Error(c.Help())
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2016-08-10 14:30:19 +00:00
|
|
|
// Check that we got exactly one node
|
|
|
|
args = flags.Args()
|
|
|
|
if len(args) != 1 {
|
|
|
|
c.Ui.Error(c.Help())
|
|
|
|
return 1
|
2016-06-21 17:50:18 +00:00
|
|
|
}
|
|
|
|
|
2016-08-10 14:30:19 +00:00
|
|
|
// Get Job struct from Jobfile
|
2017-02-06 19:48:28 +00:00
|
|
|
job, err := c.JobGetter.ApiJob(args[0])
|
2015-09-16 01:22:51 +00:00
|
|
|
if err != nil {
|
2016-08-10 14:30:19 +00:00
|
|
|
c.Ui.Error(fmt.Sprintf("Error getting job struct: %s", err))
|
2015-09-16 01:22:51 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2017-02-06 19:48:28 +00:00
|
|
|
// Get the HTTP client
|
|
|
|
client, err := c.Meta.Client()
|
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf("Error initializing client: %s", err))
|
2015-09-21 22:08:29 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2017-02-06 19:48:28 +00:00
|
|
|
// Force the region to be that of the job.
|
|
|
|
if r := job.Region; r != nil {
|
|
|
|
client.SetRegion(*r)
|
|
|
|
}
|
|
|
|
|
2017-01-20 18:33:52 +00:00
|
|
|
// Check if the job is periodic or is a parameterized job
|
2015-12-10 22:07:34 +00:00
|
|
|
periodic := job.IsPeriodic()
|
2017-01-20 18:33:52 +00:00
|
|
|
paramjob := job.IsParameterized()
|
2015-12-10 22:07:34 +00:00
|
|
|
|
2016-08-17 04:32:25 +00:00
|
|
|
// Parse the Vault token
|
|
|
|
if vaultToken == "" {
|
|
|
|
// Check the environment variable
|
|
|
|
vaultToken = os.Getenv("VAULT_TOKEN")
|
|
|
|
}
|
|
|
|
|
|
|
|
if vaultToken != "" {
|
2017-02-06 19:48:28 +00:00
|
|
|
job.VaultToken = helper.StringToPtr(vaultToken)
|
2015-09-16 01:22:51 +00:00
|
|
|
}
|
|
|
|
|
2016-11-09 01:00:59 +00:00
|
|
|
// COMPAT 0.4.1 -> 0.5 Remove in 0.6
|
2017-02-06 19:48:28 +00:00
|
|
|
OUTSIDE:
|
|
|
|
for _, tg := range job.TaskGroups {
|
|
|
|
for _, task := range tg.Tasks {
|
|
|
|
if task.Resources != nil {
|
|
|
|
if task.Resources.DiskMB != nil {
|
|
|
|
c.Ui.Error("WARNING: disk attribute is deprecated in the resources block. See https://www.nomadproject.io/docs/job-specification/ephemeral_disk.html")
|
|
|
|
break OUTSIDE
|
2016-11-09 01:00:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-26 01:24:51 +00:00
|
|
|
if output {
|
2017-02-06 19:48:28 +00:00
|
|
|
req := api.RegisterJobRequest{Job: job}
|
2016-03-29 22:02:14 +00:00
|
|
|
buf, err := json.MarshalIndent(req, "", " ")
|
2016-03-26 01:24:51 +00:00
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf("Error converting job: %s", err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Ui.Output(string(buf))
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2016-06-08 23:48:02 +00:00
|
|
|
// Parse the check-index
|
|
|
|
checkIndex, enforce, err := parseCheckIndex(checkIndexStr)
|
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf("Error parsing check-index value %q: %v", checkIndexStr, err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2015-09-16 01:22:51 +00:00
|
|
|
// Submit the job
|
2016-06-08 23:48:02 +00:00
|
|
|
var evalID string
|
|
|
|
if enforce {
|
2017-02-06 19:48:28 +00:00
|
|
|
evalID, _, err = client.Jobs().EnforceRegister(job, checkIndex, nil)
|
2016-06-08 23:48:02 +00:00
|
|
|
} else {
|
2017-02-06 19:48:28 +00:00
|
|
|
evalID, _, err = client.Jobs().Register(job, nil)
|
2016-06-08 23:48:02 +00:00
|
|
|
}
|
2015-09-16 01:22:51 +00:00
|
|
|
if err != nil {
|
2016-06-08 23:48:02 +00:00
|
|
|
if strings.Contains(err.Error(), api.RegisterEnforceIndexErrPrefix) {
|
|
|
|
// Format the error specially if the error is due to index
|
|
|
|
// enforcement
|
|
|
|
matches := enforceIndexRegex.FindStringSubmatch(err.Error())
|
|
|
|
if len(matches) == 2 {
|
|
|
|
c.Ui.Error(matches[1]) // The matched group
|
|
|
|
c.Ui.Error("Job not updated")
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-16 01:22:51 +00:00
|
|
|
c.Ui.Error(fmt.Sprintf("Error submitting job: %s", err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2015-09-16 20:58:33 +00:00
|
|
|
// Check if we should enter monitor mode
|
2017-01-20 18:33:52 +00:00
|
|
|
if detach || periodic || paramjob {
|
2015-09-21 22:55:10 +00:00
|
|
|
c.Ui.Output("Job registration successful")
|
2017-03-01 19:45:20 +00:00
|
|
|
if periodic && !paramjob {
|
2017-02-20 23:13:10 +00:00
|
|
|
loc, err := job.Periodic.GetLocation()
|
|
|
|
if err == nil {
|
|
|
|
now := time.Now().In(loc)
|
|
|
|
next := job.Periodic.Next(now)
|
|
|
|
c.Ui.Output(fmt.Sprintf("Approximate next launch time: %s (%s from now)",
|
|
|
|
formatTime(next), formatTimeDifference(now, next, time.Second)))
|
|
|
|
}
|
2017-01-20 18:33:52 +00:00
|
|
|
} else if !paramjob {
|
2015-12-10 22:07:34 +00:00
|
|
|
c.Ui.Output("Evaluation ID: " + evalID)
|
|
|
|
}
|
|
|
|
|
2015-09-18 04:09:34 +00:00
|
|
|
return 0
|
2015-09-16 20:58:33 +00:00
|
|
|
}
|
|
|
|
|
2015-09-18 04:09:34 +00:00
|
|
|
// Detach was not specified, so start monitoring
|
2016-01-14 20:57:43 +00:00
|
|
|
mon := newMonitor(c.Ui, client, length)
|
2016-01-07 23:25:08 +00:00
|
|
|
return mon.monitor(evalID, false)
|
2015-09-18 04:09:34 +00:00
|
|
|
|
2015-09-16 01:22:51 +00:00
|
|
|
}
|
|
|
|
|
2016-06-08 23:48:02 +00:00
|
|
|
// parseCheckIndex parses the check-index flag and returns the index, whether it
|
|
|
|
// was set and potentially an error during parsing.
|
|
|
|
func parseCheckIndex(input string) (uint64, bool, error) {
|
|
|
|
if input == "" {
|
|
|
|
return 0, false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
u, err := strconv.ParseUint(input, 10, 64)
|
|
|
|
return u, true, err
|
|
|
|
}
|
|
|
|
|
2015-12-21 22:19:53 +00:00
|
|
|
// convertStructJob is used to take a *structs.Job and convert it to an *api.Job.
|
2015-09-16 01:22:51 +00:00
|
|
|
// This function is just a hammer and probably needs to be revisited.
|
2015-12-21 22:19:53 +00:00
|
|
|
func convertStructJob(in *structs.Job) (*api.Job, error) {
|
2015-11-14 04:51:30 +00:00
|
|
|
gob.Register([]map[string]interface{}{})
|
2015-11-18 23:26:38 +00:00
|
|
|
gob.Register([]interface{}{})
|
2015-09-16 01:22:51 +00:00
|
|
|
var apiJob *api.Job
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
if err := gob.NewEncoder(buf).Encode(in); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err := gob.NewDecoder(buf).Decode(&apiJob); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return apiJob, nil
|
|
|
|
}
|