OLD | NEW |
1 package environs | 1 package environs |
2 | 2 |
3 import ( | 3 import ( |
4 "fmt" | 4 "fmt" |
| 5 "launchpad.net/juju-core/constraints" |
5 "launchpad.net/juju-core/environs/tools" | 6 "launchpad.net/juju-core/environs/tools" |
6 "launchpad.net/juju-core/log" | 7 "launchpad.net/juju-core/log" |
7 "launchpad.net/juju-core/state" | 8 "launchpad.net/juju-core/state" |
8 "launchpad.net/juju-core/version" | 9 "launchpad.net/juju-core/version" |
9 ) | 10 ) |
10 | 11 |
11 // ToolsList holds a list of available tools. Private tools take | 12 // ToolsList holds a list of available tools. Private tools take |
12 // precedence over public tools, even if they have a lower | 13 // precedence over public tools, even if they have a lower |
13 // version number. | 14 // version number. |
14 type ToolsList struct { | 15 type ToolsList struct { |
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
121 // returns a *NotFoundError. | 122 // returns a *NotFoundError. |
122 func FindAvailableTools(environ Environ, majorVersion int) (list tools.List, err
error) { | 123 func FindAvailableTools(environ Environ, majorVersion int) (list tools.List, err
error) { |
123 defer convertToolsError(&err) | 124 defer convertToolsError(&err) |
124 list, err = tools.ReadList(environ.Storage(), majorVersion) | 125 list, err = tools.ReadList(environ.Storage(), majorVersion) |
125 if err == tools.ErrNoTools { | 126 if err == tools.ErrNoTools { |
126 list, err = tools.ReadList(environ.PublicStorage(), majorVersion
) | 127 list, err = tools.ReadList(environ.PublicStorage(), majorVersion
) |
127 } | 128 } |
128 return list, err | 129 return list, err |
129 } | 130 } |
130 | 131 |
| 132 // EnsureAgentVersion returns a ToolsList containing only those tools with |
| 133 // which it would be reasonable to launch an environment's first machine, |
| 134 // given the supplied constraints. |
| 135 // If the environment was not already configured to use a specific agent |
| 136 // version, the newest available version will be chosen and set in the |
| 137 // environment's configuration. |
| 138 func EnsureAgentVersion(environ Environ, cons constraints.Value) (list tools.Lis
t, err error) { |
| 139 defer convertToolsError(&err) |
| 140 // Collect all possible compatible tools. |
| 141 cliVersion := version.Current.Number |
| 142 if list, err = FindAvailableTools(environ, cliVersion.Major); err != nil
{ |
| 143 return nil, err |
| 144 } |
| 145 |
| 146 // Discard all that are known to be irrelevant. |
| 147 cfg := environ.Config() |
| 148 filter := tools.Filter{Series: cfg.DefaultSeries()} |
| 149 if cons.Arch != nil && *cons.Arch != "" { |
| 150 filter.Arch = *cons.Arch |
| 151 } |
| 152 if agentVersion, ok := cfg.AgentVersion(); ok { |
| 153 // If we already have an explicit agent version set, we're done. |
| 154 filter.Number = agentVersion |
| 155 return list.Match(filter) |
| 156 } |
| 157 filter.Released = !cliVersion.IsDev() && !cfg.Development() |
| 158 if list, err = list.Match(filter); err != nil { |
| 159 return nil, err |
| 160 } |
| 161 |
| 162 // We probably still have a mix of versions available; discard older one
s |
| 163 // and update environment configuration to use only those remaining. |
| 164 vers, list := list.Newest() |
| 165 cfg, err = cfg.Apply(map[string]interface{}{ |
| 166 "agent-version": vers.String(), |
| 167 }) |
| 168 if err == nil { |
| 169 err = environ.SetConfig(cfg) |
| 170 } |
| 171 if err != nil { |
| 172 return nil, fmt.Errorf("failed to update environment configurati
on: %v", err) |
| 173 } |
| 174 return list, nil |
| 175 } |
| 176 |
| 177 // FindInstanceTools returns a ToolsList containing only those tools with which |
| 178 // it would be reasonable to start a new instance, given the supplied series and |
| 179 // constraints. |
| 180 // It is an error to call it with an environment not already configured to use |
| 181 // a specific agent version. |
| 182 func FindInstanceTools(environ Environ, series string, cons constraints.Value) (
list tools.List, err error) { |
| 183 defer convertToolsError(&err) |
| 184 // Collect all possible compatible tools. |
| 185 agentVersion, ok := environ.Config().AgentVersion() |
| 186 if !ok { |
| 187 return nil, fmt.Errorf("no agent version set in environment conf
iguration") |
| 188 } |
| 189 if list, err = FindAvailableTools(environ, agentVersion.Major); err != n
il { |
| 190 return nil, err |
| 191 } |
| 192 |
| 193 // Discard all that are known to be irrelevant. |
| 194 filter := tools.Filter{ |
| 195 Number: agentVersion, |
| 196 Series: series, |
| 197 } |
| 198 if cons.Arch != nil && *cons.Arch != "" { |
| 199 filter.Arch = *cons.Arch |
| 200 } |
| 201 return list.Match(filter) |
| 202 } |
| 203 |
131 // FindExactTools returns only the tools that match the supplied version. | 204 // FindExactTools returns only the tools that match the supplied version. |
132 // TODO(fwereade) this should not exist: it's used by cmd/jujud/Upgrader, | 205 // TODO(fwereade) this should not exist: it's used by cmd/jujud/Upgrader, |
133 // which needs to run on every agent and must absolutely *not* in general | 206 // which needs to run on every agent and must absolutely *not* in general |
134 // have access to an Environ. | 207 // have access to an Environ. |
135 func FindExactTools(environ Environ, vers version.Binary) (t *state.Tools, err e
rror) { | 208 func FindExactTools(environ Environ, vers version.Binary) (t *state.Tools, err e
rror) { |
136 defer convertToolsError(&err) | 209 defer convertToolsError(&err) |
137 list, err := FindAvailableTools(environ, vers.Major) | 210 list, err := FindAvailableTools(environ, vers.Major) |
138 if err != nil { | 211 if err != nil { |
139 return nil, err | 212 return nil, err |
140 } | 213 } |
(...skipping 14 matching lines...) Expand all Loading... |
155 return true | 228 return true |
156 } | 229 } |
157 return false | 230 return false |
158 } | 231 } |
159 | 232 |
160 func convertToolsError(err *error) { | 233 func convertToolsError(err *error) { |
161 if isToolsError(*err) { | 234 if isToolsError(*err) { |
162 *err = &NotFoundError{*err} | 235 *err = &NotFoundError{*err} |
163 } | 236 } |
164 } | 237 } |
OLD | NEW |