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