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