func (fix *SimpleToolsFixture) paths(tag string) (confPath, agentDir, toolsDir string) { confName := fmt.Sprintf("jujud-%s.conf", tag) confPath = filepath.Join(fix.initDir, confName) agentDir = agent.Dir(fix.dataDir, tag) toolsDir = tools.ToolsDir(fix.dataDir, tag) return }
func (s *ToolsSuite) SetUpTest(c *gc.C) { s.dataDir = c.MkDir() s.toolsDir = tools.SharedToolsDir(s.dataDir, version.Current) err := os.MkdirAll(s.toolsDir, 0755) c.Assert(err, gc.IsNil) err = os.Symlink(s.toolsDir, tools.ToolsDir(s.dataDir, "unit-u-123")) c.Assert(err, gc.IsNil) }
func (t *ToolsSuite) TestChangeAgentTools(c *gc.C) { files := []*testing.TarFile{ testing.NewTarFile("jujuc", 0755, "juju executable"), testing.NewTarFile("jujud", 0755, "jujuc executable"), } data, checksum := testing.TarGz(files...) testTools := &coretest.Tools{ URL: "http://foo/bar1", Version: version.MustParseBinary("1.2.3-foo-bar"), Size: int64(len(data)), SHA256: checksum, } err := agenttools.UnpackTools(t.dataDir, testTools, bytes.NewReader(data)) c.Assert(err, gc.IsNil) gotTools, err := agenttools.ChangeAgentTools(t.dataDir, "testagent", testTools.Version) c.Assert(err, gc.IsNil) c.Assert(*gotTools, gc.Equals, *testTools) assertDirNames(c, t.toolsDir(), []string{"1.2.3-foo-bar", "testagent"}) assertDirNames(c, agenttools.ToolsDir(t.dataDir, "testagent"), []string{"jujuc", "jujud", toolsFile}) // 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"), } data2, checksum2 := testing.TarGz(files2...) tools2 := &coretest.Tools{ URL: "http://foo/bar2", Version: version.MustParseBinary("1.2.4-foo-bar"), Size: int64(len(data2)), SHA256: checksum2, } err = agenttools.UnpackTools(t.dataDir, tools2, bytes.NewReader(data2)) c.Assert(err, gc.IsNil) gotTools, err = agenttools.ChangeAgentTools(t.dataDir, "testagent", tools2.Version) c.Assert(err, gc.IsNil) c.Assert(*gotTools, gc.Equals, *tools2) assertDirNames(c, t.toolsDir(), []string{"1.2.3-foo-bar", "1.2.4-foo-bar", "testagent"}) assertDirNames(c, agenttools.ToolsDir(t.dataDir, "testagent"), []string{"foo", "bar", toolsFile}) }
func (ctx *SimpleContext) RecallUnit(unitName string) error { svc := ctx.findUpstartJob(unitName) if svc == nil || !svc.Installed() { return fmt.Errorf("unit %q is not deployed", unitName) } if err := svc.StopAndRemove(); err != nil { return err } tag := names.UnitTag(unitName) dataDir := ctx.agentConfig.DataDir() agentDir := agent.Dir(dataDir, tag) if err := os.RemoveAll(agentDir); err != nil { return err } toolsDir := tools.ToolsDir(dataDir, tag) return os.Remove(toolsDir) }
func (cfg *MachineConfig) addMachineAgentToBoot(c *cloudinit.Config, tag, machineId string) error { // 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 := agenttools.ToolsDir(cfg.DataDir, tag) // TODO(dfc) ln -nfs, so it doesn't fail if for some reason that the target already exists c.AddScripts(fmt.Sprintf("ln -s %v %s", cfg.Tools.Version, shquote(toolsDir))) name := cfg.MachineAgentServiceName conf := upstart.MachineAgentUpstartService(name, toolsDir, cfg.DataDir, cfg.LogDir, tag, machineId, nil) cmds, err := conf.InstallCommands() if err != nil { return errgo.Annotatef(err, "cannot make cloud-init upstart script for the %s agent", tag) } c.AddRunCmd(cloudinit.LogProgressCmd("Starting Juju machine agent (%s)", name)) c.AddScripts(cmds...) return nil }
func (s *UpgraderSuite) TestChangeAgentTools(c *gc.C) { oldTools := &coretools.Tools{ Version: version.MustParseBinary("1.2.3-quantal-amd64"), } stor := s.Conn.Environ.Storage() newTools := envtesting.PrimeTools(c, stor, s.DataDir(), version.MustParseBinary("5.4.3-precise-amd64")) s.PatchValue(&version.Current, newTools.Version) err := envtools.MergeAndWriteMetadata(stor, coretools.List{newTools}, envtools.DoNotWriteMirrors) c.Assert(err, gc.IsNil) ugErr := &upgrader.UpgradeReadyError{ AgentName: "anAgent", OldTools: oldTools.Version, NewTools: newTools.Version, DataDir: s.DataDir(), } err = ugErr.ChangeAgentTools() c.Assert(err, gc.IsNil) link, err := os.Readlink(agenttools.ToolsDir(s.DataDir(), "anAgent")) c.Assert(err, gc.IsNil) c.Assert(link, gc.Equals, newTools.Version.String()) }
func (u *Uniter) init(unitTag string) (err error) { u.unit, err = u.st.Unit(unitTag) if err != nil { return err } if u.unit.Life() == params.Dead { // If we started up already dead, we should not progress further. If we // become Dead immediately after starting up, we may well complete any // operations in progress before detecting it; but that race is fundamental // and inescapable, whereas this one is not. return worker.ErrTerminateAgent } if err = u.setupLocks(); err != nil { return err } u.toolsDir = tools.ToolsDir(u.dataDir, unitTag) if err := EnsureJujucSymlinks(u.toolsDir); err != nil { return err } u.baseDir = filepath.Join(u.dataDir, "agents", unitTag) 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.ServiceTag()) if err != nil { return err } var env *uniter.Environment env, err = u.st.Environment() if err != nil { return err } u.uuid = env.UUID() u.envName = env.Name() u.relationers = map[int]*Relationer{} u.relationHooks = make(chan hook.Info) u.charmPath = filepath.Join(u.baseDir, "charm") deployerPath := filepath.Join(u.baseDir, "state", "deployer") bundles := charm.NewBundlesDir(filepath.Join(u.baseDir, "state", "bundles")) u.deployer, err = charm.NewDeployer(u.charmPath, deployerPath, bundles) if err != nil { return fmt.Errorf("cannot create deployer: %v", err) } u.sf = NewStateFile(filepath.Join(u.baseDir, "state", "uniter")) u.rand = rand.New(rand.NewSource(time.Now().Unix())) // If we start trying to listen for juju-run commands before we have valid // relation state, surprising things will come to pass. if err := u.restoreRelations(); err != nil { return err } runListenerSocketPath := filepath.Join(u.baseDir, RunListenerFile) logger.Debugf("starting juju-run listener on unix:%s", runListenerSocketPath) u.runListener, err = NewRunListener(u, runListenerSocketPath) if err != nil { return err } // The socket needs to have permissions 777 in order for other users to use it. return os.Chmod(runListenerSocketPath, 0777) }
func (ctx *SimpleContext) DeployUnit(unitName, initialPassword string) (err error) { // Check sanity. svc := ctx.upstartService(unitName) if svc.Installed() { return fmt.Errorf("unit %q is already deployed", unitName) } // Link the current tools for use by the new agent. tag := names.UnitTag(unitName) dataDir := ctx.agentConfig.DataDir() logDir := ctx.agentConfig.LogDir() _, err = tools.ChangeAgentTools(dataDir, tag, version.Current) toolsDir := tools.ToolsDir(dataDir, tag) defer removeOnErr(&err, toolsDir) result, err := ctx.api.ConnectionInfo() if err != nil { return err } logger.Debugf("state addresses: %q", result.StateAddresses) logger.Debugf("API addresses: %q", result.APIAddresses) containerType := ctx.agentConfig.Value(agent.ContainerType) namespace := ctx.agentConfig.Value(agent.Namespace) conf, err := agent.NewAgentConfig( agent.AgentConfigParams{ DataDir: dataDir, LogDir: logDir, UpgradedToVersion: version.Current.Number, Tag: tag, Password: initialPassword, Nonce: "unused", // TODO: remove the state addresses here and test when api only. StateAddresses: result.StateAddresses, APIAddresses: result.APIAddresses, CACert: ctx.agentConfig.CACert(), Values: map[string]string{ agent.ContainerType: containerType, agent.Namespace: namespace, }, }) if err != nil { return err } if err := conf.Write(); err != nil { return err } defer removeOnErr(&err, conf.Dir()) // Install an upstart job that runs the unit agent. logPath := path.Join(logDir, tag+".log") cmd := strings.Join([]string{ path.Join(toolsDir, "jujud"), "unit", "--data-dir", dataDir, "--unit-name", unitName, "--debug", // TODO: propagate debug state sensibly }, " ") // TODO(thumper): 2013-09-02 bug 1219630 // As much as I'd like to remove JujuContainerType now, it is still // needed as MAAS still needs it at this stage, and we can't fix // everything at once. uconf := &upstart.Conf{ Service: *svc, Desc: "juju unit agent for " + unitName, Cmd: cmd, Out: logPath, Env: map[string]string{ osenv.JujuContainerTypeEnvKey: containerType, }, } return uconf.Install() }