Puppet Plan: peadm::restore

Defined in:
plans/restore.pp

Summary

Restore puppet primary configuration

Overview

Examples:

bolt plan run peadm::restore -t primary1.example.com input_file=/tmp/peadm-backup.tar.gz

Parameters:

  • targets (Peadm::SingleTargetSpec)

    This should be the primary puppetserver for the puppet cluster

  • restore_type (Enum['recovery', 'recovery-db', 'custom']) (defaults to: 'recovery')

    Choose from ‘recovery`, `recovery-db` and `custom`

  • restore (Peadm::Recovery_opts) (defaults to: {})

    A hash of custom backup options, see the peadm::recovery_opts_default() function for the default values

  • input_file (Pattern[/.*\.tar\.gz$/])

    The file containing the backup to restore from



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
# File 'plans/restore.pp', line 10

plan peadm::restore (
  # This plan should be run on the primary server
  Peadm::SingleTargetSpec $targets,

  # restore type determines the restore options
  Enum['recovery', 'recovery-db', 'custom'] $restore_type = 'recovery',

  # Which data to restore
  Peadm::Recovery_opts $restore = {},

  # Path to the recovery tarball
  Pattern[/.*\.tar\.gz$/] $input_file,
) {
  peadm::assert_supported_bolt_version()

  $recovery_directory = "${dirname($input_file)}/${basename($input_file, '.tar.gz')}"
# lint:ignore:strict_indent
  run_command(@("CMD"/L), $targets)
    umask 0077 \
      && cd ${shellquote(dirname($recovery_directory))} \
      && tar -xzf ${shellquote($input_file)}
    | CMD
# lint:endignore

  # 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

  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'))
    out::message('Cluster configuration loaded from backup')
  } else {
    $cluster = $_cluster
  }

  out::message("cluster: ${cluster}")

  $error = getvar('cluster.error')
  if $error {
    fail_plan($error)
  }

  $arch = peadm::assert_supported_architecture(
    getvar('cluster.params.primary_host'),
    getvar('cluster.params.replica_host'),
    getvar('cluster.params.primary_postgresql_host'),
    getvar('cluster.params.replica_postgresql_host'),
    getvar('cluster.params.compiler_hosts'),
  )

  $recovery_opts = $restore_type? {
    'recovery'     => peadm::recovery_opts_default(),
    'recovery-db'  => { 'puppetdb' => true, },
    'migration'    => peadm::migration_opts_default(),
    'custom'       => peadm::recovery_opts_all() + $restore,
  }

  $primary_target   = peadm::get_targets(getvar('cluster.params.primary_host'), 1)
  $replica_target   = peadm::get_targets(getvar('cluster.params.replica_host'), 1)
  $compiler_targets = peadm::get_targets(getvar('cluster.params.compiler_hosts'))

  # Determine the array of targets to which the PuppetDB PostgreSQL database
  # should be restored to. This could be as simple as just the primary server,
  # or it could be two separate PostgreSQL servers.
  $puppetdb_postgresql_targets = peadm::flatten_compact([
      getvar('cluster.params.primary_postgresql_host') ? {
        undef   => $primary_target,
        default => peadm::get_targets(getvar('cluster.params.primary_postgresql_host'), 1),
      },
      getvar('cluster.params.replica_postgresql_host') ? {
        undef   => $replica_target,
        default => peadm::get_targets(getvar('cluster.params.replica_postgresql_host'), 1),
      },
  ])

  $puppetdb_targets = peadm::flatten_compact([
      $primary_target,
      $replica_target,
      $compiler_targets,
  ])

  # Map of recovery option name to array of database hosts to restore the
  # relevant .dump content to.
  $restore_databases = {
    'orchestrator' => [$primary_target],
    'activity'     => [$primary_target],
    'rbac'         => [$primary_target],
    'puppetdb'     => $puppetdb_postgresql_targets,
  }.filter |$key,$_| {
    $recovery_opts[$key] == true
  }

  if getvar('recovery_opts.classifier') {
    if $restore_type == 'migration' {
      out::message('# Migrating classification')
      run_task('peadm::backup_classification', $primary_target,
        directory => $recovery_directory
      )

      run_task('peadm::transform_classification_groups', $primary_target,
        source_directory  => "${recovery_directory}/classifier",
        working_directory => $recovery_directory
      )

      run_task('peadm::restore_classification', $primary_target,
        classification_file => "${recovery_directory}/transformed_classification.json",
      )
    } else {
      run_task('peadm::restore_classification', $primary_target,
        classification_file => "${recovery_directory}/classifier/classification_backup.json",
      )
    }
  }

  if $restore_type == 'recovery' {
    out::message('# Restoring ca, certs, code and config for recovery')
  # lint:ignore:strict_indent
      run_command(@("CMD"/L), $primary_target)
        /opt/puppetlabs/bin/puppet-backup restore \
          --scope=certs,code,config \
          --tempdir=${shellquote($recovery_directory)} \
          --force \
          ${shellquote($recovery_directory)}/recovery/pe_backup-*tgz
        | CMD
  # lint:endignore
  } elsif $restore_type == 'recovery-db' {
    out::message('# Restoring primary database for recovery')
  } else {
    if getvar('recovery_opts.ca') {
      out::message('# Restoring ca and ssl certificates')
  # lint:ignore:strict_indent
      run_command(@("CMD"/L), $primary_target)
        /opt/puppetlabs/bin/puppet-backup restore \
          --scope=certs \
          --tempdir=${shellquote($recovery_directory)} \
          --force \
          ${shellquote($recovery_directory)}/ca/pe_backup-*tgz
        | CMD
    }

    if getvar('recovery_opts.code') {
      out::message('# Restoring code')
      run_command(@("CMD"/L), $primary_target)
        /opt/puppetlabs/bin/puppet-backup restore \
          --scope=code \
          --tempdir=${shellquote($recovery_directory)} \
          --force \
          ${shellquote($recovery_directory)}/code/pe_backup-*tgz
        | CMD
    }

    if getvar('recovery_opts.config') {
      out::message('# Restoring config')
      run_command(@("CMD"/L), $primary_target)
        /opt/puppetlabs/bin/puppet-backup restore \
          --scope=config \
          --tempdir=${shellquote($recovery_directory)} \
          --force \
          ${shellquote($recovery_directory)}/config/pe_backup-*tgz
        | CMD
    }
  }
  # Use PuppetDB's /pdb/admin/v1/archive API to SAVE data currently in PuppetDB.
  # Otherwise we'll completely lose it if/when we restore.
  # TODO: consider adding a heuristic to skip when innappropriate due to size
  #       or other factors.
  if getvar('recovery_opts.puppetdb') and $restore_type == 'migration' {
    out::message('# Exporting puppetdb')
    run_command(@("CMD"/L), $primary_target)
      /opt/puppetlabs/bin/puppet-db export \
        --cert=$(/opt/puppetlabs/bin/puppet config print hostcert) \
        --key=$(/opt/puppetlabs/bin/puppet config print hostprivkey) \
        ${shellquote($recovery_directory)}/puppetdb-archive.bin
      | CMD
  }

  ## shutdown services
  run_command(@("CMD"/L), $primary_target)
    systemctl stop pe-console-services pe-nginx pxp-agent pe-puppetserver \
                   pe-orchestration-services puppet pe-puppetdb
    | CMD

  # Restore secrets/keys.json if it exists
  out::message('# Restoring ldap secret key if it exists')
  run_command(@("CMD"/L), $primary_target)
    test -f ${shellquote($recovery_directory)}/rbac/keys.json \
      && cp -rp ${shellquote($recovery_directory)}/keys.json /etc/puppetlabs/console-services/conf.d/secrets/ \
      || echo secret ldap key doesnt exist
    | CMD
# lint:ignore:140chars
  # IF restoring orchestrator restore the secrets to /etc/puppetlabs/orchestration-services/conf.d/secrets/
  if getvar('recovery_opts.orchestrator') {
    out::message('# Restoring orchestrator secret keys')
    run_command(@("CMD"/L), $primary_target)
      cp -rp ${shellquote($recovery_directory)}/orchestrator/secrets/* /etc/puppetlabs/orchestration-services/conf.d/secrets/ 
      | CMD
  }
# lint:endignore

  #$database_to_restore.each |Integer $index, Boolean $value | {
  $restore_databases.each |$name,$database_targets| {
    out::message("# Restoring database pe-${name}")
    $dbname = "pe-${shellquote($name)}"

    # Drop pglogical extensions and schema if present
    run_command(@("CMD"/L), $database_targets)
      su - pe-postgres -s /bin/bash -c \
        "/opt/puppetlabs/server/bin/psql \
           --tuples-only \
           -d '${dbname}' \
           -c 'DROP SCHEMA IF EXISTS pglogical CASCADE;'"
      | CMD

    run_command(@("CMD"/L), $database_targets)
      su - pe-postgres -s /bin/bash -c \
        "/opt/puppetlabs/server/bin/psql \
           -d '${dbname}' \
           -c 'DROP SCHEMA public CASCADE; CREATE SCHEMA public;'"
      | CMD

    # To allow db user to restore the database grant temporary privileges
    run_command(@("CMD"/L), $database_targets)
      su - pe-postgres -s /bin/bash -c \
        "/opt/puppetlabs/server/bin/psql \
           -d '${dbname}' \
           -c 'ALTER USER \"${dbname}\" WITH SUPERUSER;'"
      | CMD

    # Restore database. If there are multiple database restore targets, perform
    # the restore(s) in parallel.
    parallelize($database_targets) |$database_target| {
      run_command(@("CMD"/L), $primary_target)
        /opt/puppetlabs/server/bin/pg_restore \
          -j 4 \
          -d "sslmode=verify-ca \
              host=${shellquote($database_target.peadm::certname())} \
              sslcert=/etc/puppetlabs/puppetdb/ssl/${shellquote($primary_target.peadm::certname())}.cert.pem \
              sslkey=/etc/puppetlabs/puppetdb/ssl/${shellquote($primary_target.peadm::certname())}.private_key.pem \
              sslrootcert=/etc/puppetlabs/puppet/ssl/certs/ca.pem \
              dbname=${dbname} \
              user=${dbname}" \
          -Fd ${recovery_directory}/${name}/${dbname}.dump.d
        | CMD
    }

    # Remove db user privileges post restore
    run_command(@("CMD"/L), $database_targets)
      su - pe-postgres -s /bin/bash -c \
        "/opt/puppetlabs/server/bin/psql \
           -d '${dbname}' \
           -c 'ALTER USER \"${dbname}\" WITH NOSUPERUSER;'"
      | CMD

    # Drop pglogical extension and schema (again) if present after db restore
    run_command(@("CMD"/L), $database_targets)
      su - pe-postgres -s /bin/bash -c \
        "/opt/puppetlabs/server/bin/psql \
           --tuples-only \
           -d '${dbname}' \
           -c 'DROP SCHEMA IF EXISTS pglogical CASCADE;'"
      | CMD

    run_command(@("CMD"/L), $database_targets)
      su - pe-postgres -s /bin/bash -c \
        "/opt/puppetlabs/server/bin/psql \
           -d '${dbname}' \
           -c 'DROP EXTENSION IF EXISTS pglogical CASCADE;'"
      | CMD
  }

  # Use `puppet infra` to ensure correct file permissions, restart services,
  # etc. Make sure not to try and get config data from the classifier, which
  # isn't yet up and running.
  run_command(@("CMD"/L), $primary_target)
    /opt/puppetlabs/bin/puppet-infrastructure configure --no-recover
    | CMD

  # If we have replicas reinitalise them
  run_command(@("CMD"/L), $replica_target)
    /opt/puppetlabs/bin/puppet-infra reinitialize replica -y
    | CMD

  # Use PuppetDB's /pdb/admin/v1/archive API to MERGE previously saved data
  # into the restored database.
  # TODO: consider adding a heuristic to skip when innappropriate due to size
  #       or other factors.
  if getvar('recovery_opts.puppetdb') and $restore_type == 'migration' {
    run_command(@("CMD"/L), $primary_target)
      /opt/puppetlabs/bin/puppet-db import \
      --cert=$(/opt/puppetlabs/bin/puppet config print hostcert) \
      --key=$(/opt/puppetlabs/bin/puppet config print hostprivkey) \
      ${shellquote($recovery_directory)}/puppetdb-archive.bin
      | CMD
# lint:endignore
  }

# Run Puppet to pick up last remaining config tweaks
  run_task('peadm::puppet_runonce', $primary_target)

  if $restore_type == 'recovery-db' {
    run_task('peadm::puppet_runonce', $puppetdb_postgresql_targets)
  }

  apply($primary_target) {
    file { $recovery_directory :
      ensure => 'absent',
      force  => true,
    }
  }

  return('success')
}