|
| 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