github.com/hernad/nomad@v1.6.112/command/job_allocs.go (about)

     1  // Copyright (c) HashiCorp, Inc.
     2  // SPDX-License-Identifier: MPL-2.0
     3  
     4  package command
     5  
     6  import (
     7  	"fmt"
     8  	"strings"
     9  
    10  	"github.com/hernad/nomad/api"
    11  	"github.com/hernad/nomad/api/contexts"
    12  	"github.com/posener/complete"
    13  )
    14  
    15  type JobAllocsCommand struct {
    16  	Meta
    17  }
    18  
    19  func (c *JobAllocsCommand) Help() string {
    20  	helpText := `
    21  Usage: nomad job allocs [options] <job>
    22  
    23    Display allocations for a particular job.
    24  
    25    When ACLs are enabled, this command requires a token with the 'read-job'
    26    capability for the job's namespace. The 'list-jobs' capability is required to
    27    run the command with a job prefix instead of the exact job ID.
    28  
    29  General Options:
    30  
    31    ` + generalOptionsUsage(usageOptsDefault) + `
    32  
    33  Allocs Options:
    34  
    35    -all
    36      Display all allocations matching the job ID, even those from an older
    37      instance of the job.
    38  
    39    -json
    40      Output the allocations in a JSON format.
    41  
    42    -t
    43      Format and display allocations using a Go template.
    44  
    45    -verbose
    46      Display full information.
    47  `
    48  	return strings.TrimSpace(helpText)
    49  }
    50  
    51  func (c *JobAllocsCommand) Synopsis() string {
    52  	return "List allocations for a job"
    53  }
    54  
    55  func (c *JobAllocsCommand) AutocompleteFlags() complete.Flags {
    56  	return mergeAutocompleteFlags(c.Meta.AutocompleteFlags(FlagSetClient),
    57  		complete.Flags{
    58  			"-json":    complete.PredictNothing,
    59  			"-t":       complete.PredictAnything,
    60  			"-verbose": complete.PredictNothing,
    61  			"-all":     complete.PredictNothing,
    62  		})
    63  }
    64  
    65  func (c *JobAllocsCommand) AutocompleteArgs() complete.Predictor {
    66  	return complete.PredictFunc(func(a complete.Args) []string {
    67  		client, err := c.Meta.Client()
    68  		if err != nil {
    69  			return nil
    70  		}
    71  
    72  		resp, _, err := client.Search().PrefixSearch(a.Last, contexts.Jobs, nil)
    73  		if err != nil {
    74  			return []string{}
    75  		}
    76  		return resp.Matches[contexts.Jobs]
    77  	})
    78  }
    79  
    80  func (c *JobAllocsCommand) Name() string { return "job allocs" }
    81  
    82  func (c *JobAllocsCommand) Run(args []string) int {
    83  	var json, verbose, all bool
    84  	var tmpl string
    85  
    86  	flags := c.Meta.FlagSet(c.Name(), FlagSetClient)
    87  	flags.Usage = func() { c.Ui.Output(c.Help()) }
    88  	flags.BoolVar(&verbose, "verbose", false, "")
    89  	flags.BoolVar(&all, "all", false, "")
    90  	flags.BoolVar(&json, "json", false, "")
    91  	flags.StringVar(&tmpl, "t", "", "")
    92  
    93  	if err := flags.Parse(args); err != nil {
    94  		return 1
    95  	}
    96  
    97  	// Check that we got exactly one job
    98  	args = flags.Args()
    99  	if len(args) != 1 {
   100  		c.Ui.Error("This command takes one argument: <job>")
   101  		c.Ui.Error(commandErrorText(c))
   102  		return 1
   103  	}
   104  
   105  	// Get the HTTP client
   106  	client, err := c.Meta.Client()
   107  	if err != nil {
   108  		c.Ui.Error(fmt.Sprintf("Error initializing client: %s", err))
   109  		return 1
   110  	}
   111  
   112  	// Check if the job exists
   113  	jobIDPrefix := strings.TrimSpace(args[0])
   114  	jobID, namespace, err := c.JobIDByPrefix(client, jobIDPrefix, nil)
   115  	if err != nil {
   116  		c.Ui.Error(err.Error())
   117  		return 1
   118  	}
   119  
   120  	q := &api.QueryOptions{Namespace: namespace}
   121  
   122  	allocs, _, err := client.Jobs().Allocations(jobID, all, q)
   123  	if err != nil {
   124  		c.Ui.Error(fmt.Sprintf("Error retrieving allocations: %s", err))
   125  		return 1
   126  	}
   127  
   128  	if json || len(tmpl) > 0 {
   129  		out, err := Format(json, tmpl, allocs)
   130  		if err != nil {
   131  			c.Ui.Error(err.Error())
   132  			return 1
   133  		}
   134  
   135  		c.Ui.Output(out)
   136  		return 0
   137  	}
   138  
   139  	// Truncate the id unless full length is requested
   140  	length := shortId
   141  	if verbose {
   142  		length = fullId
   143  	}
   144  
   145  	c.Ui.Output(formatAllocListStubs(allocs, verbose, length))
   146  	return 0
   147  }