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

     1  package command
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  )
     7  
     8  type DeploymentPauseCommand struct {
     9  	Meta
    10  }
    11  
    12  func (c *DeploymentPauseCommand) Help() string {
    13  	helpText := `
    14  Usage: nomad deployment pause [options] <deployment id>
    15  
    16  Pause is used to pause a deployment. Pausing a deployment will pause the
    17  placement of new allocations as part of rolling deployment.
    18  
    19  General Options:
    20  
    21    ` + generalOptionsUsage() + `
    22  
    23  Pause Options:
    24  
    25    -verbose
    26      Display full information.
    27  `
    28  	return strings.TrimSpace(helpText)
    29  }
    30  
    31  func (c *DeploymentPauseCommand) Synopsis() string {
    32  	return "Pause a deployment"
    33  }
    34  
    35  func (c *DeploymentPauseCommand) Run(args []string) int {
    36  	var verbose bool
    37  
    38  	flags := c.Meta.FlagSet("deployment pause", FlagSetClient)
    39  	flags.Usage = func() { c.Ui.Output(c.Help()) }
    40  	flags.BoolVar(&verbose, "verbose", false, "")
    41  
    42  	if err := flags.Parse(args); err != nil {
    43  		return 1
    44  	}
    45  
    46  	// Check that we got no arguments
    47  	args = flags.Args()
    48  	if l := len(args); l != 1 {
    49  		c.Ui.Error(c.Help())
    50  		return 1
    51  	}
    52  
    53  	dID := args[0]
    54  
    55  	// Truncate the id unless full length is requested
    56  	length := shortId
    57  	if verbose {
    58  		length = fullId
    59  	}
    60  
    61  	// Get the HTTP client
    62  	client, err := c.Meta.Client()
    63  	if err != nil {
    64  		c.Ui.Error(fmt.Sprintf("Error initializing client: %s", err))
    65  		return 1
    66  	}
    67  
    68  	// Do a prefix lookup
    69  	deploy, possible, err := getDeployment(client.Deployments(), dID)
    70  	if err != nil {
    71  		c.Ui.Error(fmt.Sprintf("Error retrieving deployment: %s", err))
    72  		return 1
    73  	}
    74  
    75  	if len(possible) != 0 {
    76  		c.Ui.Error(fmt.Sprintf("Prefix matched multiple deployments\n\n%s", formatDeployments(possible, length)))
    77  		return 1
    78  	}
    79  
    80  	if _, _, err := client.Deployments().Pause(deploy.ID, true, nil); err != nil {
    81  		c.Ui.Error(fmt.Sprintf("Error pausing deployment: %s", err))
    82  		return 1
    83  	}
    84  
    85  	c.Ui.Output(fmt.Sprintf("Deployment %q paused", deploy.ID))
    86  	return 0
    87  }