Skip to content

Commit 82970d8

Browse files
committed
(PE-39118) Adding code manager check to add_replica
1 parent cb4be70 commit 82970d8

File tree

4 files changed

+97
-0
lines changed

4 files changed

+97
-0
lines changed

plans/add_replica.pp

+5
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@
2222
$replica_target = peadm::get_targets($replica_host, 1)
2323
$replica_postgresql_target = peadm::get_targets($replica_postgresql_host, 1)
2424

25+
$code_manager_enabled = run_task('peadm::code_manager_enabled', $primary_target).first.value['code_manager_enabled']
26+
if $code_manager_enabled == false {
27+
fail('Code Manager must be enabled to add a replica. Please refer to the docs for more information on enabling Code Manager.')
28+
}
29+
2530
run_command('systemctl stop puppet.service', peadm::flatten_compact([
2631
$primary_target,
2732
$replica_postgresql_target,

spec/plans/add_replica_spec.rb

+12
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ def allow_standard_non_returning_calls
1111
end
1212

1313
describe 'basic functionality' do
14+
let(:code_manager_enabled) { { 'code_manager_enabled' => true } }
1415
let(:params) { { 'primary_host' => 'primary', 'replica_host' => 'replica' } }
1516
let(:cfg) { { 'params' => { 'primary_host' => 'primary' } } }
1617
let(:certdata) do
@@ -30,6 +31,7 @@ def allow_standard_non_returning_calls
3031

3132
it 'runs successfully when the primary does not have alt-names' do
3233
allow_standard_non_returning_calls
34+
expect_task('peadm::code_manager_enabled').always_return(code_manager_enabled)
3335
expect_task('peadm::get_peadm_config').always_return(cfg)
3436
expect_task('peadm::cert_data').always_return(certdata).be_called_times(4)
3537
expect_task('peadm::cert_valid_status').always_return(certstatus)
@@ -50,6 +52,7 @@ def allow_standard_non_returning_calls
5052

5153
it 'runs successfully when the primary has alt-names' do
5254
allow_standard_non_returning_calls
55+
expect_task('peadm::code_manager_enabled').always_return(code_manager_enabled)
5356
expect_task('peadm::get_peadm_config').always_return(cfg)
5457
expect_task('peadm::cert_data').always_return(certdata.merge({ 'dns-alt-names' => ['primary', 'alt'] })).be_called_times(4)
5558
expect_task('peadm::cert_valid_status').always_return(certstatus)
@@ -67,5 +70,14 @@ def allow_standard_non_returning_calls
6770
expect_out_verbose.with_params('Updating classification to...')
6871
expect(run_plan('peadm::add_replica', params)).to be_ok
6972
end
73+
74+
it 'fails when code manager not enabled' do
75+
allow_standard_non_returning_calls
76+
expect_task('peadm::code_manager_enabled').always_return({ 'code_manager_enabled' => false })
77+
78+
result = run_plan('peadm::add_replica', params)
79+
expect(result).not_to be_ok
80+
expect(result.value.msg).to match(%r{Code Manager must be enabled})
81+
end
7082
end
7183
end

tasks/code_manager_enabled.json

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"description": "Run on a PE primary node to check if Code Manager is enabled.",
3+
"parameters": { },
4+
"input_method": "stdin"
5+
}

tasks/code_manager_enabled.rb

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
#!/opt/puppetlabs/puppet/bin/ruby
2+
# frozen_string_literal: true
3+
4+
require 'json'
5+
require 'uri'
6+
require 'net/http'
7+
require 'puppet'
8+
9+
# GetPEAdmConfig task class
10+
class GetPEAdmConfig
11+
def initialize(params); end
12+
13+
def execute!
14+
code_manager_enabled = groups.dig('PE Master', 'classes', 'puppet_enterprise::profile::master', 'code_manager_auto_configure')
15+
16+
puts({"code_manager_enabled" => code_manager_enabled}.to_json)
17+
end
18+
19+
# Returns a GetPEAdmConfig::NodeGroups object created from the /groups object
20+
# returned by the classifier
21+
def groups
22+
@groups ||= begin
23+
net = https(4433)
24+
res = net.get('/classifier-api/v1/groups')
25+
NodeGroup.new(JSON.parse(res.body))
26+
end
27+
end
28+
29+
def https(port)
30+
https = Net::HTTP.new('localhost', port)
31+
https.use_ssl = true
32+
https.cert = @cert ||= OpenSSL::X509::Certificate.new(File.read(Puppet.settings[:hostcert]))
33+
https.key = @key ||= OpenSSL::PKey::RSA.new(File.read(Puppet.settings[:hostprivkey]))
34+
https.verify_mode = OpenSSL::SSL::VERIFY_NONE
35+
https
36+
end
37+
38+
def pdb_query(query)
39+
pdb = https(8081)
40+
pdb_request = Net::HTTP::Get.new('/pdb/query/v4')
41+
pdb_request.set_form_data({ 'query' => query })
42+
JSON.parse(pdb.request(pdb_request).body)
43+
end
44+
45+
# Utility class to aid in retrieving useful information from the node group
46+
# data
47+
class NodeGroup
48+
attr_reader :data
49+
50+
def initialize(data)
51+
@data = data
52+
end
53+
54+
# Aids in digging into node groups by name, rather than UUID
55+
def dig(name, *args)
56+
group = @data.find { |obj| obj['name'] == name }
57+
if group.nil?
58+
nil
59+
elsif args.empty?
60+
group
61+
else
62+
group.dig(*args)
63+
end
64+
end
65+
end
66+
end
67+
68+
# Run the task unless an environment flag has been set, signaling not to. The
69+
# environment flag is used to disable auto-execution and enable Ruby unit
70+
# testing of this task.
71+
unless ENV['RSPEC_UNIT_TEST_MODE']
72+
Puppet.initialize_settings
73+
task = GetPEAdmConfig.new(JSON.parse(STDIN.read))
74+
task.execute!
75+
end

0 commit comments

Comments
 (0)