Skip to content

Commit f830c65

Browse files
ragingraCoMfUcIoS
authored andcommitted
(PE-39118) Adding code manager check to add_replica (#501)
* (PE-39118) Adding code manager check to add_replica * Fixing lint, and changing null return to false * Adding VERIFY_PEER
1 parent 438ca01 commit f830c65

File tree

5 files changed

+118
-0
lines changed

5 files changed

+118
-0
lines changed

REFERENCE.md

+15
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
* [`cert_valid_status`](#cert_valid_status): Check primary for valid state of a certificate
5959
* [`classify_compilers`](#classify_compilers): Classify compilers as legacy or non-legacy
6060
* [`code_manager`](#code_manager): Perform various code manager actions
61+
* [`code_manager_enabled`](#code_manager_enabled): Run on a PE primary node to check if Code Manager is enabled.
6162
* [`code_sync_status`](#code_sync_status): A task to confirm code is in sync accross the cluster for clusters with code manager configured
6263
* [`divert_code_manager`](#divert_code_manager): Divert the code manager live-dir setting
6364
* [`download`](#download): Download a file using curl
@@ -1092,6 +1093,20 @@ Data type: `String`
10921093

10931094
What code manager action to perform. For example: 'deploy production'; 'flush-environment-cache'; 'file-sync commit'
10941095

1096+
### <a name="code_manager_enabled"></a>`code_manager_enabled`
1097+
1098+
Run on a PE primary node to check if Code Manager is enabled.
1099+
1100+
**Supports noop?** false
1101+
1102+
#### Parameters
1103+
1104+
##### `host`
1105+
1106+
Data type: `String[1]`
1107+
1108+
Hostname of the PE primary node
1109+
10951110
### <a name="code_sync_status"></a>`code_sync_status`
10961111

10971112
A task to confirm code is in sync accross the cluster for clusters with code manager configured

plans/add_replica.pp

+8
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,14 @@
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(
26+
'peadm::code_manager_enabled', $primary_target, host => $primary_target.peadm::certname()
27+
).first.value['code_manager_enabled']
28+
29+
if $code_manager_enabled == false {
30+
fail('Code Manager must be enabled to add a replica. Please refer to the docs for more information on enabling Code Manager.')
31+
}
32+
2533
run_command('systemctl stop puppet.service', peadm::flatten_compact([
2634
$primary_target,
2735
$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

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"description": "Run on a PE primary node to check if Code Manager is enabled.",
3+
"parameters": {
4+
"host": {
5+
"type": "String[1]",
6+
"description": "Hostname of the PE primary node"
7+
}
8+
},
9+
"input_method": "stdin"
10+
}

tasks/code_manager_enabled.rb

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

0 commit comments

Comments
 (0)