2015-09-16 01:22:51 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/gob"
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"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>
|
|
|
|
|
|
|
|
Starts running a new job using the definition located at <file>.
|
|
|
|
This is the main command used to invoke new work in Nomad.
|
|
|
|
|
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-16 01:22:51 +00:00
|
|
|
General Options:
|
|
|
|
|
2015-09-16 20:58:33 +00:00
|
|
|
` + 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.
|
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 {
|
|
|
|
return "Run a new job"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *RunCommand) Run(args []string) int {
|
2015-09-18 04:09:34 +00:00
|
|
|
var detach bool
|
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, "")
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
|
|
|
// Convert it to something we can use
|
|
|
|
apiJob, err := convertJob(job)
|
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf("Error converting job: %s", err))
|
|
|
|
return 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
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2015-09-16 20:58:33 +00:00
|
|
|
// Check if we should enter monitor mode
|
2015-09-18 04:09:34 +00:00
|
|
|
if detach {
|
|
|
|
c.Ui.Output(evalID)
|
|
|
|
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
|
|
|
|
mon := newMonitor(c.Ui, client)
|
|
|
|
return mon.monitor(evalID)
|
|
|
|
|
2015-09-16 01:22:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// convertJob is used to take a *structs.Job and convert it to an *api.Job.
|
|
|
|
// This function is just a hammer and probably needs to be revisited.
|
|
|
|
func convertJob(in *structs.Job) (*api.Job, error) {
|
|
|
|
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
|
|
|
|
}
|