Puppet Plan: cd4peadm::restore::preflight

Defined in:
plans/restore/preflight.pp

Overview

A plan to check whether the configured infra is fit for CD4PE 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 (Cd4peadm::Config)

    Cd4peadm::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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'plans/restore/preflight.pp', line 11

plan cd4peadm::restore::preflight(
  Cd4peadm::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 cd4peadm::backup.
      | ERROR
    return($message)
  }
  $metadata = $metadata_results[0]['stdout'].parsejson
  if($metadata['version'] != cd4peadm::module_version()) {
    $message = @("ERROR")
      Backup version ${metadata['version']} does not match current module version ${cd4peadm::module_version()}.
      The backup must be restored using the same version of the module that generated it.
      | ERROR
    return($message)
  }

  $version_from_file_result = run_plan('cd4peadm::read_version_file_from_target', targets => [$target])
  if($version_from_file_result.error_set.empty) {
    $installed_version = chomp(regsubst($version_from_file_result[0].value[stdout],"\\\"",'', 'G'))
    if($metadata['version'] != $installed_version) {
      $version_mismatch_error = @("ERROR")
        Backup version ${metadata['version']} does not match the installed version ${installed_version}.
        The backup must be restored onto an install of the same version that it was taken from.
        | ERROR
      return($version_mismatch_error)
    }
  } else {
    $no_version_message = @("ERROR")
      Restoring backup failed. Installed application version could not be determined:
      ${version_from_file_result.error_set[0].value[stderr]}
    | ERROR
    return($no_version_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 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 'cd4peadm::apply_configuration' to correct the CD4PE installation.
      | ERROR
    return($message)
  }

  return ''
}