github.com/Ilhicas/nomad@v1.0.4-0.20210304152020-e86851182bc3/command/deployment_unblock.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 DeploymentUnblockCommand struct {
    12  	Meta
    13  }
    14  
    15  func (c *DeploymentUnblockCommand) Help() string {
    16  	helpText := `
    17  Usage: nomad deployment unblock [options] <deployment id>
    18  
    19    Unblock is used to unblock a multiregion deployment that's waiting for
    20    peer region deployments to complete.
    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  Unblock Options:
    30  
    31    -detach
    32      Return immediately instead of entering monitor mode. After deployment
    33      unblock, the evaluation ID will be printed to the screen, which can be used
    34      to examine the evaluation using the eval-status command.
    35  
    36    -verbose
    37      Display full information.
    38  `
    39  	return strings.TrimSpace(helpText)
    40  }
    41  
    42  func (c *DeploymentUnblockCommand) Synopsis() string {
    43  	return "Unblock a blocked deployment"
    44  }
    45  
    46  func (c *DeploymentUnblockCommand) AutocompleteFlags() complete.Flags {
    47  	return mergeAutocompleteFlags(c.Meta.AutocompleteFlags(FlagSetClient),
    48  		complete.Flags{
    49  			"-detach":  complete.PredictNothing,
    50  			"-verbose": complete.PredictNothing,
    51  		})
    52  }
    53  
    54  func (c *DeploymentUnblockCommand) AutocompleteArgs() complete.Predictor {
    55  	return complete.PredictFunc(func(a complete.Args) []string {
    56  		client, err := c.Meta.Client()
    57  		if err != nil {
    58  			return nil
    59  		}
    60  
    61  		resp, _, err := client.Search().PrefixSearch(a.Last, contexts.Deployments, nil)
    62  		if err != nil {
    63  			return []string{}
    64  		}
    65  		return resp.Matches[contexts.Deployments]
    66  	})
    67  }
    68  
    69  func (c *DeploymentUnblockCommand) Name() string { return "deployment unblock" }
    70  func (c *DeploymentUnblockCommand) Run(args []string) int {
    71  	var detach, verbose bool
    72  
    73  	flags := c.Meta.FlagSet(c.Name(), FlagSetClient)
    74  	flags.Usage = func() { c.Ui.Output(c.Help()) }
    75  	flags.BoolVar(&detach, "detach", false, "")
    76  	flags.BoolVar(&verbose, "verbose", false, "")
    77  
    78  	if err := flags.Parse(args); err != nil {
    79  		return 1
    80  	}
    81  
    82  	// Check that we got exactly one argument
    83  	args = flags.Args()
    84  	if l := len(args); l != 1 {
    85  		c.Ui.Error("This command takes one argument: <deployment id>")
    86  		c.Ui.Error(commandErrorText(c))
    87  		return 1
    88  	}
    89  
    90  	dID := args[0]
    91  
    92  	// Truncate the id unless full length is requested
    93  	length := shortId
    94  	if verbose {
    95  		length = fullId
    96  	}
    97  
    98  	// Get the HTTP client
    99  	client, err := c.Meta.Client()
   100  	if err != nil {
   101  		c.Ui.Error(fmt.Sprintf("Error initializing client: %s", err))
   102  		return 1
   103  	}
   104  
   105  	// Do a prefix lookup
   106  	deploy, possible, err := getDeployment(client.Deployments(), dID)
   107  	if err != nil {
   108  		c.Ui.Error(fmt.Sprintf("Error retrieving deployment: %s", err))
   109  		return 1
   110  	}
   111  
   112  	if len(possible) != 0 {
   113  		c.Ui.Error(fmt.Sprintf("Prefix matched multiple deployments\n\n%s", formatDeployments(possible, length)))
   114  		return 1
   115  	}
   116  
   117  	u, _, err := client.Deployments().Unblock(deploy.ID, nil)
   118  	if err != nil {
   119  		c.Ui.Error(fmt.Sprintf("Error unblocking deployment: %s", err))
   120  		return 1
   121  	}
   122  
   123  	c.Ui.Output(fmt.Sprintf("Deployment %q unblocked", deploy.ID))
   124  	evalCreated := u.EvalID != ""
   125  
   126  	// Nothing to do
   127  	if detach || !evalCreated {
   128  		return 0
   129  	}
   130  
   131  	c.Ui.Output("")
   132  	mon := newMonitor(c.Ui, client, length)
   133  	return mon.monitor(u.EvalID)
   134  }