From f9a8751de42c82a38eae12626daa7779571f22e1 Mon Sep 17 00:00:00 2001 From: Neil Anderson Date: Wed, 6 Mar 2024 14:57:14 +0000 Subject: [PATCH] Updating db restore --- plans/backup.pp | 8 +++ plans/restore.pp | 52 +++++++++++++++---- plans/util/sanitize_pg_pe_conf.pp | 10 ++-- .../peadm_spec/tasks/reinstall_pe.json | 5 ++ .../peadm_spec/tasks/reinstall_pe.sh | 4 +- 5 files changed, 65 insertions(+), 14 deletions(-) diff --git a/plans/backup.pp b/plans/backup.pp index 493fab35..94606709 100644 --- a/plans/backup.pp +++ b/plans/backup.pp @@ -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}": diff --git a/plans/restore.pp b/plans/restore.pp index ec6a62e6..6fbf636a 100644 --- a/plans/restore.pp +++ b/plans/restore.pp @@ -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 @@ -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) @@ -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 diff --git a/plans/util/sanitize_pg_pe_conf.pp b/plans/util/sanitize_pg_pe_conf.pp index 5888bd09..c3033bd0 100644 --- a/plans/util/sanitize_pg_pe_conf.pp +++ b/plans/util/sanitize_pg_pe_conf.pp @@ -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] } } diff --git a/spec/acceptance/peadm_spec/tasks/reinstall_pe.json b/spec/acceptance/peadm_spec/tasks/reinstall_pe.json index fb2ccd33..2d83fa72 100644 --- a/spec/acceptance/peadm_spec/tasks/reinstall_pe.json +++ b/spec/acceptance/peadm_spec/tasks/reinstall_pe.json @@ -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 } } } diff --git a/spec/acceptance/peadm_spec/tasks/reinstall_pe.sh b/spec/acceptance/peadm_spec/tasks/reinstall_pe.sh index 5175a63e..a7d8a4da 100644 --- a/spec/acceptance/peadm_spec/tasks/reinstall_pe.sh +++ b/spec/acceptance/peadm_spec/tasks/reinstall_pe.sh @@ -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}"