Skip to content

Commit

Permalink
Updating db restore
Browse files Browse the repository at this point in the history
  • Loading branch information
ragingra committed Mar 6, 2024
1 parent b0de776 commit f9a8751
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 14 deletions.
8 changes: 8 additions & 0 deletions plans/backup.pp
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,14 @@
mode => '0600',
}

# backup the recovery options
file { "${backup_directory}/peadm/recovery_opts.json":
content => stdlib::to_json_pretty($recovery_opts),
owner => 'root',
group => 'root',
mode => '0600',
}

# Create a subdir for each backup type selected
$recovery_opts.filter |$_,$val| { $val == true }.each |$dir,$_| {
file { "${backup_directory}/${dir}":
Expand Down
52 changes: 42 additions & 10 deletions plans/restore.pp
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# @summary Restore puppet primary configuration
#
# @param targets These are a list of the primary puppetservers from one or multiple puppet clusters
# @param targets This should be the primary puppetserver for the puppet cluster
# @param restore_type Choose from `recovery`, `recovery-db` and `custom`
# @param restore A hash of custom backup options, see the peadm::recovery_opts_default() function for the default values
# @param input_file The file containing the backup to restore from
# @example
# bolt plan run peadm::restore -t primary1.example.com,primary2.example.com input_file=/tmp/peadm-backup.tar.gz
# bolt plan run peadm::restore -t primary1.example.com input_file=/tmp/peadm-backup.tar.gz
#
plan peadm::restore (
# This plan should be run on the primary server
Expand Down Expand Up @@ -33,17 +33,21 @@

# try to load the cluster configuration by running peadm::get_peadm_config, but allow for errors to happen
$_cluster = run_task('peadm::get_peadm_config', $targets, { '_catch_errors' => true }).first.value
out::message("cluster: ${_cluster}")

$result = download_file("${recovery_directory}/peadm/peadm_config.json", 'peadm_config.json', $targets).first.value
$cluster_backup = loadjson(getvar('result.path'))

if $_cluster == undef or getvar('_cluster.params') == undef {
# failed to get cluster config, load from backup
out::message('Failed to get cluster configuration, loading from backup...')
$result = download_file("${recovery_directory}/peadm/peadm_config.json", 'peadm_config.json', $targets).first.value
$cluster = loadjson(getvar('result.path'))
$cluster = $cluster_backup
out::message('Cluster configuration loaded from backup')
} else {
$cluster = $_cluster
}

out::message("cluster: ${cluster}")
$error = getvar('cluster.error')
if $error {
fail_plan($error)
Expand Down Expand Up @@ -130,18 +134,46 @@
out::message('# Restoring primary database for recovery')
# lint:ignore:strict_indent
run_plan('peadm::util::update_classification', $primary_target,
postgresql_a_host => undef,
postgresql_b_host => undef,
postgresql_a_host => getvar('cluster.params.primary_host'),
peadm_config => $cluster
)
# lint:endignore
run_plan('peadm::util::sanitize_pe_conf', $primary_target)
# lint:ignore:strict_indent
run_command(@("CMD"/L), $primary_target)
rm -f /etc/puppetlabs/puppet/user_data.conf
| CMD
# lint:endignore
# lint:ignore:strict_indent
run_command(@("CMD"/L), $primary_target)
systemctl stop pe-puppetdb
| CMD
# lint:endignore
# lint:ignore:strict_indent
run_command(@("CMD"/L), $primary_target)
puppet infra configure
| CMD
# lint:endignore
run_task('peadm::puppet_runonce', $primary_target)
run_task('peadm::puppet_runonce', $primary_target)
run_plan('peadm::add_database', getvar('cluster.params.primary_postgresql_host'), primary_host => $primary_target)
run_plan('peadm::add_database', getvar('cluster_backup.params.primary_postgresql_host'), primary_host => $primary_target)
# should db data restore be done here?
} else {
# restore from snapshot
# - run classification update
# - run sanitize pe.conf
# - remove user_data.conf
# - stop puppetdb
# - run puppet infrastructure configure
# - do 2 puppet agent runs
# - add db
# fail_plan('Testing db restore')
} else {
if getvar('recovery_opts.ca') {
out::message('# Restoring ca and ssl certificates')
# lint:ignore:strict_indent
Expand Down
10 changes: 7 additions & 3 deletions plans/util/sanitize_pg_pe_conf.pp
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,15 @@
run_task('peadm::read_file', $targets,
path => $path,
).map |$result| {
$sanitized = $result['content'].loadjson() + {
out::message("Sanitizing pe.conf on ${result.value}")
$sanitized = $result.value['content'].parsejson() + {
'puppet_enterprise::puppet_master_host' => $primary_target.peadm::certname(),
'puppet_enterprise::database_host' => $result.target.peadm::certname(),
'puppet_enterprise::puppetdb_database_host' => $result.target.peadm::certname(),
}

out::message("Sanitized ${sanitized}")

# Return the result of file_content_upload. There is only one target
peadm::file_content_upload($sanitized, $path, $result.target)[0]
peadm::file_content_upload(stdlib::to_json_pretty($sanitized), $path, $result.target)[0]
}
}
5 changes: 5 additions & 0 deletions spec/acceptance/peadm_spec/tasks/reinstall_pe.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@
"type": "String[1]",
"description": "The PE installation platform",
"default": "el-8-x86_64"
},
"uninstall": {
"type": "Boolean",
"description": "Whether we want to uninstall PE before installing",
"default": false
}
}
}
4 changes: 3 additions & 1 deletion spec/acceptance/peadm_spec/tasks/reinstall_pe.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
# This task reinstalls PE and needs to run as root.

# Uninstall PE if installed
/opt/puppetlabs/bin/puppet-enterprise-uninstaller -p -d -y || true
if [[ "$PT_uninstall" == true ]]; then
/opt/puppetlabs/bin/puppet-enterprise-uninstaller -p -d -y || true
fi

# Download PE
INSTALLER="puppet-enterprise-${PT_version}-${PT_arch}"
Expand Down

0 comments on commit f9a8751

Please sign in to comment.