Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(1696)

Side by Side Diff: environs/dummy/environs.go

Issue 6408049: environs: turn environment configuration into a concrete type
Patch Set: Config 3000. Created 12 years, 8 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
OLDNEW
1 // The dummy provider implements an environment provider for testing 1 // The dummy provider implements an environment provider for testing
2 // purposes, registered with environs under the name "dummy". 2 // purposes, registered with environs under the name "dummy".
3 //· 3 //·
4 // The configuration YAML for the testing environment 4 // The configuration YAML for the testing environment
5 // must specify a "zookeeper" property with a boolean 5 // must specify a "zookeeper" property with a boolean
6 // value. If this is true, a zookeeper instance will be started 6 // value. If this is true, a zookeeper instance will be started
7 // the first time StateInfo is called on a newly reset environment. 7 // the first time StateInfo is called on a newly reset environment.
8 // NOTE: ZooKeeper isn't actually being started yet. 8 // NOTE: ZooKeeper isn't actually being started yet.
9 //· 9 //·
10 // The configuration data also accepts a "broken" property 10 // The configuration data also accepts a "broken" property
11 // of type boolean. If this is non-empty, any operation 11 // of type boolean. If this is non-empty, any operation
12 // after the environment has been opened will return 12 // after the environment has been opened will return
13 // the error "broken environment", and will also log that. 13 // the error "broken environment", and will also log that.
14 //· 14 //·
15 // The DNS name of instances is the same as the Id, 15 // The DNS name of instances is the same as the Id,
16 // with ".dns" appended. 16 // with ".dns" appended.
17 package dummy 17 package dummy
18 18
19 import ( 19 import (
20 "errors" 20 "errors"
21 "fmt" 21 "fmt"
22 "launchpad.net/juju-core/environs" 22 "launchpad.net/juju-core/environs"
23 "launchpad.net/juju-core/environs/config"
23 "launchpad.net/juju-core/log" 24 "launchpad.net/juju-core/log"
24 "launchpad.net/juju-core/schema" 25 "launchpad.net/juju-core/schema"
25 "launchpad.net/juju-core/state" 26 "launchpad.net/juju-core/state"
26 "launchpad.net/juju-core/testing" 27 "launchpad.net/juju-core/testing"
27 "net" 28 "net"
28 "net/http" 29 "net/http"
29 "sync" 30 "sync"
30 ) 31 )
31 32
32 // stateInfo returns a *state.Info which allows clients to connect to the 33 // stateInfo returns a *state.Info which allows clients to connect to the
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
115 // storage holds the storage for an environState. 116 // storage holds the storage for an environState.
116 // There are two instances for each environState 117 // There are two instances for each environState
117 // instance, one for public files and one for private. 118 // instance, one for public files and one for private.
118 type storage struct { 119 type storage struct {
119 path string // path prefix in http space. 120 path string // path prefix in http space.
120 state *environState 121 state *environState
121 files map[string][]byte 122 files map[string][]byte
122 } 123 }
123 124
124 type environConfig struct { 125 type environConfig struct {
125 » provider *environProvider 126 » *config.Config
126 » name string
127 zookeeper bool 127 zookeeper bool
128 broken bool 128 broken bool
fwereade 2012/07/17 07:19:06 My instinct warns me against the duplication here,
niemeyer 2012/07/17 12:10:30 I'm actually hoping to improve it further in the c
129 } 129 }
130 130
131 var providerInstance environProvider 131 var providerInstance environProvider
132 132
133 // discardOperations discards all Operations written to it. 133 // discardOperations discards all Operations written to it.
134 var discardOperations chan<- Operation 134 var discardOperations chan<- Operation
135 135
136 func init() { 136 func init() {
137 environs.RegisterProvider("dummy", &providerInstance) 137 environs.RegisterProvider("dummy", &providerInstance)
138 138
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
208 c = discardOperations 208 c = discardOperations
209 } 209 }
210 if p.ops != discardOperations { 210 if p.ops != discardOperations {
211 close(p.ops) 211 close(p.ops)
212 } 212 }
213 p.ops = c 213 p.ops = c
214 } 214 }
215 215
216 var checker = schema.StrictFieldMap( 216 var checker = schema.StrictFieldMap(
217 schema.Fields{ 217 schema.Fields{
218 "type": schema.Const("dummy"),
219 "zookeeper": schema.Bool(), 218 "zookeeper": schema.Bool(),
220 "broken": schema.Bool(), 219 "broken": schema.Bool(),
221 "name": schema.String(),
222 }, 220 },
223 []string{ 221 []string{
224 "broken", 222 "broken",
225 }, 223 },
226 ) 224 )
227 225
228 func (p *environProvider) NewConfig(attrs map[string]interface{}) (environs.Envi ronConfig, error) { 226 func newConfig(cfg *config.Config) (*environConfig, error) {
229 » m0, err := checker.Coerce(attrs, nil) 227 » m0, err := checker.Coerce(cfg.TypeMap(), nil)
230 if err != nil { 228 if err != nil {
231 return nil, err 229 return nil, err
232 } 230 }
233 m1 := m0.(schema.MapType) 231 m1 := m0.(schema.MapType)
234 » cfg := &environConfig{ 232 » ecfg := &environConfig{
235 » » provider: p, 233 » » Config: cfg,
236 » » name: m1["name"].(string),
237 zookeeper: m1["zookeeper"].(bool), 234 zookeeper: m1["zookeeper"].(bool),
238 } 235 }
239 » cfg.broken, _ = m1["broken"].(bool) 236 » ecfg.broken, _ = m1["broken"].(bool)
240 » return cfg, nil 237 » return ecfg, nil
241 } 238 }
242 239
243 func (cfg *environConfig) Open() (environs.Environ, error) { 240 func (p *environProvider) Open(cfg *config.Config) (environs.Environ, error) {
244 » p := cfg.provider
245 p.mu.Lock() 241 p.mu.Lock()
246 defer p.mu.Unlock() 242 defer p.mu.Unlock()
247 » state := p.state[cfg.name] 243 » name := cfg.Name()
244 » ecfg, err := newConfig(cfg)
245 » if err != nil {
246 » » return nil, err
247 » }
248 » state := p.state[name]
248 if state == nil { 249 if state == nil {
249 » » if cfg.zookeeper && len(p.state) != 0 { 250 » » if ecfg.zookeeper && len(p.state) != 0 {
250 panic("cannot share a zookeeper between two dummy enviro ns") 251 panic("cannot share a zookeeper between two dummy enviro ns")
251 } 252 }
252 » » state = newState(cfg.name, p.ops) 253 » » state = newState(name, p.ops)
253 » » p.state[cfg.name] = state 254 » » p.state[name] = state
254 } 255 }
255 env := &environ{ 256 env := &environ{
256 » » state: state, 257 » » state: state,
258 » » config: ecfg,
257 } 259 }
258 env.SetConfig(cfg)
259 return env, nil 260 return env, nil
260 } 261 }
261 262
262 var errBroken = errors.New("broken environment") 263 var errBroken = errors.New("broken environment")
263 264
264 func (e *environ) Name() string { 265 func (e *environ) Name() string {
265 return e.state.name 266 return e.state.name
266 } 267 }
267 268
268 func (e *environ) Bootstrap(uploadTools bool) error { 269 func (e *environ) Bootstrap(uploadTools bool) error {
(...skipping 11 matching lines...) Expand all
280 e.state.ops <- OpBootstrap{Env: e.state.name} 281 e.state.ops <- OpBootstrap{Env: e.state.name}
281 if e.state.bootstrapped { 282 if e.state.bootstrapped {
282 return fmt.Errorf("environment is already bootstrapped") 283 return fmt.Errorf("environment is already bootstrapped")
283 } 284 }
284 if e.config.zookeeper { 285 if e.config.zookeeper {
285 info := stateInfo() 286 info := stateInfo()
286 st, err := state.Initialize(info) 287 st, err := state.Initialize(info)
287 if err != nil { 288 if err != nil {
288 panic(err) 289 panic(err)
289 } 290 }
290 cfg, err := st.EnvironConfig() 291 cfg, err := st.EnvironConfig()
rog 2012/07/17 12:55:07 isn't EnvironConfig supposed to return a *config.C
niemeyer 2012/07/17 13:16:25 Please see the message on juju-dev regarding follo
291 if err != nil { 292 if err != nil {
292 panic(err) 293 panic(err)
293 } 294 }
294 cfg.Set("type", "dummy") 295 cfg.Set("type", "dummy")
295 cfg.Set("zookeeper", true) 296 cfg.Set("zookeeper", true)
296 » » cfg.Set("name", e.config.name) 297 » » cfg.Set("name", e.config.Name())
297 _, err = cfg.Write() 298 _, err = cfg.Write()
298 if err != nil { 299 if err != nil {
299 panic(err) 300 panic(err)
300 } 301 }
301 err = st.Close() 302 err = st.Close()
302 if err != nil { 303 if err != nil {
303 panic(err) 304 panic(err)
304 } 305 }
305 } 306 }
306 e.state.bootstrapped = true 307 e.state.bootstrapped = true
(...skipping 10 matching lines...) Expand all
317 if !e.state.bootstrapped { 318 if !e.state.bootstrapped {
318 return nil, errors.New("dummy environment not bootstrapped") 319 return nil, errors.New("dummy environment not bootstrapped")
319 } 320 }
320 return stateInfo(), nil 321 return stateInfo(), nil
321 } 322 }
322 323
323 func (e *environ) AssignmentPolicy() state.AssignmentPolicy { 324 func (e *environ) AssignmentPolicy() state.AssignmentPolicy {
324 return state.AssignUnused 325 return state.AssignUnused
325 } 326 }
326 327
327 func (e *environ) SetConfig(cfg environs.EnvironConfig) { 328 func (e *environ) SetConfig(cfg *config.Config) error {
328 » config := cfg.(*environConfig) 329 » config, err := newConfig(cfg)
330 » if err != nil {
331 » » return err
332 » }
329 e.configMutex.Lock() 333 e.configMutex.Lock()
330 defer e.configMutex.Unlock() 334 defer e.configMutex.Unlock()
331 e.config = config 335 e.config = config
336 return nil
332 } 337 }
333 338
334 func (e *environ) Destroy([]environs.Instance) error { 339 func (e *environ) Destroy([]environs.Instance) error {
335 if e.isBroken() { 340 if e.isBroken() {
336 return errBroken 341 return errBroken
337 } 342 }
338 e.state.mu.Lock() 343 e.state.mu.Lock()
339 defer e.state.mu.Unlock() 344 defer e.state.mu.Unlock()
340 e.state.ops <- OpDestroy{Env: e.state.name} 345 e.state.ops <- OpDestroy{Env: e.state.name}
341 if testing.ZkAddr != "" { 346 if testing.ZkAddr != "" {
(...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after
488 if inst.machineId != machineId { 493 if inst.machineId != machineId {
489 panic(fmt.Errorf("Ports with mismatched machine id, expected %d got %d", inst.machineId, machineId)) 494 panic(fmt.Errorf("Ports with mismatched machine id, expected %d got %d", inst.machineId, machineId))
490 } 495 }
491 inst.state.mu.Lock() 496 inst.state.mu.Lock()
492 defer inst.state.mu.Unlock() 497 defer inst.state.mu.Unlock()
493 for p := range inst.ports { 498 for p := range inst.ports {
494 ports = append(ports, p) 499 ports = append(ports, p)
495 } 500 }
496 return 501 return
497 } 502 }
OLDNEW

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld f62528b