diff --git a/Gemfile b/Gemfile index 5a971723..88633d21 100644 --- a/Gemfile +++ b/Gemfile @@ -4,7 +4,7 @@ source 'https://rubygems.org' # We don't want to list psych since that updates the bundled version gem 'rdoc', '< 6.4' -gem 'kafo', '>= 7.3', '< 8' +gem 'kafo', '>= 7.6', '< 8' gem 'librarian-puppet', '>= 3.0' gem 'puppet', ENV.key?('PUPPET_VERSION') ? "~> #{ENV['PUPPET_VERSION']}" : '~> 7.0' gem 'facter', '>= 3.0', '!= 4.0.52' diff --git a/hooks/boot/01-kafo-hook-extensions.rb b/hooks/boot/01-kafo-hook-extensions.rb index 32826ff4..23abaa4b 100644 --- a/hooks/boot/01-kafo-hook-extensions.rb +++ b/hooks/boot/01-kafo-hook-extensions.rb @@ -101,8 +101,8 @@ def log_and_say(level, message, do_say = true, do_log = true) Kafo::KafoConfigure.logger.send(level, message) if do_log end - def execute!(command, do_say = true, do_log = true) - stdout_stderr, status = execute_command(command, do_say, do_log) + def execute!(command, do_say = true, do_log = true, extra_env = {}) + stdout_stderr, status = execute_command(command, do_say, do_log, extra_env) if stdout_stderr.nil? log_and_say(:error, "Command #{command} not found", do_say, do_log) @@ -115,21 +115,21 @@ def execute!(command, do_say = true, do_log = true) end end - def execute_as!(user, command, do_say = true, do_log = true) + def execute_as!(user, command, do_say = true, do_log = true, extra_env = {}) runuser_command = "runuser -l #{user} -c '#{command}'" - execute!(runuser_command, do_say, do_log) + execute!(runuser_command, do_say, do_log, extra_env) end - def execute(command, do_say, do_log) - _stdout_stderr, status = execute_command(command, do_say, do_log) + def execute(command, do_say, do_log, extra_env = {}) + _stdout_stderr, status = execute_command(command, do_say, do_log, extra_env) status end - def execute_command(command, do_say, do_log) + def execute_command(command, do_say, do_log, extra_env = {}) log_and_say(:debug, "Executing: #{command}", do_say, do_log) begin - stdout_stderr, status = Open3.capture2e(*Kafo::PuppetCommand.format_command(command)) + stdout_stderr, status = Open3.capture2e(*Kafo::PuppetCommand.format_command(command, extra_env)) rescue Errno::ENOENT return [nil, false] end diff --git a/hooks/pre/10-reset_data.rb b/hooks/pre/10-reset_data.rb index 88a424aa..bb3db431 100644 --- a/hooks/pre/10-reset_data.rb +++ b/hooks/pre/10-reset_data.rb @@ -56,18 +56,24 @@ def reset_candlepin empty_db_in_postgresql('candlepin') end -def pg_command_base(config, command, args) - "PGPASSWORD='#{config[:password]}' #{command} -U #{config[:username]} -h #{config[:host]} -p #{config[:port]} #{args}" +def pg_env(config) + { + 'PGHOST' => config.fetch(:host, 'localhost'), + 'PGPORT' => config.fetch(:port, '5432').to_s, + 'PGUSER' => config[:username], + 'PGPASSWORD' => config[:password], + 'PGDATABASE' => config[:database], + } end -def pg_sql_statement(config, statement) - pg_command_base(config, 'psql', "-d #{config[:database]} -t -c \"#{statement}\"") +def pg_sql_statement(statement) + "psql -t -c \"#{statement}\"" end # WARNING: deletes all the data owned by the user. No warnings. No confirmations. def empty_database!(config) delete_statement = 'DROP OWNED BY CURRENT_USER CASCADE;' - execute!(pg_sql_statement(config, delete_statement), false, true) + execute!(pg_sql_statement(delete_statement), false, true, pg_env(config)) end def clear_pulpcore_content(content_dir) diff --git a/spec/hook_context_extension_spec.rb b/spec/hook_context_extension_spec.rb index 9116430d..6438e7e9 100644 --- a/spec/hook_context_extension_spec.rb +++ b/spec/hook_context_extension_spec.rb @@ -153,7 +153,7 @@ it 'executes a command' do expect(subject).to be_nil - expect(context).to have_received(:execute_command).with(command, true, true) + expect(context).to have_received(:execute_command).with(command, true, true, {}) end end @@ -168,7 +168,7 @@ it 'executes a command' do expect(subject).to be_nil - expect(context).to have_received(:execute!).with("runuser -l postgres -c 'uptime'", true, true) + expect(context).to have_received(:execute!).with("runuser -l postgres -c 'uptime'", true, true, {}) end end end diff --git a/spec/postgresql_upgrade_hook_context_extension_spec.rb b/spec/postgresql_upgrade_hook_context_extension_spec.rb index 469ef05b..e7f068ee 100644 --- a/spec/postgresql_upgrade_hook_context_extension_spec.rb +++ b/spec/postgresql_upgrade_hook_context_extension_spec.rb @@ -70,12 +70,12 @@ it 'runs postgresql-setup --upgrade' do expect(subject).to be_nil - expect(context).to have_received(:'execute!').with("runuser -l postgres -c 'PGSETUP_INITDB_OPTIONS=\"--lc-collate=en_US.UTF-8 --lc-ctype=en_US.UTF-8 --locale=en_US.UTF-8\" postgresql-setup --upgrade'", false, true) + expect(context).to have_received(:'execute!').with("runuser -l postgres -c 'PGSETUP_INITDB_OPTIONS=\"--lc-collate=en_US.UTF-8 --lc-ctype=en_US.UTF-8 --locale=en_US.UTF-8\" postgresql-setup --upgrade'", false, true, {}) end it 'runs vacuumdb --all --analyze-in-stages' do expect(subject).to be_nil - expect(context).to have_received(:'execute!').with("runuser -l postgres -c 'vacuumdb --all --analyze-in-stages'", false, true) + expect(context).to have_received(:'execute!').with("runuser -l postgres -c 'vacuumdb --all --analyze-in-stages'", false, true, {}) end end end