// SetBootstrapTools returns the newest tools from the given tools list, // and updates the agent-version configuration attribute. func SetBootstrapTools(environ environs.Environ, possibleTools coretools.List) (coretools.List, error) { if len(possibleTools) == 0 { return nil, fmt.Errorf("no bootstrap tools available") } var newVersion version.Number newVersion, toolsList := possibleTools.Newest() logger.Infof("newest version: %s", newVersion) cfg := environ.Config() if agentVersion, _ := cfg.AgentVersion(); agentVersion != newVersion { cfg, err := cfg.Apply(map[string]interface{}{ "agent-version": newVersion.String(), }) if err == nil { err = environ.SetConfig(cfg) } if err != nil { return nil, fmt.Errorf("failed to update environment configuration: %v", err) } } bootstrapVersion := newVersion // We should only ever bootstrap the exact same version as the client, // or we risk bootstrap incompatibility. We still set agent-version to // the newest version, so the agent will immediately upgrade itself. if !isCompatibleVersion(newVersion, version.Current.Number) { compatibleVersion, compatibleTools := findCompatibleTools(possibleTools, version.Current.Number) if len(compatibleTools) == 0 { logger.Warningf( "failed to find %s tools, will attempt to use %s", version.Current.Number, newVersion, ) } else { bootstrapVersion, toolsList = compatibleVersion, compatibleTools } } logger.Infof("picked bootstrap tools version: %s", bootstrapVersion) return toolsList, nil }
// SetEnvironAgentVersion changes the agent version for the // environment to the given version, only if the environment is in a // stable state (all agents are running the current version). func (st *State) SetEnvironAgentVersion(newVersion version.Number) error { for i := 0; i < 5; i++ { settings, err := readSettings(st, environGlobalKey) if err != nil { return err } agentVersion, ok := settings.Get("agent-version") if !ok { return fmt.Errorf("no agent version set in the environment") } currentVersion, ok := agentVersion.(string) if !ok { return fmt.Errorf("invalid agent version format: expected string, got %v", agentVersion) } if newVersion.String() == currentVersion { // Nothing to do. return nil } if err := st.checkCanUpgrade(currentVersion, newVersion.String()); err != nil { return err } ops := []txn.Op{{ C: st.settings.Name, Id: environGlobalKey, Assert: bson.D{{"txn-revno", settings.txnRevno}}, Update: bson.D{{"$set", bson.D{{"agent-version", newVersion.String()}}}}, }} if err := st.runTransaction(ops); err == nil { return nil } else if err != txn.ErrAborted { return fmt.Errorf("cannot set agent-version: %v", err) } } return ErrExcessiveContention }
// SetAgentVersion sets the current agent version in the state's // environment configuration. // This is similar to state.SetEnvironAgentVersion but it doesn't require that // the environment have all agents at the same version already. func SetAgentVersion(st *state.State, vers version.Number) error { return st.UpdateEnvironConfig(map[string]interface{}{"agent-version": vers.String()}, nil, nil) }