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