Skip to content

Commit 9a9703e

Browse files
authored
PE-38801 Task added to fetch rules for PE Infrastructure Agent group and warn user that they will be replaced during convert and upgrade (#510)
1 parent 866fbf4 commit 9a9703e

File tree

7 files changed

+66
-0
lines changed

7 files changed

+66
-0
lines changed

REFERENCE.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
* [`download`](#download): Download a file using curl
6565
* [`enable_replica`](#enable_replica): Execute the enable replica puppet command
6666
* [`filesize`](#filesize): Return the size of a file in bytes
67+
* [`get_group_rules`](#get_group_rules): Run on a PE primary node to return the rules currently applied to the PE Infrastructure Agent group
6768
* [`get_peadm_config`](#get_peadm_config): Run on a PE primary node to return the currently configured PEAdm parameters
6869
* [`get_psql_version`](#get_psql_version): Run on a PE PSQL node to return the major version of the PSQL server currently installed
6970
* [`infrastatus`](#infrastatus): Runs puppet infra status and returns the output
@@ -1185,6 +1186,12 @@ Data type: `String`
11851186

11861187
Path to the file to return the size of
11871188

1189+
### <a name="get_group_rules"></a>`get_group_rules`
1190+
1191+
Run on a PE primary node to return the rules currently applied to the PE Infrastructure Agent group
1192+
1193+
**Supports noop?** false
1194+
11881195
### <a name="get_peadm_config"></a>`get_peadm_config`
11891196

11901197
Run on a PE primary node to return the currently configured PEAdm parameters

plans/convert.pp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,10 @@
261261
# the existing groups are correct enough to function until the upgrade is
262262
# performed.
263263
if (versioncmp($pe_version, '2019.7.0') >= 0) {
264+
$rules = run_task('peadm::get_group_rules', $primary_target).first.value['_output']
265+
$rules_formatted = stdlib::to_json_pretty(parsejson($rules))
266+
out::message("WARNING: The following existing rules on the PE Infrastructure Agent group will be overwritten with default values:\n ${rules_formatted}")
267+
264268
apply($primary_target) {
265269
class { 'peadm::setup::node_manager_yaml':
266270
primary_host => $primary_target.peadm::certname(),

plans/upgrade.pp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,10 @@
326326
default => $primary_postgresql_target.peadm::certname(),
327327
}
328328

329+
$rules = run_task('peadm::get_group_rules', $primary_target).first.value['_output']
330+
$rules_formatted = stdlib::to_json_pretty(parsejson($rules))
331+
out::message("WARNING: The following existing rules on the PE Infrastructure Agent group will be overwritten with default values:\n ${rules_formatted}")
332+
329333
apply($primary_target) {
330334
class { 'peadm::setup::node_manager_yaml':
331335
primary_host => $primary_target.peadm::certname(),

spec/plans/convert_spec.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
expect_task('peadm::cert_data').return_for_targets('primary' => trustedjson)
2222
expect_task('peadm::read_file').always_return({ 'content' => '2021.7.9' })
23+
expect_task('peadm::get_group_rules').return_for_targets('primary' => { '_output' => '{"rules": []}' })
2324

2425
# For some reason, expect_plan() was not working??
2526
allow_plan('peadm::modify_certificate').always_return({})

spec/plans/upgrade_spec.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ def allow_standard_non_returning_calls
2222

2323
it 'minimum variables to run' do
2424
allow_standard_non_returning_calls
25+
expect_task('peadm::get_group_rules').return_for_targets('primary' => { '_output' => '{"rules": []}' })
2526

2627
expect_task('peadm::read_file')
2728
.with_params('path' => '/opt/puppetlabs/server/pe_build')
@@ -36,6 +37,7 @@ def allow_standard_non_returning_calls
3637

3738
it 'runs with a primary, compilers, but no replica' do
3839
allow_standard_non_returning_calls
40+
expect_task('peadm::get_group_rules').return_for_targets('primary' => { '_output' => '{"rules": []}' })
3941

4042
expect_task('peadm::read_file')
4143
.with_params('path' => '/opt/puppetlabs/server/pe_build')
@@ -92,6 +94,7 @@ def allow_standard_non_returning_calls
9294
.always_return({ 'content' => installed_version })
9395

9496
expect_task('peadm::cert_data').return_for_targets('primary' => trusted_primary)
97+
expect_task('peadm::get_group_rules').return_for_targets('primary' => { '_output' => '{"rules": []}' })
9598
end
9699

97100
it 'updates pe.conf if r10k_known_hosts is set' do

tasks/get_group_rules.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"description": "Run on a PE primary node to return the rules currently applied to the PE Infrastructure Agent group",
3+
"parameters": { },
4+
"input_method": "stdin"
5+
}

tasks/get_group_rules.rb

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#!/opt/puppetlabs/puppet/bin/ruby
2+
# frozen_string_literal: true
3+
4+
require 'json'
5+
require 'net/http'
6+
require 'puppet'
7+
8+
# GetInfrastructureAgentGroupRules task class
9+
class GetInfrastructureAgentGroupRules
10+
def execute!
11+
infrastructure_agent_group = groups.find { |obj| obj['name'] == 'PE Infrastructure Agent' }
12+
if infrastructure_agent_group
13+
puts JSON.pretty_generate(infrastructure_agent_group['rule'])
14+
else
15+
puts JSON.pretty_generate({ 'error' => 'PE Infrastructure Agent group does not exist' })
16+
end
17+
end
18+
19+
def groups
20+
net = https(4433)
21+
res = net.get('/classifier-api/v1/groups')
22+
JSON.parse(res.body)
23+
end
24+
25+
def https(port)
26+
https = Net::HTTP.new(Puppet.settings[:certname], port)
27+
https.use_ssl = true
28+
https.cert = OpenSSL::X509::Certificate.new(File.read(Puppet.settings[:hostcert]))
29+
https.key = OpenSSL::PKey::RSA.new(File.read(Puppet.settings[:hostprivkey]))
30+
https.verify_mode = OpenSSL::SSL::VERIFY_PEER
31+
https.ca_file = Puppet.settings[:localcacert]
32+
https
33+
end
34+
end
35+
36+
# Run the task unless an environment flag has been set, signaling not to. The
37+
# environment flag is used to disable auto-execution and enable Ruby unit
38+
# testing of this task.
39+
unless ENV['RSPEC_UNIT_TEST_MODE']
40+
Puppet.initialize_settings
41+
GetInfrastructureAgentGroupRules.new.execute!
42+
end

0 commit comments

Comments
 (0)