Skip to content

Commit ce8ae41

Browse files
committed
Ensure plan is runnable via PCP
Changes logic which decides when and where to start, stop, or restart Puppet Server and PuppetDB so that orchestration over PCP is always available. Cleans up some code along the way.
1 parent dafca1f commit ce8ae41

File tree

1 file changed

+68
-48
lines changed

1 file changed

+68
-48
lines changed

plans/add_database.pp

+68-48
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,10 @@
4040
out::message("Operating mode overridden by parameter mode set to ${mode}")
4141
} else {
4242
# If array is empty then no external databases were previously configured
43-
$no_external_db = peadm::flatten_compact([$primary_postgresql_host, $replica_postgresql_host]).empty
43+
$no_external_db = peadm::flatten_compact([
44+
$primary_postgresql_host,
45+
$replica_postgresql_host
46+
]).empty
4447

4548
# Pick operating mode based on array check
4649
if $no_external_db {
@@ -53,7 +56,11 @@
5356

5457
if $operating_mode == 'init' {
5558
# If no other PSQL node then match primary group letter
56-
$avail_group_letter = peadm::flatten_compact($roles['server'].map |$k,$v| { if $v == $primary_host { $k } })[0]
59+
$avail_group_letter = peadm::flatten_compact($roles['server'].map |$k,$v| {
60+
if $v == $primary_host {
61+
$k
62+
}
63+
})[0]
5764
# Assume PuppetDB backend hosted on Primary if in init mode
5865
$source_db_host = $primary_host
5966
} else {
@@ -65,7 +72,10 @@
6572
}
6673
})[0]
6774
# When in pair mode we assume the other PSQL node will serve as our source
68-
$source_db_host = peadm::flatten_compact([$primary_postgresql_host, $replica_postgresql_host]).reject($targets.peadm::certname())[0]
75+
$source_db_host = peadm::flatten_compact([
76+
$primary_postgresql_host,
77+
$replica_postgresql_host
78+
]).reject($targets.peadm::certname())[0]
6979
}
7080

7181
out::message("Adding PostgreSQL server ${targets.peadm::certname()} to availability group ${avail_group_letter}")
@@ -84,17 +94,17 @@
8494
}
8595

8696
# Stop Puppet to ensure catalogs are not being compiled for PE infrastructure nodes
87-
run_command('systemctl stop puppet', peadm::flatten_compact([
97+
run_command('systemctl stop puppet.service', peadm::flatten_compact([
8898
$targets,
8999
$compilers,
90100
$primary_target,
91101
$replica_target,
92102
$source_db_target
93103
]))
94104

95-
# Stop frontend services that causes changes to PuppetDB backend when agents request
96-
# catalogs, except for primary because we need it for the next step
97-
run_command('systemctl stop pe-puppetserver pe-puppetdb', $compilers)
105+
# Stop frontend compiler services that causes changes to PuppetDB backend when
106+
# agents request catalogs
107+
run_command('systemctl stop pe-puppetserver.service pe-puppetdb.service', $compilers)
98108

99109
peadm::plan_step('replicate-db') || {
100110
# Replicate content from source to newly installed PSQL server
@@ -104,20 +114,17 @@
104114
run_task('peadm::puppet_runonce', $targets)
105115
}
106116

107-
# Now stop the rest of the services that can cause PuppetDB changes
108-
run_command('systemctl stop pe-puppetserver pe-puppetdb', peadm::flatten_compact([
109-
$primary_target,
110-
$replica_target
111-
]))
112-
113-
# Update classification and database.ini setting
114117
if $operating_mode == 'init' {
118+
119+
# Update classification and database.ini settings, assume a replica PSQL
120+
# does not exist
115121
peadm::plan_step('update-classification') || {
116122
run_plan('peadm::util::update_classification', $primary_target,
117123
primary_postgresql_host => pick($primary_postgresql_host, $targets),
118124
peadm_config => $peadm_config
119125
)
120126
}
127+
121128
peadm::plan_step('update-db-settings') || {
122129
run_plan('peadm::util::update_db_setting', peadm::flatten_compact([
123130
$compilers,
@@ -127,6 +134,41 @@
127134
primary_postgresql_host => $targets,
128135
peadm_config => $peadm_config
129136
)
137+
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+
}
144+
145+
# Clean up old puppetdb database on primary and those which were copied to
146+
# new host.
147+
peadm::plan_step('cleanup-db') || {
148+
149+
$target_db_purge = [
150+
'pe-activity',
151+
'pe-classifier',
152+
'pe-inventory',
153+
'pe-orchestrator',
154+
'pe-rbac'
155+
]
156+
157+
# If a primary replica exists then pglogical is enabled and will prevent
158+
# the clean up of databases on our target because it opens a connection.
159+
if $replica_host {
160+
run_plan('peadm::util::db_disable_pglogical', $targets, databases => $target_db_purge)
161+
}
162+
163+
# Clean up old databases
164+
$clean_source = peadm::flatten_compact([
165+
$source_db_target,
166+
$primary_target,
167+
$replica_target
168+
])
169+
170+
run_plan('peadm::util::db_purge', $clean_source, databases => ['pe-puppetdb'])
171+
run_plan('peadm::util::db_purge', $targets, databases => $target_db_purge)
130172
}
131173
} else {
132174
peadm::plan_step('update-classification') || {
@@ -136,6 +178,7 @@
136178
peadm_config => $peadm_config
137179
)
138180
}
181+
139182
# Plan needs to know which node is being added as well as primary and
140183
# replica designation
141184
peadm::plan_step('update-db-settings') || {
@@ -149,44 +192,21 @@
149192
replica_postgresql_host => pick($replica_postgresql_host, $targets),
150193
peadm_config => $peadm_config
151194
)
152-
}
153-
}
154195

155-
# If in mode init clean up old puppetdb database on primary and those which
156-
# were copied to new host.
157-
if $operating_mode == 'init' {
158-
159-
$target_db_purge = [
160-
'pe-activity',
161-
'pe-classifier',
162-
'pe-inventory',
163-
'pe-orchestrator',
164-
'pe-rbac'
165-
]
166-
167-
# If a replica exists then pglogical is enabled and will prevent the clean up
168-
# of databases on our target because it opens a connection.
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+
}
169202
peadm::plan_step('cleanup-db') || {
170-
if $replica_host {
171-
run_plan('peadm::util::db_disable_pglogical', $targets, databases => $target_db_purge)
172-
}
173-
174-
# Clean up old databases
175-
$clean_source = peadm::flatten_compact([$source_db_target, $primary_target, $replica_target])
176-
run_plan('peadm::util::db_purge', $clean_source, databases => ['pe-puppetdb'])
177-
run_plan('peadm::util::db_purge', $targets, databases => $target_db_purge)
203+
out::message("No databases to cleanup when in ${operating_mode}")
178204
}
179-
} else {
180-
peadm::plan_step('cleanup-db') || {}
181-
out::message("No databases to cleanup when in ${operating_mode}")
182205
}
183206

184-
# Start services so catalogs can once again be compiled
185-
run_command('systemctl start pe-puppetserver pe-puppetdb', peadm::flatten_compact([
186-
$compilers,
187-
$primary_target,
188-
$replica_target
189-
]))
207+
# Start frontend compiler services so catalogs can once again be compiled by
208+
# agents
209+
run_command('systemctl start pe-puppetserver.service pe-puppetdb.service', $compilers)
190210

191211

192212
peadm::plan_step('finalize') || {
@@ -199,7 +219,7 @@
199219
]))
200220

201221
# Start Puppet agent
202-
run_command('systemctl start puppet', peadm::flatten_compact([
222+
run_command('systemctl start puppet.service', peadm::flatten_compact([
203223
$targets,
204224
$compilers,
205225
$primary_target,

0 commit comments

Comments
 (0)