github.com/jrxfive/nomad@v0.6.1-0.20170802162750-1fef470e89bf/command/deployment_fail.go (about)

     1  package command
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  )
     7  
     8  type DeploymentFailCommand struct {
     9  	Meta
    10  }
    11  
    12  func (c *DeploymentFailCommand) Help() string {
    13  	helpText := `
    14  Usage: nomad deployment fail [options] <deployment id>
    15  
    16  Fail is used to mark a deployment as failed. Failing a deployment will
    17  stop the placement of new allocations as part of rolling deployment and
    18  if the job is configured to auto revert, the job will attempt to roll back to a
    19  stable version.
    20  
    21  General Options:
    22  
    23    ` + generalOptionsUsage() + `
    24  
    25  Fail Options:
    26  
    27    -detach
    28      Return immediately instead of entering monitor mode. After deployment
    29      resume, the evaluation ID will be printed to the screen, which can be used
    30      to examine the evaluation using the eval-status command.
    31  
    32    -verbose
    33      Display full information.
    34  `
    35  	return strings.TrimSpace(helpText)
    36  }
    37  
    38  func (c *DeploymentFailCommand) Synopsis() string {
    39  	return "Manually fail a deployment"
    40  }
    41  
    42  func (c *DeploymentFailCommand) Run(args []string) int {
    43  	var detach, verbose bool
    44  
    45  	flags := c.Meta.FlagSet("deployment fail", FlagSetClient)
    46  	flags.Usage = func() { c.Ui.Output(c.Help()) }
    47  	flags.BoolVar(&detach, "detach", false, "")
    48  	flags.BoolVar(&verbose, "verbose", false, "")
    49  
    50  	if err := flags.Parse(args); err != nil {
    51  		return 1
    52  	}
    53  
    54  	// Check that we got no arguments
    55  	args = flags.Args()
    56  	if l := len(args); l != 1 {
    57  		c.Ui.Error(c.Help())
    58  		return 1
    59  	}
    60  
    61  	dID := args[0]
    62  
    63  	// Truncate the id unless full length is requested
    64  	length := shortId
    65  	if verbose {
    66  		length = fullId
    67  	}
    68  
    69  	// Get the HTTP client
    70  	client, err := c.Meta.Client()
    71  	if err != nil {
    72  		c.Ui.Error(fmt.Sprintf("Error initializing client: %s", err))
    73  		return 1
    74  	}
    75  
    76  	// Do a prefix lookup
    77  	deploy, possible, err := getDeployment(client.Deployments(), dID)
    78  	if err != nil {
    79  		c.Ui.Error(fmt.Sprintf("Error retrieving deployment: %s", err))
    80  		return 1
    81  	}
    82  
    83  	if len(possible) != 0 {
    84  		c.Ui.Error(fmt.Sprintf("Prefix matched multiple deployments\n\n%s", formatDeployments(possible, length)))
    85  		return 1
    86  	}
    87  
    88  	u, _, err := client.Deployments().Fail(deploy.ID, nil)
    89  	if err != nil {
    90  		c.Ui.Error(fmt.Sprintf("Error failing deployment: %s", err))
    91  		return 1
    92  	}
    93  
    94  	if u.RevertedJobVersion == nil {
    95  		c.Ui.Output(fmt.Sprintf("Deployment %q failed", deploy.ID))
    96  	} else {
    97  		c.Ui.Output(fmt.Sprintf("Deployment %q failed. Auto-reverted to job version %d.", deploy.ID, *u.RevertedJobVersion))
    98  	}
    99  
   100  	evalCreated := u.EvalID != ""
   101  
   102  	// Nothing to do
   103  	if detach || !evalCreated {
   104  		return 0
   105  	}
   106  
   107  	c.Ui.Output("")
   108  	mon := newMonitor(c.Ui, client, length)
   109  	return mon.monitor(u.EvalID, false)
   110  }