github.com/mhilton/juju-juju@v0.0.0-20150901100907-a94dd2c73455/cmd/juju/environment/retryprovisioning.go (about)

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