Skip to content

Commit

Permalink
state, etc: state.Info.Tag is now a names.Tag not a string
Browse files Browse the repository at this point in the history
  • Loading branch information
davecheney committed Jul 8, 2014
1 parent 7c789b8 commit 8404bcf
Show file tree
Hide file tree
Showing 35 changed files with 171 additions and 105 deletions.
21 changes: 13 additions & 8 deletions agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (

"github.com/juju/errors"
"github.com/juju/loggo"
"github.com/juju/names"
"github.com/juju/utils"

"github.com/juju/juju/juju/paths"
Expand Down Expand Up @@ -226,7 +227,7 @@ type configInternal struct {
configFilePath string
dataDir string
logDir string
tag string
tag names.Tag
nonce string
jobs []params.MachineJob
upgradedToVersion version.Number
Expand Down Expand Up @@ -274,14 +275,18 @@ func NewAgentConfig(configParams AgentConfigParams) (ConfigSetterWriter, error)
if len(configParams.CACert) == 0 {
return nil, errors.Trace(requiredError("CA certificate"))
}
tag, err := names.ParseTag(configParams.Tag)
if err != nil {
return nil, err
}
// Note that the password parts of the state and api information are
// blank. This is by design.
config := &configInternal{
logDir: logDir,
dataDir: configParams.DataDir,
jobs: configParams.Jobs,
upgradedToVersion: configParams.UpgradedToVersion,
tag: configParams.Tag,
tag: tag,
nonce: configParams.Nonce,
caCert: configParams.CACert,
oldPassword: configParams.Password,
Expand Down Expand Up @@ -331,17 +336,17 @@ func NewStateMachineConfig(configParams AgentConfigParams, serverInfo params.Sta
}

// Dir returns the agent-specific data directory.
func Dir(dataDir, agentName string) string {
func Dir(dataDir string, tag names.Tag) string {
// Note: must use path, not filepath, as this
// function is used by the client on Windows.
return path.Join(dataDir, "agents", agentName)
return path.Join(dataDir, "agents", tag.String())
}

// ConfigPath returns the full path to the agent config file.
// NOTE: Delete this once all agents accept --config instead
// of --data-dir - it won't be needed anymore.
func ConfigPath(dataDir, agentName string) string {
return filepath.Join(Dir(dataDir, agentName), agentConfigFilename)
func ConfigPath(dataDir string, tag names.Tag) string {
return filepath.Join(Dir(dataDir, tag), agentConfigFilename)
}

// ReadConfig reads configuration data from the given location.
Expand Down Expand Up @@ -554,7 +559,7 @@ func (c *configInternal) OldPassword() string {
}

func (c *configInternal) Tag() string {
return c.tag
return c.tag.String()
}

func (c *configInternal) Dir() string {
Expand Down Expand Up @@ -634,7 +639,7 @@ func (c *configInternal) APIInfo() *api.Info {
Addrs: addrs,
Password: c.apiDetails.password,
CACert: c.caCert,
Tag: c.tag,
Tag: c.tag.String(),
Nonce: c.nonce,
}
}
Expand Down
41 changes: 24 additions & 17 deletions agent/agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"reflect"

"github.com/juju/names"
jc "github.com/juju/testing/checkers"
gc "launchpad.net/gocheck"

Expand Down Expand Up @@ -44,7 +45,7 @@ var agentConfigTests = []struct {
about: "missing upgraded to version",
params: agent.AgentConfigParams{
DataDir: "/data/dir",
Tag: "omg",
Tag: "user-omg", // a user calle omg
},
checkErr: "upgradedToVersion not found in configuration",
}, {
Expand All @@ -59,7 +60,7 @@ var agentConfigTests = []struct {
about: "missing CA cert",
params: agent.AgentConfigParams{
DataDir: "/data/dir",
Tag: "omg",
Tag: "user-omg",
UpgradedToVersion: version.Current.Number,
Password: "sekrit",
},
Expand All @@ -68,7 +69,7 @@ var agentConfigTests = []struct {
about: "need either state or api addresses",
params: agent.AgentConfigParams{
DataDir: "/data/dir",
Tag: "omg",
Tag: "user-omg",
UpgradedToVersion: version.Current.Number,
Password: "sekrit",
CACert: "ca cert",
Expand All @@ -78,7 +79,7 @@ var agentConfigTests = []struct {
about: "invalid state address",
params: agent.AgentConfigParams{
DataDir: "/data/dir",
Tag: "omg",
Tag: "user-omg",
UpgradedToVersion: version.Current.Number,
Password: "sekrit",
CACert: "ca cert",
Expand All @@ -89,7 +90,7 @@ var agentConfigTests = []struct {
about: "invalid api address",
params: agent.AgentConfigParams{
DataDir: "/data/dir",
Tag: "omg",
Tag: "user-omg",
UpgradedToVersion: version.Current.Number,
Password: "sekrit",
CACert: "ca cert",
Expand All @@ -100,7 +101,7 @@ var agentConfigTests = []struct {
about: "good state addresses",
params: agent.AgentConfigParams{
DataDir: "/data/dir",
Tag: "omg",
Tag: "user-omg",
UpgradedToVersion: version.Current.Number,
Password: "sekrit",
CACert: "ca cert",
Expand All @@ -110,7 +111,7 @@ var agentConfigTests = []struct {
about: "good api addresses",
params: agent.AgentConfigParams{
DataDir: "/data/dir",
Tag: "omg",
Tag: "user-omg",
UpgradedToVersion: version.Current.Number,
Password: "sekrit",
CACert: "ca cert",
Expand All @@ -120,7 +121,7 @@ var agentConfigTests = []struct {
about: "both state and api addresses",
params: agent.AgentConfigParams{
DataDir: "/data/dir",
Tag: "omg",
Tag: "user-omg",
UpgradedToVersion: version.Current.Number,
Password: "sekrit",
CACert: "ca cert",
Expand All @@ -131,7 +132,7 @@ var agentConfigTests = []struct {
about: "everything...",
params: agent.AgentConfigParams{
DataDir: "/data/dir",
Tag: "omg",
Tag: "user-omg",
Password: "sekrit",
UpgradedToVersion: version.Current.Number,
CACert: "ca cert",
Expand All @@ -143,7 +144,7 @@ var agentConfigTests = []struct {
about: "missing logDir sets default",
params: agent.AgentConfigParams{
DataDir: "/data/dir",
Tag: "omg",
Tag: "user-omg",
Password: "sekrit",
UpgradedToVersion: version.Current.Number,
CACert: "ca cert",
Expand Down Expand Up @@ -173,7 +174,7 @@ func (*suite) TestMigrate(c *gc.C) {
initialParams := agent.AgentConfigParams{
DataDir: c.MkDir(),
LogDir: c.MkDir(),
Tag: "omg",
Tag: "user-omg",
Nonce: "nonce",
Password: "secret",
UpgradedToVersion: version.MustParse("1.16.5"),
Expand Down Expand Up @@ -263,7 +264,9 @@ func (*suite) TestMigrate(c *gc.C) {

// Make sure we can read it back successfully and it
// matches what we wrote.
configPath := agent.ConfigPath(newConfig.DataDir(), newConfig.Tag())
tag, err := names.ParseTag(newConfig.Tag())
c.Assert(err, gc.IsNil)
configPath := agent.ConfigPath(newConfig.DataDir(), tag)
readConfig, err := agent.ReadConfig(configPath)
c.Check(err, gc.IsNil)
c.Check(newConfig, jc.DeepEquals, readConfig)
Expand Down Expand Up @@ -356,7 +359,7 @@ func (*suite) TestNewStateMachineConfig(c *gc.C) {

var attributeParams = agent.AgentConfigParams{
DataDir: "/data/dir",
Tag: "omg",
Tag: "user-omg", // the omg user
UpgradedToVersion: version.Current.Number,
Password: "sekrit",
CACert: "ca cert",
Expand All @@ -370,8 +373,8 @@ func (*suite) TestAttributes(c *gc.C) {
c.Assert(err, gc.IsNil)
c.Assert(conf.DataDir(), gc.Equals, "/data/dir")
c.Assert(conf.SystemIdentityPath(), gc.Equals, "/data/dir/system-identity")
c.Assert(conf.Tag(), gc.Equals, "omg")
c.Assert(conf.Dir(), gc.Equals, "/data/dir/agents/omg")
c.Assert(conf.Tag(), gc.Equals, "user-omg")
c.Assert(conf.Dir(), gc.Equals, "/data/dir/agents/user-omg")
c.Assert(conf.Nonce(), gc.Equals, "a nonce")
c.Assert(conf.UpgradedToVersion(), jc.DeepEquals, version.Current.Number)
}
Expand Down Expand Up @@ -433,7 +436,9 @@ func (*suite) TestWriteAndRead(c *gc.C) {
c.Assert(err, gc.IsNil)

c.Assert(conf.Write(), gc.IsNil)
reread, err := agent.ReadConfig(agent.ConfigPath(conf.DataDir(), conf.Tag()))
tag, err := names.ParseTag(conf.Tag())
c.Assert(err, gc.IsNil)
reread, err := agent.ReadConfig(agent.ConfigPath(conf.DataDir(), tag))
c.Assert(err, gc.IsNil)
c.Assert(reread, jc.DeepEquals, conf)
}
Expand Down Expand Up @@ -492,12 +497,14 @@ func (*suite) TestSetPassword(c *gc.C) {
}
c.Assert(conf.APIInfo(), jc.DeepEquals, expectAPIInfo)
addr := fmt.Sprintf("127.0.0.1:%d", servingInfo.StatePort)
userTag, err := names.ParseTag(attrParams.Tag)
c.Assert(err, gc.IsNil)
expectStateInfo := &state.Info{
Info: mongo.Info{
Addrs: []string{addr},
CACert: attrParams.CACert,
},
Tag: attrParams.Tag,
Tag: userTag,
Password: "",
}
info, ok := conf.StateInfo()
Expand Down
2 changes: 1 addition & 1 deletion agent/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func InitializeState(c ConfigSetter, envCfg *config.Config, machineCfg Bootstrap
if !ok {
return nil, nil, fmt.Errorf("stateinfo not available")
}
info.Tag = ""
info.Tag = nil
info.Password = ""

logger.Debugf("initializing address %v", info.Addrs)
Expand Down
5 changes: 3 additions & 2 deletions agent/bootstrap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package agent_test

import (
"github.com/juju/names"
gitjujutesting "github.com/juju/testing"
jc "github.com/juju/testing/checkers"
"github.com/juju/utils"
Expand Down Expand Up @@ -147,7 +148,7 @@ func (s *bootstrapSuite) TestInitializeState(c *gc.C) {

// Check that the machine agent's config has been written
// and that we can use it to connect to the state.
newCfg, err := agent.ReadConfig(agent.ConfigPath(dataDir, "machine-0"))
newCfg, err := agent.ReadConfig(agent.ConfigPath(dataDir, names.NewMachineTag("0")))
c.Assert(err, gc.IsNil)
c.Assert(newCfg.Tag(), gc.Equals, "machine-0")
c.Assert(agent.Password(newCfg), gc.Not(gc.Equals), pwHash)
Expand Down Expand Up @@ -235,7 +236,7 @@ func (*bootstrapSuite) assertCanLogInAsAdmin(c *gc.C, password string) {
Addrs: []string{gitjujutesting.MgoServer.Addr()},
CACert: testing.CACert,
},
Tag: "",
Tag: nil, // admin user
Password: password,
}
st, err := state.Open(info, mongo.DialOpts{}, environs.NewStatePolicy())
Expand Down
7 changes: 6 additions & 1 deletion agent/format-1.16.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"net"
"strconv"

"github.com/juju/names"
"launchpad.net/goyaml"

"github.com/juju/juju/state/api/params"
Expand Down Expand Up @@ -90,8 +91,12 @@ func (formatter_1_16) unmarshal(data []byte) (*configInternal, error) {
upgradedToVersion := version.MustParse("1.16.0")
format.UpgradedToVersion = &upgradedToVersion
}
tag, err := names.ParseTag(format.Tag)
if err != nil {
return nil, err
}
config := &configInternal{
tag: format.Tag,
tag: tag,
nonce: format.Nonce,
dataDir: DefaultDataDir,
logDir: DefaultLogDir,
Expand Down
2 changes: 1 addition & 1 deletion agent/format-1.16_whitebox_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ apiport: 17070
`[1:]

const configDataWithoutNewAttributes = `
tag: omg
tag: user-omg
nonce: a nonce
cacert: Y2EgY2VydA==
stateaddresses:
Expand Down
9 changes: 7 additions & 2 deletions agent/format-1.18.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"net"
"strconv"

"github.com/juju/names"
"launchpad.net/goyaml"

"github.com/juju/juju/state/api/params"
Expand Down Expand Up @@ -71,8 +72,12 @@ func (formatter_1_18) unmarshal(data []byte) (*configInternal, error) {
upgradedToVersion := version.MustParse("1.16.0")
format.UpgradedToVersion = &upgradedToVersion
}
tag, err := names.ParseTag(format.Tag)
if err != nil {
return nil, err
}
config := &configInternal{
tag: format.Tag,
tag: tag,
dataDir: format.DataDir,
logDir: format.LogDir,
jobs: format.Jobs,
Expand Down Expand Up @@ -134,7 +139,7 @@ func (formatter_1_18) unmarshal(data []byte) (*configInternal, error) {

func (formatter_1_18) marshal(config *configInternal) ([]byte, error) {
format := &format_1_18Serialization{
Tag: config.tag,
Tag: config.tag.String(),
DataDir: config.dataDir,
LogDir: config.logDir,
Jobs: config.jobs,
Expand Down
17 changes: 11 additions & 6 deletions agent/format_whitebox_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"os"
"path/filepath"

"github.com/juju/names"
jc "github.com/juju/testing/checkers"
gc "launchpad.net/gocheck"

Expand All @@ -24,7 +25,7 @@ var _ = gc.Suite(&formatSuite{})
// The agentParams are used by the specific formatter whitebox tests, and is
// located here for easy reuse.
var agentParams = AgentConfigParams{
Tag: "omg",
Tag: "user-omg",
UpgradedToVersion: version.Current.Number,
Jobs: []params.MachineJob{params.JobHostUnits},
Password: "sekrit",
Expand All @@ -48,17 +49,19 @@ func (*formatSuite) TestWriteCommands(c *gc.C) {
commands, err := config.WriteCommands()
c.Assert(err, gc.IsNil)
c.Assert(commands, gc.HasLen, 3)
c.Assert(commands[0], gc.Matches, `mkdir -p '\S+/agents/omg'`)
c.Assert(commands[1], gc.Matches, `install -m 600 /dev/null '\S+/agents/omg/agent.conf'`)
c.Assert(commands[2], gc.Matches, `printf '%s\\n' '(.|\n)*' > '\S+/agents/omg/agent.conf'`)
c.Assert(commands[0], gc.Matches, `mkdir -p '\S+/agents/user-omg'`)
c.Assert(commands[1], gc.Matches, `install -m 600 /dev/null '\S+/agents/user-omg/agent.conf'`)
c.Assert(commands[2], gc.Matches, `printf '%s\\n' '(.|\n)*' > '\S+/agents/user-omg/agent.conf'`)
}

func (*formatSuite) TestWriteAgentConfig(c *gc.C) {
config := newTestConfig(c)
err := config.Write()
c.Assert(err, gc.IsNil)

configPath := ConfigPath(config.DataDir(), config.Tag())
tag, err := names.ParseTag(config.Tag())
c.Assert(err, gc.IsNil)
configPath := ConfigPath(config.DataDir(), tag)
formatPath := filepath.Join(config.Dir(), legacyFormatFilename)
assertFileExists(c, configPath)
assertFileNotExist(c, formatPath)
Expand Down Expand Up @@ -90,7 +93,9 @@ func (*formatSuite) TestReadWriteStateConfig(c *gc.C) {
func assertWriteAndRead(c *gc.C, config *configInternal) {
err := config.Write()
c.Assert(err, gc.IsNil)
configPath := ConfigPath(config.DataDir(), config.Tag())
tag, err := names.ParseTag(config.Tag())
c.Assert(err, gc.IsNil)
configPath := ConfigPath(config.DataDir(), tag)
readConfig, err := ReadConfig(configPath)
c.Assert(err, gc.IsNil)
c.Assert(readConfig, jc.DeepEquals, config)
Expand Down
2 changes: 1 addition & 1 deletion agent/identity_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type identitySuite struct {
var _ = gc.Suite(&identitySuite{})

var attributeParams = AgentConfigParams{
Tag: "omg",
Tag: "user-omg", // the omg user
UpgradedToVersion: version.Current.Number,
Password: "sekrit",
CACert: "ca cert",
Expand Down
Loading

0 comments on commit 8404bcf

Please sign in to comment.