func (s *ToolsSuite) SetUpTest(c *C) { s.dataDir = c.MkDir() s.toolsDir = agent.SharedToolsDir(s.dataDir, version.Current) err := os.MkdirAll(s.toolsDir, 0755) c.Assert(err, IsNil) err = os.Symlink(s.toolsDir, agent.ToolsDir(s.dataDir, "unit-u-123")) c.Assert(err, IsNil) }
func (env *localEnviron) setupLocalMachineAgent(cons constraints.Value) error { dataDir := env.config.rootDir() toolList, err := environs.FindBootstrapTools(env, cons) if err != nil { return err } // ensure we have at least one valid tools if len(toolList) == 0 { return fmt.Errorf("No bootstrap tools found") } // unpack the first tools into the agent dir. tools := toolList[0] logger.Debugf("tools: %#v", tools) // brutally abuse our knowledge of storage to directly open the file toolsUrl, err := url.Parse(tools.URL) toolsLocation := filepath.Join(env.config.storageDir(), toolsUrl.Path) logger.Infof("tools location: %v", toolsLocation) toolsFile, err := os.Open(toolsLocation) defer toolsFile.Close() // Again, brutally abuse our knowledge here. // The tools that FindBootstrapTools has returned us are based on the // default series in the config. However we are running potentially on a // different series. When the machine agent is started, it will be // looking based on the current series, so we need to override the series // returned in the tools to be the current series. tools.Binary.Series = version.CurrentSeries() err = agent.UnpackTools(dataDir, tools, toolsFile) machineId := "0" // Always machine 0 tag := state.MachineTag(machineId) toolsDir := agent.SharedToolsDir(dataDir, tools.Binary) logDir := env.config.logDir() logConfig := "--debug" // TODO(thumper): specify loggo config agent := upstart.MachineAgentUpstartService( env.machineAgentServiceName(), toolsDir, dataDir, logDir, tag, machineId, logConfig, env.config.Type()) agent.Env["USER"] = env.config.user agent.Env["HOME"] = os.Getenv("HOME") agent.Env["JUJU_STORAGE_DIR"] = env.config.storageDir() agent.Env["JUJU_STORAGE_ADDR"] = env.config.storageAddr() agent.Env["JUJU_SHARED_STORAGE_DIR"] = env.config.sharedStorageDir() agent.Env["JUJU_SHARED_STORAGE_ADDR"] = env.config.sharedStorageAddr() agent.InitDir = upstartScriptLocation logger.Infof("installing service %s to %s", env.machineAgentServiceName(), agent.InitDir) if err := agent.Install(); err != nil { logger.Errorf("could not install machine agent service: %v", err) return err } return nil }
// assertToolsContents asserts that the directory for the tools // has the given contents. func (t *ToolsSuite) assertToolsContents(c *C, tools *state.Tools, files []*testing.TarFile) { var wantNames []string for _, f := range files { wantNames = append(wantNames, f.Header.Name) } wantNames = append(wantNames, urlFile) dir := agent.SharedToolsDir(t.dataDir, tools.Binary) assertDirNames(c, dir, wantNames) assertFileContents(c, dir, urlFile, tools.URL, 0200) for _, f := range files { assertFileContents(c, dir, f.Header.Name, f.Contents, 0400) } gotTools, err := agent.ReadTools(t.dataDir, tools.Binary) c.Assert(err, IsNil) c.Assert(*gotTools, Equals, *tools) }
func (t *ToolsSuite) TestReadToolsErrors(c *C) { vers := version.MustParseBinary("1.2.3-precise-amd64") tools, err := agent.ReadTools(t.dataDir, vers) c.Assert(tools, IsNil) c.Assert(err, ErrorMatches, "cannot read URL in tools directory: .*") dir := agent.SharedToolsDir(t.dataDir, vers) err = os.MkdirAll(dir, 0755) c.Assert(err, IsNil) err = ioutil.WriteFile(filepath.Join(dir, urlFile), []byte(" \t\n"), 0644) c.Assert(err, IsNil) tools, err = agent.ReadTools(t.dataDir, vers) c.Assert(tools, IsNil) c.Assert(err, ErrorMatches, "empty URL in tools directory.*") }
func (s *lxcProvisionerSuite) SetUpTest(c *C) { s.CommonProvisionerSuite.SetUpTest(c) s.lxcSuite.SetUpTest(c) // Write the tools file. toolsDir := agent.SharedToolsDir(s.DataDir(), version.Current) c.Assert(os.MkdirAll(toolsDir, 0755), IsNil) urlPath := filepath.Join(toolsDir, "downloaded-url.txt") err := ioutil.WriteFile(urlPath, []byte("http://testing.invalid/tools"), 0644) c.Assert(err, IsNil) // The lxc provisioner actually needs the machine it is being created on // to be in state, in order to get the watcher. m, err := s.State.AddMachine(config.DefaultSeries, state.JobHostUnits) c.Assert(err, IsNil) s.machineId = m.Id() s.events = make(chan mock.Event, 25) s.Factory.AddListener(s.events) }
func (s *UpgraderSuite) TestUpgrader(c *C) { for i, test := range upgraderTests { c.Logf("\ntest %d: %s", i, test.about) // Note: primeTools sets version.Current... currentTools := s.primeTools(c, test.current) // ...but it also puts tools in storage we don't need, which is why we // don't clean up garbage from earlier runs first. envtesting.RemoveAllTools(c, s.Conn.Environ) uploaded := make(map[version.Binary]*state.Tools) for _, vers := range test.available { tools := s.uploadTools(c, vers) uploaded[vers] = tools } func() { u := s.startUpgrader(c, currentTools) defer u.Stop() s.proposeVersion(c, test.propose) s.State.StartSync() if test.upgrade.Number == version.Zero { assertNothingHappens(c, u) c.Assert(u.Stop(), IsNil) return } ug := waitDeath(c, u) tools := uploaded[test.upgrade] c.Check(ug.NewTools, DeepEquals, tools) c.Check(ug.OldTools.Binary, Equals, version.Current) c.Check(ug.DataDir, Equals, s.DataDir()) c.Check(ug.AgentName, Equals, "testagent") // Check that the upgraded version was really downloaded. path := agent.SharedToolsDir(s.DataDir(), tools.Binary) data, err := ioutil.ReadFile(filepath.Join(path, "jujud")) c.Check(err, IsNil) c.Check(string(data), Equals, "jujud contents "+tools.Binary.String()) }() } }
func (fix *SimpleToolsFixture) SetUp(c *C, dataDir string) { fix.dataDir = dataDir fix.initDir = c.MkDir() fix.logDir = c.MkDir() fix.syslogConfigDir = c.MkDir() toolsDir := agent.SharedToolsDir(fix.dataDir, version.Current) err := os.MkdirAll(toolsDir, 0755) c.Assert(err, IsNil) jujudPath := filepath.Join(toolsDir, "jujud") err = ioutil.WriteFile(jujudPath, []byte(fakeJujud), 0755) c.Assert(err, IsNil) urlPath := filepath.Join(toolsDir, "downloaded-url.txt") err = ioutil.WriteFile(urlPath, []byte("http://testing.invalid/tools"), 0644) c.Assert(err, IsNil) fix.binDir = c.MkDir() fix.origPath = os.Getenv("PATH") os.Setenv("PATH", fix.binDir+":"+fix.origPath) fix.makeBin(c, "status", `echo "blah stop/waiting"`) fix.makeBin(c, "stopped-status", `echo "blah stop/waiting"`) fix.makeBin(c, "started-status", `echo "blah start/running, process 666"`) fix.makeBin(c, "start", "cp $(which started-status) $(which status)") fix.makeBin(c, "stop", "cp $(which stopped-status) $(which status)") }
func (cfg *MachineConfig) jujuTools() string { return agent.SharedToolsDir(cfg.DataDir, cfg.Tools.Binary) }
func (t *ToolsSuite) TestSharedToolsDir(c *C) { dir := agent.SharedToolsDir("/var/lib/juju", version.MustParseBinary("1.2.3-precise-amd64")) c.Assert(dir, Equals, "/var/lib/juju/tools/1.2.3-precise-amd64") }