github.com/Ilhicas/nomad@v1.0.4-0.20210304152020-e86851182bc3/command/deployment_pause.go (about)

     1  package command
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  
     7  	"github.com/hashicorp/nomad/api/contexts"
     8  	"github.com/posener/complete"
     9  )
    10  
    11  type DeploymentPauseCommand struct {
    12  	Meta
    13  }
    14  
    15  func (c *DeploymentPauseCommand) Help() string {
    16  	helpText := `
    17  Usage: nomad deployment pause [options] <deployment id>
    18  
    19    Pause is used to pause a deployment. Pausing a deployment will pause the
    20    placement of new allocations as part of rolling deployment.
    21  
    22    When ACLs are enabled, this command requires a token with the 'submit-job'
    23    and 'read-job' capabilities for the deployment's namespace.
    24  
    25  General Options:
    26  
    27    ` + generalOptionsUsage(usageOptsDefault) + `
    28  
    29  Pause Options:
    30  
    31    -verbose
    32      Display full information.
    33  `
    34  	return strings.TrimSpace(helpText)
    35  }
    36  
    37  func (c *DeploymentPauseCommand) Synopsis() string {
    38  	return "Pause a deployment"
    39  }
    40  
    41  func (c *DeploymentPauseCommand) AutocompleteFlags() complete.Flags {
    42  	return mergeAutocompleteFlags(c.Meta.AutocompleteFlags(FlagSetClient),
    43  		complete.Flags{
    44  			"-verbose": complete.PredictNothing,
    45  		})
    46  }
    47  
    48  func (c *DeploymentPauseCommand) AutocompleteArgs() complete.Predictor {
    49  	return complete.PredictFunc(func(a complete.Args) []string {
    50  		client, err := c.Meta.Client()
    51  		if err != nil {
    52  			return nil
    53  		}
    54  
    55  		resp, _, err := client.Search().PrefixSearch(a.Last, contexts.Deployments, nil)
    56  		if err != nil {
    57  			return []string{}
    58  		}
    59  		return resp.Matches[contexts.Deployments]
    60  	})
    61  }
    62  
    63  func (c *DeploymentPauseCommand) Name() string { return "deployment pause" }
    64  
    65  func (c *DeploymentPauseCommand) Run(args []string) int {
    66  	var verbose bool
    67  
    68  	flags := c.Meta.FlagSet(c.Name(), FlagSetClient)
    69  	flags.Usage = func() { c.Ui.Output(c.Help()) }
    70  	flags.BoolVar(&verbose, "verbose", false, "")
    71  
    72  	if err := flags.Parse(args); err != nil {
    73  		return 1
    74  	}
    75  
    76  	// Check that we got exactly 1 argument
    77  	args = flags.Args()
    78  	if l := len(args); l != 1 {
    79  		c.Ui.Error("This command takes one argument: <deployment id>")
    80  		c.Ui.Error(commandErrorText(c))
    81  		return 1
    82  	}
    83  
    84  	dID := args[0]
    85  
    86  	// Truncate the id unless full length is requested
    87  	length := shortId
    88  	if verbose {
    89  		length = fullId
    90  	}
    91  
    92  	// Get the HTTP client
    93  	client, err := c.Meta.Client()
    94  	if err != nil {
    95  		c.Ui.Error(fmt.Sprintf("Error initializing client: %s", err))
    96  		return 1
    97  	}
    98  
    99  	// Do a prefix lookup
   100  	deploy, possible, err := getDeployment(client.Deployments(), dID)
   101  	if err != nil {
   102  		c.Ui.Error(fmt.Sprintf("Error retrieving deployment: %s", err))
   103  		return 1
   104  	}
   105  
   106  	if len(possible) != 0 {
   107  		c.Ui.Error(fmt.Sprintf("Prefix matched multiple deployments\n\n%s", formatDeployments(possible, length)))
   108  		return 1
   109  	}
   110  
   111  	if _, _, err := client.Deployments().Pause(deploy.ID, true, nil); err != nil {
   112  		c.Ui.Error(fmt.Sprintf("Error pausing deployment: %s", err))
   113  		return 1
   114  	}
   115  
   116  	c.Ui.Output(fmt.Sprintf("Deployment %q paused", deploy.ID))
   117  	return 0
   118  }