-
Notifications
You must be signed in to change notification settings - Fork 72
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
ianballou
wants to merge
5
commits into
theforeman:master
Choose a base branch
from
ianballou:backup-container-gateway
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
c8a7ad2
Backup container gateway DB
ianballou 068d4ac
Allow executing commands as a different user
ianballou c8fe3c2
Use --file and --format with pg_dump
ianballou fb56093
Refactor DBUps into subclasses
ianballou 0c1dfc0
Fix rubocop errors
ianballou File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | ||
@configuration['database'] = uri.path[1..] | ||
end | ||
@configuration | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
definitions/procedures/backup/online/container_gateway_db.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.