Skip to content

Commit

Permalink
environs.tools.PreferredStream -> PreferredStreams
Browse files Browse the repository at this point in the history
It now returns a slice of fallback streams in preference order. For the
moment callers have been updated to just use the first one, they'll be
updated to do the fallback next.
  • Loading branch information
babbageclunk committed Nov 16, 2017
1 parent 724924b commit a865679
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 28 deletions.
2 changes: 1 addition & 1 deletion apiserver/common/tools.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ func (f *ToolsFinder) findMatchingTools(args params.FindToolsParams) (coretools.
}
filter := toolsFilter(args)
cfg := env.Config()
stream := envtools.PreferredStream(&args.Number, cfg.Development(), cfg.AgentStream())
stream := envtools.PreferredStreams(&args.Number, cfg.Development(), cfg.AgentStream())[0]
simplestreamsList, err := envtoolsFindTools(
env, args.MajorVersion, args.MinorVersion, stream, filter,
)
Expand Down
2 changes: 1 addition & 1 deletion apiserver/facades/controller/agenttools/agenttools.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func checkToolsAvailability(newEnviron newEnvironFunc, modelCfg *config.Config,
// We'll try the released stream first, then fall back to the current configured stream
// if no released tools are found.
vers, err := finder(env, currentVersion.Major, currentVersion.Minor, tools.ReleasedStream, coretools.Filter{})
preferredStream := tools.PreferredStream(&currentVersion, modelCfg.Development(), modelCfg.AgentStream())
preferredStream := tools.PreferredStreams(&currentVersion, modelCfg.Development(), modelCfg.AgentStream())[0]
if preferredStream != tools.ReleasedStream && errors.Cause(err) == coretools.ErrNoMatches {
vers, err = finder(env, currentVersion.Major, currentVersion.Minor, preferredStream, coretools.Filter{})
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/jujud/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ func (c *BootstrapCommand) Run(_ *cmd.Context) error {
if ok && desiredVersion != jujuversion.Current {
// If we have been asked for a newer version, ensure the newer
// tools can actually be found, or else bootstrap won't complete.
stream := envtools.PreferredStream(&desiredVersion, args.ControllerModelConfig.Development(), args.ControllerModelConfig.AgentStream())
stream := envtools.PreferredStreams(&desiredVersion, args.ControllerModelConfig.Development(), args.ControllerModelConfig.AgentStream())[0]
logger.Infof("newer agent binaries requested, looking for %v in stream %v", desiredVersion, stream)
hostSeries, err := series.HostSeries()
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions environs/bootstrap/tools.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,6 @@ func findBootstrapTools(env environs.Environ, vers *version.Number, arch, series
if vers != nil {
filter.Number = *vers
}
stream := envtools.PreferredStream(vers, env.Config().Development(), env.Config().AgentStream())
return findTools(env, cliVersion.Major, cliVersion.Minor, stream, filter)
streams := envtools.PreferredStreams(vers, env.Config().Development(), env.Config().AgentStream())
return findTools(env, cliVersion.Major, cliVersion.Minor, streams[0], filter)
}
4 changes: 3 additions & 1 deletion environs/sync/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,9 @@ func SyncTools(syncContext *SyncContext) error {
// We now store the tools in a directory named after their stream, but the
// legacy behaviour is to store all tools in a single "releases" directory.
toolsDir = envtools.ReleasedStream
syncContext.Stream = envtools.PreferredStream(&jujuversion.Current, false, "")
// Always use the primary stream here - the user can specify
// to override that decision.
syncContext.Stream = envtools.PreferredStreams(&jujuversion.Current, false, "")[0]
}
sourceTools, err := envtools.FindToolsForCloud(
[]simplestreams.DataSource{sourceDataSource}, simplestreams.CloudSpec{},
Expand Down
30 changes: 23 additions & 7 deletions environs/tools/tools.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ func FindExactTools(env environs.Environ, vers version.Number, series string, ar
Series: series,
Arch: arch,
}
stream := PreferredStream(&vers, env.Config().Development(), env.Config().AgentStream())
stream := PreferredStreams(&vers, env.Config().Development(), env.Config().AgentStream())[0]
logger.Debugf("looking for agent binaries in stream %q", stream)
availableTools, err := FindTools(env, vers.Major, vers.Minor, stream, filter)
if err != nil {
Expand Down Expand Up @@ -215,12 +215,22 @@ func convertToolsError(err *error) {
}
}

// PreferredStream returns the tools stream used to search for tools, based
// on the required version, whether devel mode is required, and any user specified stream.
func PreferredStream(vers *version.Number, forceDevel bool, stream string) string {
var streamFallbacks = map[string][]string{
ReleasedStream: {ReleasedStream},
ProposedStream: {ProposedStream, ReleasedStream},
DevelStream: {DevelStream, ProposedStream, ReleasedStream},
TestingStream: {TestingStream, DevelStream, ProposedStream, ReleasedStream},
}

// PreferredStreams returns the tools streams that should be searched
// for tools, based on the required version, whether devel mode is
// required, and any user specified stream. The streams are in
// fallback order - if there are no matching tools in one stream the
// next should be checked.
func PreferredStreams(vers *version.Number, forceDevel bool, stream string) []string {
// If the use has already nominated a specific stream, we'll use that.
if stream != "" && stream != ReleasedStream {
return stream
return copyStrings(streamFallbacks[stream])
}
// If we're not upgrading from a known version, we use the
// currently running version.
Expand All @@ -230,7 +240,13 @@ func PreferredStream(vers *version.Number, forceDevel bool, stream string) strin
// Devel versions are alpha or beta etc as defined by the version tag.
// The user can also force the use of devel streams via config.
if forceDevel || jujuversion.IsDev(*vers) {
return DevelStream
return copyStrings(streamFallbacks[DevelStream])
}
return ReleasedStream
return copyStrings(streamFallbacks[ReleasedStream])
}

func copyStrings(vals []string) []string {
result := make([]string, len(vals))
copy(result, vals)
return result
}
36 changes: 22 additions & 14 deletions environs/tools/tools_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ func (s *SimpleStreamsToolsSuite) TestFindTools(c *gc.C) {
s.reset(c, nil)
custom := s.uploadCustom(c, test.custom...)
public := s.uploadPublic(c, test.public...)
stream := envtools.PreferredStream(&jujuversion.Current, s.env.Config().Development(), s.env.Config().AgentStream())
stream := envtools.PreferredStreams(&jujuversion.Current, s.env.Config().Development(), s.env.Config().AgentStream())[0]
actual, err := envtools.FindTools(s.env, test.major, test.minor, stream, coretools.Filter{})
if test.err != nil {
if len(actual) > 0 {
Expand Down Expand Up @@ -287,44 +287,52 @@ var preferredStreamTests = []struct {
currentVers string
forceDevel bool
streamInConfig string
expected string
expected []string
}{{
currentVers: "1.22.0",
streamInConfig: "released",
expected: "released",
expected: []string{"released"},
}, {
currentVers: "1.22.0",
streamInConfig: "proposed",
expected: []string{"proposed", "released"},
}, {
currentVers: "1.22.0",
streamInConfig: "devel",
expected: "devel",
expected: []string{"devel", "proposed", "released"},
}, {
currentVers: "1.22.0",
streamInConfig: "testing",
expected: []string{"testing", "devel", "proposed", "released"},
}, {
currentVers: "1.22.0",
expected: "released",
expected: []string{"released"},
}, {
currentVers: "1.22-beta1",
expected: "devel",
expected: []string{"devel", "proposed", "released"},
}, {
currentVers: "1.22-beta1",
streamInConfig: "released",
expected: "devel",
expected: []string{"devel", "proposed", "released"},
}, {
currentVers: "1.22-beta1",
streamInConfig: "devel",
expected: "devel",
expected: []string{"devel", "proposed", "released"},
}, {
currentVers: "1.22.0",
forceDevel: true,
expected: "devel",
expected: []string{"devel", "proposed", "released"},
}, {
currentVers: "1.22.0",
explicitVers: "1.22-beta1",
expected: "devel",
expected: []string{"devel", "proposed", "released"},
}, {
currentVers: "1.22-bta1",
explicitVers: "1.22.0",
expected: "released",
expected: []string{"released"},
}}

func (s *SimpleStreamsToolsSuite) TestPreferredStream(c *gc.C) {
func (s *SimpleStreamsToolsSuite) TestPreferredStreams(c *gc.C) {
for i, test := range preferredStreamTests {
c.Logf("\ntest %d", i)
s.PatchValue(&jujuversion.Current, version.MustParse(test.currentVers))
Expand All @@ -333,8 +341,8 @@ func (s *SimpleStreamsToolsSuite) TestPreferredStream(c *gc.C) {
v := version.MustParse(test.explicitVers)
vers = &v
}
obtained := envtools.PreferredStream(vers, test.forceDevel, test.streamInConfig)
c.Check(obtained, gc.Equals, test.expected)
obtained := envtools.PreferredStreams(vers, test.forceDevel, test.streamInConfig)
c.Check(obtained, gc.DeepEquals, test.expected)
}
}

Expand Down
2 changes: 1 addition & 1 deletion juju/testing/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ func fillinStartInstanceParams(env environs.Environ, machineId string, isControl
if params.Constraints.Arch != nil {
filter.Arch = *params.Constraints.Arch
}
stream := tools.PreferredStream(&agentVersion, env.Config().Development(), env.Config().AgentStream())
stream := tools.PreferredStreams(&agentVersion, env.Config().Development(), env.Config().AgentStream())[0]
possibleTools, err := tools.FindTools(env, -1, -1, stream, filter)
if err != nil {
return errors.Trace(err)
Expand Down

0 comments on commit a865679

Please sign in to comment.