Skip to content

Commit

Permalink
mongo: always install mongodb in EnsureServer
Browse files Browse the repository at this point in the history
With the recent changes, we were leaving the
installation of mongodb until too late. We now
ensure that mongodb is installed early and we
do this unconditionally.

Fixes https://bugs.launchpad.net/juju-core/+bug/1350700
  • Loading branch information
axw committed Jul 31, 2014
1 parent c438f59 commit d047884
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 18 deletions.
29 changes: 14 additions & 15 deletions mongo/mongo.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,11 +170,12 @@ func EnsureServer(args EnsureServerParams) error {
"Ensuring mongo server is running; data directory %s; port %d",
args.DataDir, args.StatePort,
)
dbDir := filepath.Join(args.DataDir, "db")

dbDir := filepath.Join(args.DataDir, "db")
if err := os.MkdirAll(dbDir, 0700); err != nil {
return fmt.Errorf("cannot create mongo database directory: %v", err)
}

oplogSizeMB := args.OplogSize
if oplogSizeMB == 0 {
var err error
Expand All @@ -183,7 +184,16 @@ func EnsureServer(args EnsureServerParams) error {
}
}

svc, mongoPath, err := upstartService(args.Namespace, args.DataDir, dbDir, args.StatePort, oplogSizeMB)
if err := aptGetInstallMongod(); err != nil {
return fmt.Errorf("cannot install mongod: %v", err)
}
mongoPath, err := Path()
if err != nil {
return err
}
logVersion(mongoPath)

svc, err := upstartService(args.Namespace, args.DataDir, dbDir, mongoPath, args.StatePort, oplogSizeMB)
if err != nil {
return err
}
Expand Down Expand Up @@ -220,12 +230,6 @@ func EnsureServer(args EnsureServerParams) error {
}
}

if err := aptGetInstallMongod(); err != nil {
return fmt.Errorf("cannot install mongod: %v", err)
}

logVersion(mongoPath)

if err := upstartServiceStop(svc); err != nil {
return fmt.Errorf("failed to stop mongo: %v", err)
}
Expand Down Expand Up @@ -282,12 +286,7 @@ func sharedSecretPath(dataDir string) string {
// 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.Service, string, error) {
mongoPath, err := Path()
if err != nil {
return nil, "", err
}

func upstartService(namespace, dataDir, dbDir, mongoPath string, port, oplogSizeMB int) (*upstart.Service, error) {
mongoCmd := mongoPath + " --auth" +
" --dbpath=" + utils.ShQuote(dbDir) +
" --sslOnNormalPorts" +
Expand All @@ -311,7 +310,7 @@ func upstartService(namespace, dataDir, dbDir string, port, oplogSizeMB int) (*u
Cmd: mongoCmd,
}
svc := upstart.NewService(ServiceName(namespace), conf)
return svc, mongoPath, nil
return svc, nil
}

func aptGetInstallMongod() error {
Expand Down
31 changes: 28 additions & 3 deletions mongo/mongo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,26 +295,50 @@ func (s *MongoSuite) TestInstallMongod(c *gc.C) {
}
}

func (s *MongoSuite) TestInstallMongodServiceExists(c *gc.C) {
output := mockShellCommand(c, &s.CleanupSuite, "apt-get")
dataDir := c.MkDir()
namespace := "namespace"

s.PatchValue(mongo.UpstartServiceExists, func(svc *upstart.Service) bool {
return true
})
s.PatchValue(mongo.UpstartServiceRunning, func(svc *upstart.Service) bool {
return true
})
s.PatchValue(mongo.UpstartServiceStart, func(svc *upstart.Service) error {
return fmt.Errorf("shouldn't be called")
})

err := mongo.EnsureServer(makeEnsureServerParams(dataDir, namespace))
c.Assert(err, gc.IsNil)
c.Assert(s.installed, gc.HasLen, 0)

// We still attempt to install mongodb, despite the service existing.
cmds := getMockShellCalls(c, output)
c.Assert(cmds, gc.HasLen, 1)
}

func (s *MongoSuite) TestUpstartServiceWithReplSet(c *gc.C) {
dataDir := c.MkDir()

svc, _, err := mongo.UpstartService("", dataDir, dataDir, 1234, 1024)
svc, err := mongo.UpstartService("", dataDir, dataDir, mongo.JujuMongodPath, 1234, 1024)
c.Assert(err, gc.IsNil)
c.Assert(strings.Contains(svc.Conf.Cmd, "--replSet"), jc.IsTrue)
}

func (s *MongoSuite) TestUpstartServiceIPv6(c *gc.C) {
dataDir := c.MkDir()

svc, _, err := mongo.UpstartService("", dataDir, dataDir, 1234, 1024)
svc, err := mongo.UpstartService("", dataDir, dataDir, mongo.JujuMongodPath, 1234, 1024)
c.Assert(err, gc.IsNil)
c.Assert(strings.Contains(svc.Conf.Cmd, "--ipv6"), jc.IsTrue)
}

func (s *MongoSuite) TestUpstartServiceWithJournal(c *gc.C) {
dataDir := c.MkDir()

svc, _, err := mongo.UpstartService("", dataDir, dataDir, 1234, 1024)
svc, err := mongo.UpstartService("", dataDir, dataDir, mongo.JujuMongodPath, 1234, 1024)
c.Assert(err, gc.IsNil)
journalPresent := strings.Contains(svc.Conf.Cmd, " --journal ") || strings.HasSuffix(svc.Conf.Cmd, " --journal")
c.Assert(journalPresent, jc.IsTrue)
Expand Down Expand Up @@ -356,6 +380,7 @@ func (s *MongoSuite) TestQuantalAptAddRepo(c *gc.C) {
c.Assert(err, gc.ErrorMatches, "cannot install mongod: cannot add apt repository: exit status 1.*")

s.PatchValue(&version.Current.Series, "trusty")
failCmd(filepath.Join(dir, "mongod"))
err = mongo.EnsureServer(makeEnsureServerParams(dir, ""))
c.Assert(err, gc.IsNil)
}
Expand Down

0 comments on commit d047884

Please sign in to comment.