func (s *localJujuTestSuite) makeFakeUpstartScripts(c *gc.C, env environs.Environ, ) (mongoService *upstart.Service, machineAgent *upstart.Service) { upstartDir := c.MkDir() s.PatchValue(&upstart.InitDir, upstartDir) s.MakeTool(c, "start", `echo "some-service start/running, process 123"`) namespace := env.Config().AllAttrs()["namespace"].(string) mongoService = upstart.NewService(mongo.ServiceName(namespace)) mongoConf := upstart.Conf{ Service: *mongoService, Desc: "fake mongo", Cmd: "echo FAKE", } err := mongoConf.Install() c.Assert(err, gc.IsNil) c.Assert(mongoService.Installed(), jc.IsTrue) machineAgent = upstart.NewService(fmt.Sprintf("juju-agent-%s", namespace)) agentConf := upstart.Conf{ Service: *machineAgent, Desc: "fake agent", Cmd: "echo FAKE", } err = agentConf.Install() c.Assert(err, gc.IsNil) c.Assert(machineAgent.Installed(), jc.IsTrue) return mongoService, machineAgent }
// upstartService returns the upstart config for the mongo state service. // It also returns the path to the mongod executable that the upstart config // will be using. func upstartService(namespace, dataDir, dbDir string, port, oplogSizeMB int) (*upstart.Conf, string, error) { svc := upstart.NewService(ServiceName(namespace)) mongoPath, err := Path() if err != nil { return nil, "", err } mongoCmd := mongoPath + " --auth" + " --dbpath=" + utils.ShQuote(dbDir) + " --sslOnNormalPorts" + " --sslPEMKeyFile " + utils.ShQuote(sslKeyPath(dataDir)) + " --sslPEMKeyPassword ignored" + " --port " + fmt.Sprint(port) + " --noprealloc" + " --syslog" + " --smallfiles" + " --journal" + " --keyFile " + utils.ShQuote(sharedSecretPath(dataDir)) + " --replSet " + ReplicaSetName + " --ipv6 " + " --oplogSize " + strconv.Itoa(oplogSizeMB) conf := &upstart.Conf{ Service: *svc, Desc: "juju state database", Limit: map[string]string{ "nofile": fmt.Sprintf("%d %d", maxFiles, maxFiles), "nproc": fmt.Sprintf("%d %d", maxProcs, maxProcs), }, Cmd: mongoCmd, } return conf, mongoPath, nil }
func (a *MachineAgent) uninstallAgent(agentConfig agent.Config) error { var errors []error agentServiceName := agentConfig.Value(agent.AgentServiceName) if agentServiceName == "" { // For backwards compatibility, handle lack of AgentServiceName. agentServiceName = os.Getenv("UPSTART_JOB") } if agentServiceName != "" { if err := upstart.NewService(agentServiceName).Remove(); err != nil { errors = append(errors, fmt.Errorf("cannot remove service %q: %v", agentServiceName, err)) } } // Remove the juju-run symlink. if err := os.Remove(jujuRun); err != nil && !os.IsNotExist(err) { errors = append(errors, err) } namespace := agentConfig.Value(agent.Namespace) if err := mongo.RemoveService(namespace); err != nil { errors = append(errors, fmt.Errorf("cannot stop/remove mongo service with namespace %q: %v", namespace, err)) } if err := os.RemoveAll(agentConfig.DataDir()); err != nil { errors = append(errors, err) } if len(errors) == 0 { return nil } return fmt.Errorf("uninstall failed: %v", errors) }
// upstartService returns an upstart.Service corresponding to the specified // unit. func (ctx *SimpleContext) upstartService(unitName string) *upstart.Service { tag := names.NewUnitTag(unitName).String() svcName := "jujud-" + tag svc := upstart.NewService(svcName) svc.InitDir = ctx.initDir return svc }
// findUpstartJob tries to find an upstart job matching the // given unit name in one of these formats: // jujud-<deployer-tag>:<unit-tag>.conf (for compatibility) // jujud-<unit-tag>.conf (default) func (ctx *SimpleContext) findUpstartJob(unitName string) *upstart.Service { unitsAndJobs, err := ctx.deployedUnitsUpstartJobs() if err != nil { return nil } if job, ok := unitsAndJobs[unitName]; ok { svc := upstart.NewService(job) svc.InitDir = ctx.initDir return svc } return nil }
// RemoveService removes the mongoDB upstart service from this machine. func RemoveService(namespace string) error { svc := upstart.NewService(ServiceName(namespace)) return upstartServiceStopAndRemove(svc) }
// EnsureAdminUser ensures that the specified user and password // are added to the admin database. // // This function will stop the Mongo service if it needs to add // the admin user, as it must restart Mongo in --noauth mode. func EnsureAdminUser(p EnsureAdminUserParams) (added bool, err error) { if len(p.DialInfo.Addrs) > 1 { logger.Infof("more than one state server; admin user must exist") return false, nil } p.DialInfo.Addrs = []string{fmt.Sprintf("127.0.0.1:%d", p.Port)} p.DialInfo.Direct = true // Attempt to login to the admin database first. session, err := mgo.DialWithInfo(p.DialInfo) if err != nil { return false, fmt.Errorf("can't dial mongo to ensure admin user: %v", err) } err = session.DB("admin").Login(p.User, p.Password) session.Close() if err == nil { return false, nil } logger.Debugf("admin login failed: %v", err) // Login failed, so we need to add the user. // Stop mongo, so we can start it in --noauth mode. mongoServiceName := ServiceName(p.Namespace) mongoService := upstart.NewService(mongoServiceName) if err := upstartServiceStop(mongoService); err != nil { return false, fmt.Errorf("failed to stop %v: %v", mongoServiceName, err) } // Start mongod in --noauth mode. logger.Debugf("starting mongo with --noauth") cmd, err := noauthCommand(p.DataDir, p.Port) if err != nil { return false, fmt.Errorf("failed to prepare mongod command: %v", err) } if err := cmd.Start(); err != nil { return false, fmt.Errorf("failed to start mongod: %v", err) } defer cmd.Process.Kill() // Add the user to the admin database. logger.Debugf("setting admin password") if session, err = mgo.DialWithInfo(p.DialInfo); err != nil { return false, fmt.Errorf("can't dial mongo to ensure admin user: %v", err) } err = SetAdminMongoPassword(session, p.User, p.Password) session.Close() if err != nil { return false, fmt.Errorf("failed to add %q to admin database: %v", p.User, err) } logger.Infof("added %q to admin database", p.User) // Restart mongo using upstart. if err := processSignal(cmd.Process, syscall.SIGTERM); err != nil { return false, fmt.Errorf("cannot kill mongod: %v", err) } if err := cmd.Wait(); err != nil { if _, ok := err.(*exec.ExitError); !ok { return false, fmt.Errorf("mongod did not cleanly terminate: %v", err) } } if err := upstartServiceStart(mongoService); err != nil { return false, err } return true, nil }
func (s *UpstartSuite) TestInitDir(c *gc.C) { svc := upstart.NewService("blah") c.Assert(svc.InitDir, gc.Equals, "/etc/init") }