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