Skip to content

Commit

Permalink
Fixes #23498 - run with mongo 3.4 from scl
Browse files Browse the repository at this point in the history
To get version of the db server (may be remote) we assume the
'mongo' command is always available. If there is only mongo from SCL
present the mongo client has to be called from  within a scl.

With current state of things it is safe to assume there is at least
some mongo client installed. For the future We will need to figure out
how to install a correct version of the client.
  • Loading branch information
mbacovsky committed May 16, 2018
1 parent 9daa671 commit 4a5e6d2
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 12 deletions.
54 changes: 43 additions & 11 deletions definitions/features/mongo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,31 @@ def initialize
@configuration = load_db_config(config_file)
end

# guess mongo version from installed packages
# safe method to run mongo commands prior we know
# what version uses the target DB
def available_core
return @core unless @core.nil? # return correct mongo ver if known
if @available_core.nil?
@available_core = ForemanMaintain::Utils::MongoCoreInstalled.new
end
@available_core
end

def core
if @core.nil?
version = server_version
@core = if version =~ /^3\.4/
logger.debug("Mongo #{version} detected, using commands from rh-mongodb34 SCL")
ForemanMaintain::Utils::MongoCore34.new
else
logger.debug("Mongo #{version} detected, using default commands")
ForemanMaintain::Utils::MongoCore.new
end
begin
version = server_version
@core = if version =~ /^3\.4/
load_mongo_core_34(version)
else
load_mongo_core_default(version)
end
rescue ForemanMaintain::Error::ExecutionError
# server version detection failed
logger.debug('Mongo version detection failed, choosing from installed versions')
@core = @available_core
end
end
@core
end
Expand Down Expand Up @@ -78,14 +93,15 @@ def dropdb(config = configuration)
end

def ping(config = configuration)
execute?(mongo_command("--eval 'ping:1'"),
execute?(mongo_command("--eval 'ping:1'", config),
:hidden_patterns => [config['password']].compact)
end

def server_version(config = configuration)
# do not use any core methods as we need this prior the core is created
version = execute(base_command('mongo', config, "--eval 'db.version()' #{config['name']}"),
:hidden_patterns => [config['password']].compact)
mongo_cmd = base_command(available_core.client_command, config,
"--eval 'db.version()' #{config['name']}")
version = execute!(mongo_cmd, :hidden_patterns => [config['password']].compact)
version.split("\n").last
end

Expand All @@ -110,6 +126,22 @@ def find_base_directory(directory)

private

def load_mongo_core_default(version)
unless find_package('mongodb')
raise ForemanMaintain::Error::Fail, 'Mongo client was not found'
end
logger.debug("Mongo #{version} detected, using default commands")
ForemanMaintain::Utils::MongoCore.new
end

def load_mongo_core_34(version)
unless find_package('rh-mongodb34-mongodb')
raise ForemanMaintain::Error::Fail, 'Mongo client ver. 3.4 was not found'
end
logger.debug("Mongo #{version} detected, using commands from rh-mongodb34 SCL")
ForemanMaintain::Utils::MongoCore34.new
end

def norm_value(value)
value = value.strip
case value
Expand Down
3 changes: 2 additions & 1 deletion lib/foreman_maintain/reporter/cli_reporter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ def before_scenario_starts(scenario)
end

def before_execution_starts(execution)
logger.info("--- Execution step '#{execution.name}' started ---")
label = execution.step.label.to_s.tr('_', '-')
logger.info("--- Execution step '#{execution.name}' [#{label}] started ---")
puts(execution_info(execution, ''))
end

Expand Down
33 changes: 33 additions & 0 deletions lib/foreman_maintain/utils/mongo_core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,37 @@ def dump_command
'scl enable rh-mongodb34 -- mongodump'
end
end

class MongoCoreInstalled < MongoCore
include ForemanMaintain::Concerns::SystemHelpers

attr_reader :services, :server_config_files, :client_command, :dump_command

def initialize
@services = {}
@server_config_files = []

if find_package('mongodb-server')
@services['mongod'] = 5
@server_config_files << '/etc/mongod.conf'
end

if find_package('mongodb')
@client_command = 'mongo'
@dump_command = 'mongodump'
end

if find_package('rh-mongodb34-mongodb-server')
@services['rh-mongodb34-mongod'] = 5
@server_config_files << '/etc/opt/rh/rh-mongodb34/mongod.conf'
end

if find_package('rh-mongodb34-mongodb')
@client_command = 'scl enable rh-mongodb34 -- mongo'
@dump_command = 'scl enable rh-mongodb34 -- mongodump'
end

raise ForemanMaintain::Error::Fail, 'Mongo client was not found' unless @client_command
end
end
end

0 comments on commit 4a5e6d2

Please sign in to comment.