OLD | NEW |
1 // Copyright 2014 Canonical Ltd. | 1 // Copyright 2014 Canonical Ltd. |
2 // Licensed under the AGPLv3, see LICENCE file for details. | 2 // Licensed under the AGPLv3, see LICENCE file for details. |
3 | 3 |
4 package main | 4 package main |
5 | 5 |
6 import ( | 6 import ( |
| 7 "bytes" |
7 "fmt" | 8 "fmt" |
| 9 "strings" |
8 | 10 |
9 "launchpad.net/gnuflag" | 11 "launchpad.net/gnuflag" |
10 | 12 |
11 "launchpad.net/juju-core/cmd" | 13 "launchpad.net/juju-core/cmd" |
12 "launchpad.net/juju-core/cmd/envcmd" | 14 "launchpad.net/juju-core/cmd/envcmd" |
13 "launchpad.net/juju-core/constraints" | 15 "launchpad.net/juju-core/constraints" |
14 "launchpad.net/juju-core/juju" | 16 "launchpad.net/juju-core/juju" |
15 ) | 17 ) |
16 | 18 |
17 type EnsureAvailabilityCommand struct { | 19 type EnsureAvailabilityCommand struct { |
18 envcmd.EnvCommandBase | 20 envcmd.EnvCommandBase |
| 21 out cmd.Output |
| 22 |
19 NumStateServers int | 23 NumStateServers int |
20 // If specified, use this series for newly created machines, | 24 // If specified, use this series for newly created machines, |
21 // else use the environment's default-series | 25 // else use the environment's default-series |
22 Series string | 26 Series string |
23 // If specified, these constraints will be merged with those | 27 // If specified, these constraints will be merged with those |
24 // already in the environment when creating new machines. | 28 // already in the environment when creating new machines. |
25 Constraints constraints.Value | 29 Constraints constraints.Value |
26 } | 30 } |
27 | 31 |
28 const ensureAvailabilityDoc = ` | 32 const ensureAvailabilityDoc = ` |
(...skipping 11 matching lines...) Expand all Loading... |
40 then that number will be ensured. | 44 then that number will be ensured. |
41 juju ensure-availability -n 5 --series=trusty | 45 juju ensure-availability -n 5 --series=trusty |
42 Ensure that 5 state servers are available, with newly created | 46 Ensure that 5 state servers are available, with newly created |
43 state server machines having the "trusty" series. | 47 state server machines having the "trusty" series. |
44 juju ensure-availability -n 7 --constraints mem=8G | 48 juju ensure-availability -n 7 --constraints mem=8G |
45 Ensure that 7 state servers are available, with newly created | 49 Ensure that 7 state servers are available, with newly created |
46 state server machines having the default series, and at least | 50 state server machines having the default series, and at least |
47 8GB RAM. | 51 8GB RAM. |
48 ` | 52 ` |
49 | 53 |
| 54 // FormatYaml marshals value to a yaml-formatted []byte, unless value is nil. |
| 55 func formatSimple(value interface{}) ([]byte, error) { |
| 56 ensureAvailabilityResult, ok := value.(formattedResult) |
| 57 if !ok { |
| 58 return nil, nil |
| 59 } |
| 60 |
| 61 buff := &bytes.Buffer{} |
| 62 |
| 63 if len(ensureAvailabilityResult.Maintained) > 0 { |
| 64 _, err := fmt.Fprintf(buff, "maintaining machines: %s\n", machin
eList(ensureAvailabilityResult.Maintained)) |
| 65 if err != nil { |
| 66 return nil, err |
| 67 } |
| 68 } |
| 69 if len(ensureAvailabilityResult.Added) > 0 { |
| 70 _, err := fmt.Fprintf(buff, "adding machines: %s\n", machineList
(ensureAvailabilityResult.Added)) |
| 71 if err != nil { |
| 72 return nil, err |
| 73 } |
| 74 } |
| 75 if len(ensureAvailabilityResult.Removed) > 0 { |
| 76 _, err := fmt.Fprintf(buff, "removing machines: %s\n", machineLi
st(ensureAvailabilityResult.Removed)) |
| 77 if err != nil { |
| 78 return nil, err |
| 79 } |
| 80 } |
| 81 return buff.Bytes(), nil |
| 82 } |
| 83 |
50 func (c *EnsureAvailabilityCommand) Info() *cmd.Info { | 84 func (c *EnsureAvailabilityCommand) Info() *cmd.Info { |
51 return &cmd.Info{ | 85 return &cmd.Info{ |
52 Name: "ensure-availability", | 86 Name: "ensure-availability", |
53 Purpose: "ensure the availability of Juju state servers", | 87 Purpose: "ensure the availability of Juju state servers", |
54 Doc: ensureAvailabilityDoc, | 88 Doc: ensureAvailabilityDoc, |
55 } | 89 } |
56 } | 90 } |
57 | 91 |
58 func (c *EnsureAvailabilityCommand) SetFlags(f *gnuflag.FlagSet) { | 92 func (c *EnsureAvailabilityCommand) SetFlags(f *gnuflag.FlagSet) { |
59 f.IntVar(&c.NumStateServers, "n", 0, "number of state servers to make av
ailable") | 93 f.IntVar(&c.NumStateServers, "n", 0, "number of state servers to make av
ailable") |
60 f.StringVar(&c.Series, "series", "", "the charm series") | 94 f.StringVar(&c.Series, "series", "", "the charm series") |
61 f.Var(constraints.ConstraintsValue{&c.Constraints}, "constraints", "addi
tional machine constraints") | 95 f.Var(constraints.ConstraintsValue{&c.Constraints}, "constraints", "addi
tional machine constraints") |
| 96 c.out.AddFlags(f, "simple", map[string]cmd.Formatter{ |
| 97 "yaml": cmd.FormatYaml, |
| 98 "json": cmd.FormatJson, |
| 99 "simple": formatSimple, |
| 100 }) |
| 101 |
62 } | 102 } |
63 | 103 |
64 func (c *EnsureAvailabilityCommand) Init(args []string) error { | 104 func (c *EnsureAvailabilityCommand) Init(args []string) error { |
65 if c.NumStateServers < 0 || (c.NumStateServers%2 != 1 && c.NumStateServe
rs != 0) { | 105 if c.NumStateServers < 0 || (c.NumStateServers%2 != 1 && c.NumStateServe
rs != 0) { |
66 return fmt.Errorf("must specify a number of state servers odd an
d non-negative") | 106 return fmt.Errorf("must specify a number of state servers odd an
d non-negative") |
67 } | 107 } |
68 return cmd.CheckEmpty(args) | 108 return cmd.CheckEmpty(args) |
69 } | 109 } |
70 | 110 |
| 111 type formattedResult struct { |
| 112 Maintained []string `json:"maintained,omitempty" yaml:",flow,omitempty"` |
| 113 Removed []string `json:"removed,omitempty" yaml:",flow,omitempty"` |
| 114 Added []string `json:"added,omitempty" yaml:",flow,omitempty"` |
| 115 } |
| 116 |
71 // Run connects to the environment specified on the command line | 117 // Run connects to the environment specified on the command line |
72 // and calls EnsureAvailability. | 118 // and calls EnsureAvailability. |
73 func (c *EnsureAvailabilityCommand) Run(_ *cmd.Context) error { | 119 func (c *EnsureAvailabilityCommand) Run(ctx *cmd.Context) error { |
74 client, err := juju.NewAPIClientFromName(c.EnvName) | 120 client, err := juju.NewAPIClientFromName(c.EnvName) |
75 if err != nil { | 121 if err != nil { |
76 return err | 122 return err |
77 } | 123 } |
78 defer client.Close() | 124 defer client.Close() |
79 » return client.EnsureAvailability(c.NumStateServers, c.Constraints, c.Ser
ies) | 125 » ensureAvailabilityResult, err := client.EnsureAvailability(c.NumStateSer
vers, c.Constraints, c.Series) |
| 126 » if err != nil { |
| 127 » » return err |
| 128 » } |
| 129 |
| 130 » result := formattedResult{ |
| 131 » » Added: ensureAvailabilityResult.Added, |
| 132 » » Removed: ensureAvailabilityResult.Removed, |
| 133 » » Maintained: ensureAvailabilityResult.Maintained, |
| 134 » } |
| 135 » return c.out.Write(ctx, result) |
80 } | 136 } |
| 137 |
| 138 // Format machine list for presentation. |
| 139 func machineList(machines []string) string { |
| 140 quoted := []string{} |
| 141 for _, machineId := range machines { |
| 142 quoted = append(quoted, fmt.Sprintf("%q", machineId)) |
| 143 } |
| 144 return strings.Join(quoted, ", ") |
| 145 } |
OLD | NEW |