LEFT | RIGHT |
(no file at all) | |
1 package main | 1 package main |
2 | 2 |
3 import ( | 3 import ( |
4 "fmt" | 4 "fmt" |
5 "launchpad.net/gnuflag" | 5 "launchpad.net/gnuflag" |
6 "launchpad.net/juju-core/cmd" | 6 "launchpad.net/juju-core/cmd" |
7 "launchpad.net/juju-core/log" | 7 "launchpad.net/juju-core/log" |
8 "launchpad.net/juju-core/state" | 8 "launchpad.net/juju-core/state" |
9 "regexp" | 9 "regexp" |
10 "strings" | 10 "strings" |
11 ) | 11 ) |
12 | 12 |
13 // requiredError is useful when complaining about missing command-line options. | 13 // requiredError is useful when complaining about missing command-line options. |
14 func requiredError(name string) error { | 14 func requiredError(name string) error { |
15 return fmt.Errorf("--%s option must be set", name) | 15 return fmt.Errorf("--%s option must be set", name) |
16 } | 16 } |
17 | 17 |
18 // stateInfoValue implements gnuflag.Value on a state.Info. | 18 // stateInfoValue implements gnuflag.Value on a state.Info. |
19 type stateInfoValue state.Info | 19 type stateInfoValue state.Info |
20 | 20 |
21 var validAddr = regexp.MustCompile("^.+:[0-9]+$") | 21 var validAddr = regexp.MustCompile("^.+:[0-9]+$") |
22 | 22 |
23 // Set splits the comma-separated list of ZooKeeper addresses and stores | 23 // Set splits the comma-separated list of state server addresses and stores |
24 // onto v's Addrs. Addresses must include port numbers. | 24 // onto v's Addrs. Addresses must include port numbers. |
25 func (v *stateInfoValue) Set(value string) error { | 25 func (v *stateInfoValue) Set(value string) error { |
26 addrs := strings.Split(value, ",") | 26 addrs := strings.Split(value, ",") |
27 for _, addr := range addrs { | 27 for _, addr := range addrs { |
28 if !validAddr.MatchString(addr) { | 28 if !validAddr.MatchString(addr) { |
29 » » » return fmt.Errorf("%q is not a valid zookeeper address",
addr) | 29 » » » return fmt.Errorf("%q is not a valid state server addres
s", addr) |
30 } | 30 } |
31 } | 31 } |
32 v.Addrs = addrs | 32 v.Addrs = addrs |
33 return nil | 33 return nil |
34 } | 34 } |
35 | 35 |
36 // String returns the list of ZooKeeper addresses joined by commas. | 36 // String returns the list of server addresses joined by commas. |
37 func (v *stateInfoValue) String() string { | 37 func (v *stateInfoValue) String() string { |
38 if v.Addrs != nil { | 38 if v.Addrs != nil { |
39 return strings.Join(v.Addrs, ",") | 39 return strings.Join(v.Addrs, ",") |
40 } | 40 } |
41 return "" | 41 return "" |
42 } | 42 } |
43 | 43 |
44 // stateInfoVar sets up a gnuflag flag analagously to FlagSet.*Var methods. | 44 // stateInfoVar sets up a gnuflag flag analagously to FlagSet.*Var methods. |
45 func stateInfoVar(fs *gnuflag.FlagSet, target *state.Info, name string, value []
string, usage string) { | 45 func stateInfoVar(fs *gnuflag.FlagSet, target *state.Info, name string, value []
string, usage string) { |
46 target.Addrs = value | 46 target.Addrs = value |
47 fs.Var((*stateInfoValue)(target), name, usage) | 47 fs.Var((*stateInfoValue)(target), name, usage) |
48 } | 48 } |
49 | 49 |
50 // AgentConf handles command-line flags shared by all agents. | 50 // AgentConf handles command-line flags shared by all agents. |
51 type AgentConf struct { | 51 type AgentConf struct { |
52 DataDir string | 52 DataDir string |
53 StateInfo state.Info | 53 StateInfo state.Info |
54 } | 54 } |
55 | 55 |
56 // addFlags injects common agent flags into f. | 56 // addFlags injects common agent flags into f. |
57 func (c *AgentConf) addFlags(f *gnuflag.FlagSet) { | 57 func (c *AgentConf) addFlags(f *gnuflag.FlagSet) { |
58 f.StringVar(&c.DataDir, "data-dir", "/var/lib/juju", "directory for juju
data") | 58 f.StringVar(&c.DataDir, "data-dir", "/var/lib/juju", "directory for juju
data") |
59 » stateInfoVar(f, &c.StateInfo, "zookeeper-servers", nil, "zookeeper serve
rs to connect to") | 59 » stateInfoVar(f, &c.StateInfo, "state-servers", nil, "state servers to co
nnect to") |
60 } | 60 } |
61 | 61 |
62 // checkArgs checks that required flags have been set and that args is empty. | 62 // checkArgs checks that required flags have been set and that args is empty. |
63 func (c *AgentConf) checkArgs(args []string) error { | 63 func (c *AgentConf) checkArgs(args []string) error { |
64 if c.DataDir == "" { | 64 if c.DataDir == "" { |
65 return requiredError("data-dir") | 65 return requiredError("data-dir") |
66 } | 66 } |
67 if c.StateInfo.Addrs == nil { | 67 if c.StateInfo.Addrs == nil { |
68 » » return requiredError("zookeeper-servers") | 68 » » return requiredError("state-servers") |
69 } | 69 } |
70 return cmd.CheckEmpty(args) | 70 return cmd.CheckEmpty(args) |
71 } | 71 } |
72 | 72 |
73 type task interface { | 73 type task interface { |
74 Stop() error | 74 Stop() error |
75 Wait() error | 75 Wait() error |
76 String() string | 76 String() string |
77 } | 77 } |
78 | 78 |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
122 log.Printf("%s: %v", tasks[i], err) | 122 log.Printf("%s: %v", tasks[i], err) |
123 } | 123 } |
124 } | 124 } |
125 return chosen.err | 125 return chosen.err |
126 } | 126 } |
127 | 127 |
128 func isUpgraded(err error) bool { | 128 func isUpgraded(err error) bool { |
129 _, ok := err.(*UpgradedError) | 129 _, ok := err.(*UpgradedError) |
130 return ok | 130 return ok |
131 } | 131 } |
LEFT | RIGHT |