Left: | ||
Right: |
OLD | NEW |
---|---|
1 package environs | 1 package environs |
2 | 2 |
3 import ( | 3 import ( |
4 "fmt" | 4 "fmt" |
5 "launchpad.net/juju-core/environs/tools" | 5 "launchpad.net/juju-core/environs/tools" |
6 "launchpad.net/juju-core/log" | 6 "launchpad.net/juju-core/log" |
7 "launchpad.net/juju-core/state" | 7 "launchpad.net/juju-core/state" |
8 "launchpad.net/juju-core/version" | 8 "launchpad.net/juju-core/version" |
9 ) | 9 ) |
10 | 10 |
11 // ToolsList holds a list of available tools. Private tools take | 11 // ToolsList holds a list of available tools. Private tools take |
12 // precedence over public tools, even if they have a lower | 12 // precedence over public tools, even if they have a lower |
13 // version number. | 13 // version number. |
14 type ToolsList struct { | 14 type ToolsList struct { |
15 Private tools.List | 15 Private tools.List |
16 Public tools.List | 16 Public tools.List |
17 } | 17 } |
18 | 18 |
19 // ListTools returns a ToolsList holding all the tools | 19 // ListTools returns a ToolsList holding all the tools |
20 // available in the given environment that have the | 20 // available in the given environment that have the |
21 // given major version. | 21 // given major version. |
22 func ListTools(env Environ, majorVersion int) (*ToolsList, error) { | 22 func ListTools(env Environ, majorVersion int) (*ToolsList, error) { |
23 private, err := tools.ReadList(env.Storage(), majorVersion) | 23 private, err := tools.ReadList(env.Storage(), majorVersion) |
24 » if err != nil && err != tools.ErrNoMatches { | 24 » if err := ignoreMissingTools(err); err != nil { |
25 return nil, err | 25 return nil, err |
26 } | 26 } |
27 public, err := tools.ReadList(env.PublicStorage(), majorVersion) | 27 public, err := tools.ReadList(env.PublicStorage(), majorVersion) |
28 » if err != nil && err != tools.ErrNoMatches { | 28 » if err := ignoreMissingTools(err); err != nil { |
29 return nil, err | 29 return nil, err |
30 } | 30 } |
31 return &ToolsList{ | 31 return &ToolsList{ |
32 Private: private, | 32 Private: private, |
33 Public: public, | 33 Public: public, |
34 }, nil | 34 }, nil |
35 } | 35 } |
36 | 36 |
37 func ignoreMissingTools(err error) error { | |
rog
2013/04/12 17:30:06
this function name isn't greatly obvious.
i'd thin
fwereade
2013/04/14 01:35:08
Done.
| |
38 switch err { | |
39 case tools.ErrNoTools, tools.ErrNoMatches: | |
rog
2013/04/12 17:30:06
again, no real reason for there to be a difference
fwereade
2013/04/14 01:35:08
FindAvailableTools
| |
40 return nil | |
41 } | |
42 return err | |
43 } | |
44 | |
37 // BestTools returns the most recent version | 45 // BestTools returns the most recent version |
38 // from the set of tools in the ToolsList that are | 46 // from the set of tools in the ToolsList that are |
39 // compatible with the given version, using flags | 47 // compatible with the given version, using flags |
40 // to determine possible candidates. | 48 // to determine possible candidates. |
41 // It returns nil if no such tools are found. | 49 // It returns nil if no such tools are found. |
42 func BestTools(list *ToolsList, vers version.Binary, flags ToolsSearchFlags) *st ate.Tools { | 50 func BestTools(list *ToolsList, vers version.Binary, flags ToolsSearchFlags) *st ate.Tools { |
43 if flags&CompatVersion == 0 { | 51 if flags&CompatVersion == 0 { |
44 panic("CompatVersion not implemented") | 52 panic("CompatVersion not implemented") |
45 } | 53 } |
46 if tools := bestTools(list.Private, vers, flags); tools != nil { | 54 if tools := bestTools(list.Private, vers, flags); tools != nil { |
47 return tools | 55 return tools |
48 } | 56 } |
49 return bestTools(list.Public, vers, flags) | 57 return bestTools(list.Public, vers, flags) |
50 } | 58 } |
51 | 59 |
52 // bestTools is like BestTools but operates on a single list of tools. | 60 // bestTools is like BestTools but operates on a single list of tools. |
53 func bestTools(toolsList []*state.Tools, vers version.Binary, flags ToolsSearchF lags) *state.Tools { | 61 func bestTools(toolsList []*state.Tools, vers version.Binary, flags ToolsSearchF lags) *state.Tools { |
54 var bestTools *state.Tools | 62 var bestTools *state.Tools |
55 allowDev := vers.IsDev() || flags&DevVersion != 0 | 63 allowDev := vers.IsDev() || flags&DevVersion != 0 |
56 allowHigher := flags&HighestVersion != 0 | 64 allowHigher := flags&HighestVersion != 0 |
57 » log.Debugf("finding best tools for version: %v", vers) | 65 » log.Debugf("environs: finding best tools for version %v (dev=%v)", vers, allowDev) |
dimitern
2013/04/12 17:30:47
environs/tools:
fwereade
2013/04/14 01:35:08
wrong package
| |
58 for _, t := range toolsList { | 66 for _, t := range toolsList { |
59 » » log.Debugf("checking tools %v", t) | 67 » » log.Debugf("environs: checking tools %v", t) |
dimitern
2013/04/12 17:30:47
ditto
| |
60 if t.Major != vers.Major || | 68 if t.Major != vers.Major || |
61 t.Series != vers.Series || | 69 t.Series != vers.Series || |
62 t.Arch != vers.Arch || | 70 t.Arch != vers.Arch || |
63 !allowDev && t.IsDev() || | 71 !allowDev && t.IsDev() || |
64 !allowHigher && vers.Number.Less(t.Number) { | 72 !allowHigher && vers.Number.Less(t.Number) { |
65 continue | 73 continue |
66 } | 74 } |
67 if bestTools == nil || bestTools.Number.Less(t.Number) { | 75 if bestTools == nil || bestTools.Number.Less(t.Number) { |
76 log.Debugf("environs: new best tools found: %v", t) | |
dimitern
2013/04/12 17:30:47
ditto
| |
68 bestTools = t | 77 bestTools = t |
69 } | 78 } |
70 } | 79 } |
71 return bestTools | 80 return bestTools |
72 } | 81 } |
73 | 82 |
74 // ToolsSearchFlags gives options when searching | 83 // ToolsSearchFlags gives options when searching |
75 // for tools. | 84 // for tools. |
76 type ToolsSearchFlags int | 85 type ToolsSearchFlags int |
77 | 86 |
(...skipping 24 matching lines...) Expand all Loading... | |
102 toolsList, err := ListTools(env, vers.Major) | 111 toolsList, err := ListTools(env, vers.Major) |
103 if err != nil { | 112 if err != nil { |
104 return nil, err | 113 return nil, err |
105 } | 114 } |
106 tools := BestTools(toolsList, vers, flags) | 115 tools := BestTools(toolsList, vers, flags) |
107 if tools == nil { | 116 if tools == nil { |
108 return tools, &NotFoundError{fmt.Errorf("no compatible tools fou nd")} | 117 return tools, &NotFoundError{fmt.Errorf("no compatible tools fou nd")} |
109 } | 118 } |
110 return tools, nil | 119 return tools, nil |
111 } | 120 } |
121 | |
122 // FindAvailableTools returns a tools.List containing all tools with a given | |
123 // version number in the environment's private storage. If no tools are | |
124 // present in private storage, it falls back to public storage; if no tools | |
125 // are present there, it returns ErrNoTools. Tools from public and private | |
126 // buckets are not mixed. | |
dimitern
2013/04/12 17:30:47
also probably good to mention what does "mixed" me
fwereade
2013/04/14 01:35:08
Rewritten
| |
127 func FindAvailableTools(environ Environ, majorVersion int) (tools.List, error) { | |
128 list, err := tools.ReadList(environ.Storage(), majorVersion) | |
129 if err == tools.ErrNoTools { | |
fwereade
2013/04/14 01:35:08
FWIW, this here is the place we care about the dis
| |
130 list, err = tools.ReadList(environ.PublicStorage(), majorVersion ) | |
131 } | |
132 return list, err | |
133 } | |
OLD | NEW |