Skip to content

Fix classification when adding some components #258

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Jun 15, 2022
1 change: 1 addition & 0 deletions .fixtures.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ fixtures:
forge_modules:
ruby_task_helper: "puppetlabs/ruby_task_helper"
service: "puppetlabs/service"
package: "puppetlabs/package"
repositories:
facts: 'https://github.com/puppetlabs/puppetlabs-facts.git'
puppet_agent: 'https://github.com/puppetlabs/puppetlabs-puppet_agent.git'
Expand Down
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"python.linting.pylintEnabled": true,
"python.linting.enabled": true
"python.linting.enabled": true,
"git.ignoreLimitWarning": true
}
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ group :development do
gem "puppet-module-win-dev-r#{minor_version}", '~> 1.0', require: false, platforms: [:mswin, :mingw, :x64_mingw]
gem "puppet-debugger", '>= 0.18.0', require: false
gem "bolt", '>= 3.17.0', require: false
gem "github_changelog_generator", require: false
gem "github_changelog_generator", '>= 1.16.4', require: false
gem "octokit", '4.21.0', require: false
end
group :system_tests do
Expand Down
2 changes: 1 addition & 1 deletion documentation/automated_recovery.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Procedure:

2. Temporarily set both primary and replica server nodes so that they use the remaining healthy PE-PostgreSQL server

bolt plan run peadm::util::update_db_setting --target <primary-server-fqdn>,<replica-server-fqdn> primary_postgresql_host=<working-postgres-server-fqdn>
bolt plan run peadm::util::update_db_setting --target <primary-server-fqdn>,<replica-server-fqdn> primary_postgresql_host=<working-postgres-server-fqdn> override=true

3. Restart `pe-puppetdb.service` on Puppet server primary and replica

Expand Down
4 changes: 4 additions & 0 deletions metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@
"name": "puppetlabs/service",
"version_requirement": ">= 1.3.0 < 3.0.0"
},
{
"name": "puppetlabs/package",
"version_requirement": ">= 2.1.0 < 3.0.0"
},
{
"name": "puppetlabs/inifile",
"version_requirement": ">= 5.2.0 < 6.0.0"
Expand Down
141 changes: 65 additions & 76 deletions plans/add_database.pp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
) {

$primary_target = peadm::get_targets($primary_host, 1)
$postgresql_target = peadm::get_targets($targets, 1)

$postgresql_host = $postgresql_target.peadm::certname()

# Get current peadm config before making modifications and shutting down
# PuppetDB
Expand All @@ -21,13 +24,13 @@

# Bail if this is trying to be ran against Standard
if $compilers.empty {
fail_plan('Plan Peadm::Add_database only applicable for L and XL deployments')
fail_plan('Plan peadm::add_database is only applicable for L and XL deployments')
}

# Existing nodes and their assignments
$replica_host = $peadm_config['params']['replica_host']
$primary_postgresql_host = $peadm_config['params']['primary_postgresql_host']
$replica_postgresql_host = $peadm_config['params']['replica_postgresql_host']
$postgresql_a_host = $peadm_config['role-letter']['postgresql']['A']
$postgresql_b_host = $peadm_config['role-letter']['postgresql']['B']

$replica_target = peadm::get_targets($replica_host, 1)

Expand All @@ -41,8 +44,8 @@
} else {
# If array is empty then no external databases were previously configured
$no_external_db = peadm::flatten_compact([
$primary_postgresql_host,
$replica_postgresql_host
$postgresql_a_host,
$postgresql_b_host
]).empty

# Pick operating mode based on array check
Expand All @@ -67,26 +70,26 @@
# The letter which doesn't yet have a server assigned or in the event this
# is a replacement operation, the letter this node was assigned to previously
$avail_group_letter = peadm::flatten_compact($roles['postgresql'].map |$k,$v| {
if (! $v) or ($v == $targets.peadm::certname()) {
if (! $v) or ($v == $postgresql_host) {
$k
}
})[0]
# When in pair mode we assume the other PSQL node will serve as our source
$source_db_host = peadm::flatten_compact([
$primary_postgresql_host,
$replica_postgresql_host
]).reject($targets.peadm::certname())[0]
$postgresql_a_host,
$postgresql_b_host
]).reject($postgresql_host)[0]
}

out::message("Adding PostgreSQL server ${targets.peadm::certname()} to availability group ${avail_group_letter}")
out::message("Using ${source_db_host} to populate ${targets.peadm::certname()}")
out::message("Adding PostgreSQL server ${postgresql_host} to availability group ${avail_group_letter}")
out::message("Using ${source_db_host} to populate ${postgresql_host}")

$source_db_target = peadm::get_targets($source_db_host, 1)

peadm::plan_step('init-db-node') || {
# Install PSQL on new node to be used as external PuppetDB backend by using
# puppet in lieu of installer
run_plan('peadm::subplans::component_install', $targets,
run_plan('peadm::subplans::component_install', $postgresql_target,
primary_host => $primary_target,
avail_group_letter => $avail_group_letter,
role => 'puppet/puppetdb-database'
Expand All @@ -95,7 +98,7 @@

# Stop Puppet to ensure catalogs are not being compiled for PE infrastructure nodes
run_command('systemctl stop puppet.service', peadm::flatten_compact([
$targets,
$postgresql_target,
$compilers,
$primary_target,
$replica_target,
Expand All @@ -108,44 +111,60 @@

peadm::plan_step('replicate-db') || {
# Replicate content from source to newly installed PSQL server
run_plan('peadm::subplans::db_populate', $targets, source_host => $source_db_target.peadm::certname())
run_plan('peadm::subplans::db_populate', $postgresql_target, source_host => $source_db_target.peadm::certname())

# Run Puppet on new PSQL node to fix up certificates and permissions
run_task('peadm::puppet_runonce', $targets)
run_task('peadm::puppet_runonce', $postgresql_target)
}

if $operating_mode == 'init' {

# Update classification and database.ini settings, assume a replica PSQL
# does not exist
peadm::plan_step('update-classification') || {
# Update classification and database.ini settings, assume a replica PSQL
# does not exist
peadm::plan_step('update-classification') || {

# To ensure everything is functional when a replica exists but only a single
# PostgreSQL node has been deployed, configure alternate availability group
# to connect to other group's new node
if ($operating_mode == 'init' and $replica_host) {
$a_host = $avail_group_letter ? { 'A' => $postgresql_host, default => undef }
$b_host = $avail_group_letter ? { 'B' => $postgresql_host, default => undef }
$host = pick($a_host, $b_host)
out::verbose("In transitive state, setting classification to ${host}")
run_plan('peadm::util::update_classification', $primary_target,
primary_postgresql_host => pick($primary_postgresql_host, $targets),
peadm_config => $peadm_config
postgresql_a_host => $host,
postgresql_b_host => $host,
peadm_config => $peadm_config
)
} else {
run_plan('peadm::util::update_classification', $primary_target,
postgresql_a_host => $avail_group_letter ? { 'A' => $postgresql_host, default => undef },
postgresql_b_host => $avail_group_letter ? { 'B' => $postgresql_host, default => undef },
peadm_config => $peadm_config
)
}
}

peadm::plan_step('update-db-settings') || {
run_plan('peadm::util::update_db_setting', peadm::flatten_compact([
$compilers,
$primary_target,
$replica_target
]),
primary_postgresql_host => $targets,
peadm_config => $peadm_config
)
peadm::plan_step('update-db-settings') || {
run_plan('peadm::util::update_db_setting', peadm::flatten_compact([
$compilers,
$primary_target,
$replica_target
]),
postgresql_host => $postgresql_host,
peadm_config => $peadm_config
)

# (Re-)Start PuppetDB now that we are done making modifications
run_command('systemctl restart pe-puppetdb.service', peadm::flatten_compact([
$primary_target,
$replica_target
]))
}
# (Re-)Start PuppetDB now that we are done making modifications
run_command('systemctl restart pe-puppetdb.service', peadm::flatten_compact([
$primary_target,
$replica_target
]))
}

# Clean up old puppetdb database on primary and those which were copied to
# new host.
peadm::plan_step('cleanup-db') || {
peadm::plan_step('cleanup-db') || {

if $operating_mode == 'init' {
# Clean up old puppetdb database on primary and those which were copied to
# new host.
$target_db_purge = [
'pe-activity',
'pe-classifier',
Expand All @@ -157,7 +176,7 @@
# If a primary replica exists then pglogical is enabled and will prevent
# the clean up of databases on our target because it opens a connection.
if $replica_host {
run_plan('peadm::util::db_disable_pglogical', $targets, databases => $target_db_purge)
run_plan('peadm::util::db_disable_pglogical', $postgresql_target, databases => $target_db_purge)
}

# Clean up old databases
Expand All @@ -167,39 +186,9 @@
$replica_target
])

run_plan('peadm::util::db_purge', $clean_source, databases => ['pe-puppetdb'])
run_plan('peadm::util::db_purge', $targets, databases => $target_db_purge)
}
} else {
peadm::plan_step('update-classification') || {
run_plan('peadm::util::update_classification', $primary_target,
primary_postgresql_host => pick($primary_postgresql_host, $targets),
replica_postgresql_host => pick($replica_postgresql_host, $targets),
peadm_config => $peadm_config
)
}

# Plan needs to know which node is being added as well as primary and
# replica designation
peadm::plan_step('update-db-settings') || {
run_plan('peadm::util::update_db_setting', peadm::flatten_compact([
$compilers,
$primary_target,
$replica_target
]),
new_postgresql_host => $targets,
primary_postgresql_host => pick($primary_postgresql_host, $targets),
replica_postgresql_host => pick($replica_postgresql_host, $targets),
peadm_config => $peadm_config
)

# (Re-)Start PuppetDB now that we are done making modifications
run_command('systemctl restart pe-puppetdb.service', peadm::flatten_compact([
$primary_target,
$replica_target
]))
}
peadm::plan_step('cleanup-db') || {
run_plan('peadm::util::db_purge', $clean_source, databases => ['pe-puppetdb'])
run_plan('peadm::util::db_purge', $postgresql_target, databases => $target_db_purge)
} else {
out::message("No databases to cleanup when in ${operating_mode}")
}
}
Expand All @@ -212,15 +201,15 @@
peadm::plan_step('finalize') || {
# Run Puppet to sweep up but no restarts should occur so do them in parallel
run_task('peadm::puppet_runonce', peadm::flatten_compact([
$targets,
$postgresql_target,
$primary_target,
$compilers,
$replica_target
]))

# Start Puppet agent
run_command('systemctl start puppet.service', peadm::flatten_compact([
$targets,
$postgresql_target,
$compilers,
$primary_target,
$replica_target,
Expand Down
Loading