Skip to content

Commit

Permalink
Backup container gateway DB
Browse files Browse the repository at this point in the history
  • Loading branch information
ianballou committed Jul 22, 2024
1 parent 0478459 commit 1d0a3f5
Show file tree
Hide file tree
Showing 9 changed files with 144 additions and 15 deletions.
29 changes: 29 additions & 0 deletions definitions/checks/container_gateway/db_up.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
module Checks
module ContainerGateway
class DBUp < ForemanMaintain::Check
metadata do
description 'Make sure ContainerGateway DB is up'
label :container_gateway_db_up
for_feature :container_gateway_database
end

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

def next_steps
if feature(:container_gateway_database).local?
[Procedures::Service::Start.new(:only => 'postgresql')]
else
[] # there is nothing we can do for remote db
end
end
end
end
end
43 changes: 43 additions & 0 deletions definitions/features/container_gateway_database.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
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['adapter'] = uri.scheme
@configuration['host'] = uri.host
@configuration['port'] = uri.port
@configuration['database'] = uri.path[1..-1]
@configuration['username'] = uri.user
@configuration['password'] = uri.password
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
20 changes: 20 additions & 0 deletions definitions/procedures/backup/online/container_gateway_db.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
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
30 changes: 30 additions & 0 deletions definitions/procedures/restore/container_gateway_dump.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
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
2 changes: 1 addition & 1 deletion definitions/procedures/restore/extract_files.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def extract_pulp_data(backup)
end

def any_database
feature(:foreman_database) || feature(:candlepin_database) || feature(:pulpcore_database)
feature(:foreman_database) || feature(:candlepin_database) || feature(:pulpcore_database) || feature(:container_gateway_database)
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
25 changes: 14 additions & 11 deletions lib/foreman_maintain/utils/backup.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def initialize(backup_dir)
@foreman_offline_files = ['pgsql_data.tar.gz']
@katello_online_files = @foreman_online_files + ['candlepin.dump', 'pulpcore.dump']
@katello_offline_files = ['pgsql_data.tar.gz']
@fpc_online_files = ['pulpcore.dump']
@fpc_online_files = ['pulpcore.dump', 'container_gateway.dump']
@fpc_offline_files = ['pgsql_data.tar.gz']
end

Expand All @@ -35,6 +35,7 @@ def file_map
:config_files => map_file(@backup_dir, 'config_files.tar.gz'),
:metadata => map_file(@backup_dir, 'metadata.yml'),
:pulpcore_dump => map_file(@backup_dir, 'pulpcore.dump'),
:container_gateway_dump => map_file(@backup_dir, 'container_gateway.dump'),
}
end

Expand Down Expand Up @@ -99,70 +100,71 @@ def check_file_existence(existence_map)

def katello_standard_backup?
present = [:pgsql_data]
absent = [:candlepin_dump, :foreman_dump, :pulpcore_dump]
absent = [:candlepin_dump, :foreman_dump, :pulpcore_dump, :container_gateway_dump]
check_file_existence(:present => present,
:absent => absent)
end

def katello_online_backup?
present = [:candlepin_dump, :foreman_dump, :pulpcore_dump]
absent = [:pgsql_data]
absent = [:pgsql_data, :container_gateway_dump]
check_file_existence(:present => present,
:absent => absent)
end

def katello_logical_backup?
present = [:pgsql_data, :candlepin_dump, :foreman_dump, :pulpcore_dump]
absent = []
absent = [:container_gateway_dump]
check_file_existence(:present => present,
:absent => absent)
end

def katello_hybrid_db_backup?
all_dbs = { :pgsql_data => %w[candlepin foreman pulpcore] }
present, absent = dumps_for_hybrid_db_setup(all_dbs)
absent.concat [:container_gateway_dump]
check_file_existence(:present => present, :absent => absent)
end

def fpc_standard_backup?
present = [:pgsql_data]
absent = [:candlepin_dump, :foreman_dump, :pulpcore_dump]
absent = [:candlepin_dump, :foreman_dump, :pulpcore_dump, :container_gateway_dump]
check_file_existence(:present => present,
:absent => absent)
end

def fpc_online_backup?
present = [:pulpcore_dump]
present = [:pulpcore_dump, :container_gateway_dump]
absent = [:pgsql_data, :candlepin_dump, :foreman_dump]
check_file_existence(:present => present, :absent => absent)
end

def fpc_logical_backup?
present = [:pulpcore_dump, :pgsql_data]
present = [:pulpcore_dump, :container_gateway_dump, :pgsql_data]
absent = [:candlepin_dump, :foreman_dump]
check_file_existence(:present => present, :absent => absent)
end

def fpc_hybrid_db_backup?
all_dbs = { :pgsql_data => ['pulpcore'] }
all_dbs = { :pgsql_data => ['pulpcore', 'container_gateway'] }
present, absent = dumps_for_hybrid_db_setup(all_dbs)
absent.concat [:candlepin_dump, :foreman_dump]
check_file_existence(:present => present, :absent => absent)
end

def foreman_standard_backup?
check_file_existence(:present => [:pgsql_data],
:absent => [:candlepin_dump, :foreman_dump, :pulpcore_dump])
:absent => [:candlepin_dump, :foreman_dump, :pulpcore_dump, :container_gateway_dump])
end

def foreman_online_backup?
check_file_existence(:present => [:foreman_dump],
:absent => [:candlepin_dump, :pgsql_data, :pulpcore_dump])
:absent => [:candlepin_dump, :pgsql_data, :pulpcore_dump, :container_gateway_dump])
end

def foreman_logical_backup?
check_file_existence(:present => [:pgsql_data, :foreman_dump],
:absent => [:candlepin_dump, :pulpcore_dump])
:absent => [:candlepin_dump, :pulpcore_dump, :container_gateway_dump])
end

def dumps_for_hybrid_db_setup(dbs_hash)
Expand Down Expand Up @@ -234,6 +236,7 @@ def sql_dump_files_exist?
file_map[:foreman_dump][:present] ||
file_map[:candlepin_dump][:present] ||
file_map[:pulpcore_dump][:present]
file_map[:container_gateway_dump][:present]
end

def sql_needs_dump_restore?
Expand Down

0 comments on commit 1d0a3f5

Please sign in to comment.