Skip to content

Commit 977a9f2

Browse files
committed
WIP: Code clean-up and pcp
1 parent dafca1f commit 977a9f2

File tree

1 file changed

+70
-48
lines changed

1 file changed

+70
-48
lines changed

plans/add_database.pp

+70-48
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
$primary_target = peadm::get_targets($primary_host, 1)
1515

16+
$transport = $primary_target[0].config['transport']
17+
1618
# Get current peadm config before making modifications and shutting down
1719
# PuppetDB
1820
$peadm_config = run_task('peadm::get_peadm_config', $primary_target).first.value
@@ -40,7 +42,10 @@
4042
out::message("Operating mode overridden by parameter mode set to ${mode}")
4143
} else {
4244
# 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
45+
$no_external_db = peadm::flatten_compact([
46+
$primary_postgresql_host,
47+
$replica_postgresql_host
48+
]).empty
4449

4550
# Pick operating mode based on array check
4651
if $no_external_db {
@@ -53,7 +58,11 @@
5358

5459
if $operating_mode == 'init' {
5560
# 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]
61+
$avail_group_letter = peadm::flatten_compact($roles['server'].map |$k,$v| {
62+
if $v == $primary_host {
63+
$k
64+
}
65+
})[0]
5766
# Assume PuppetDB backend hosted on Primary if in init mode
5867
$source_db_host = $primary_host
5968
} else {
@@ -65,7 +74,10 @@
6574
}
6675
})[0]
6776
# 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]
77+
$source_db_host = peadm::flatten_compact([
78+
$primary_postgresql_host,
79+
$replica_postgresql_host
80+
]).reject($targets.peadm::certname())[0]
6981
}
7082

7183
out::message("Adding PostgreSQL server ${targets.peadm::certname()} to availability group ${avail_group_letter}")
@@ -84,17 +96,17 @@
8496
}
8597

8698
# Stop Puppet to ensure catalogs are not being compiled for PE infrastructure nodes
87-
run_command('systemctl stop puppet', peadm::flatten_compact([
99+
run_command('systemctl stop puppet.service', peadm::flatten_compact([
88100
$targets,
89101
$compilers,
90102
$primary_target,
91103
$replica_target,
92104
$source_db_target
93105
]))
94106

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)
107+
# Stop frontend compiler services that causes changes to PuppetDB backend when
108+
# agents request catalogs
109+
run_command('systemctl stop pe-puppetserver.service pe-puppetdb.service', $compilers)
98110

99111
peadm::plan_step('replicate-db') || {
100112
# Replicate content from source to newly installed PSQL server
@@ -104,20 +116,17 @@
104116
run_task('peadm::puppet_runonce', $targets)
105117
}
106118

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
114119
if $operating_mode == 'init' {
120+
121+
# Update classification and database.ini settings, assume a replica PSQL
122+
# does not exist
115123
peadm::plan_step('update-classification') || {
116124
run_plan('peadm::util::update_classification', $primary_target,
117125
primary_postgresql_host => pick($primary_postgresql_host, $targets),
118126
peadm_config => $peadm_config
119127
)
120128
}
129+
121130
peadm::plan_step('update-db-settings') || {
122131
run_plan('peadm::util::update_db_setting', peadm::flatten_compact([
123132
$compilers,
@@ -127,6 +136,41 @@
127136
primary_postgresql_host => $targets,
128137
peadm_config => $peadm_config
129138
)
139+
140+
# (Re-)Start PuppetDB now that we are done making modifications
141+
run_command('systemctl restart pe-puppetdb.service', peadm::flatten_compact([
142+
$primary_target,
143+
$replica_target
144+
]))
145+
}
146+
147+
# Clean up old puppetdb database on primary and those which were copied to
148+
# new host.
149+
peadm::plan_step('cleanup-db') || {
150+
151+
$target_db_purge = [
152+
'pe-activity',
153+
'pe-classifier',
154+
'pe-inventory',
155+
'pe-orchestrator',
156+
'pe-rbac'
157+
]
158+
159+
# If a primary replica exists then pglogical is enabled and will prevent
160+
# the clean up of databases on our target because it opens a connection.
161+
if $replica_host {
162+
run_plan('peadm::util::db_disable_pglogical', $targets, databases => $target_db_purge)
163+
}
164+
165+
# Clean up old databases
166+
$clean_source = peadm::flatten_compact([
167+
$source_db_target,
168+
$primary_target,
169+
$replica_target
170+
])
171+
172+
run_plan('peadm::util::db_purge', $clean_source, databases => ['pe-puppetdb'])
173+
run_plan('peadm::util::db_purge', $targets, databases => $target_db_purge)
130174
}
131175
} else {
132176
peadm::plan_step('update-classification') || {
@@ -136,6 +180,7 @@
136180
peadm_config => $peadm_config
137181
)
138182
}
183+
139184
# Plan needs to know which node is being added as well as primary and
140185
# replica designation
141186
peadm::plan_step('update-db-settings') || {
@@ -149,44 +194,21 @@
149194
replica_postgresql_host => pick($replica_postgresql_host, $targets),
150195
peadm_config => $peadm_config
151196
)
152-
}
153-
}
154197

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.
198+
# (Re-)Start PuppetDB now that we are done making modifications
199+
run_command('systemctl restart pe-puppetdb.service', peadm::flatten_compact([
200+
$primary_target,
201+
$replica_target
202+
]))
203+
}
169204
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)
205+
out::message("No databases to cleanup when in ${operating_mode}")
178206
}
179-
} else {
180-
peadm::plan_step('cleanup-db') || {}
181-
out::message("No databases to cleanup when in ${operating_mode}")
182207
}
183208

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-
]))
209+
# Start frontend compiler services so catalogs can once again be compiled by
210+
# agents
211+
run_command('systemctl start pe-puppetserver.service pe-puppetdb.service', $compilers)
190212

191213

192214
peadm::plan_step('finalize') || {
@@ -199,7 +221,7 @@
199221
]))
200222

201223
# Start Puppet agent
202-
run_command('systemctl start puppet', peadm::flatten_compact([
224+
run_command('systemctl start puppet.service', peadm::flatten_compact([
203225
$targets,
204226
$compilers,
205227
$primary_target,

0 commit comments

Comments
 (0)