github.com/hhrutter/nomad@v0.6.0-rc2.0.20170723054333-80c4b03f0705/command/job_deployments.go (about) 1 package command 2 3 import ( 4 "fmt" 5 "strings" 6 ) 7 8 type JobDeploymentsCommand struct { 9 Meta 10 } 11 12 func (c *JobDeploymentsCommand) Help() string { 13 helpText := ` 14 Usage: nomad job deployments [options] <job> 15 16 Deployments is used to display the deployments for a particular job. 17 18 General Options: 19 20 ` + generalOptionsUsage() + ` 21 22 Deployments Options: 23 24 -json 25 Output the deployments in a JSON format. 26 27 -t 28 Format and display deployments using a Go template. 29 30 -latest 31 Display the latest deployment only. 32 33 -verbose 34 Display full information. 35 ` 36 return strings.TrimSpace(helpText) 37 } 38 39 func (c *JobDeploymentsCommand) Synopsis() string { 40 return "List deployments for a job" 41 } 42 43 func (c *JobDeploymentsCommand) Run(args []string) int { 44 var json, latest, verbose bool 45 var tmpl string 46 47 flags := c.Meta.FlagSet("job deployments", FlagSetClient) 48 flags.Usage = func() { c.Ui.Output(c.Help()) } 49 flags.BoolVar(&latest, "latest", false, "") 50 flags.BoolVar(&verbose, "verbose", false, "") 51 flags.BoolVar(&json, "json", false, "") 52 flags.StringVar(&tmpl, "t", "", "") 53 54 if err := flags.Parse(args); err != nil { 55 return 1 56 } 57 58 // Check that we got exactly one node 59 args = flags.Args() 60 if l := len(args); l != 1 { 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 74 // Check if the job exists 75 jobs, _, err := client.Jobs().PrefixList(jobID) 76 if err != nil { 77 c.Ui.Error(fmt.Sprintf("Error listing jobs: %s", err)) 78 return 1 79 } 80 if len(jobs) == 0 { 81 c.Ui.Error(fmt.Sprintf("No job(s) with prefix or id %q found", jobID)) 82 return 1 83 } 84 if len(jobs) > 1 && strings.TrimSpace(jobID) != jobs[0].ID { 85 c.Ui.Error(fmt.Sprintf("Prefix matched multiple jobs\n\n%s", createStatusListOutput(jobs))) 86 return 1 87 } 88 jobID = jobs[0].ID 89 90 // Truncate the id unless full length is requested 91 length := shortId 92 if verbose { 93 length = fullId 94 } 95 96 if latest { 97 deploy, _, err := client.Jobs().LatestDeployment(jobID, nil) 98 if err != nil { 99 c.Ui.Error(fmt.Sprintf("Error retrieving deployments: %s", err)) 100 return 1 101 } 102 103 if json || len(tmpl) > 0 { 104 out, err := Format(json, tmpl, deploy) 105 if err != nil { 106 c.Ui.Error(err.Error()) 107 return 1 108 } 109 110 c.Ui.Output(out) 111 return 0 112 } 113 114 c.Ui.Output(c.Colorize().Color(formatDeployment(deploy, length))) 115 return 0 116 } 117 118 deploys, _, err := client.Jobs().Deployments(jobID, nil) 119 if err != nil { 120 c.Ui.Error(fmt.Sprintf("Error retrieving deployments: %s", err)) 121 return 1 122 } 123 124 if json || len(tmpl) > 0 { 125 out, err := Format(json, tmpl, deploys) 126 if err != nil { 127 c.Ui.Error(err.Error()) 128 return 1 129 } 130 131 c.Ui.Output(out) 132 return 0 133 } 134 135 c.Ui.Output(formatDeployments(deploys, length)) 136 return 0 137 }