github.com/hernad/nomad@v1.6.112/command/deployment_resume.go (about)

     1  // Copyright (c) HashiCorp, Inc.
     2  // SPDX-License-Identifier: MPL-2.0
     3  
     4  package command
     5  
     6  import (
     7  	"fmt"
     8  	"strings"
     9  
    10  	"github.com/hernad/nomad/api/contexts"
    11  	"github.com/posener/complete"
    12  )
    13  
    14  type DeploymentResumeCommand struct {
    15  	Meta
    16  }
    17  
    18  func (c *DeploymentResumeCommand) Help() string {
    19  	helpText := `
    20  Usage: nomad deployment resume [options] <deployment id>
    21  
    22    Resume is used to unpause a paused deployment. Resuming a deployment will
    23    resume the placement of new allocations as part of rolling deployment.
    24  
    25    When ACLs are enabled, this command requires a token with the 'submit-job'
    26    and 'read-job' capabilities for the deployment's namespace.
    27  
    28  General Options:
    29  
    30    ` + generalOptionsUsage(usageOptsDefault) + `
    31  
    32  Resume Options:
    33  
    34    -detach
    35      Return immediately instead of entering monitor mode. After deployment
    36      resume, the evaluation ID will be printed to the screen, which can be used
    37      to examine the evaluation using the eval-status command.
    38  
    39    -verbose
    40      Display full information.
    41  `
    42  	return strings.TrimSpace(helpText)
    43  }
    44  
    45  func (c *DeploymentResumeCommand) Synopsis() string {
    46  	return "Resume a paused deployment"
    47  }
    48  
    49  func (c *DeploymentResumeCommand) AutocompleteFlags() complete.Flags {
    50  	return mergeAutocompleteFlags(c.Meta.AutocompleteFlags(FlagSetClient),
    51  		complete.Flags{
    52  			"-detach":  complete.PredictNothing,
    53  			"-verbose": complete.PredictNothing,
    54  		})
    55  }
    56  
    57  func (c *DeploymentResumeCommand) AutocompleteArgs() complete.Predictor {
    58  	return complete.PredictFunc(func(a complete.Args) []string {
    59  		client, err := c.Meta.Client()
    60  		if err != nil {
    61  			return nil
    62  		}
    63  
    64  		resp, _, err := client.Search().PrefixSearch(a.Last, contexts.Deployments, nil)
    65  		if err != nil {
    66  			return []string{}
    67  		}
    68  		return resp.Matches[contexts.Deployments]
    69  	})
    70  }
    71  
    72  func (c *DeploymentResumeCommand) Name() string { return "deployment resume" }
    73  func (c *DeploymentResumeCommand) Run(args []string) int {
    74  	var detach, verbose bool
    75  
    76  	flags := c.Meta.FlagSet(c.Name(), FlagSetClient)
    77  	flags.Usage = func() { c.Ui.Output(c.Help()) }
    78  	flags.BoolVar(&detach, "detach", false, "")
    79  	flags.BoolVar(&verbose, "verbose", false, "")
    80  
    81  	if err := flags.Parse(args); err != nil {
    82  		return 1
    83  	}
    84  
    85  	// Check that we got exactly one argument
    86  	args = flags.Args()
    87  	if l := len(args); l != 1 {
    88  		c.Ui.Error("This command takes one argument: <deployment id>")
    89  		c.Ui.Error(commandErrorText(c))
    90  		return 1
    91  	}
    92  
    93  	dID := args[0]
    94  
    95  	// Truncate the id unless full length is requested
    96  	length := shortId
    97  	if verbose {
    98  		length = fullId
    99  	}
   100  
   101  	// Get the HTTP client
   102  	client, err := c.Meta.Client()
   103  	if err != nil {
   104  		c.Ui.Error(fmt.Sprintf("Error initializing client: %s", err))
   105  		return 1
   106  	}
   107  
   108  	// Do a prefix lookup
   109  	deploy, possible, err := getDeployment(client.Deployments(), dID)
   110  	if err != nil {
   111  		c.Ui.Error(fmt.Sprintf("Error retrieving deployment: %s", err))
   112  		return 1
   113  	}
   114  
   115  	if len(possible) != 0 {
   116  		c.Ui.Error(fmt.Sprintf("Prefix matched multiple deployments\n\n%s", formatDeployments(possible, length)))
   117  		return 1
   118  	}
   119  
   120  	u, _, err := client.Deployments().Pause(deploy.ID, false, nil)
   121  	if err != nil {
   122  		c.Ui.Error(fmt.Sprintf("Error resuming deployment: %s", err))
   123  		return 1
   124  	}
   125  
   126  	c.Ui.Output(fmt.Sprintf("Deployment %q resumed", deploy.ID))
   127  	evalCreated := u.EvalID != ""
   128  
   129  	// Nothing to do
   130  	if !evalCreated {
   131  		return 0
   132  	}
   133  
   134  	if detach {
   135  		c.Ui.Output("Evaluation ID: " + u.EvalID)
   136  		return 0
   137  	}
   138  
   139  	c.Ui.Output("")
   140  	mon := newMonitor(c.Ui, client, length)
   141  	return mon.monitor(u.EvalID)
   142  }