Exemple #1
0
func (u *Uniter) init(name string) (err error) {
	defer trivial.ErrorContextf(&err, "failed to initialize uniter for unit %q", name)
	u.unit, err = u.st.Unit(name)
	if err != nil {
		return err
	}
	ename := u.unit.EntityName()
	u.toolsDir = environs.AgentToolsDir(u.dataDir, ename)
	if err := EnsureJujucSymlinks(u.toolsDir); err != nil {
		return err
	}
	u.baseDir = filepath.Join(u.dataDir, "agents", ename)
	u.relationsDir = filepath.Join(u.baseDir, "state", "relations")
	if err := os.MkdirAll(u.relationsDir, 0755); err != nil {
		return err
	}
	u.service, err = u.st.Service(u.unit.ServiceName())
	if err != nil {
		return err
	}
	u.relationers = map[int]*Relationer{}
	u.relationHooks = make(chan hook.Info)
	u.charm = charm.NewGitDir(filepath.Join(u.baseDir, "charm"))
	u.bundles = charm.NewBundlesDir(filepath.Join(u.baseDir, "state", "bundles"))
	u.deployer = charm.NewDeployer(filepath.Join(u.baseDir, "state", "deployer"))
	u.sf = NewStateFile(filepath.Join(u.baseDir, "state", "uniter"))
	u.rand = rand.New(rand.NewSource(time.Now().Unix()))
	return nil
}
Exemple #2
0
func (fix *SimpleToolsFixture) paths(entityName, xName string) (confPath, agentDir, toolsDir string) {
	confName := fmt.Sprintf("jujud-%s:%s.conf", xName, entityName)
	confPath = filepath.Join(fix.initDir, confName)
	agentDir = environs.AgentDir(fix.dataDir, entityName)
	toolsDir = environs.AgentToolsDir(fix.dataDir, entityName)
	return
}
Exemple #3
0
func (s *ToolsSuite) SetUpTest(c *C) {
	s.dataDir = c.MkDir()
	s.toolsDir = environs.ToolsDir(s.dataDir, version.Current)
	err := os.MkdirAll(s.toolsDir, 0755)
	c.Assert(err, IsNil)
	err = os.Symlink(s.toolsDir, environs.AgentToolsDir(s.dataDir, "unit-u-123"))
	c.Assert(err, IsNil)
}
Exemple #4
0
func (t *ToolsSuite) TestChangeAgentTools(c *C) {
	files := []*testing.TarFile{
		testing.NewTarFile("jujuc", 0755, "juju executable"),
		testing.NewTarFile("jujud", 0755, "jujuc executable"),
	}
	tools := &state.Tools{
		URL:    "http://foo/bar1",
		Binary: version.MustParseBinary("1.2.3-foo-bar"),
	}
	err := environs.UnpackTools(t.dataDir, tools, bytes.NewReader(testing.TarGz(files...)))
	c.Assert(err, IsNil)

	gotTools, err := environs.ChangeAgentTools(t.dataDir, "testagent", tools.Binary)
	c.Assert(err, IsNil)
	c.Assert(*gotTools, Equals, *tools)

	assertDirNames(c, t.toolsDir(), []string{"1.2.3-foo-bar", "testagent"})
	assertDirNames(c, environs.AgentToolsDir(t.dataDir, "testagent"), []string{"jujuc", "jujud", urlFile})

	// Upgrade again to check that the link replacement logic works ok.
	files2 := []*testing.TarFile{
		testing.NewTarFile("foo", 0755, "foo content"),
		testing.NewTarFile("bar", 0755, "bar content"),
	}
	tools2 := &state.Tools{
		URL:    "http://foo/bar2",
		Binary: version.MustParseBinary("1.2.4-foo-bar"),
	}
	err = environs.UnpackTools(t.dataDir, tools2, bytes.NewReader(testing.TarGz(files2...)))
	c.Assert(err, IsNil)

	gotTools, err = environs.ChangeAgentTools(t.dataDir, "testagent", tools2.Binary)
	c.Assert(err, IsNil)
	c.Assert(*gotTools, Equals, *tools2)

	assertDirNames(c, t.toolsDir(), []string{"1.2.3-foo-bar", "1.2.4-foo-bar", "testagent"})
	assertDirNames(c, environs.AgentToolsDir(t.dataDir, "testagent"), []string{"foo", "bar", urlFile})
}
Exemple #5
0
func (s *UniterSuite) SetUpSuite(c *C) {
	s.JujuConnSuite.SetUpSuite(c)
	s.HTTPSuite.SetUpSuite(c)
	s.dataDir = c.MkDir()
	toolsDir := environs.AgentToolsDir(s.dataDir, "unit-u-0")
	err := os.MkdirAll(toolsDir, 0755)
	c.Assert(err, IsNil)
	cmd := exec.Command("go", "build", "launchpad.net/juju-core/cmd/jujuc")
	cmd.Dir = toolsDir
	out, err := cmd.CombinedOutput()
	c.Logf(string(out))
	c.Assert(err, IsNil)
	s.oldLcAll = os.Getenv("LC_ALL")
	os.Setenv("LC_ALL", "en_US")
}
Exemple #6
0
func (s *UpgraderSuite) TestUpgraderReadyErrorUpgrade(c *C) {
	currentTools := s.primeTools(c, version.MustParseBinary("2.0.2-foo-bar"))
	ug := &UpgradeReadyError{
		AgentName: "foo",
		OldTools:  &state.Tools{Binary: version.MustParseBinary("2.0.0-foo-bar")},
		NewTools:  currentTools,
		DataDir:   s.DataDir(),
	}
	err := ug.ChangeAgentTools()
	c.Assert(err, IsNil)
	d := environs.AgentToolsDir(s.DataDir(), "foo")
	data, err := ioutil.ReadFile(filepath.Join(d, "jujud"))
	c.Assert(err, IsNil)
	c.Assert(string(data), Equals, "jujud contents 2.0.2-foo-bar")
}
Exemple #7
0
func (mgr *SimpleManager) RecallUnit(unitName string) error {
	svc := mgr.upstartService(unitName)
	if !svc.Installed() {
		return fmt.Errorf("unit %q is not deployed", unitName)
	}
	if err := svc.Remove(); err != nil {
		return err
	}
	entityName := state.UnitEntityName(unitName)
	agentDir := environs.AgentDir(mgr.DataDir, entityName)
	if err := os.RemoveAll(agentDir); err != nil {
		return err
	}
	toolsDir := environs.AgentToolsDir(mgr.DataDir, entityName)
	return os.Remove(toolsDir)
}
Exemple #8
0
func (mgr *SimpleManager) DeployUnit(unitName, initialPassword string) (err error) {
	// Check sanity.
	svc := mgr.upstartService(unitName)
	if svc.Installed() {
		return fmt.Errorf("unit %q is already deployed", unitName)
	}

	// Link the current tools for use by the new agent.
	entityName := state.UnitEntityName(unitName)
	_, err = environs.ChangeAgentTools(mgr.DataDir, entityName, version.Current)
	toolsDir := environs.AgentToolsDir(mgr.DataDir, entityName)
	defer removeOnErr(&err, toolsDir)

	info := *mgr.StateInfo
	// Prepare the agent's configuration data.
	conf := &agent.Conf{
		DataDir:     mgr.DataDir,
		OldPassword: initialPassword,
		StateInfo:   &info,
	}
	conf.StateInfo.EntityName = entityName
	conf.StateInfo.Password = ""
	if err := conf.Write(); err != nil {
		return err
	}
	defer removeOnErr(&err, conf.Dir())

	// Install an upstart job that runs the unit agent.
	logPath := filepath.Join(mgr.LogDir, entityName+".log")
	cmd := strings.Join([]string{
		filepath.Join(toolsDir, "jujud"), "unit",
		"--data-dir", conf.DataDir,
		"--unit-name", unitName,
		"--debug", // TODO: propagate debug state sensibly
	}, " ")
	uconf := &upstart.Conf{
		Service: *svc,
		Desc:    "juju unit agent for " + unitName,
		Cmd:     cmd,
		Out:     logPath,
	}
	return uconf.Install()
}
Exemple #9
0
func addAgentToBoot(c *cloudinit.Config, cfg *MachineConfig, kind, entityName, args string) (*agent.Conf, error) {
	acfg, err := addAgentInfo(c, cfg, entityName)
	if err != nil {
		return nil, err
	}

	// Make the agent run via a symbolic link to the actual tools
	// directory, so it can upgrade itself without needing to change
	// the upstart script.
	toolsDir := environs.AgentToolsDir(cfg.DataDir, entityName)
	// TODO(dfc) ln -nfs, so it doesn't fail if for some reason that the target already exists
	addScripts(c, fmt.Sprintf("ln -s %v %s", cfg.Tools.Binary, shquote(toolsDir)))

	svc := upstart.NewService("jujud-" + entityName)
	logPath := fmt.Sprintf("/var/log/juju/%s.log", entityName)
	cmd := fmt.Sprintf(
		"%s/jujud %s"+
			" --log-file %s"+
			" --data-dir '%s'"+
			" %s",
		toolsDir, kind,
		logPath,
		cfg.DataDir,
		args,
	)
	conf := &upstart.Conf{
		Service: *svc,
		Desc:    fmt.Sprintf("juju %s agent", entityName),
		Cmd:     cmd,
		Out:     logPath,
	}
	cmds, err := conf.InstallCommands()
	if err != nil {
		return nil, fmt.Errorf("cannot make cloud-init upstart script for the %s agent: %v", entityName, err)
	}
	addScripts(c, cmds...)
	return acfg, nil
}