-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(PE-39577) Optimise legacy compiler support
- Loading branch information
petergmurphy
committed
Jan 16, 2025
1 parent
26c3ddf
commit 4b1a560
Showing
4 changed files
with
160 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
{ | ||
"description": "Unpins a node from a specified PE node group", | ||
"parameters": { | ||
"selected_node": { | ||
"type": "String", | ||
"description": "The certname of the node to unpin" | ||
}, | ||
"group": { | ||
"type": "String", | ||
"description": "The name of the node group to unpin the node from" | ||
} | ||
}, | ||
"input_method": "stdin", | ||
"implementations": [ | ||
{"name": "node_group_unpin.rb"} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
#!/opt/puppetlabs/puppet/bin/ruby | ||
Check failure on line 1 in tasks/node_group_unpin.rb GitHub Actions / Spec / Spec tests (Puppet: ~> 7.24, Ruby Ver: 2.7)
|
||
# frozen_string_literal: true | ||
|
||
require 'json' | ||
require 'yaml' | ||
require 'net/https' | ||
require 'puppet' | ||
|
||
# NodeGroupUnpin task class | ||
class NodeGroupUnpin | ||
def initialize(params) | ||
@params = params | ||
raise "Missing required parameter 'selected_node'" unless @params['selected_node'] | ||
raise "Missing required parameter 'group'" unless @params['group'] | ||
@auth = YAML.load_file('/etc/puppetlabs/puppet/classifier.yaml') | ||
rescue Errno::ENOENT | ||
raise 'Could not find classifier.yaml at /etc/puppetlabs/puppet/classifier.yaml' | ||
end | ||
|
||
def https_client | ||
client = Net::HTTP.new(Puppet.settings[:certname], 4433) | ||
client.use_ssl = true | ||
client.cert = @cert ||= OpenSSL::X509::Certificate.new(File.read(Puppet.settings[:hostcert])) | ||
client.key = @key ||= OpenSSL::PKey::RSA.new(File.read(Puppet.settings[:hostprivkey])) | ||
client.verify_mode = OpenSSL::SSL::VERIFY_PEER | ||
client.ca_file = Puppet.settings[:localcacert] | ||
client | ||
end | ||
|
||
def groups | ||
puts 'Debug: Fetching groups' | ||
@groups ||= begin | ||
net = https_client | ||
puts 'Debug: Making GET request to /classifier-api/v1/groups' | ||
res = net.get('/classifier-api/v1/groups') | ||
puts "Debug: Response code: #{res.code}" | ||
puts "Debug: Response body preview: #{res.body[0..100]}..." if res.body | ||
|
||
unless res.code == '200' | ||
raise "Failed to fetch groups: HTTP #{res.code} - #{res.body}" | ||
end | ||
|
||
NodeGroup.new(JSON.parse(res.body)) | ||
rescue JSON::ParserError => e | ||
puts "Debug: JSON parse error: #{e.message}" | ||
raise "Invalid JSON response from server: #{e.message}" | ||
rescue StandardError => e | ||
puts "Debug: Error in groups method: #{e.class} - #{e.message}" | ||
raise "Error fetching groups: #{e.message}" | ||
end | ||
end | ||
|
||
def pe_master_group | ||
groups.dig('PE Master') | ||
end | ||
|
||
def unpin_node(group, node) | ||
raise 'Invalid group object' unless group.is_a?(Hash) && group['id'] && group['name'] | ||
|
||
net = https_client | ||
begin | ||
data = { "nodes": [node] }.to_json | ||
url = "/classifier-api/v1/groups/#{group['id']}/unpin" | ||
|
||
puts "Debug: Making POST request to #{url}" | ||
puts "Debug: Request body: #{data}" | ||
|
||
req = Net::HTTP::Post.new(url) | ||
req['Content-Type'] = 'application/json' | ||
req.body = data | ||
|
||
res = net.request(req) | ||
puts "Debug: Response code: #{res.code}" | ||
puts "Debug: Response body: #{res.body}" if res.body | ||
|
||
case res.code | ||
when '204' | ||
puts "Successfully unpinned node '#{node}' from group '#{group['name']}'" | ||
else | ||
begin | ||
error_body = JSON.parse(res.body.to_s) | ||
raise "Failed to unpin node: #{error_body['kind'] || error_body}" | ||
rescue JSON::ParserError | ||
raise "Invalid response from server (status #{res.code}): #{res.body}" | ||
end | ||
end | ||
rescue StandardError => e | ||
raise "Error during unpin request: #{e.message}" | ||
end | ||
end | ||
|
||
# Utility class to aid in retrieving useful information from the node group | ||
# data | ||
class NodeGroup | ||
attr_reader :data | ||
|
||
def initialize(data) | ||
@data = data | ||
end | ||
|
||
# Aids in digging into node groups by name, rather than UUID | ||
def dig(name, *args) | ||
group = @data.find { |obj| obj['name'] == name } | ||
if group.nil? | ||
nil | ||
elsif args.empty? | ||
group | ||
else | ||
group.dig(*args) | ||
end | ||
end | ||
end | ||
|
||
def execute! | ||
group = pe_master_group | ||
if group | ||
unpin_node(group, @params['selected_node']) | ||
puts "Unpinned #{@params['selected_node']} from #{@params['group']}" | ||
else | ||
puts "Group #{@params['group']} not found" | ||
end | ||
end | ||
end | ||
|
||
# Run the task unless an environment flag has been set | ||
unless ENV['RSPEC_UNIT_TEST_MODE'] | ||
Puppet.initialize_settings | ||
task = NodeGroupUnpin.new(JSON.parse(STDIN.read)) | ||
task.execute! | ||
end |