Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
davecheney committed Jul 14, 2014
1 parent 7ecb35a commit 150e65f
Show file tree
Hide file tree
Showing 21 changed files with 99 additions and 109 deletions.
6 changes: 3 additions & 3 deletions agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ type Config interface {

// Tag returns the tag of the entity on whose behalf the state connection
// will be made.
Tag() string
Tag() names.Tag

// Dir returns the agent's directory.
Dir() string
Expand Down Expand Up @@ -555,8 +555,8 @@ func (c *configInternal) OldPassword() string {
return c.oldPassword
}

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

func (c *configInternal) Dir() string {
Expand Down
10 changes: 3 additions & 7 deletions agent/agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,9 +264,7 @@ func (*suite) TestMigrate(c *gc.C) {

// Make sure we can read it back successfully and it
// matches what we wrote.
tag, err := names.ParseTag(newConfig.Tag())
c.Assert(err, gc.IsNil)
configPath := agent.ConfigPath(newConfig.DataDir(), tag)
configPath := agent.ConfigPath(newConfig.DataDir(), newConfig.Tag())
readConfig, err := agent.ReadConfig(configPath)
c.Check(err, gc.IsNil)
c.Check(newConfig, jc.DeepEquals, readConfig)
Expand Down Expand Up @@ -373,7 +371,7 @@ 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, "user-omg")
c.Assert(conf.Tag(), gc.Equals, names.NewUserTag("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 @@ -436,9 +434,7 @@ func (*suite) TestWriteAndRead(c *gc.C) {
c.Assert(err, gc.IsNil)

c.Assert(conf.Write(), gc.IsNil)
tag, err := names.ParseTag(conf.Tag())
c.Assert(err, gc.IsNil)
reread, err := agent.ReadConfig(agent.ConfigPath(conf.DataDir(), tag))
reread, err := agent.ReadConfig(agent.ConfigPath(conf.DataDir(), conf.Tag()))
c.Assert(err, gc.IsNil)
c.Assert(reread, jc.DeepEquals, conf)
}
Expand Down
2 changes: 1 addition & 1 deletion agent/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ type BootstrapMachineConfig struct {
const BootstrapMachineId = "0"

func InitializeState(c ConfigSetter, envCfg *config.Config, machineCfg BootstrapMachineConfig, timeout mongo.DialOpts, policy state.Policy) (_ *state.State, _ *state.Machine, resultErr error) {
if c.Tag() != names.NewMachineTag(BootstrapMachineId).String() {
if c.Tag() != names.NewMachineTag(BootstrapMachineId) {
return nil, nil, fmt.Errorf("InitializeState not called with bootstrap machine's configuration")
}
servingInfo, ok := c.StateServingInfo()
Expand Down
5 changes: 3 additions & 2 deletions agent/bootstrap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,10 @@ 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, names.NewMachineTag("0")))
machine0 := names.NewMachineTag("0")
newCfg, err := agent.ReadConfig(agent.ConfigPath(dataDir, machine0))
c.Assert(err, gc.IsNil)
c.Assert(newCfg.Tag(), gc.Equals, "machine-0")
c.Assert(newCfg.Tag(), gc.Equals, machine0)
c.Assert(agent.Password(newCfg), gc.Not(gc.Equals), pwHash)
c.Assert(agent.Password(newCfg), gc.Not(gc.Equals), testing.DefaultMongoPassword)
info, ok := cfg.MongoInfo()
Expand Down
8 changes: 2 additions & 6 deletions agent/format_whitebox_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,7 @@ func (*formatSuite) TestWriteAgentConfig(c *gc.C) {
err := config.Write()
c.Assert(err, gc.IsNil)

tag, err := names.ParseTag(config.Tag())
c.Assert(err, gc.IsNil)
configPath := ConfigPath(config.DataDir(), tag)
configPath := ConfigPath(config.DataDir(), config.Tag())
formatPath := filepath.Join(config.Dir(), legacyFormatFilename)
assertFileExists(c, configPath)
assertFileNotExist(c, formatPath)
Expand Down Expand Up @@ -93,9 +91,7 @@ func (*formatSuite) TestReadWriteStateConfig(c *gc.C) {
func assertWriteAndRead(c *gc.C, config *configInternal) {
err := config.Write()
c.Assert(err, gc.IsNil)
tag, err := names.ParseTag(config.Tag())
c.Assert(err, gc.IsNil)
configPath := ConfigPath(config.DataDir(), tag)
configPath := ConfigPath(config.DataDir(), config.Tag())
readConfig, err := ReadConfig(configPath)
c.Assert(err, gc.IsNil)
c.Assert(readConfig, jc.DeepEquals, config)
Expand Down
18 changes: 5 additions & 13 deletions cmd/jujud/agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -400,11 +400,8 @@ func (e *errorAPIOpener) OpenAPI(_ api.DialOpts) (*api.State, string, error) {
return nil, "", e.err
}

func (s *agentSuite) assertCanOpenState(c *gc.C, tag, dataDir string) {
// TODO(dfc) tag should be a Tag not a string
t, err := names.ParseTag(tag)
c.Assert(err, gc.IsNil)
config, err := agent.ReadConfig(agent.ConfigPath(dataDir, t))
func (s *agentSuite) assertCanOpenState(c *gc.C, tag names.Tag, dataDir string) {
config, err := agent.ReadConfig(agent.ConfigPath(dataDir, tag))
c.Assert(err, gc.IsNil)
info, ok := config.MongoInfo()
c.Assert(ok, jc.IsTrue)
Expand All @@ -413,20 +410,15 @@ func (s *agentSuite) assertCanOpenState(c *gc.C, tag, dataDir string) {
st.Close()
}

func (s *agentSuite) assertCannotOpenState(c *gc.C, tag, dataDir string) {
// TODO(dfc) tag should be a Tag not a string
t, err := names.ParseTag(tag)
c.Assert(err, gc.IsNil)
config, err := agent.ReadConfig(agent.ConfigPath(dataDir, t))
func (s *agentSuite) assertCannotOpenState(c *gc.C, tag names.Tag, dataDir string) {
config, err := agent.ReadConfig(agent.ConfigPath(dataDir, tag))
c.Assert(err, gc.IsNil)
_, ok := config.MongoInfo()
c.Assert(ok, jc.IsFalse)
}

func refreshConfig(c *gc.C, config agent.Config) agent.ConfigSetterWriter {
tag, err := names.ParseTag(config.Tag())
c.Assert(err, gc.IsNil)
config1, err := agent.ReadConfig(agent.ConfigPath(config.DataDir(), tag))
config1, err := agent.ReadConfig(agent.ConfigPath(config.DataDir(), config.Tag()))
c.Assert(err, gc.IsNil)
return config1
}
3 changes: 2 additions & 1 deletion cmd/jujud/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -683,7 +683,8 @@ func openState(agentConfig agent.Config, dialOpts mongo.DialOpts) (_ *state.Stat
st.Close()
}
}()
m0, err := st.FindEntity(agentConfig.Tag())
// TODO(dfc)
m0, err := st.FindEntity(agentConfig.Tag().String())
if err != nil {
if errors.IsNotFound(err) {
err = worker.ErrTerminateAgent
Expand Down
4 changes: 1 addition & 3 deletions cmd/jujud/machine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -683,9 +683,7 @@ func (s *MachineSuite) TestManageEnvironServesAPI(c *gc.C) {
st, err := api.Open(conf.APIInfo(), fastDialOpts)
c.Assert(err, gc.IsNil)
defer st.Close()
tag, err := names.ParseMachineTag(conf.Tag())
c.Assert(err, gc.IsNil)
m, err := st.Machiner().Machine(tag)
m, err := st.Machiner().Machine(conf.Tag().(names.MachineTag))
c.Assert(err, gc.IsNil)
c.Assert(m.Life(), gc.Equals, params.Alive)
})
Expand Down
10 changes: 6 additions & 4 deletions state/api/logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ package logger
import (
"fmt"

"github.com/juju/names"

"github.com/juju/juju/state/api/base"
"github.com/juju/juju/state/api/params"
"github.com/juju/juju/state/api/watcher"
Expand All @@ -28,10 +30,10 @@ func NewState(caller base.Caller) *State {

// LoggingConfig returns the loggo configuration string for the agent
// specified by agentTag.
func (st *State) LoggingConfig(agentTag string) (string, error) {
func (st *State) LoggingConfig(agentTag names.Tag) (string, error) {
var results params.StringResults
args := params.Entities{
Entities: []params.Entity{{Tag: agentTag}},
Entities: []params.Entity{{Tag: agentTag.String()}},
}
err := st.call("LoggingConfig", args, &results)
if err != nil {
Expand All @@ -51,10 +53,10 @@ func (st *State) LoggingConfig(agentTag string) (string, error) {

// WatchLoggingConfig returns a notify watcher that looks for changes in the
// logging-config for the agent specifed by agentTag.
func (st *State) WatchLoggingConfig(agentTag string) (watcher.NotifyWatcher, error) {
func (st *State) WatchLoggingConfig(agentTag names.Tag) (watcher.NotifyWatcher, error) {
var results params.NotifyWatchResults
args := params.Entities{
Entities: []params.Entity{{Tag: agentTag}},
Entities: []params.Entity{{Tag: agentTag.String()}},
}
err := st.call("WatchLoggingConfig", args, &results)
if err != nil {
Expand Down
7 changes: 4 additions & 3 deletions state/api/logger/logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package logger_test

import (
"github.com/juju/names"
gc "launchpad.net/gocheck"

jujutesting "github.com/juju/juju/juju/testing"
Expand Down Expand Up @@ -38,13 +39,13 @@ func (s *loggerSuite) SetUpTest(c *gc.C) {
}

func (s *loggerSuite) TestLoggingConfigWrongMachine(c *gc.C) {
config, err := s.logger.LoggingConfig("42")
config, err := s.logger.LoggingConfig(names.NewMachineTag("42"))
c.Assert(err, gc.ErrorMatches, "permission denied")
c.Assert(config, gc.Equals, "")
}

func (s *loggerSuite) TestLoggingConfig(c *gc.C) {
config, err := s.logger.LoggingConfig(s.rawMachine.Tag().String())
config, err := s.logger.LoggingConfig(s.rawMachine.Tag())
c.Assert(err, gc.IsNil)
c.Assert(config, gc.Not(gc.Equals), "")
}
Expand All @@ -55,7 +56,7 @@ func (s *loggerSuite) setLoggingConfig(c *gc.C, loggingConfig string) {
}

func (s *loggerSuite) TestWatchLoggingConfig(c *gc.C) {
watcher, err := s.logger.WatchLoggingConfig(s.rawMachine.Tag().String())
watcher, err := s.logger.WatchLoggingConfig(s.rawMachine.Tag())
c.Assert(err, gc.IsNil)
defer testing.AssertStop(c, watcher)
wc := testing.NewNotifyWatcherC(c, s.BackingState, watcher)
Expand Down
10 changes: 6 additions & 4 deletions worker/authenticationworker/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/juju/errors"
"github.com/juju/loggo"
"github.com/juju/names"
"github.com/juju/utils/set"
"launchpad.net/tomb"

Expand All @@ -27,7 +28,7 @@ var logger = loggo.GetLogger("juju.worker.authenticationworker")
type keyupdaterWorker struct {
st *keyupdater.State
tomb tomb.Tomb
tag string
tag names.Tag
// jujuKeys are the most recently retrieved keys from state.
jujuKeys set.Strings
// nonJujuKeys are those added externally to auth keys file
Expand All @@ -48,7 +49,8 @@ func NewWorker(st *keyupdater.State, agentConfig agent.Config) worker.Worker {
// SetUp is defined on the worker.NotifyWatchHandler interface.
func (kw *keyupdaterWorker) SetUp() (watcher.NotifyWatcher, error) {
// Record the keys Juju knows about.
jujuKeys, err := kw.st.AuthorisedKeys(kw.tag)
// TODO(dfc)
jujuKeys, err := kw.st.AuthorisedKeys(kw.tag.String())
if err != nil {
return nil, errors.LoggedErrorf(logger, "reading Juju ssh keys for %q: %v", kw.tag, err)
}
Expand All @@ -72,7 +74,7 @@ func (kw *keyupdaterWorker) SetUp() (watcher.NotifyWatcher, error) {
return nil, errors.LoggedErrorf(logger, "adding current Juju keys to ssh authorised keys: %v", err)
}

w, err := kw.st.WatchAuthorisedKeys(kw.tag)
w, err := kw.st.WatchAuthorisedKeys(kw.tag.String())
if err != nil {
return nil, errors.LoggedErrorf(logger, "starting key updater worker: %v", err)
}
Expand All @@ -95,7 +97,7 @@ func (kw *keyupdaterWorker) writeSSHKeys(jujuKeys []string) error {
// Handle is defined on the worker.NotifyWatchHandler interface.
func (kw *keyupdaterWorker) Handle() error {
// Read the keys that Juju has.
newKeys, err := kw.st.AuthorisedKeys(kw.tag)
newKeys, err := kw.st.AuthorisedKeys(kw.tag.String())
if err != nil {
return errors.LoggedErrorf(logger, "reading Juju ssh keys for %q: %v", kw.tag, err)
}
Expand Down
19 changes: 10 additions & 9 deletions worker/authenticationworker/worker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
stdtesting "testing"
"time"

"github.com/juju/names"
gc "launchpad.net/gocheck"

"github.com/juju/juju/agent"
Expand Down Expand Up @@ -76,14 +77,14 @@ func stop(c *gc.C, w worker.Worker) {
type mockConfig struct {
agent.Config
c *gc.C
tag string
tag names.Tag
}

func (mock *mockConfig) Tag() string {
func (mock *mockConfig) Tag() names.Tag {
return mock.tag
}

func agentConfig(c *gc.C, tag string) *mockConfig {
func agentConfig(c *gc.C, tag names.MachineTag) *mockConfig {
return &mockConfig{c: c, tag: tag}
}

Expand Down Expand Up @@ -114,7 +115,7 @@ func (s *workerSuite) waitSSHKeys(c *gc.C, expected []string) {
}

func (s *workerSuite) TestKeyUpdateRetainsExisting(c *gc.C) {
authWorker := authenticationworker.NewWorker(s.keyupdaterApi, agentConfig(c, s.machine.Tag().String()))
authWorker := authenticationworker.NewWorker(s.keyupdaterApi, agentConfig(c, s.machine.Tag().(names.MachineTag)))
defer stop(c, authWorker)

newKey := sshtesting.ValidKeyThree.Key + " user@host"
Expand All @@ -127,15 +128,15 @@ func (s *workerSuite) TestNewKeysInJujuAreSavedOnStartup(c *gc.C) {
newKey := sshtesting.ValidKeyThree.Key + " user@host"
s.setAuthorisedKeys(c, newKey)

authWorker := authenticationworker.NewWorker(s.keyupdaterApi, agentConfig(c, s.machine.Tag().String()))
authWorker := authenticationworker.NewWorker(s.keyupdaterApi, agentConfig(c, s.machine.Tag().(names.MachineTag)))
defer stop(c, authWorker)

newKeyWithCommentPrefix := sshtesting.ValidKeyThree.Key + " Juju:user@host"
s.waitSSHKeys(c, append(s.existingKeys, newKeyWithCommentPrefix))
}

func (s *workerSuite) TestDeleteKey(c *gc.C) {
authWorker := authenticationworker.NewWorker(s.keyupdaterApi, agentConfig(c, s.machine.Tag().String()))
authWorker := authenticationworker.NewWorker(s.keyupdaterApi, agentConfig(c, s.machine.Tag().(names.MachineTag)))
defer stop(c, authWorker)

// Add another key
Expand All @@ -150,7 +151,7 @@ func (s *workerSuite) TestDeleteKey(c *gc.C) {
}

func (s *workerSuite) TestMultipleChanges(c *gc.C) {
authWorker := authenticationworker.NewWorker(s.keyupdaterApi, agentConfig(c, s.machine.Tag().String()))
authWorker := authenticationworker.NewWorker(s.keyupdaterApi, agentConfig(c, s.machine.Tag().(names.MachineTag)))
defer stop(c, authWorker)
s.waitSSHKeys(c, append(s.existingKeys, s.existingEnvKey))

Expand All @@ -163,7 +164,7 @@ func (s *workerSuite) TestMultipleChanges(c *gc.C) {
}

func (s *workerSuite) TestWorkerRestart(c *gc.C) {
authWorker := authenticationworker.NewWorker(s.keyupdaterApi, agentConfig(c, s.machine.Tag().String()))
authWorker := authenticationworker.NewWorker(s.keyupdaterApi, agentConfig(c, s.machine.Tag().(names.MachineTag)))
defer stop(c, authWorker)
s.waitSSHKeys(c, append(s.existingKeys, s.existingEnvKey))

Expand All @@ -174,7 +175,7 @@ func (s *workerSuite) TestWorkerRestart(c *gc.C) {
s.setAuthorisedKeys(c, sshtesting.ValidKeyThree.Key+" yetanother@host")

// Restart the worker and check that the ssh auth keys are as expected.
authWorker = authenticationworker.NewWorker(s.keyupdaterApi, agentConfig(c, s.machine.Tag().String()))
authWorker = authenticationworker.NewWorker(s.keyupdaterApi, agentConfig(c, s.machine.Tag().(names.MachineTag)))
defer stop(c, authWorker)

yetAnotherKeyWithCommentPrefix := sshtesting.ValidKeyThree.Key + " Juju:yetanother@host"
Expand Down
11 changes: 6 additions & 5 deletions worker/deployer/deployer.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package deployer
import (
"fmt"

"github.com/juju/errors"
"github.com/juju/loggo"
"github.com/juju/names"
"github.com/juju/utils"
Expand Down Expand Up @@ -62,12 +63,12 @@ func NewDeployer(st *apideployer.State, ctx Context) worker.Worker {
}

func (d *Deployer) SetUp() (watcher.StringsWatcher, error) {
machineTag := d.ctx.AgentConfig().Tag()
tag, err := names.ParseMachineTag(machineTag)
if err != nil {
return nil, err
tag := d.ctx.AgentConfig().Tag()
machineTag, ok := tag.(names.MachineTag)
if !ok {
return nil, errors.Errorf("expected names.MachineTag, got %T", tag)
}
machine, err := d.st.Machine(tag)
machine, err := d.st.Machine(machineTag)
if err != nil {
return nil, err
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ var _ worker.NotifyWatchHandler = (*MachineEnvironmentWorker)(nil)
func NewMachineEnvironmentWorker(api *environment.Facade, agentConfig agent.Config) worker.Worker {
// We don't write out system files for the local provider on machine zero
// as that is the host machine.
writeSystemFiles := (agentConfig.Tag() != names.NewMachineTag("0").String() ||
writeSystemFiles := (agentConfig.Tag() != names.NewMachineTag("0") ||
agentConfig.Value(agent.ProviderType) != provider.Local)
logger.Debugf("write system files: %v", writeSystemFiles)
envWorker := &MachineEnvironmentWorker{
Expand Down
Loading

0 comments on commit 150e65f

Please sign in to comment.