Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backup and restore container gateway postgres DB #905

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 7 additions & 19 deletions definitions/checks/candlepin/db_up.rb
Original file line number Diff line number Diff line change
@@ -1,32 +1,20 @@
require_relative '../db_up_check'

module Checks
module Candlepin
class DBUp < ForemanMaintain::Check
class DBUp < DBUpCheck
metadata do
description 'Make sure Candlepin DB is up'
label :candlepin_db_up
for_feature :candlepin_database
end

def run
status = false
if feature(:candlepin_database).psql_cmd_available?
with_spinner('Checking connection to the Candlepin DB') do
status = feature(:candlepin_database).ping
end
assert(status, 'Candlepin DB is not responding. ' \
'It needs to be up and running to perform the following steps',
:next_steps => start_pgsql)
else
feature(:candlepin_database).raise_psql_missing_error
end
def database_feature
:candlepin_database
end

def start_pgsql
if feature(:candlepin_database).local?
[Procedures::Service::Start.new(:only => 'postgresql')]
else
[] # there is nothing we can do for remote db
end
def database_name
'Candlepin'
end
end
end
Expand Down
21 changes: 21 additions & 0 deletions definitions/checks/container_gateway/db_up.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
require_relative '../db_up_check'

module Checks
module ContainerGateway
class DBUp < DBUpCheck
metadata do
description 'Make sure ContainerGateway DB is up'
label :container_gateway_db_up
for_feature :container_gateway_database
end

def database_feature
:container_gateway_database
end

def database_name
'Container Gateway'
end
end
end
end
33 changes: 33 additions & 0 deletions definitions/checks/db_up_check.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
module Checks
class DBUpCheck < ForemanMaintain::Check
def run
status = false
if feature(database_feature).psql_cmd_available?
with_spinner("Checking connection to the #{database_name} DB") do
status = feature(database_feature).ping
end
assert(status, "#{database_name} DB is not responding. " \
"It needs to be up and running to perform the following steps",
:next_steps => start_pgsql)
else
feature(database_feature).raise_psql_missing_error
end
end

def start_pgsql
if feature(database_feature).local?
[Procedures::Service::Start.new(:only => 'postgresql')]
else
[] # there is nothing we can do for remote db
end
end

def database_feature
raise NotImplementedError, 'Subclasses must define `database_feature`'
end

def database_name
raise NotImplementedError, 'Subclasses must define `database_name`'
end
end
end
26 changes: 7 additions & 19 deletions definitions/checks/foreman/db_up.rb
Original file line number Diff line number Diff line change
@@ -1,32 +1,20 @@
require_relative '../db_up_check'

module Checks
module Foreman
class DBUp < ForemanMaintain::Check
class DBUp < DBUpCheck
metadata do
description 'Make sure Foreman DB is up'
label :foreman_db_up
for_feature :foreman_database
end

def run
status = false
if feature(:foreman_database).psql_cmd_available?
with_spinner('Checking connection to the Foreman DB') do
status = feature(:foreman_database).ping
end
assert(status, 'Foreman DB is not responding. ' \
'It needs to be up and running to perform the following steps',
:next_steps => start_pgsql)
else
feature(:foreman_database).raise_psql_missing_error
end
def database_feature
:foreman_database
end

def start_pgsql
if feature(:foreman_database).local?
[Procedures::Service::Start.new(:only => 'postgresql')]
else
[] # there is nothing we can do for remote db
end
def database_name
'Foreman'
end
end
end
Expand Down
22 changes: 7 additions & 15 deletions definitions/checks/pulpcore/db_up.rb
Original file line number Diff line number Diff line change
@@ -1,28 +1,20 @@
require_relative '../db_up_check'

module Checks
module Pulpcore
class DBUp < ForemanMaintain::Check
class DBUp < DBUpCheck
metadata do
description 'Make sure Pulpcore DB is up'
label :pulpcore_db_up
for_feature :pulpcore_database
end

def run
status = false
with_spinner('Checking connection to the Pulpcore DB') do
status = feature(:pulpcore_database).ping
end
assert(status, 'Pulpcore DB is not responding. ' \
'It needs to be up and running to perform the following steps',
:next_steps => next_steps)
def database_feature
:pulpcore_database
end

def next_steps
if feature(:pulpcore_database).local?
[Procedures::Service::Start.new(:only => 'postgresql')]
else
[] # there is nothing we can do for remote db
end
def database_name
'Pulpcore'
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def run
backup = ForemanMaintain::Utils::Backup.new(@backup_dir)
if feature(:instance).postgresql_local?
errors = []
[:candlepin_dump, :foreman_dump, :pulpcore_dump].each do |dump|
[:candlepin_dump, :foreman_dump, :pulpcore_dump, :container_gateway_dump].each do |dump|
next unless backup.file_map[dump][:present]

unless system("runuser - postgres -c 'test -r #{backup.file_map[dump][:path]}'")
Expand Down
40 changes: 40 additions & 0 deletions definitions/features/container_gateway_database.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
class Features::ContainerGatewayDatabase < ForemanMaintain::Feature
CONTAINER_GATEWAY_DB_CONFIG = '/etc/foreman-proxy/settings.d/container_gateway.yml'.freeze

include ForemanMaintain::Concerns::BaseDatabase
include ForemanMaintain::Concerns::DirectoryMarker

metadata do
label :container_gateway_database

confine do
file_nonzero?(CONTAINER_GATEWAY_DB_CONFIG)
end
end

def configuration
@configuration || load_configuration
end

def services
[
system_service('postgresql', 10, :component => 'container_gateway',
:db_feature => feature(:container_gateway_database)),
]
end

private

def load_configuration
config = YAML.load(File.read(CONTAINER_GATEWAY_DB_CONFIG))
@configuration = {}
connection_string = config[:db_connection_string]
if connection_string
uri = URI.parse(connection_string)
@configuration['connection_string'] = connection_string
@configuration['user'] = 'foreman-proxy'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can't the user configure a different db user, and this one is just the fallback?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh wait, no, reading further below, that's the "shell user" not "database user"? I'm confused :D

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is indeed a shell user. The container gateway database (and maybe more in the future) does not configure any password by default since it uses unix-domain socket authentication. As such, root can't (as far as I can tell) log into the database in any way, so I've created a generic way to set the shell user for doing database commands.

@configuration['database'] = uri.path[1..]
end
@configuration
end
end
3 changes: 2 additions & 1 deletion definitions/features/instance.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ def database_local?(feature)
def postgresql_local?
database_local?(:candlepin_database) ||
database_local?(:foreman_database) ||
database_local?(:pulpcore_database)
database_local?(:pulpcore_database) ||
database_local?(:container_gateway_database)
end

def foreman_proxy_with_content?
Expand Down
21 changes: 21 additions & 0 deletions definitions/procedures/backup/online/container_gateway_db.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
module Procedures::Backup
module Online
class ContainerGatewayDB < ForemanMaintain::Procedure
metadata do
description 'Backup Container Gateway database'
tags :backup
label :backup_online_container_gateway_db
for_feature :container_gateway_database
preparation_steps { Checks::ContainerGateway::DBUp.new }
param :backup_dir, 'Directory where to backup to', :required => true
end

def run
with_spinner('Getting Container Gateway DB dump') do
feature(:container_gateway_database).
dump_db(File.join(@backup_dir, 'container_gateway.dump'))
end
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ module Procedures::Repositories
class IndexKatelloRepositoriesContainerMetatdata < ForemanMaintain::Procedure
metadata do
description 'Import container manifest metadata'
for_feature :repositories
confine do
feature(:katello)
end
Expand Down
31 changes: 31 additions & 0 deletions definitions/procedures/restore/container_gateway_dump.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
module Procedures::Restore
class ContainerGatewayDump < ForemanMaintain::Procedure
metadata do
description 'Restore container gateway postgresql dump from backup'
param :backup_dir,
'Path to backup directory',
:required => true
preparation_steps { Checks::ContainerGateway::DBUp.new }
confine do
feature(:container_gateway_database)
end
end

def run
backup = ForemanMaintain::Utils::Backup.new(@backup_dir)

with_spinner('Restoring container gateway postgresql dump') do |spinner|
restore_container_gateway_dump(backup, spinner)
end
end

def restore_container_gateway_dump(backup, spinner)
if backup.file_map[:container_gateway_dump][:present]
spinner.update('Restoring container gateway dump')
local = feature(:container_gateway_database).local?
feature(:container_gateway_database).
restore_dump(backup.file_map[:container_gateway_dump][:path], local)
end
end
end
end
8 changes: 8 additions & 0 deletions definitions/procedures/restore/drop_databases.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ def run
drop_foreman(backup, spinner)
drop_candlepin(backup, spinner)
drop_pulpcore(backup, spinner)
drop_container_gateway(backup, spinner)
end
end

Expand All @@ -43,5 +44,12 @@ def drop_pulpcore(backup, spinner)
feature(:pulpcore_database).dropdb
end
end

def drop_container_gateway(backup, spinner)
if backup.file_map[:container_gateway_dump][:present]
spinner.update('Dropping container gateway database')
feature(:container_gateway_database).dropdb
end
end
end
end
7 changes: 6 additions & 1 deletion definitions/procedures/restore/extract_files.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,12 @@ def extract_pulp_data(backup)
end

def any_database
feature(:foreman_database) || feature(:candlepin_database) || feature(:pulpcore_database)
%i[
foreman_database
candlepin_database
pulpcore_database
container_gateway_database
].any? { |db| feature(db) }
end

def extract_pgsql_data(backup)
Expand Down
6 changes: 4 additions & 2 deletions definitions/scenarios/backup.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ def set_context_mapping
Procedures::Backup::Pulp => :backup_dir,
Procedures::Backup::Online::CandlepinDB => :backup_dir,
Procedures::Backup::Online::ForemanDB => :backup_dir,
Procedures::Backup::Online::PulpcoreDB => :backup_dir)
Procedures::Backup::Online::PulpcoreDB => :backup_dir,
Procedures::Backup::Online::ContainerGatewayDB => :backup_dir)
context.map(:preserve_dir,
Procedures::Backup::PrepareDirectory => :preserve_dir)
context.map(:incremental_dir,
Expand Down Expand Up @@ -102,7 +103,8 @@ def add_database_backup_steps
add_steps_with_context(
Procedures::Backup::Online::CandlepinDB,
Procedures::Backup::Online::ForemanDB,
Procedures::Backup::Online::PulpcoreDB
Procedures::Backup::Online::PulpcoreDB,
Procedures::Backup::Online::ContainerGatewayDB
)
end

Expand Down
7 changes: 6 additions & 1 deletion definitions/scenarios/restore.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def compose
add_step_with_context(Procedures::Installer::UpgradeRakeTask)
add_step_with_context(Procedures::Crond::Start) if feature(:cron)
end
# rubocop:enable Metrics/MethodLength,Metrics/AbcSize
# rubocop:enable Metrics/MethodLength

def restore_sql_dumps(backup)
if feature(:instance).postgresql_local?
Expand All @@ -65,10 +65,14 @@ def restore_sql_dumps(backup)
if backup.file_map[:pulpcore_dump][:present]
add_steps_with_context(Procedures::Restore::PulpcoreDump)
end
if backup.file_map[:container_gateway_dump][:present]
add_steps_with_context(Procedures::Restore::ContainerGatewayDump)
end
if feature(:instance).postgresql_local?
add_step(Procedures::Service::Stop.new(:only => ['postgresql']))
end
end
# rubocop:enable Metrics/AbcSize

def set_context_mapping
context.map(:backup_dir,
Expand All @@ -82,6 +86,7 @@ def set_context_mapping
Procedures::Restore::CandlepinDump => :backup_dir,
Procedures::Restore::ForemanDump => :backup_dir,
Procedures::Restore::PulpcoreDump => :backup_dir,
Procedures::Restore::ContainerGatewayDump => :backup_dir,
Procedures::Restore::ExtractFiles => :backup_dir)
end
end
Expand Down
Loading
Loading