github.com/mattyw/juju@v0.0.0-20140610034352-732aecd63861/cmd/juju/retryprovisioning.go (about)

     1  // Copyright 2014 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package main
     5  
     6  import (
     7  	"fmt"
     8  
     9  	"github.com/juju/names"
    10  
    11  	"github.com/juju/juju/cmd"
    12  	"github.com/juju/juju/cmd/envcmd"
    13  	"github.com/juju/juju/juju"
    14  )
    15  
    16  // RetryProvisioningCommand updates machines' error status to tell
    17  // the provisoner that it should try to re-provision the machine.
    18  type RetryProvisioningCommand struct {
    19  	envcmd.EnvCommandBase
    20  	Machines []string
    21  }
    22  
    23  func (c *RetryProvisioningCommand) Info() *cmd.Info {
    24  	return &cmd.Info{
    25  		Name:    "retry-provisioning",
    26  		Args:    "<machine> [...]",
    27  		Purpose: "retries provisioning for failed machines",
    28  	}
    29  }
    30  
    31  func (c *RetryProvisioningCommand) Init(args []string) error {
    32  	if len(args) == 0 {
    33  		return fmt.Errorf("no machine specified")
    34  	}
    35  	c.Machines = make([]string, len(args))
    36  	for i, arg := range args {
    37  		if !names.IsMachine(arg) {
    38  			return fmt.Errorf("invalid machine %q", arg)
    39  		}
    40  		c.Machines[i] = names.MachineTag(arg)
    41  	}
    42  	return nil
    43  }
    44  
    45  func (c *RetryProvisioningCommand) Run(context *cmd.Context) error {
    46  	client, err := juju.NewAPIClientFromName(c.EnvName)
    47  	if err != nil {
    48  		return err
    49  	}
    50  	defer client.Close()
    51  	results, err := client.RetryProvisioning(c.Machines...)
    52  	if err != nil {
    53  		return err
    54  	}
    55  	for i, result := range results {
    56  		if result.Error != nil {
    57  			fmt.Fprintf(context.Stderr, "cannot retry provisioning %q: %v\n", c.Machines[i], result.Error)
    58  		}
    59  	}
    60  	return nil
    61  }