|
| 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 | +# CheckLegacyCompilers task class |
| 10 | +class CheckLegacyCompilers |
| 11 | + def initialize(params) |
| 12 | + @nodes = params['legacy_compilers'].split(',') if params['legacy_compilers'].is_a?(String) |
| 13 | + end |
| 14 | + |
| 15 | + def execute! |
| 16 | + pinned_nodes = [] |
| 17 | + @nodes.each do |node| |
| 18 | + node_classification = get_node_classification(node) |
| 19 | + pinned = false |
| 20 | + node_classification['groups'].each do |group| |
| 21 | + if group['name'] == 'PE Master' |
| 22 | + pinned_nodes << node |
| 23 | + pinned = true |
| 24 | + end |
| 25 | + end |
| 26 | + next if pinned |
| 27 | + next unless node_classification.key?('parameters') |
| 28 | + next unless node_classification['parameters'].key?('pe_master') |
| 29 | + if node_classification['parameters']['pe_master'] |
| 30 | + pinned_nodes << node |
| 31 | + end |
| 32 | + end |
| 33 | + |
| 34 | + return unless !pinned_nodes.empty? |
| 35 | + puts 'The following legacy compilers are classified as Puppet primary:' |
| 36 | + puts pinned_nodes.join(', ') |
| 37 | + puts 'You will not be able to upgrade if you dont remediate this.' |
| 38 | + end |
| 39 | + |
| 40 | + def https(port) |
| 41 | + https = Net::HTTP.new('localhost', port) |
| 42 | + https.use_ssl = true |
| 43 | + https.cert = @cert ||= OpenSSL::X509::Certificate.new(File.read(Puppet.settings[:hostcert])) |
| 44 | + https.key = @key ||= OpenSSL::PKey::RSA.new(File.read(Puppet.settings[:hostprivkey])) |
| 45 | + https.verify_mode = OpenSSL::SSL::VERIFY_NONE |
| 46 | + https |
| 47 | + end |
| 48 | + |
| 49 | + def get_node_classification(certname) |
| 50 | + pdb = https(4433) |
| 51 | + pdb_request = Net::HTTP::Post.new('/classifier-api/v2/classified/nodes/' + certname) |
| 52 | + pdb_request['Content-Type'] = 'application/json' |
| 53 | + |
| 54 | + response = JSON.parse(pdb.request(pdb_request).body) |
| 55 | + |
| 56 | + response |
| 57 | + end |
| 58 | +end |
| 59 | + |
| 60 | +# Run the task unless an environment flag has been set, signaling not to. The |
| 61 | +# environment flag is used to disable auto-execution and enable Ruby unit |
| 62 | +# testing of this task. |
| 63 | +unless ENV['RSPEC_UNIT_TEST_MODE'] |
| 64 | + Puppet.initialize_settings |
| 65 | + task = CheckLegacyCompilers.new(JSON.parse(STDIN.read)) |
| 66 | + task.execute! |
| 67 | +end |
0 commit comments