|  | 
| 12 | 12 | ) { | 
| 13 | 13 | 
 | 
| 14 | 14 |   $primary_target = peadm::get_targets($primary_host, 1) | 
|  | 15 | +  $postgresql_target = peadm::get_targets($targets, 1) | 
|  | 16 | + | 
|  | 17 | +  $postgresql_host = $postgresql_target.peadm::certname() | 
| 15 | 18 | 
 | 
| 16 | 19 |   # Get current peadm config before making modifications and shutting down | 
| 17 | 20 |   # PuppetDB | 
|  | 
| 21 | 24 | 
 | 
| 22 | 25 |   # Bail if this is trying to be ran against Standard | 
| 23 | 26 |   if $compilers.empty { | 
| 24 |  | -    fail_plan('Plan Peadm::Add_database only applicable for L and XL deployments') | 
|  | 27 | +    fail_plan('Plan peadm::add_database is only applicable for L and XL deployments') | 
| 25 | 28 |   } | 
| 26 | 29 | 
 | 
| 27 | 30 |   # Existing nodes and their assignments | 
| 28 | 31 |   $replica_host = $peadm_config['params']['replica_host'] | 
| 29 |  | -  $primary_postgresql_host = $peadm_config['params']['primary_postgresql_host'] | 
| 30 |  | -  $replica_postgresql_host = $peadm_config['params']['replica_postgresql_host'] | 
|  | 32 | +  $postgresql_a_host = $peadm_config['role-letter']['postgresql']['A'] | 
|  | 33 | +  $postgresql_b_host = $peadm_config['role-letter']['postgresql']['B'] | 
| 31 | 34 | 
 | 
| 32 | 35 |   $replica_target = peadm::get_targets($replica_host, 1) | 
| 33 | 36 | 
 | 
|  | 
| 41 | 44 |   } else { | 
| 42 | 45 |     # If array is empty then no external databases were previously configured  | 
| 43 | 46 |     $no_external_db = peadm::flatten_compact([ | 
| 44 |  | -      $primary_postgresql_host, | 
| 45 |  | -      $replica_postgresql_host | 
|  | 47 | +      $postgresql_a_host, | 
|  | 48 | +      $postgresql_b_host | 
| 46 | 49 |     ]).empty | 
| 47 | 50 | 
 | 
| 48 | 51 |     # Pick operating mode based on array check | 
|  | 
| 67 | 70 |     # The letter which doesn't yet have a server assigned or in the event this | 
| 68 | 71 |     # is a replacement operation, the letter this node was assigned to previously | 
| 69 | 72 |     $avail_group_letter = peadm::flatten_compact($roles['postgresql'].map |$k,$v| { | 
| 70 |  | -      if (! $v) or ($v == $targets.peadm::certname()) { | 
|  | 73 | +      if (! $v) or ($v == $postgresql_host) { | 
| 71 | 74 |         $k | 
| 72 | 75 |       } | 
| 73 | 76 |     })[0] | 
| 74 | 77 |     # When in pair mode we assume the other PSQL node will serve as our source | 
| 75 | 78 |     $source_db_host = peadm::flatten_compact([ | 
| 76 |  | -      $primary_postgresql_host, | 
| 77 |  | -      $replica_postgresql_host | 
| 78 |  | -    ]).reject($targets.peadm::certname())[0] | 
|  | 79 | +      $postgresql_a_host, | 
|  | 80 | +      $postgresql_b_host | 
|  | 81 | +    ]).reject($postgresql_host)[0] | 
| 79 | 82 |   } | 
| 80 | 83 | 
 | 
| 81 |  | -  out::message("Adding PostgreSQL server ${targets.peadm::certname()} to availability group ${avail_group_letter}") | 
| 82 |  | -  out::message("Using ${source_db_host} to populate ${targets.peadm::certname()}") | 
|  | 84 | +  out::message("Adding PostgreSQL server ${postgresql_host} to availability group ${avail_group_letter}") | 
|  | 85 | +  out::message("Using ${source_db_host} to populate ${postgresql_host}") | 
| 83 | 86 | 
 | 
| 84 | 87 |   $source_db_target = peadm::get_targets($source_db_host, 1) | 
| 85 | 88 | 
 | 
| 86 | 89 |   peadm::plan_step('init-db-node') || { | 
| 87 | 90 |     # Install PSQL on new node to be used as external PuppetDB backend by using | 
| 88 | 91 |     # puppet in lieu of installer  | 
| 89 |  | -    run_plan('peadm::subplans::component_install', $targets, | 
|  | 92 | +    run_plan('peadm::subplans::component_install', $postgresql_target, | 
| 90 | 93 |       primary_host       => $primary_target, | 
| 91 | 94 |       avail_group_letter => $avail_group_letter, | 
| 92 | 95 |       role               => 'puppet/puppetdb-database' | 
|  | 
| 95 | 98 | 
 | 
| 96 | 99 |   # Stop Puppet to ensure catalogs are not being compiled for PE infrastructure nodes | 
| 97 | 100 |   run_command('systemctl stop puppet.service', peadm::flatten_compact([ | 
| 98 |  | -    $targets, | 
|  | 101 | +    $postgresql_target, | 
| 99 | 102 |     $compilers, | 
| 100 | 103 |     $primary_target, | 
| 101 | 104 |     $replica_target, | 
|  | 
| 108 | 111 | 
 | 
| 109 | 112 |   peadm::plan_step('replicate-db') || { | 
| 110 | 113 |     # Replicate content from source to newly installed PSQL server | 
| 111 |  | -    run_plan('peadm::subplans::db_populate', $targets, source_host => $source_db_target.peadm::certname()) | 
|  | 114 | +    run_plan('peadm::subplans::db_populate', $postgresql_target, source_host => $source_db_target.peadm::certname()) | 
| 112 | 115 | 
 | 
| 113 | 116 |     # Run Puppet on new PSQL node to fix up certificates and permissions | 
| 114 |  | -    run_task('peadm::puppet_runonce', $targets) | 
|  | 117 | +    run_task('peadm::puppet_runonce', $postgresql_target) | 
| 115 | 118 |   } | 
| 116 | 119 | 
 | 
| 117 |  | -  if $operating_mode == 'init' { | 
| 118 |  | - | 
| 119 |  | -    # Update classification and database.ini settings, assume a replica PSQL | 
| 120 |  | -    # does not exist | 
| 121 |  | -    peadm::plan_step('update-classification') || { | 
|  | 120 | +  # Update classification and database.ini settings, assume a replica PSQL | 
|  | 121 | +  # does not exist | 
|  | 122 | +  peadm::plan_step('update-classification') || { | 
|  | 123 | + | 
|  | 124 | +    # To ensure everything is functional when a replica exists but only a single | 
|  | 125 | +    # PostgreSQL node has been deployed, configure alternate availability group | 
|  | 126 | +    # to connect to other group's new node | 
|  | 127 | +    if ($operating_mode == 'init' and $replica_host) { | 
|  | 128 | +      $a_host = $avail_group_letter ? { 'A' => $postgresql_host, default => undef } | 
|  | 129 | +      $b_host = $avail_group_letter ? { 'B' => $postgresql_host, default => undef } | 
|  | 130 | +      $host = pick($a_host, $b_host) | 
|  | 131 | +      out::verbose("In transitive state, setting classification to ${host}") | 
| 122 | 132 |       run_plan('peadm::util::update_classification', $primary_target, | 
| 123 |  | -        primary_postgresql_host => pick($primary_postgresql_host, $targets), | 
| 124 |  | -        peadm_config            => $peadm_config | 
|  | 133 | +        postgresql_a_host => $host, | 
|  | 134 | +        postgresql_b_host => $host, | 
|  | 135 | +        peadm_config      => $peadm_config | 
|  | 136 | +      ) | 
|  | 137 | +    } else { | 
|  | 138 | +      run_plan('peadm::util::update_classification', $primary_target, | 
|  | 139 | +        postgresql_a_host => $avail_group_letter ? { 'A' => $postgresql_host, default => undef }, | 
|  | 140 | +        postgresql_b_host => $avail_group_letter ? { 'B' => $postgresql_host, default => undef }, | 
|  | 141 | +        peadm_config      => $peadm_config | 
| 125 | 142 |       ) | 
| 126 | 143 |     } | 
|  | 144 | +  } | 
| 127 | 145 | 
 | 
| 128 |  | -    peadm::plan_step('update-db-settings') || { | 
| 129 |  | -      run_plan('peadm::util::update_db_setting', peadm::flatten_compact([ | 
| 130 |  | -        $compilers, | 
| 131 |  | -        $primary_target, | 
| 132 |  | -        $replica_target | 
| 133 |  | -      ]), | 
| 134 |  | -        primary_postgresql_host => $targets, | 
| 135 |  | -        peadm_config            => $peadm_config | 
| 136 |  | -      ) | 
|  | 146 | +  peadm::plan_step('update-db-settings') || { | 
|  | 147 | +    run_plan('peadm::util::update_db_setting', peadm::flatten_compact([ | 
|  | 148 | +      $compilers, | 
|  | 149 | +      $primary_target, | 
|  | 150 | +      $replica_target | 
|  | 151 | +    ]), | 
|  | 152 | +      postgresql_host => $postgresql_host, | 
|  | 153 | +      peadm_config    => $peadm_config | 
|  | 154 | +    ) | 
| 137 | 155 | 
 | 
| 138 |  | -      # (Re-)Start PuppetDB now that we are done making modifications | 
| 139 |  | -      run_command('systemctl restart pe-puppetdb.service', peadm::flatten_compact([ | 
| 140 |  | -        $primary_target, | 
| 141 |  | -        $replica_target | 
| 142 |  | -      ])) | 
| 143 |  | -    } | 
|  | 156 | +    # (Re-)Start PuppetDB now that we are done making modifications | 
|  | 157 | +    run_command('systemctl restart pe-puppetdb.service', peadm::flatten_compact([ | 
|  | 158 | +      $primary_target, | 
|  | 159 | +      $replica_target | 
|  | 160 | +    ])) | 
|  | 161 | +  } | 
| 144 | 162 | 
 | 
| 145 |  | -    # Clean up old puppetdb database on primary and those which were copied to | 
| 146 |  | -    # new host. | 
| 147 |  | -    peadm::plan_step('cleanup-db') || { | 
|  | 163 | +  peadm::plan_step('cleanup-db') || { | 
| 148 | 164 | 
 | 
|  | 165 | +    if $operating_mode == 'init' { | 
|  | 166 | +      # Clean up old puppetdb database on primary and those which were copied to | 
|  | 167 | +      # new host. | 
| 149 | 168 |       $target_db_purge = [ | 
| 150 | 169 |         'pe-activity', | 
| 151 | 170 |         'pe-classifier', | 
|  | 
| 157 | 176 |       # If a primary replica exists then pglogical is enabled and will prevent | 
| 158 | 177 |       # the clean up of databases on our target because it opens a connection.  | 
| 159 | 178 |       if $replica_host { | 
| 160 |  | -        run_plan('peadm::util::db_disable_pglogical', $targets, databases => $target_db_purge) | 
|  | 179 | +        run_plan('peadm::util::db_disable_pglogical', $postgresql_target, databases => $target_db_purge) | 
| 161 | 180 |       } | 
| 162 | 181 | 
 | 
| 163 | 182 |       # Clean up old databases | 
|  | 
| 167 | 186 |         $replica_target | 
| 168 | 187 |       ]) | 
| 169 | 188 | 
 | 
| 170 |  | -      run_plan('peadm::util::db_purge', $clean_source, databases => ['pe-puppetdb']) | 
| 171 |  | -      run_plan('peadm::util::db_purge', $targets,      databases => $target_db_purge) | 
| 172 |  | -    } | 
| 173 |  | -  } else { | 
| 174 |  | -    peadm::plan_step('update-classification') || { | 
| 175 |  | -      run_plan('peadm::util::update_classification', $primary_target, | 
| 176 |  | -        primary_postgresql_host => pick($primary_postgresql_host, $targets), | 
| 177 |  | -        replica_postgresql_host => pick($replica_postgresql_host, $targets), | 
| 178 |  | -        peadm_config            => $peadm_config | 
| 179 |  | -      ) | 
| 180 |  | -    } | 
| 181 |  | - | 
| 182 |  | -    # Plan needs to know which node is being added as well as primary and | 
| 183 |  | -    # replica designation | 
| 184 |  | -    peadm::plan_step('update-db-settings') || { | 
| 185 |  | -      run_plan('peadm::util::update_db_setting', peadm::flatten_compact([ | 
| 186 |  | -        $compilers, | 
| 187 |  | -        $primary_target, | 
| 188 |  | -        $replica_target | 
| 189 |  | -      ]), | 
| 190 |  | -        new_postgresql_host     => $targets, | 
| 191 |  | -        primary_postgresql_host => pick($primary_postgresql_host, $targets), | 
| 192 |  | -        replica_postgresql_host => pick($replica_postgresql_host, $targets), | 
| 193 |  | -        peadm_config            => $peadm_config | 
| 194 |  | -      ) | 
| 195 |  | - | 
| 196 |  | -      # (Re-)Start PuppetDB now that we are done making modifications | 
| 197 |  | -      run_command('systemctl restart pe-puppetdb.service', peadm::flatten_compact([ | 
| 198 |  | -        $primary_target, | 
| 199 |  | -        $replica_target | 
| 200 |  | -      ])) | 
| 201 |  | -    } | 
| 202 |  | -    peadm::plan_step('cleanup-db') || { | 
|  | 189 | +      run_plan('peadm::util::db_purge', $clean_source,      databases => ['pe-puppetdb']) | 
|  | 190 | +      run_plan('peadm::util::db_purge', $postgresql_target, databases => $target_db_purge) | 
|  | 191 | +    } else { | 
| 203 | 192 |       out::message("No databases to cleanup when in ${operating_mode}") | 
| 204 | 193 |     } | 
| 205 | 194 |   } | 
|  | 
| 212 | 201 |   peadm::plan_step('finalize') || { | 
| 213 | 202 |     # Run Puppet to sweep up but no restarts should occur so do them in parallel | 
| 214 | 203 |     run_task('peadm::puppet_runonce', peadm::flatten_compact([ | 
| 215 |  | -      $targets, | 
|  | 204 | +      $postgresql_target, | 
| 216 | 205 |       $primary_target, | 
| 217 | 206 |       $compilers, | 
| 218 | 207 |       $replica_target | 
| 219 | 208 |     ])) | 
| 220 | 209 | 
 | 
| 221 | 210 |     # Start Puppet agent | 
| 222 | 211 |     run_command('systemctl start puppet.service', peadm::flatten_compact([ | 
| 223 |  | -      $targets, | 
|  | 212 | +      $postgresql_target, | 
| 224 | 213 |       $compilers, | 
| 225 | 214 |       $primary_target, | 
| 226 | 215 |       $replica_target, | 
|  | 
0 commit comments