class PuppetDBQuery::PuppetDB
access puppetdb data
Constants
- FACTS
- NODES
Public Class Methods
new(host = HOST, port = 443, protocol = "https", nodes = NODES, facts = FACTS)
click to toggle source
# File lib/puppetdb_query/puppetdb.rb, line 14 def initialize(host = HOST, port = 443, protocol = "https", nodes = NODES, facts = FACTS) @nodes_url = "#{protocol}://#{host}:#{port}#{nodes}" @facts_url = "#{protocol}://#{host}:#{port}#{facts}" @lock = Mutex.new end
Public Instance Methods
all_nodes()
click to toggle source
get array of node names
# File lib/puppetdb_query/puppetdb.rb, line 21 def all_nodes api_nodes.reject { |data| data['deactivated'] }.map { |data| data['certname'] } end
api_nodes()
click to toggle source
# File lib/puppetdb_query/puppetdb.rb, line 67 def api_nodes get_json(@nodes_url, 10) end
facts()
click to toggle source
get all nodes with all facts
# File lib/puppetdb_query/puppetdb.rb, line 56 def facts json = get_json(@facts_url, 60) result = {} json.each do |fact| data = result[fact["certname"]] result[fact["certname"]] = data = {} unless data data[fact["name"]] = fact["value"] end result end
node_properties()
click to toggle source
get hash of node update properties
# File lib/puppetdb_query/puppetdb.rb, line 26 def node_properties result = {} api_nodes.each do |data| next if data['deactivated'] # in '/v3/nodes' we must take 'name' name = data['certname'] values = data.dup %w[deactivated certname].each { |key| values.delete(key) } result[name] = values end result end
nodes_update_facts_since(timestamp)
click to toggle source
get all nodes that have updated facts
# File lib/puppetdb_query/puppetdb.rb, line 40 def nodes_update_facts_since(timestamp) ts = (timestamp.is_a?(String) ? Time.iso8601(ts) : timestamp) node_properties.delete_if do |_k, data| # in '/v3/nodes' we must take 'facts-timestamp' !data["facts_timestamp"] || Time.iso8601(data["facts_timestamp"]) < ts end.keys end
single_node_facts(node)
click to toggle source
get hash of facts for given node name
# File lib/puppetdb_query/puppetdb.rb, line 49 def single_node_facts(node) json = get_json("#{@nodes_url}/#{node}/facts", 10) return nil if json.include?("error") Hash[json.map { |data| [data["name"], data["value"]] }] end
Private Instance Methods
get_json(url, timeout)
click to toggle source
# File lib/puppetdb_query/puppetdb.rb, line 73 def get_json(url, timeout) @lock.synchronize do logger.info " get json from #{url}" uri = URI.parse(url) http = Net::HTTP.new(uri.host, uri.port) http.use_ssl = uri.scheme == 'https' http.read_timeout = timeout request = Net::HTTP::Get.new(uri.request_uri) request['Accept'] = "application/json" response = http.request(request) logger.info " got #{response.body.size} characters from #{url}" JSON.parse(response.body) end end