github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/cmd/juju/model/retryprovisioning.go (about)

     1  // Copyright 2014, 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package model
     5  
     6  import (
     7  	"fmt"
     8  
     9  	"github.com/juju/cmd"
    10  	"github.com/juju/errors"
    11  	"gopkg.in/juju/names.v2"
    12  
    13  	"github.com/juju/juju/apiserver/params"
    14  	jujucmd "github.com/juju/juju/cmd"
    15  	"github.com/juju/juju/cmd/juju/block"
    16  	"github.com/juju/juju/cmd/modelcmd"
    17  )
    18  
    19  func NewRetryProvisioningCommand() cmd.Command {
    20  	return modelcmd.Wrap(&retryProvisioningCommand{})
    21  }
    22  
    23  // retryProvisioningCommand updates machines' error status to tell
    24  // the provisoner that it should try to re-provision the machine.
    25  type retryProvisioningCommand struct {
    26  	modelcmd.ModelCommandBase
    27  	modelcmd.IAASOnlyCommand
    28  	Machines []names.MachineTag
    29  	api      RetryProvisioningAPI
    30  }
    31  
    32  // RetryProvisioningAPI defines methods on the client API
    33  // that the retry-provisioning command calls.
    34  type RetryProvisioningAPI interface {
    35  	Close() error
    36  	RetryProvisioning(machines ...names.MachineTag) ([]params.ErrorResult, error)
    37  }
    38  
    39  func (c *retryProvisioningCommand) Info() *cmd.Info {
    40  	return jujucmd.Info(&cmd.Info{
    41  		Name:    "retry-provisioning",
    42  		Args:    "<machine> [...]",
    43  		Purpose: "Retries provisioning for failed machines.",
    44  	})
    45  }
    46  
    47  func (c *retryProvisioningCommand) Init(args []string) error {
    48  	if len(args) == 0 {
    49  		return errors.Errorf("no machine specified")
    50  	}
    51  	c.Machines = make([]names.MachineTag, len(args))
    52  	for i, arg := range args {
    53  		if !names.IsValidMachine(arg) {
    54  			return errors.Errorf("invalid machine %q", arg)
    55  		}
    56  		if names.IsContainerMachine(arg) {
    57  			return errors.Errorf("invalid machine %q retry-provisioning does not support containers", arg)
    58  		}
    59  		c.Machines[i] = names.NewMachineTag(arg)
    60  	}
    61  	return nil
    62  }
    63  
    64  func (c *retryProvisioningCommand) getAPI() (RetryProvisioningAPI, error) {
    65  	if c.api != nil {
    66  		return c.api, nil
    67  	}
    68  	return c.NewAPIClient()
    69  }
    70  
    71  func (c *retryProvisioningCommand) Run(context *cmd.Context) error {
    72  	client, err := c.getAPI()
    73  	if err != nil {
    74  		return err
    75  	}
    76  	defer client.Close()
    77  
    78  	results, err := client.RetryProvisioning(c.Machines...)
    79  	if err != nil {
    80  		return block.ProcessBlockedError(err, block.BlockChange)
    81  	}
    82  	for _, result := range results {
    83  		if result.Error != nil {
    84  			fmt.Fprintf(context.Stderr, "%v\n", result.Error)
    85  		}
    86  	}
    87  	return nil
    88  }