github.com/hhrutter/nomad@v0.6.0-rc2.0.20170723054333-80c4b03f0705/command/inspect.go (about)

     1  package command
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  
     7  	"github.com/hashicorp/nomad/api"
     8  )
     9  
    10  type InspectCommand struct {
    11  	Meta
    12  }
    13  
    14  func (c *InspectCommand) Help() string {
    15  	helpText := `
    16  Usage: nomad inspect [options] <job>
    17  
    18    Inspect is used to see the specification of a submitted job.
    19  
    20  General Options:
    21  
    22    ` + generalOptionsUsage() + `
    23  
    24  Inspect Options:
    25  
    26    -version <job version>
    27      Display only the history for the given job version.
    28  
    29    -json
    30      Output the job in its JSON format.
    31  
    32    -t
    33      Format and display job using a Go template.
    34  `
    35  	return strings.TrimSpace(helpText)
    36  }
    37  
    38  func (c *InspectCommand) Synopsis() string {
    39  	return "Inspect a submitted job"
    40  }
    41  
    42  func (c *InspectCommand) Run(args []string) int {
    43  	var json bool
    44  	var tmpl, versionStr string
    45  
    46  	flags := c.Meta.FlagSet("inspect", FlagSetClient)
    47  	flags.Usage = func() { c.Ui.Output(c.Help()) }
    48  	flags.BoolVar(&json, "json", false, "")
    49  	flags.StringVar(&tmpl, "t", "", "")
    50  	flags.StringVar(&versionStr, "version", "", "")
    51  
    52  	if err := flags.Parse(args); err != nil {
    53  		return 1
    54  	}
    55  	args = flags.Args()
    56  
    57  	// Get the HTTP client
    58  	client, err := c.Meta.Client()
    59  	if err != nil {
    60  		c.Ui.Error(fmt.Sprintf("Error initializing client: %s", err))
    61  		return 1
    62  	}
    63  
    64  	// If args not specified but output format is specified, format and output the jobs data list
    65  	if len(args) == 0 && json || len(tmpl) > 0 {
    66  		jobs, _, err := client.Jobs().List(nil)
    67  		if err != nil {
    68  			c.Ui.Error(fmt.Sprintf("Error querying jobs: %v", err))
    69  			return 1
    70  		}
    71  
    72  		out, err := Format(json, tmpl, jobs)
    73  		if err != nil {
    74  			c.Ui.Error(err.Error())
    75  			return 1
    76  		}
    77  
    78  		c.Ui.Output(out)
    79  		return 0
    80  	}
    81  
    82  	// Check that we got exactly one job
    83  	if len(args) != 1 {
    84  		c.Ui.Error(c.Help())
    85  		return 1
    86  	}
    87  	jobID := args[0]
    88  
    89  	// Check if the job exists
    90  	jobs, _, err := client.Jobs().PrefixList(jobID)
    91  	if err != nil {
    92  		c.Ui.Error(fmt.Sprintf("Error inspecting job: %s", err))
    93  		return 1
    94  	}
    95  	if len(jobs) == 0 {
    96  		c.Ui.Error(fmt.Sprintf("No job(s) with prefix or id %q found", jobID))
    97  		return 1
    98  	}
    99  	if len(jobs) > 1 && strings.TrimSpace(jobID) != jobs[0].ID {
   100  		c.Ui.Error(fmt.Sprintf("Prefix matched multiple jobs\n\n%s", createStatusListOutput(jobs)))
   101  		return 1
   102  	}
   103  
   104  	var version *uint64
   105  	if versionStr != "" {
   106  		v, _, err := parseVersion(versionStr)
   107  		if err != nil {
   108  			c.Ui.Error(fmt.Sprintf("Error parsing version value %q: %v", versionStr, err))
   109  			return 1
   110  		}
   111  
   112  		version = &v
   113  	}
   114  
   115  	// Prefix lookup matched a single job
   116  	job, err := getJob(client, jobs[0].ID, version)
   117  	if err != nil {
   118  		c.Ui.Error(fmt.Sprintf("Error inspecting job: %s", err))
   119  		return 1
   120  	}
   121  
   122  	// If output format is specified, format and output the data
   123  	if json || len(tmpl) > 0 {
   124  		out, err := Format(json, tmpl, job)
   125  		if err != nil {
   126  			c.Ui.Error(err.Error())
   127  			return 1
   128  		}
   129  
   130  		c.Ui.Output(out)
   131  		return 0
   132  	}
   133  
   134  	// Print the contents of the job
   135  	req := api.RegisterJobRequest{Job: job}
   136  	f, err := DataFormat("json", "")
   137  	if err != nil {
   138  		c.Ui.Error(fmt.Sprintf("Error getting formatter: %s", err))
   139  		return 1
   140  	}
   141  
   142  	out, err := f.TransformData(req)
   143  	if err != nil {
   144  		c.Ui.Error(fmt.Sprintf("Error formatting the data: %s", err))
   145  		return 1
   146  	}
   147  	c.Ui.Output(out)
   148  	return 0
   149  }
   150  
   151  // getJob retrieves the job optionally at a particular version.
   152  func getJob(client *api.Client, jobID string, version *uint64) (*api.Job, error) {
   153  	if version == nil {
   154  		job, _, err := client.Jobs().Info(jobID, nil)
   155  		return job, err
   156  	}
   157  
   158  	versions, _, _, err := client.Jobs().Versions(jobID, false, nil)
   159  	if err != nil {
   160  		return nil, err
   161  	}
   162  
   163  	for _, j := range versions {
   164  		if *j.Version != *version {
   165  			continue
   166  		}
   167  		return j, nil
   168  	}
   169  
   170  	return nil, fmt.Errorf("job %q with version %d couldn't be found", jobID, *version)
   171  }