-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Upgrade step and deprecating env vars.
- Loading branch information
1 parent
68b4ff9
commit 6429d09
Showing
9 changed files
with
235 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
// Copyright 2015 Canonical Ltd. | ||
// Licensed under the AGPLv3, see LICENCE file for details. | ||
|
||
package upgrades | ||
|
||
import ( | ||
"github.com/juju/errors" | ||
|
||
"github.com/juju/juju/api/block" | ||
"github.com/juju/juju/environs/config" | ||
"github.com/juju/juju/state" | ||
"reflect" | ||
) | ||
|
||
func moveBlocksFromEnvironToState(context Context) error { | ||
logger.Infof("recording existing blocks") | ||
st := context.State() | ||
if st == nil { | ||
logger.Debugf("no state connection, no block recording required") | ||
// We're running on a different node than the state server. | ||
return nil | ||
} | ||
blocks, err := getCurrentBlocks(st) | ||
|
||
if err != nil { | ||
return errors.Trace(err) | ||
} | ||
err = recordCurrentBlocks(context, blocks) | ||
if err != nil { | ||
return errors.Trace(err) | ||
} | ||
err = removeBlockEnvVar(st) | ||
if err != nil { | ||
return errors.Trace(err) | ||
} | ||
return nil | ||
} | ||
|
||
func recordCurrentBlocks(context Context, blocks []string) error { | ||
if len(blocks) == 0 { | ||
// no existing blocks = nothing to do here :) | ||
return nil | ||
} | ||
blockClient := block.NewClient(context.APIState()) | ||
for _, one := range blocks { | ||
err := blockClient.SwitchBlockOn(one, "") | ||
if !reflect.ValueOf(err).IsNil() { | ||
return errors.Annotatef(err, "switching on %v", one) | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func getCurrentBlocks(st *state.State) ([]string, error) { | ||
cfg, err := getEnvironConfig(st) | ||
if err != nil { | ||
return nil, errors.Trace(err) | ||
} | ||
return getBlocks(cfg), nil | ||
} | ||
|
||
func getEnvironConfig(st *state.State) (*config.Config, error) { | ||
envConfig, err := st.EnvironConfig() | ||
if err != nil { | ||
return nil, errors.Annotatef(err, "reading current config") | ||
} | ||
return envConfig, nil | ||
} | ||
|
||
func getBlocks(cfg *config.Config) []string { | ||
var blocks []string | ||
addBlock := func(aType string) { | ||
blocks = append(blocks, aType) | ||
} | ||
|
||
if cfg.PreventAllChanges() { | ||
addBlock(state.ChangeBlock.String()) | ||
} | ||
if cfg.PreventRemoveObject() { | ||
addBlock(state.RemoveBlock.String()) | ||
} | ||
if cfg.PreventDestroyEnvironment() { | ||
addBlock(state.DestroyBlock.String()) | ||
} | ||
return blocks | ||
} | ||
|
||
func removeBlockEnvVar(st *state.State) error { | ||
removeAttrs := []string{ | ||
config.PreventAllChangesKey, | ||
config.PreventDestroyEnvironmentKey, | ||
config.PreventRemoveObjectKey, | ||
} | ||
return st.UpdateEnvironConfig(map[string]interface{}{}, removeAttrs, nil) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
// Copyright 2015 Canonical Ltd. | ||
// Licensed under the AGPLv3, see LICENCE file for details. | ||
|
||
package upgrades_test | ||
|
||
import ( | ||
jc "github.com/juju/testing/checkers" | ||
gc "gopkg.in/check.v1" | ||
|
||
"github.com/juju/juju/api" | ||
"github.com/juju/juju/api/block" | ||
"github.com/juju/juju/juju" | ||
jujutesting "github.com/juju/juju/juju/testing" | ||
"github.com/juju/juju/state" | ||
"github.com/juju/juju/upgrades" | ||
) | ||
|
||
type blockSuite struct { | ||
jujutesting.JujuConnSuite | ||
ctx upgrades.Context | ||
blockClient *block.Client | ||
} | ||
|
||
var _ = gc.Suite(&blockSuite{}) | ||
|
||
func (s *blockSuite) SetUpTest(c *gc.C) { | ||
s.JujuConnSuite.SetUpTest(c) | ||
// apiState, _ := s.OpenAPIAsNewMachine(c, state.JobManageEnviron) | ||
|
||
conn, err := juju.NewAPIState(s.AdminUserTag(c), s.Environ, api.DialOpts{}) | ||
c.Assert(err, jc.ErrorIsNil) | ||
s.AddCleanup(func(*gc.C) { conn.Close() }) | ||
|
||
s.ctx = &mockContext{ | ||
agentConfig: &mockAgentConfig{dataDir: s.DataDir()}, | ||
apiState: conn, | ||
state: s.State, | ||
} | ||
s.blockClient = block.NewClient(conn) | ||
} | ||
|
||
func (s *blockSuite) TestRecordBlocksNone(c *gc.C) { | ||
err := upgrades.MoveBlocksFromEnvironToState(s.ctx) | ||
c.Assert(err, jc.ErrorIsNil) | ||
s.ensureBlocksRecorded(c, nil) | ||
s.ensureEnvBlocksOff(c) | ||
} | ||
|
||
func (s *blockSuite) ensureEnvBlocksOff(c *gc.C) { | ||
cfg, err := s.State.EnvironConfig() | ||
c.Assert(err, jc.ErrorIsNil) | ||
attrs := cfg.AllAttrs() | ||
_, exists := attrs["block-destroy-environment"] | ||
c.Assert(exists, jc.IsFalse) | ||
_, exists = attrs["block-remove-object"] | ||
c.Assert(exists, jc.IsFalse) | ||
_, exists = attrs["block-all-changes"] | ||
c.Assert(exists, jc.IsFalse) | ||
} | ||
|
||
func (s *blockSuite) ensureBlocksRecorded(c *gc.C, expected []string) { | ||
blocks, err := s.blockClient.List() | ||
c.Assert(err, jc.ErrorIsNil) | ||
|
||
var types []string | ||
for _, ablock := range blocks { | ||
types = append(types, ablock.Type) | ||
} | ||
c.Assert(types, jc.SameContents, expected) | ||
} | ||
|
||
func (s *blockSuite) TestUpgradeBlocks(c *gc.C) { | ||
err := s.State.UpdateEnvironConfig(map[string]interface{}{ | ||
"block-destroy-environment": true, | ||
"block-remove-object": true, | ||
"block-all-changes": true, | ||
}, nil, nil) | ||
c.Assert(err, jc.ErrorIsNil) | ||
|
||
err = upgrades.MoveBlocksFromEnvironToState(s.ctx) | ||
|
||
c.Assert(err, jc.ErrorIsNil) | ||
s.ensureBlocksRecorded(c, []string{ | ||
state.ChangeBlock.String(), | ||
state.DestroyBlock.String(), | ||
state.RemoveBlock.String(), | ||
}) | ||
s.ensureEnvBlocksOff(c) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters