func rebootstrap(cfg *config.Config, ctx *cmd.Context, cons constraints.Value) (environs.Environ, error) { progress("re-bootstrapping environment") // Turn on safe mode so that the newly bootstrapped instance // will not destroy all the instances it does not know about. cfg, err := cfg.Apply(map[string]interface{}{ "provisioner-safe-mode": true, }) if err != nil { return nil, errors.Annotate(err, "cannot enable provisioner-safe-mode") } env, err := environs.New(cfg) if err != nil { return nil, err } instanceIds, err := env.StateServerInstances() switch errors.Cause(err) { case nil, environs.ErrNoInstances: // Some providers will return a nil error even // if there are no live state server instances. break case environs.ErrNotBootstrapped: return nil, errors.Trace(err) default: return nil, errors.Annotate(err, "cannot determine state server instances") } if len(instanceIds) > 0 { instances, err := env.Instances(instanceIds) switch errors.Cause(err) { case nil, environs.ErrPartialInstances: return nil, fmt.Errorf("old bootstrap instances %q still seems to exist; will not replace", instances) case environs.ErrNoInstances: // No state server instances, so keep running. break default: return nil, errors.Annotate(err, "cannot detect whether old instance is still running") } } // Remove the storage so that we can bootstrap without the provider complaining. if env, ok := env.(environs.EnvironStorage); ok { if err := env.Storage().Remove(common.StateFile); err != nil { return nil, errors.Annotate(err, fmt.Sprintf("cannot remove %q from storage", common.StateFile)) } } // TODO If we fail beyond here, then we won't have a state file and // we won't be able to re-run this script because it fails without it. // We could either try to recreate the file if we fail (which is itself // error-prone) or we could provide a --no-check flag to make // it go ahead anyway without the check. args := bootstrap.BootstrapParams{Constraints: cons} if err := bootstrap.Bootstrap(envcmd.BootstrapContextNoVerify(ctx), env, args); err != nil { return nil, errors.Annotate(err, "cannot bootstrap new instance") } return env, nil }
func (c *imageMetadataCommandBase) prepare(context *cmd.Context, store configstore.Storage) (environs.Environ, error) { cfg, err := c.Config(store, nil) if err != nil { return nil, errors.Annotate(err, "could not get config from store") } // We are preparing an environment to access parameters needed to access // image metadata. We don't need, nor want, credential verification. // In most cases, credentials will not be available. ctx := envcmd.BootstrapContextNoVerify(context) return environs.Prepare(cfg, ctx, store) }
func (s *ToolsMetadataSuite) SetUpTest(c *gc.C) { s.FakeJujuHomeSuite.SetUpTest(c) s.AddCleanup(func(*gc.C) { dummy.Reset() loggo.ResetLoggers() }) env, err := environs.PrepareFromName( "erewhemos", envcmd.BootstrapContextNoVerify(coretesting.Context(c)), configstore.NewMem()) c.Assert(err, jc.ErrorIsNil) s.env = env loggo.GetLogger("").SetLogLevel(loggo.INFO) // Switch the default tools location. s.publicStorageDir = c.MkDir() s.PatchValue(&tools.DefaultBaseURL, s.publicStorageDir) }
func (s *EnvironmentCommandSuite) TestBootstrapContextNoVerify(c *gc.C) { ctx := envcmd.BootstrapContextNoVerify(&cmd.Context{}) c.Assert(ctx.ShouldVerifyCredentials(), jc.IsFalse) }