github.com/iqoqo/nomad@v0.11.3-0.20200911112621-d7021c74d101/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  General Options:
    23  
    24    ` + generalOptionsUsage() + `
    25  
    26  Pause Options:
    27  
    28    -verbose
    29      Display full information.
    30  `
    31  	return strings.TrimSpace(helpText)
    32  }
    33  
    34  func (c *DeploymentPauseCommand) Synopsis() string {
    35  	return "Pause a deployment"
    36  }
    37  
    38  func (c *DeploymentPauseCommand) AutocompleteFlags() complete.Flags {
    39  	return mergeAutocompleteFlags(c.Meta.AutocompleteFlags(FlagSetClient),
    40  		complete.Flags{
    41  			"-verbose": complete.PredictNothing,
    42  		})
    43  }
    44  
    45  func (c *DeploymentPauseCommand) AutocompleteArgs() complete.Predictor {
    46  	return complete.PredictFunc(func(a complete.Args) []string {
    47  		client, err := c.Meta.Client()
    48  		if err != nil {
    49  			return nil
    50  		}
    51  
    52  		resp, _, err := client.Search().PrefixSearch(a.Last, contexts.Deployments, nil)
    53  		if err != nil {
    54  			return []string{}
    55  		}
    56  		return resp.Matches[contexts.Deployments]
    57  	})
    58  }
    59  
    60  func (c *DeploymentPauseCommand) Name() string { return "deployment pause" }
    61  
    62  func (c *DeploymentPauseCommand) Run(args []string) int {
    63  	var verbose bool
    64  
    65  	flags := c.Meta.FlagSet(c.Name(), FlagSetClient)
    66  	flags.Usage = func() { c.Ui.Output(c.Help()) }
    67  	flags.BoolVar(&verbose, "verbose", false, "")
    68  
    69  	if err := flags.Parse(args); err != nil {
    70  		return 1
    71  	}
    72  
    73  	// Check that we got exactly 1 argument
    74  	args = flags.Args()
    75  	if l := len(args); l != 1 {
    76  		c.Ui.Error("This command takes one argument: <deployment id>")
    77  		c.Ui.Error(commandErrorText(c))
    78  		return 1
    79  	}
    80  
    81  	dID := args[0]
    82  
    83  	// Truncate the id unless full length is requested
    84  	length := shortId
    85  	if verbose {
    86  		length = fullId
    87  	}
    88  
    89  	// Get the HTTP client
    90  	client, err := c.Meta.Client()
    91  	if err != nil {
    92  		c.Ui.Error(fmt.Sprintf("Error initializing client: %s", err))
    93  		return 1
    94  	}
    95  
    96  	// Do a prefix lookup
    97  	deploy, possible, err := getDeployment(client.Deployments(), dID)
    98  	if err != nil {
    99  		c.Ui.Error(fmt.Sprintf("Error retrieving deployment: %s", err))
   100  		return 1
   101  	}
   102  
   103  	if len(possible) != 0 {
   104  		c.Ui.Error(fmt.Sprintf("Prefix matched multiple deployments\n\n%s", formatDeployments(possible, length)))
   105  		return 1
   106  	}
   107  
   108  	if _, _, err := client.Deployments().Pause(deploy.ID, true, nil); err != nil {
   109  		c.Ui.Error(fmt.Sprintf("Error pausing deployment: %s", err))
   110  		return 1
   111  	}
   112  
   113  	c.Ui.Output(fmt.Sprintf("Deployment %q paused", deploy.ID))
   114  	return 0
   115  }