Skip to content

Commit

Permalink
Remove dead code for installing mongo on centos7 controllers
Browse files Browse the repository at this point in the history
Currently, controllers can only be bootstrapped using ubuntu images.
Therefore, the centos-specific code can now be safely removed. Note that
centos7 is supported for running workloads.
  • Loading branch information
achilleasa committed Aug 28, 2019
1 parent bbdd5b5 commit 6dd3336
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 161 deletions.
21 changes: 0 additions & 21 deletions mongo/mongo.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
"github.com/juju/utils/featureflag"
"gopkg.in/mgo.v2"

"github.com/juju/juju/controller"
"github.com/juju/juju/core/network"
"github.com/juju/juju/feature"
"github.com/juju/juju/packaging"
Expand Down Expand Up @@ -709,26 +708,6 @@ func installMongod(operatingsystem string, numaCtl bool, dataDir string) error {
return err
}

// Work around SELinux on centos7
if operatingsystem == "centos7" {
cmd := []string{"chcon", "-R", "-v", "-t", "mongod_var_lib_t", "/var/lib/juju/"}
logger.Infof("running %s %v", cmd[0], cmd[1:])
_, err := utils.RunCommand(cmd[0], cmd[1:]...)
if err != nil {
logger.Errorf("chcon failed to change file security context error %s", err)
return err
}

cmd = []string{"semanage", "port", "-a", "-t", "mongod_port_t", "-p", "tcp", strconv.Itoa(controller.DefaultStatePort)}
logger.Infof("running %s %v", cmd[0], cmd[1:])
_, err = utils.RunCommand(cmd[0], cmd[1:]...)
if err != nil {
if !strings.Contains(err.Error(), "exit status 1") {
logger.Errorf("semanage failed to provide access on port %d error %s", controller.DefaultStatePort, err)
return err
}
}
}
return nil
}

Expand Down
133 changes: 1 addition & 132 deletions mongo/mongo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ import (
"os/exec"
"path"
"path/filepath"
"regexp"
"strconv"
"strings"

"github.com/juju/errors"
Expand All @@ -22,7 +20,6 @@ import (
jc "github.com/juju/testing/checkers"
gc "gopkg.in/check.v1"

"github.com/juju/juju/controller"
"github.com/juju/juju/core/network"
"github.com/juju/juju/mongo"
"github.com/juju/juju/service/common"
Expand Down Expand Up @@ -54,47 +51,15 @@ var testInfo = struct {
}

var expectedArgs = struct {
MongoInstall []jc.SimpleMessage
YumBase []string
AptGetBase []string
Semanage []string
Chcon []string
AptGetBase []string
}{
MongoInstall: []jc.SimpleMessage{
{loggo.INFO, "Ensuring mongo server is running; data directory.*"},
{loggo.INFO, regexp.QuoteMeta(`installing "epel-release" via "yum"`)},
{loggo.INFO, "Running: yum --assumeyes --debuglevel=1 install epel-release"},
{loggo.INFO, regexp.QuoteMeta(`installing "mongodb-server" via "yum"`)},
{loggo.INFO, "Running: yum --assumeyes --debuglevel=1 install mongodb-server"},
},
YumBase: []string{
"--assumeyes",
"--debuglevel=1",
"install",
},
AptGetBase: []string{
"--option=Dpkg::Options::=--force-confold",
"--option=Dpkg::Options::=--force-unsafe-io",
"--assume-yes",
"--quiet",
"install",
},
Semanage: []string{
"port",
"-a",
"-t",
"mongod_port_t",
"-p",
"tcp",
strconv.Itoa(controller.DefaultStatePort),
},
Chcon: []string{
"-R",
"-v",
"-t",
"mongod_var_lib_t",
"/var/lib/juju/",
},
}

func makeEnsureServerParams(dataDir string) mongo.EnsureServerParams {
Expand Down Expand Up @@ -420,106 +385,10 @@ func (s *MongoSuite) TestInstallMongodOnUbuntuViaApt(c *gc.C) {
}
}

func (s *MongoSuite) TestInstallFailChconMongodCentOS(c *gc.C) {
returnCode := 1
execNameFail := "chcon"

exec := []string{"yum", "chcon"}

expectedResult := append(expectedArgs.MongoInstall, []jc.SimpleMessage{
{loggo.INFO, "running " + execNameFail + " .*"},
{loggo.ERROR, execNameFail + " failed to change file security context error exit status " + strconv.Itoa(returnCode)},
{loggo.ERROR, regexp.QuoteMeta("cannot install/upgrade mongod (will proceed anyway): exit status " + strconv.Itoa(returnCode))},
}...)
s.assertSuccessWithInstallStepFailCentOS(c, exec, execNameFail, returnCode, expectedResult)
}

func (s *MongoSuite) TestSemanageRuleExistsDoesNotFail(c *gc.C) {
// if the return code is 1 then the rule already exists and we do not fail
returnCode := 1
execNameFail := "semanage"

exec := []string{"yum", "chcon"}

expectedResult := append(expectedArgs.MongoInstall, []jc.SimpleMessage{
{loggo.INFO, "running chcon .*"},
{loggo.INFO, "running " + execNameFail + " .*"},
}...)

s.assertSuccessWithInstallStepFailCentOS(c, exec, execNameFail, returnCode, expectedResult)
}

func (s *MongoSuite) TestInstallFailSemanageMongodCentOS(c *gc.C) {
returnCode := 2
execNameFail := "semanage"

exec := []string{"yum", "chcon"}

expectedResult := append(expectedArgs.MongoInstall, []jc.SimpleMessage{
{loggo.INFO, "running chcon .*"},
{loggo.INFO, "running " + execNameFail + " .*"},
{loggo.ERROR, execNameFail + " failed to provide access on port " + strconv.Itoa(controller.DefaultStatePort) + " error exit status " + strconv.Itoa(returnCode)},
{loggo.ERROR, regexp.QuoteMeta("cannot install/upgrade mongod (will proceed anyway): exit status " + strconv.Itoa(returnCode))},
}...)
s.assertSuccessWithInstallStepFailCentOS(c, exec, execNameFail, returnCode, expectedResult)
}

func (s *MongoSuite) assertSuccessWithInstallStepFailCentOS(c *gc.C, exec []string, execNameFail string, returnCode int, expectedResult []jc.SimpleMessage) {
for _, e := range exec {
testing.PatchExecutableAsEchoArgs(c, s, e)
}

testing.PatchExecutableThrowError(c, s, execNameFail, returnCode)

dataDir := c.MkDir()
s.patchSeries("centos7")

var tw loggo.TestWriter
c.Assert(loggo.RegisterWriter("mongosuite", &tw), jc.ErrorIsNil)
defer loggo.RemoveWriter("mongosuite")

_, err := mongo.EnsureServer(makeEnsureServerParams(dataDir))
c.Assert(err, jc.ErrorIsNil)
c.Assert(tw.Log(), jc.LogMatches, expectedResult)
}

func (s *MongoSuite) TestInstallSuccessMongodCentOS(c *gc.C) {
type installs struct {
series string
pkgs []string
}
test := installs{
"centos7", []string{"epel-release", "mongodb-server"},
}

testing.PatchExecutableAsEchoArgs(c, s, "yum")
testing.PatchExecutableAsEchoArgs(c, s, "chcon")
testing.PatchExecutableAsEchoArgs(c, s, "semanage")

dataDir := c.MkDir()
s.patchSeries(test.series)

_, err := mongo.EnsureServer(makeEnsureServerParams(dataDir))
c.Assert(err, jc.ErrorIsNil)

for _, pkg := range test.pkgs {
exp := append(append([]string{}, expectedArgs.YumBase...), pkg)
testing.AssertEchoArgs(c, "yum", exp...)
}

testing.AssertEchoArgs(c, "chcon", expectedArgs.Chcon...)

testing.AssertEchoArgs(c, "semanage", expectedArgs.Semanage...)
}

func (s *MongoSuite) TestMongoAptGetFails(c *gc.C) {
s.assertTestMongoGetFails(c, "trusty", "apt-get")
}

func (s *MongoSuite) TestMongoYumFails(c *gc.C) {
s.assertTestMongoGetFails(c, "centos7", "yum")
}

func (s *MongoSuite) assertTestMongoGetFails(c *gc.C, series string, packageManager string) {
s.patchSeries(series)

Expand Down
11 changes: 3 additions & 8 deletions packaging/dependency/mongo.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package dependency

import (
"github.com/juju/errors"
"github.com/juju/juju/packaging"
)

Expand All @@ -22,19 +23,13 @@ func (dep mongoDependency) PackageList(series string) ([]packaging.Package, erro
var pkgList []string
var pm = packaging.AptPackageManager

// Same package name for all distros (centos, ubuntu etc.)
if dep.useNUMA {
pkgList = append(pkgList, "numactl")
}

switch series {
case "centos7":
// Use yum for centos
pm = packaging.YumPackageManager

// CentOS requires also requires "epel-release" for the
// epel repo mongodb-server is in.
pkgList = append(pkgList, "epel-release", "mongodb-server")
case "centos7", "opensuseleap", "precise":
return nil, errors.NotSupportedf("installing mongo on series %q", series)
case "trusty":
pkgList = append(pkgList, "juju-mongodb")
case "xenial":
Expand Down

0 comments on commit 6dd3336

Please sign in to comment.