Sha256: 442922b02bf6c5a2a7a77cf6cebe325642a8a0912fb4b0b7448d41e20d6f0713

Contents?: true

Size: 1.59 KB

Versions: 2

Compression:

Stored size: 1.59 KB

Contents

require 'puppetdb'
require 'puppet'

class PuppetGhostbuster
  class PuppetDB
    Puppet.initialize_settings

    begin
      require 'puppet/util/puppetdb'
      @@puppetdb = Puppet::Util::Puppetdb.config.server_urls[0]
    rescue LoadError
      @@puppetdb = "https://#{Puppet[:server]}:8081"
    end

    def self.client
      @@client ||= begin
        options = {
          server: ENV['PUPPETDB_URL'] || @@puppetdb,
        }

        if ENV['PE_TOKEN']
          token_file = File.expand_path(ENV['PE_TOKEN'])
          options[:token] = File.exist?(token_file) ? File.read(token_file) : ENV.fetch('PE_TOKEN')
          options[:cacert] = ENV['PUPPETDB_CACERT_FILE'] || Puppet[:localcacert]
        else
          options[:pem] = {
            'key' => ENV['PUPPETDB_KEY_FILE'] || Puppet[:hostprivkey],
            'cert' => ENV['PUPPETDB_CERT_FILE'] || Puppet[:hostcert],
            'ca_file' => ENV['PUPPETDB_CACERT_FILE'] || Puppet[:localcacert],
          }
        end

        ::PuppetDB::Client.new(options, 4)
      end
    end

    def client
      self.class.client
    end

    def self.classes
      @@classes ||= client.request('',
                                   'resources[title] { type = "Class" and nodes { deactivated is null } group by title }').data.map do |r|
        r['title']
      end
    end

    def classes
      self.class.classes
    end

    def self.resources
      @@resources ||= client.request('', 'resources[type] { nodes { deactivated is null } group by type }').data.map do |r|
        r['type']
      end
    end

    def resources
      self.class.resources
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
puppet-ghostbuster-1.2.1 lib/puppet-ghostbuster/puppetdb.rb
puppet-ghostbuster-1.2.0 lib/puppet-ghostbuster/puppetdb.rb