open-nomad/command/run.go

178 lines
4.3 KiB
Go
Raw Normal View History

2015-09-16 01:22:51 +00:00
package command
import (
"bytes"
"encoding/gob"
"encoding/json"
2015-09-16 01:22:51 +00:00
"fmt"
"strings"
2015-12-10 22:07:34 +00:00
"time"
2015-09-16 01:22:51 +00:00
"github.com/hashicorp/nomad/api"
"github.com/hashicorp/nomad/jobspec"
"github.com/hashicorp/nomad/nomad/structs"
)
type RunCommand struct {
Meta
}
func (c *RunCommand) Help() string {
helpText := `
Usage: nomad run [options] <file>
2015-09-23 01:17:30 +00:00
Starts running a new job or updates an existing job using
the specification located at <file>. This is the main command
used to interact with Nomad.
2015-09-16 01:22:51 +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.
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.
2015-09-16 01:22:51 +00:00
General Options:
` + generalOptionsUsage() + `
Run Options:
2015-09-18 04:09:34 +00:00
-detach
2015-09-18 21:55:40 +00:00
Return immediately instead of entering monitor mode. After job
2015-09-18 04:09:34 +00:00
submission, the evaluation ID will be printed to the screen.
You can use this ID to start a monitor using the eval-monitor
command later if needed.
-verbose
Display full information.
-output
Output the JSON that would be submitted to the HTTP API without submitting
the job.
`
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 {
var detach, verbose, output bool
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, "")
flags.BoolVar(&verbose, "verbose", false, "")
flags.BoolVar(&output, "output", false, "")
2015-09-16 01:22:51 +00:00
if err := flags.Parse(args); err != nil {
return 1
}
// Truncate the id unless full length is requested
length := shortId
if verbose {
length = fullId
}
2015-09-16 01:22:51 +00:00
// Check that we got exactly one node
args = flags.Args()
if len(args) != 1 {
c.Ui.Error(c.Help())
return 1
}
file := args[0]
// Parse the job file
job, err := jobspec.ParseFile(file)
if err != nil {
c.Ui.Error(fmt.Sprintf("Error parsing job file %s: %s", file, err))
return 1
}
2015-12-18 20:17:50 +00:00
// Initialize any fields that need to be.
job.InitFields()
// Check that the job is valid
if err := job.Validate(); err != nil {
c.Ui.Error(fmt.Sprintf("Error validating job: %s", err))
return 1
}
2015-12-10 22:07:34 +00:00
// Check if the job is periodic.
periodic := job.IsPeriodic()
2015-09-16 01:22:51 +00:00
// Convert it to something we can use
apiJob, err := convertStructJob(job)
2015-09-16 01:22:51 +00:00
if err != nil {
c.Ui.Error(fmt.Sprintf("Error converting job: %s", err))
return 1
}
if output {
2016-03-29 22:02:14 +00:00
req := api.RegisterJobRequest{apiJob}
buf, err := json.MarshalIndent(req, "", " ")
if err != nil {
c.Ui.Error(fmt.Sprintf("Error converting job: %s", err))
return 1
}
c.Ui.Output(string(buf))
return 0
}
2015-09-16 01:22:51 +00:00
// Get the HTTP client
client, err := c.Meta.Client()
if err != nil {
c.Ui.Error(fmt.Sprintf("Error initializing client: %s", err))
return 1
}
// Submit the job
evalID, _, err := client.Jobs().Register(apiJob, nil)
if err != nil {
c.Ui.Error(fmt.Sprintf("Error submitting job: %s", err))
return 1
}
// Check if we should enter monitor mode
2015-12-10 22:07:34 +00:00
if detach || periodic {
2015-09-21 22:55:10 +00:00
c.Ui.Output("Job registration successful")
2015-12-10 22:07:34 +00:00
if periodic {
c.Ui.Output(fmt.Sprintf("Approximate next launch time: %v", job.Periodic.Next(time.Now().UTC())))
2015-12-10 22:07:34 +00:00
} else {
c.Ui.Output("Evaluation ID: " + evalID)
}
2015-09-18 04:09:34 +00:00
return 0
}
2015-09-18 04:09:34 +00:00
// Detach was not specified, so start monitoring
mon := newMonitor(c.Ui, client, length)
return mon.monitor(evalID, false)
2015-09-18 04:09:34 +00:00
2015-09-16 01:22:51 +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.
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
}