github.com/hhrutter/nomad@v0.6.0-rc2.0.20170723054333-80c4b03f0705/command/job_revert.go (about) 1 package command 2 3 import ( 4 "fmt" 5 "strings" 6 ) 7 8 type JobRevertCommand struct { 9 Meta 10 } 11 12 func (c *JobRevertCommand) Help() string { 13 helpText := ` 14 Usage: nomad job revert [options] <job> <version> 15 16 Revert is used to revert a job to a prior version of the job. The available 17 versions to revert to can be found using "nomad job history" command. 18 19 General Options: 20 21 ` + generalOptionsUsage() + ` 22 23 Revert Options: 24 25 -detach 26 Return immediately instead of entering monitor mode. After job revert, 27 the evaluation ID will be printed to the screen, which can be used to 28 examine the evaluation using the eval-status command. 29 30 -verbose 31 Display full information. 32 ` 33 return strings.TrimSpace(helpText) 34 } 35 36 func (c *JobRevertCommand) Synopsis() string { 37 return "Revert to a prior version of the job" 38 } 39 40 func (c *JobRevertCommand) Run(args []string) int { 41 var detach, verbose bool 42 43 flags := c.Meta.FlagSet("job revert", FlagSetClient) 44 flags.Usage = func() { c.Ui.Output(c.Help()) } 45 flags.BoolVar(&detach, "detach", false, "") 46 flags.BoolVar(&verbose, "verbose", false, "") 47 48 if err := flags.Parse(args); err != nil { 49 return 1 50 } 51 52 // Truncate the id unless full length is requested 53 length := shortId 54 if verbose { 55 length = fullId 56 } 57 58 // Check that we got two args 59 args = flags.Args() 60 if l := len(args); l != 2 { 61 c.Ui.Error(c.Help()) 62 return 1 63 } 64 65 // Get the HTTP client 66 client, err := c.Meta.Client() 67 if err != nil { 68 c.Ui.Error(fmt.Sprintf("Error initializing client: %s", err)) 69 return 1 70 } 71 72 jobID := args[0] 73 revertVersion, ok, err := parseVersion(args[1]) 74 if !ok { 75 c.Ui.Error("The job version to revert to must be specified using the -job-version flag") 76 return 1 77 } 78 if err != nil { 79 c.Ui.Error(fmt.Sprintf("Failed to parse job-version flag: %v", err)) 80 return 1 81 } 82 83 // Check if the job exists 84 jobs, _, err := client.Jobs().PrefixList(jobID) 85 if err != nil { 86 c.Ui.Error(fmt.Sprintf("Error listing jobs: %s", err)) 87 return 1 88 } 89 if len(jobs) == 0 { 90 c.Ui.Error(fmt.Sprintf("No job(s) with prefix or id %q found", jobID)) 91 return 1 92 } 93 if len(jobs) > 1 && strings.TrimSpace(jobID) != jobs[0].ID { 94 c.Ui.Error(fmt.Sprintf("Prefix matched multiple jobs\n\n%s", createStatusListOutput(jobs))) 95 return 1 96 } 97 98 // Prefix lookup matched a single job 99 resp, _, err := client.Jobs().Revert(jobs[0].ID, revertVersion, nil, nil) 100 if err != nil { 101 c.Ui.Error(fmt.Sprintf("Error retrieving job versions: %s", err)) 102 return 1 103 } 104 105 // Nothing to do 106 evalCreated := resp.EvalID != "" 107 if detach || !evalCreated { 108 return 0 109 } 110 111 mon := newMonitor(c.Ui, client, length) 112 return mon.monitor(resp.EvalID, false) 113 }