|
| 1 | +# @api private |
| 2 | +# @summary Restore the core user settings for puppet infrastructure from backup |
| 3 | +# |
| 4 | +# This plan can restore data to puppet infrastructure for DR and rebuilds |
| 5 | +# |
| 6 | +plan peadm::restore ( |
| 7 | + # This plan should be run on the primary server |
| 8 | + Peadm::SingleTargetSpec $targets, |
| 9 | + |
| 10 | + # Which data to restore |
| 11 | + Peadm::Recovery_opts $restore = {}, |
| 12 | + |
| 13 | + # Path to the recovery tarball |
| 14 | + Pattern[/.*\.tar\.gz$/] $input_file, |
| 15 | +) { |
| 16 | + peadm::assert_supported_bolt_version() |
| 17 | + |
| 18 | + $recovery_opts = (peadm::recovery_opts_default() + $restore) |
| 19 | + $cluster = run_task('peadm::get_peadm_config', $targets).first.value |
| 20 | + $arch = peadm::assert_supported_architecture( |
| 21 | + getvar('cluster.params.primary_host'), |
| 22 | + getvar('cluster.params.replica_host'), |
| 23 | + getvar('cluster.params.primary_postgresql_host'), |
| 24 | + getvar('cluster.params.replica_postgresql_host'), |
| 25 | + getvar('cluster.params.compiler_hosts'), |
| 26 | + ) |
| 27 | + |
| 28 | + $primary_target = peadm::get_targets(getvar('cluster.params.primary_host'), 1) |
| 29 | + $replica_target = peadm::get_targets(getvar('cluster.params.replica_host'), 1) |
| 30 | + $compiler_targets = peadm::get_targets(getvar('cluster.params.compiler_hosts')) |
| 31 | + |
| 32 | + # Determine the array of targets to which the PuppetDB PostgreSQL database |
| 33 | + # should be restored to. This could be as simple as just the primary server, |
| 34 | + # or it could be two separate PostgreSQL servers. |
| 35 | + $puppetdb_postgresql_targets = peadm::flatten_compact([ |
| 36 | + getvar('cluster.params.primary_postgresql_host') ? { |
| 37 | + undef => $primary_target, |
| 38 | + default => peadm::get_targets(getvar('cluster.params.primary_postgresql_host'), 1), |
| 39 | + }, |
| 40 | + getvar('cluster.params.replica_postgresql_host') ? { |
| 41 | + undef => $replica_target, |
| 42 | + default => peadm::get_targets(getvar('cluster.params.replica_postgresql_host'), 1), |
| 43 | + }, |
| 44 | + ]) |
| 45 | + |
| 46 | + $puppetdb_targets = peadm::flatten_compact([ |
| 47 | + $primary_target, |
| 48 | + $replica_target, |
| 49 | + $compiler_targets, |
| 50 | + ]) |
| 51 | + |
| 52 | + $recovery_directory = "${dirname($input_file)}/${basename("${input_file}", '.tar.gz')}" |
| 53 | + |
| 54 | + run_command(@("CMD"/L), $primary_target) |
| 55 | + umask 0077 \ |
| 56 | + && cd ${shellquote(dirname($recovery_directory))} \ |
| 57 | + && tar -xzf ${shellquote($input_file)} |
| 58 | + | CMD |
| 59 | + |
| 60 | + # Map of recovery option name to array of database hosts to restore the |
| 61 | + # relevant .dump content to. |
| 62 | + $restore_databases = { |
| 63 | + 'orchestrator' => [$primary_target], |
| 64 | + 'activity' => [$primary_target], |
| 65 | + 'rbac' => [$primary_target], |
| 66 | + 'puppetdb' => $puppetdb_postgresql_targets, |
| 67 | + }.filter |$key,$_| { |
| 68 | + $recovery_opts[$key] == true |
| 69 | + } |
| 70 | + |
| 71 | + if getvar('recovery_opts.classifier') { |
| 72 | + out::message('# Restoring classification') |
| 73 | + run_task('peadm::backup_classification', $primary_target, |
| 74 | + directory => $recovery_directory |
| 75 | + ) |
| 76 | + out::message("# Backed up current classification to ${recovery_directory}/classification_backup.json") |
| 77 | + |
| 78 | + run_task('peadm::transform_classification_groups', $primary_target, |
| 79 | + source_directory => "${recovery_directory}/classifier", |
| 80 | + working_directory => $recovery_directory |
| 81 | + ) |
| 82 | + |
| 83 | + run_task('peadm::restore_classification', $primary_target, |
| 84 | + classification_file => "${recovery_directory}/classification_backup.json", |
| 85 | + ) |
| 86 | + } |
| 87 | + |
| 88 | + if getvar('recovery_opts.ca') { |
| 89 | + out::message('# Restoring ca and ssl certificates') |
| 90 | + run_command(@("CMD"/L), $primary_target) |
| 91 | + /opt/puppetlabs/bin/puppet-backup restore \ |
| 92 | + --scope=certs \ |
| 93 | + --tempdir=${shellquote($recovery_directory)} \ |
| 94 | + --force \ |
| 95 | + ${shellquote($recovery_directory)}/classifier/pe_backup-*tgz |
| 96 | + | CMD |
| 97 | + } |
| 98 | + |
| 99 | + # Use PuppetDB's /pdb/admin/v1/archive API to SAVE data currently in PuppetDB. |
| 100 | + # Otherwise we'll completely lose it if/when we restore. |
| 101 | + # TODO: consider adding a heuristic to skip when innappropriate due to size |
| 102 | + # or other factors. |
| 103 | + if getvar('recovery_opts.puppetdb') { |
| 104 | + run_command(@("CMD"/L), $primary_target) |
| 105 | + /opt/puppetlabs/bin/puppet-db export ${shellquote($recovery_directory)}/puppetdb-archive.bin |
| 106 | + | CMD |
| 107 | + } |
| 108 | + |
| 109 | + ## shutdown services |
| 110 | + run_command(@("CMD"/L), $primary_target) |
| 111 | + systemctl stop pe-console-services pe-nginx pxp-agent pe-puppetserver \ |
| 112 | + pe-orchestration-services puppet pe-puppetdb |
| 113 | + | CMD |
| 114 | + |
| 115 | + # Restore secrets/keys.json if it exists |
| 116 | + out::message('# Restoring ldap secret key if it exists') |
| 117 | + run_command(@("CMD"/L), $primary_target) |
| 118 | + test -f ${shellquote($recovery_directory)}/rbac/keys.json \ |
| 119 | + && cp -rp ${shellquote($recovery_directory)}/keys.json /etc/puppetlabs/console-services/conf.d/secrets/ \ |
| 120 | + || echo secret ldap key doesnt exist |
| 121 | + | CMD |
| 122 | + |
| 123 | + # IF restoring orchestrator restore the secrets to /etc/puppetlabs/orchestration-services/conf.d/secrets/ |
| 124 | + if getvar('recovery_opts.orchestrator') { |
| 125 | + out::message('# Restoring orchestrator secret keys') |
| 126 | + run_command(@("CMD"/L), $primary_target) |
| 127 | + cp -rp ${shellquote($recovery_directory)}/orchestrator/secrets/* /etc/puppetlabs/orchestration-services/conf.d/secrets/ |
| 128 | + | CMD |
| 129 | + } |
| 130 | + |
| 131 | + #$database_to_restore.each |Integer $index, Boolean $value | { |
| 132 | + $restore_databases.each |$name,$database_targets| { |
| 133 | + out::message("# Restoring ${name} database") |
| 134 | + $dbname = "pe-${shellquote($name)}" |
| 135 | + |
| 136 | + # Drop pglogical extensions and schema if present |
| 137 | + run_command(@("CMD"/L), $database_targets) |
| 138 | + su - pe-postgres -s /bin/bash -c \ |
| 139 | + "/opt/puppetlabs/server/bin/psql \ |
| 140 | + --tuples-only \ |
| 141 | + -d '${dbname}' \ |
| 142 | + -c 'DROP SCHEMA IF EXISTS pglogical CASCADE;'" |
| 143 | + | CMD |
| 144 | + |
| 145 | + run_command(@("CMD"/L), $database_targets) |
| 146 | + su - pe-postgres -s /bin/bash -c \ |
| 147 | + "/opt/puppetlabs/server/bin/psql \ |
| 148 | + -d '${dbname}' \ |
| 149 | + -c 'DROP SCHEMA public CASCADE; CREATE SCHEMA public;'" |
| 150 | + | CMD |
| 151 | + |
| 152 | + # To allow db user to restore the database grant temporary privileges |
| 153 | + run_command(@("CMD"/L), $database_targets) |
| 154 | + su - pe-postgres -s /bin/bash -c \ |
| 155 | + "/opt/puppetlabs/server/bin/psql \ |
| 156 | + -d '${dbname}' \ |
| 157 | + -c 'ALTER USER \"${dbname}\" WITH SUPERUSER;'" |
| 158 | + | CMD |
| 159 | + |
| 160 | + # Restore database. If there are multiple database restore targets, perform |
| 161 | + # the restore(s) in parallel. |
| 162 | + parallelize($database_targets) |$database_target| { |
| 163 | + run_command(@("CMD"/L), $primary_target) |
| 164 | + /opt/puppetlabs/server/bin/pg_restore \ |
| 165 | + -j 4 \ |
| 166 | + -d "sslmode=verify-ca \ |
| 167 | + host=${shellquote($database_target.peadm::certname())} \ |
| 168 | + sslcert=/etc/puppetlabs/puppetdb/ssl/${shellquote($primary_target.peadm::certname())}.cert.pem \ |
| 169 | + sslkey=/etc/puppetlabs/puppetdb/ssl/${shellquote($primary_target.peadm::certname())}.private_key.pem \ |
| 170 | + sslrootcert=/etc/puppetlabs/puppet/ssl/certs/ca.pem \ |
| 171 | + dbname=${dbname} \ |
| 172 | + user=${dbname}" \ |
| 173 | + -Fd ${recovery_directory}/${name}/${dbname}.dump.d |
| 174 | + | CMD |
| 175 | + } |
| 176 | + |
| 177 | + # Remove db user privileges post restore |
| 178 | + run_command(@("CMD"/L), $database_targets) |
| 179 | + su - pe-postgres -s /bin/bash -c \ |
| 180 | + "/opt/puppetlabs/server/bin/psql \ |
| 181 | + -d '${dbname}' \ |
| 182 | + -c 'ALTER USER \"${dbname}\" WITH NOSUPERUSER;'" |
| 183 | + | CMD |
| 184 | + |
| 185 | + # Drop pglogical extension and schema (again) if present after db restore |
| 186 | + run_command(@("CMD"/L), $database_targets) |
| 187 | + su - pe-postgres -s /bin/bash -c \ |
| 188 | + "/opt/puppetlabs/server/bin/psql \ |
| 189 | + --tuples-only \ |
| 190 | + -d '${dbname}' \ |
| 191 | + -c 'DROP SCHEMA IF EXISTS pglogical CASCADE;'" |
| 192 | + | CMD |
| 193 | + |
| 194 | + run_command(@("CMD"/L), $database_targets) |
| 195 | + su - pe-postgres -s /bin/bash -c \ |
| 196 | + "/opt/puppetlabs/server/bin/psql \ |
| 197 | + -d '${dbname}' \ |
| 198 | + -c 'DROP EXTENSION IF EXISTS pglogical CASCADE;'" |
| 199 | + | CMD |
| 200 | + } |
| 201 | + |
| 202 | + # Use `puppet infra` to ensure correct file permissions, restart services, |
| 203 | + # etc. Make sure not to try and get config data from the classifier, which |
| 204 | + # isn't yet up and running. |
| 205 | + run_command(@("CMD"/L), $primary_target) |
| 206 | + /opt/puppetlabs/bin/puppet-infrastructure configure --no-recover |
| 207 | + | CMD |
| 208 | + |
| 209 | + # If we have replicas reinitalise them |
| 210 | + run_command(@("CMD"/L), $replica_target) |
| 211 | + /opt/puppetlabs/bin/puppet-infra reinitialize replica -y |
| 212 | + | CMD |
| 213 | + |
| 214 | + # Use PuppetDB's /pdb/admin/v1/archive API to MERGE previously saved data |
| 215 | + # into the restored database. |
| 216 | + # TODO: consider adding a heuristic to skip when innappropriate due to size |
| 217 | + # or other factors. |
| 218 | + if getvar('recovery_opts.puppetdb') { |
| 219 | + run_command(@("CMD"/L), $primary_target) |
| 220 | + /opt/puppetlabs/bin/puppet-db import ${shellquote($recovery_directory)}/puppetdb-archive.bin |
| 221 | + | CMD |
| 222 | + } |
| 223 | + |
| 224 | + # Run Puppet to pick up last remaining config tweaks |
| 225 | + run_task('peadm::puppet_runonce', $primary_target) |
| 226 | + |
| 227 | + apply($primary_target){ |
| 228 | + file { $recovery_directory : |
| 229 | + ensure => 'absent', |
| 230 | + force => true |
| 231 | + } |
| 232 | + } |
| 233 | + |
| 234 | + return("success") |
| 235 | +} |
0 commit comments