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

Side by Side Diff: provider/common/bootstrap.go

Issue 14433058: Providers are responsible for selecting tools
Patch Set: Providers are responsible for selecting tools Created 11 years, 4 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
« no previous file with comments | « provider/azure/environ_test.go ('k') | provider/common/bootstrap_test.go » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 common 4 package common
5 5
6 import ( 6 import (
7 "fmt" 7 "fmt"
8 8
9 "launchpad.net/loggo" 9 "launchpad.net/loggo"
10 10
11 "launchpad.net/juju-core/constraints" 11 "launchpad.net/juju-core/constraints"
12 "launchpad.net/juju-core/environs" 12 "launchpad.net/juju-core/environs"
13 "launchpad.net/juju-core/environs/bootstrap"
13 "launchpad.net/juju-core/instance" 14 "launchpad.net/juju-core/instance"
14 coretools "launchpad.net/juju-core/tools" 15 coretools "launchpad.net/juju-core/tools"
15 ) 16 )
16 17
17 var logger = loggo.GetLogger("juju.provider.common") 18 var logger = loggo.GetLogger("juju.provider.common")
18 19
19 // Bootstrap is a common implementation of the Bootstrap method defined on 20 // Bootstrap is a common implementation of the Bootstrap method defined on
20 // environs.Environ; we strongly recommend that this implementation be used 21 // environs.Environ; we strongly recommend that this implementation be used
21 // when writing a new provider. 22 // when writing a new provider.
22 func Bootstrap(env environs.Environ, cons constraints.Value, possibleTools coret ools.List) error { 23 func Bootstrap(env environs.Environ, cons constraints.Value) error {
23 // TODO make safe in the case of racing Bootstraps 24 // TODO make safe in the case of racing Bootstraps
24 // If two Bootstraps are called concurrently, there's 25 // If two Bootstraps are called concurrently, there's
25 // no way to make sure that only one succeeds. 26 // no way to make sure that only one succeeds.
26 27
27 // Create an empty bootstrap state file so we can get its URL. 28 // Create an empty bootstrap state file so we can get its URL.
28 // It will be updated with the instance id and hardware characteristics 29 // It will be updated with the instance id and hardware characteristics
29 // after the bootstrap instance is started. 30 // after the bootstrap instance is started.
30 » stateFileURL, err := CreateStateFile(env.Storage()) 31 » stateFileURL, err := bootstrap.CreateStateFile(env.Storage())
31 if err != nil { 32 if err != nil {
32 return err 33 return err
33 } 34 }
34 machineConfig := environs.NewBootstrapMachineConfig(stateFileURL) 35 machineConfig := environs.NewBootstrapMachineConfig(stateFileURL)
35 » inst, hw, err := env.StartInstance(cons, possibleTools, machineConfig) 36
37 » selectedTools, err := EnsureBootstrapTools(env, env.Config().DefaultSeri es(), cons.Arch)
38 » if err != nil {
39 » » return err
40 » }
41
42 » inst, hw, err := env.StartInstance(cons, selectedTools, machineConfig)
36 if err != nil { 43 if err != nil {
37 return fmt.Errorf("cannot start bootstrap instance: %v", err) 44 return fmt.Errorf("cannot start bootstrap instance: %v", err)
38 } 45 }
39 var characteristics []instance.HardwareCharacteristics 46 var characteristics []instance.HardwareCharacteristics
40 if hw != nil { 47 if hw != nil {
41 characteristics = []instance.HardwareCharacteristics{*hw} 48 characteristics = []instance.HardwareCharacteristics{*hw}
42 } 49 }
43 » err = SaveState( 50 » err = bootstrap.SaveState(
44 env.Storage(), 51 env.Storage(),
45 » » &BootstrapState{ 52 » » &bootstrap.BootstrapState{
46 StateInstances: []instance.Id{inst.Id()}, 53 StateInstances: []instance.Id{inst.Id()},
47 Characteristics: characteristics, 54 Characteristics: characteristics,
48 }) 55 })
49 if err != nil { 56 if err != nil {
50 stoperr := env.StopInstances([]instance.Instance{inst}) 57 stoperr := env.StopInstances([]instance.Instance{inst})
51 if stoperr != nil { 58 if stoperr != nil {
52 // Failure upon failure. Log it, but return the origina l error. 59 // Failure upon failure. Log it, but return the origina l error.
53 logger.Errorf("cannot stop failed bootstrap instance %q: %v", inst.Id(), stoperr) 60 logger.Errorf("cannot stop failed bootstrap instance %q: %v", inst.Id(), stoperr)
54 } 61 }
55 return fmt.Errorf("cannot save state: %v", err) 62 return fmt.Errorf("cannot save state: %v", err)
56 } 63 }
57 return nil 64 return nil
58 } 65 }
66
67 // EnsureBootstrapTools finds tools, syncing with an external tools source as
68 // necessary; it then selects the newest tools to bootstrap with, and sets
69 // agent-version.
70 func EnsureBootstrapTools(env environs.Environ, series string, arch *string) (co retools.List, error) {
71 possibleTools, err := bootstrap.EnsureToolsAvailability(env, series, arc h)
72 if err != nil {
73 return nil, err
74 }
75 return bootstrap.SetBootstrapTools(env, possibleTools)
76 }
77
78 // EnsureNotBootstrapped returns null if the environment is not bootstrapped,
79 // and an error if it is or if the function was not able to tell.
80 func EnsureNotBootstrapped(env environs.Environ) error {
81 _, err := bootstrap.LoadState(env.Storage())
82 // If there is no error loading the bootstrap state, then we are
83 // bootstrapped.
84 if err == nil {
85 return fmt.Errorf("environment is already bootstrapped")
86 }
87 if err == environs.ErrNotBootstrapped {
88 return nil
89 }
90 return err
91 }
OLDNEW
« no previous file with comments | « provider/azure/environ_test.go ('k') | provider/common/bootstrap_test.go » ('j') | no next file with comments »

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