Puppet Plan: complyadm::restore::preflight

Defined in:
plans/restore/preflight.pp

Overview

A plan to check whether the configured infra is fit for Comply restore and prints results. Specifically checks whether the postgres database is up and the version of the backup matches the current module version.

Parameters:

  • config (Complyadm::Config)

    Complyadm::Config config object from hiera

  • backup (String[1])

    String The name of the backup zip archive to restore



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
# File 'plans/restore/preflight.pp', line 11

plan complyadm::restore::preflight(
  Complyadm::Config $config,
  String[1] $backup,
) {
  $target = $config['roles']['backend']['targets'][0]

  # confirm backup is the same version as the current module version
  $metadata_cmd = "unzip -p ${config['backup_dir']}/${backup} ${backup[0,-('.zip'.length()+1)]}/metadata.json"
  $metadata_results = run_command($metadata_cmd, $target, '_run_as' => 'root', '_catch_errors' => true,)
  if(!$metadata_results.ok) {
    $message = @("ERROR")
      Unable to extract metadata.json from backup archive:
        ${metadata_results[0].error}
      Confirm it was created with complyadm::backup.
      | ERROR
    return($message)
  }
  $metadata = $metadata_results[0]['stdout'].parsejson
  if($metadata['version'] != complyadm::module_version()) {
    $message = @("ERROR")
      Backup version ${metadata['version']} does not match current module version ${complyadm::module_version()}.
      The backup must be restored using the same version of the module that generated it.
      | ERROR
    return($message)
  }

  # Check if postgres is up.  We need it to be running in order to restore the SQL file from the backup
  # TODO: When we add support for multiple database targets, we'll need to run against the right one
  $runtime = $config['runtime']
  $sql_command = '\l'
  $db_subcommand = "psql --user postgres --command \"${sql_command}\""
  $db_command = "${runtime} exec comply_postgres ${db_subcommand}"
  $db_connect_results = run_command(
    $db_command,
    $target,
    { '_run_as' => 'root', '_catch_errors' => true, },
  )
  if(!$db_connect_results.ok) {
    $message = @("ERROR")
      Unable to connect to postgres database:
        ${db_connect_results[0]['stderr']}
      Run 'complyadm::configure' to correct the Comply installation.
      | ERROR
    return($message)
  }

  return ''
}