Left: | ||
Right: |
OLD | NEW |
---|---|
1 // Copyright 2013 Canonical Ltd. | 1 // Copyright 2013 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 client | 4 package client |
5 | 5 |
6 import ( | 6 import ( |
7 "fmt" | 7 "fmt" |
8 "net/url" | 8 "net/url" |
9 "os" | 9 "os" |
10 "sort" | |
10 "strings" | 11 "strings" |
11 | 12 |
12 "github.com/juju/errors" | 13 "github.com/juju/errors" |
13 "github.com/juju/loggo" | 14 "github.com/juju/loggo" |
14 | 15 |
15 "launchpad.net/juju-core/charm" | 16 "launchpad.net/juju-core/charm" |
16 "launchpad.net/juju-core/environs" | 17 "launchpad.net/juju-core/environs" |
17 "launchpad.net/juju-core/environs/config" | 18 "launchpad.net/juju-core/environs/config" |
18 "launchpad.net/juju-core/environs/manual" | 19 "launchpad.net/juju-core/environs/manual" |
19 envtools "launchpad.net/juju-core/environs/tools" | 20 envtools "launchpad.net/juju-core/environs/tools" |
(...skipping 1043 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1063 } | 1064 } |
1064 | 1065 |
1065 // APIHostPorts returns the API host/port addresses stored in state. | 1066 // APIHostPorts returns the API host/port addresses stored in state. |
1066 func (c *Client) APIHostPorts() (result params.APIHostPortsResult, err error) { | 1067 func (c *Client) APIHostPorts() (result params.APIHostPortsResult, err error) { |
1067 if result.Servers, err = c.api.state.APIHostPorts(); err != nil { | 1068 if result.Servers, err = c.api.state.APIHostPorts(); err != nil { |
1068 return params.APIHostPortsResult{}, err | 1069 return params.APIHostPortsResult{}, err |
1069 } | 1070 } |
1070 return result, nil | 1071 return result, nil |
1071 } | 1072 } |
1072 | 1073 |
1074 // sortable slice of machine ids | |
1075 type machineIdSlice []string | |
1076 | |
1077 func (ms machineIdSlice) Len() int { return len(ms) } | |
1078 func (ms machineIdSlice) Swap(i, j int) { ms[i], ms[j] = ms[j], ms[i] } | |
1079 func (ms machineIdSlice) Less(i, j int) bool { | |
1080 return state.MachineIdLessThan(ms[i], ms[j]) | |
1081 } | |
1082 | |
1083 // Generate an EnsureAvailabilityResult structure from before and after | |
1084 // StateServerInfo. | |
1085 func ensureAvailabilityResultFromSSI(SSIBefore, SSIAfter *state.StateServerInfo) params.EnsureAvailabilityResult { | |
fwereade
2014/06/02 09:50:03
it's jarring to have vars with uppercase at the be
| |
1086 result := params.EnsureAvailabilityResult{} | |
1087 | |
1088 before := SSIBefore.MachineIds | |
1089 after := SSIAfter.MachineIds | |
1090 | |
1091 sort.Sort(machineIdSlice(before)) | |
1092 sort.Sort(machineIdSlice(after)) | |
1093 | |
1094 lnB := len(before) | |
1095 lnA := len(after) | |
1096 | |
1097 i := 0 | |
1098 j := 0 | |
1099 for i < lnB && j < lnA { | |
1100 if before[i] == after[j] { | |
1101 result.Maintained = append( | |
1102 result.Maintained, | |
1103 before[i]) | |
1104 i++ | |
1105 j++ | |
1106 } else if state.MachineIdLessThan(before[i], after[j]) { | |
1107 result.Removed = append( | |
1108 result.Removed, | |
1109 before[i]) | |
1110 i++ | |
1111 } else if state.MachineIdLessThan(after[j], before[i]) { | |
1112 result.Added = append( | |
1113 result.Added, | |
1114 after[j]) | |
1115 j++ | |
1116 } | |
1117 } | |
1118 if i < lnB { | |
1119 result.Removed = append( | |
1120 result.Removed, | |
1121 before[i:]...) | |
1122 } | |
1123 if j < lnA { | |
1124 result.Added = append( | |
1125 result.Added, | |
1126 after[j:]...) | |
1127 } | |
1128 return result | |
fwereade
2014/06/02 09:50:03
I feel we could probably do this more easily with
| |
1129 } | |
1130 | |
1073 // EnsureAvailability ensures the availability of Juju state servers. | 1131 // EnsureAvailability ensures the availability of Juju state servers. |
1074 func (c *Client) EnsureAvailability(args params.EnsureAvailability) error { | 1132 func (c *Client) EnsureAvailability(args params.EnsureAvailability) (params.Ensu reAvailabilityResult, error) { |
fwereade
2014/06/02 09:50:03
Grar. Nate, have we yet released a stable with thi
| |
1075 series := args.Series | 1133 series := args.Series |
1134 ssiBefore, err := c.api.state.StateServerInfo() | |
1135 if err != nil { | |
1136 return params.EnsureAvailabilityResult{}, err | |
1137 } | |
1138 | |
1076 if series == "" { | 1139 if series == "" { |
1077 ssi, err := c.api.state.StateServerInfo() | |
1078 if err != nil { | |
1079 return err | |
1080 } | |
1081 // We should always have at least one voting machine | 1140 // We should always have at least one voting machine |
1082 // If we *really* wanted we could just pick whatever series is | 1141 // If we *really* wanted we could just pick whatever series is |
1083 // in the majority, but really, if we always copy the value of | 1142 // in the majority, but really, if we always copy the value of |
1084 // the first one, then they'll stay in sync. | 1143 // the first one, then they'll stay in sync. |
1085 » » if len(ssi.VotingMachineIds) == 0 { | 1144 » » if len(ssiBefore.VotingMachineIds) == 0 { |
1086 // Better than a panic()? | 1145 // Better than a panic()? |
1087 » » » return fmt.Errorf("internal error, failed to find any vo ting machines") | 1146 » » » return params.EnsureAvailabilityResult{}, fmt.Errorf("in ternal error, failed to find any voting machines") |
1088 } | 1147 } |
1089 » » templateMachine, err := c.api.state.Machine(ssi.VotingMachineIds [0]) | 1148 » » templateMachine, err := c.api.state.Machine(ssiBefore.VotingMach ineIds[0]) |
1090 if err != nil { | 1149 if err != nil { |
1091 » » » return err | 1150 » » » return params.EnsureAvailabilityResult{}, err |
1092 } | 1151 } |
1093 series = templateMachine.Series() | 1152 series = templateMachine.Series() |
1094 } | 1153 } |
1095 » return c.api.state.EnsureAvailability(args.NumStateServers, args.Constra ints, series) | 1154 » err = c.api.state.EnsureAvailability(args.NumStateServers, args.Constrai nts, series) |
1155 » if err != nil { | |
1156 » » return params.EnsureAvailabilityResult{}, err | |
1157 » } | |
1158 | |
1159 » ssiAfter, err := c.api.state.StateServerInfo() | |
1160 » if err != nil { | |
1161 » » return params.EnsureAvailabilityResult{}, err | |
1162 » } | |
1163 » return ensureAvailabilityResultFromSSI(ssiBefore, ssiAfter), nil | |
1096 } | 1164 } |
OLD | NEW |