github.com/rogpeppe/juju@v0.0.0-20140613142852-6337964b789e/cmd/juju/ensureavailability.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/cmd"
    10  	"launchpad.net/gnuflag"
    11  
    12  	"github.com/juju/juju/cmd/envcmd"
    13  	"github.com/juju/juju/constraints"
    14  	"github.com/juju/juju/juju"
    15  )
    16  
    17  type EnsureAvailabilityCommand struct {
    18  	envcmd.EnvCommandBase
    19  	NumStateServers int
    20  	// If specified, use this series for newly created machines,
    21  	// else use the environment's default-series
    22  	Series string
    23  	// If specified, these constraints will be merged with those
    24  	// already in the environment when creating new machines.
    25  	Constraints constraints.Value
    26  }
    27  
    28  const ensureAvailabilityDoc = `
    29  To ensure availability of deployed services, the Juju infrastructure
    30  must itself be highly available.  Ensure-availability must be called
    31  to ensure that the specified number of state servers are made available.
    32  
    33  An odd number of state servers is required.
    34  
    35  Examples:
    36   juju ensure-availability
    37       Ensure that the system is still in highly available mode. If
    38       there is only 1 state server running, this will ensure there
    39       are 3 running. If you have previously requested more than 3,
    40       then that number will be ensured.
    41   juju ensure-availability -n 5 --series=trusty
    42       Ensure that 5 state servers are available, with newly created
    43       state server machines having the "trusty" series.
    44   juju ensure-availability -n 7 --constraints mem=8G
    45       Ensure that 7 state servers are available, with newly created
    46       state server machines having the default series, and at least
    47       8GB RAM.
    48  `
    49  
    50  func (c *EnsureAvailabilityCommand) Info() *cmd.Info {
    51  	return &cmd.Info{
    52  		Name:    "ensure-availability",
    53  		Purpose: "ensure the availability of Juju state servers",
    54  		Doc:     ensureAvailabilityDoc,
    55  	}
    56  }
    57  
    58  func (c *EnsureAvailabilityCommand) SetFlags(f *gnuflag.FlagSet) {
    59  	f.IntVar(&c.NumStateServers, "n", 0, "number of state servers to make available")
    60  	f.StringVar(&c.Series, "series", "", "the charm series")
    61  	f.Var(constraints.ConstraintsValue{&c.Constraints}, "constraints", "additional machine constraints")
    62  }
    63  
    64  func (c *EnsureAvailabilityCommand) Init(args []string) error {
    65  	if c.NumStateServers < 0 || (c.NumStateServers%2 != 1 && c.NumStateServers != 0) {
    66  		return fmt.Errorf("must specify a number of state servers odd and non-negative")
    67  	}
    68  	return cmd.CheckEmpty(args)
    69  }
    70  
    71  // Run connects to the environment specified on the command line
    72  // and calls EnsureAvailability.
    73  func (c *EnsureAvailabilityCommand) Run(_ *cmd.Context) error {
    74  	client, err := juju.NewAPIClientFromName(c.EnvName)
    75  	if err != nil {
    76  		return err
    77  	}
    78  	defer client.Close()
    79  	return client.EnsureAvailability(c.NumStateServers, c.Constraints, c.Series)
    80  }