Skip to content

Commit 4b1a560

Browse files
author
petergmurphy
committed
(PE-39577) Optimise legacy compiler support
1 parent 26c3ddf commit 4b1a560

File tree

4 files changed

+160
-3
lines changed

4 files changed

+160
-3
lines changed

Diff for: plans/convert.pp

+12-2
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@
232232
run_plan('peadm::modify_certificate', $legacy_compiler_a_targets,
233233
primary_host => $primary_target,
234234
add_extensions => {
235-
peadm::oid('pp_auth_role') => 'pe_compiler',
235+
peadm::oid('pp_auth_role') => 'legacy_compiler',
236236
peadm::oid('peadm_availability_group') => 'A',
237237
peadm::oid('peadm_legacy_compiler') => 'true',
238238
},
@@ -242,7 +242,7 @@
242242
run_plan('peadm::modify_certificate', $legacy_compiler_b_targets,
243243
primary_host => $primary_target,
244244
add_extensions => {
245-
peadm::oid('pp_auth_role') => 'pe_compiler',
245+
peadm::oid('pp_auth_role') => 'legacy_compiler',
246246
peadm::oid('peadm_availability_group') => 'B',
247247
peadm::oid('peadm_legacy_compiler') => 'true',
248248
},
@@ -283,6 +283,16 @@
283283
284284
include peadm::setup::convert_node_manager
285285
}
286+
287+
# Unpin legacy compilers from PE Master group
288+
if $legacy_compiler_targets {
289+
$legacy_compiler_targets.each |$target| {
290+
run_task('peadm::node_group_unpin', $primary_target,
291+
selected_node => $target.peadm::certname(),
292+
group => 'PE Master',
293+
)
294+
}
295+
}
286296
}
287297
else {
288298
# lint:ignore:strict_indent

Diff for: tasks/get_peadm_config.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def execute!
2222

2323
def config
2424
# Compute values
25-
primary = groups.pinned('PE Master')
25+
primary = groups.pinned('PE Certificate Authority')
2626
replica = groups.pinned('PE HA Replica')
2727
server_a = server('puppet/server', 'A', [primary, replica].compact)
2828
server_b = server('puppet/server', 'B', [primary, replica].compact)

Diff for: tasks/node_group_unpin.json

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"description": "Unpins a node from a specified PE node group",
3+
"parameters": {
4+
"selected_node": {
5+
"type": "String",
6+
"description": "The certname of the node to unpin"
7+
},
8+
"group": {
9+
"type": "String",
10+
"description": "The name of the node group to unpin the node from"
11+
}
12+
},
13+
"input_method": "stdin",
14+
"implementations": [
15+
{"name": "node_group_unpin.rb"}
16+
]
17+
}

Diff for: tasks/node_group_unpin.rb

+130
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
#!/opt/puppetlabs/puppet/bin/ruby
2+
# frozen_string_literal: true
3+
4+
require 'json'
5+
require 'yaml'
6+
require 'net/https'
7+
require 'puppet'
8+
9+
# NodeGroupUnpin task class
10+
class NodeGroupUnpin
11+
def initialize(params)
12+
@params = params
13+
raise "Missing required parameter 'selected_node'" unless @params['selected_node']
14+
raise "Missing required parameter 'group'" unless @params['group']
15+
@auth = YAML.load_file('/etc/puppetlabs/puppet/classifier.yaml')
16+
rescue Errno::ENOENT
17+
raise 'Could not find classifier.yaml at /etc/puppetlabs/puppet/classifier.yaml'
18+
end
19+
20+
def https_client
21+
client = Net::HTTP.new(Puppet.settings[:certname], 4433)
22+
client.use_ssl = true
23+
client.cert = @cert ||= OpenSSL::X509::Certificate.new(File.read(Puppet.settings[:hostcert]))
24+
client.key = @key ||= OpenSSL::PKey::RSA.new(File.read(Puppet.settings[:hostprivkey]))
25+
client.verify_mode = OpenSSL::SSL::VERIFY_PEER
26+
client.ca_file = Puppet.settings[:localcacert]
27+
client
28+
end
29+
30+
def groups
31+
puts 'Debug: Fetching groups'
32+
@groups ||= begin
33+
net = https_client
34+
puts 'Debug: Making GET request to /classifier-api/v1/groups'
35+
res = net.get('/classifier-api/v1/groups')
36+
puts "Debug: Response code: #{res.code}"
37+
puts "Debug: Response body preview: #{res.body[0..100]}..." if res.body
38+
39+
unless res.code == '200'
40+
raise "Failed to fetch groups: HTTP #{res.code} - #{res.body}"
41+
end
42+
43+
NodeGroup.new(JSON.parse(res.body))
44+
rescue JSON::ParserError => e
45+
puts "Debug: JSON parse error: #{e.message}"
46+
raise "Invalid JSON response from server: #{e.message}"
47+
rescue StandardError => e
48+
puts "Debug: Error in groups method: #{e.class} - #{e.message}"
49+
raise "Error fetching groups: #{e.message}"
50+
end
51+
end
52+
53+
def pe_master_group
54+
groups.dig('PE Master')
55+
end
56+
57+
def unpin_node(group, node)
58+
raise 'Invalid group object' unless group.is_a?(Hash) && group['id'] && group['name']
59+
60+
net = https_client
61+
begin
62+
data = { "nodes": [node] }.to_json
63+
url = "/classifier-api/v1/groups/#{group['id']}/unpin"
64+
65+
puts "Debug: Making POST request to #{url}"
66+
puts "Debug: Request body: #{data}"
67+
68+
req = Net::HTTP::Post.new(url)
69+
req['Content-Type'] = 'application/json'
70+
req.body = data
71+
72+
res = net.request(req)
73+
puts "Debug: Response code: #{res.code}"
74+
puts "Debug: Response body: #{res.body}" if res.body
75+
76+
case res.code
77+
when '204'
78+
puts "Successfully unpinned node '#{node}' from group '#{group['name']}'"
79+
else
80+
begin
81+
error_body = JSON.parse(res.body.to_s)
82+
raise "Failed to unpin node: #{error_body['kind'] || error_body}"
83+
rescue JSON::ParserError
84+
raise "Invalid response from server (status #{res.code}): #{res.body}"
85+
end
86+
end
87+
rescue StandardError => e
88+
raise "Error during unpin request: #{e.message}"
89+
end
90+
end
91+
92+
# Utility class to aid in retrieving useful information from the node group
93+
# data
94+
class NodeGroup
95+
attr_reader :data
96+
97+
def initialize(data)
98+
@data = data
99+
end
100+
101+
# Aids in digging into node groups by name, rather than UUID
102+
def dig(name, *args)
103+
group = @data.find { |obj| obj['name'] == name }
104+
if group.nil?
105+
nil
106+
elsif args.empty?
107+
group
108+
else
109+
group.dig(*args)
110+
end
111+
end
112+
end
113+
114+
def execute!
115+
group = pe_master_group
116+
if group
117+
unpin_node(group, @params['selected_node'])
118+
puts "Unpinned #{@params['selected_node']} from #{@params['group']}"
119+
else
120+
puts "Group #{@params['group']} not found"
121+
end
122+
end
123+
end
124+
125+
# Run the task unless an environment flag has been set
126+
unless ENV['RSPEC_UNIT_TEST_MODE']
127+
Puppet.initialize_settings
128+
task = NodeGroupUnpin.new(JSON.parse(STDIN.read))
129+
task.execute!
130+
end

0 commit comments

Comments
 (0)