Class: PuppetDBQuery::MongoDB
- Inherits:
-
Object
- Object
- PuppetDBQuery::MongoDB
- Includes:
- Logging
- Defined in:
- lib/puppetdb_query/mongodb.rb
Overview
access nodes and their facts from mongo database
Instance Attribute Summary collapse
-
#connection ⇒ Object
readonly
Returns the value of attribute connection.
-
#meta_collection ⇒ Object
readonly
Returns the value of attribute meta_collection.
-
#node_properties_collection ⇒ Object
readonly
Returns the value of attribute node_properties_collection.
-
#nodes_collection ⇒ Object
readonly
Returns the value of attribute nodes_collection.
Instance Method Summary collapse
-
#facts ⇒ Object
get all nodes and their facts.
-
#initialize(connection, nodes = :nodes, node_properties = :node_properties, meta = :meta) ⇒ MongoDB
constructor
initialize access to mongodb.
-
#meta ⇒ Object
get meta informations about updates.
-
#meta_fact_update(method, ts_begin, ts_end) ⇒ Object
update or insert timestamps for given fact update method.
-
#meta_node_properties_update(ts_begin, ts_end) ⇒ Object
update or insert timestamps for node_properties_update.
-
#node_delete(node) ⇒ Object
delete node data for given node name.
-
#node_facts(node) ⇒ Object
get facts for given node name.
-
#node_properties ⇒ Object
get all nodes and their update dates.
-
#node_properties_update(new_node_properties) ⇒ Object
update node properties.
-
#node_update(node, facts) ⇒ Object
update or insert facts for given node name.
-
#nodes ⇒ Object
get all node names.
-
#query_facts(query, facts) ⇒ Object
get nodes and their facts that fulfill given mongodb query.
-
#query_nodes(query) ⇒ Object
get node names that fulfill given mongodb query.
Methods included from Logging
Constructor Details
#initialize(connection, nodes = :nodes, node_properties = :node_properties, meta = :meta) ⇒ MongoDB
initialize access to mongodb
You might want to adjust the logging level, for example:
::Mongo::Logger.logger.level = logger.level
23 24 25 26 27 28 |
# File 'lib/puppetdb_query/mongodb.rb', line 23 def initialize(connection, nodes = :nodes, node_properties = :node_properties, = :meta) @connection = connection @nodes_collection = nodes @node_properties_collection = node_properties = end |
Instance Attribute Details
#connection ⇒ Object (readonly)
Returns the value of attribute connection.
9 10 11 |
# File 'lib/puppetdb_query/mongodb.rb', line 9 def connection @connection end |
#meta_collection ⇒ Object (readonly)
Returns the value of attribute meta_collection.
12 13 14 |
# File 'lib/puppetdb_query/mongodb.rb', line 12 def end |
#node_properties_collection ⇒ Object (readonly)
Returns the value of attribute node_properties_collection.
11 12 13 |
# File 'lib/puppetdb_query/mongodb.rb', line 11 def node_properties_collection @node_properties_collection end |
#nodes_collection ⇒ Object (readonly)
Returns the value of attribute nodes_collection.
10 11 12 |
# File 'lib/puppetdb_query/mongodb.rb', line 10 def nodes_collection @nodes_collection end |
Instance Method Details
#facts ⇒ Object
get all nodes and their facts
74 75 76 77 78 79 80 81 82 |
# File 'lib/puppetdb_query/mongodb.rb', line 74 def facts collection = connection[nodes_collection] result = {} collection.find.batch_size(999).each do |values| id = values.delete('_id') result[id] = values end result end |
#meta ⇒ Object
get meta informations about updates
85 86 87 88 89 90 |
# File 'lib/puppetdb_query/mongodb.rb', line 85 def collection = connection[] result = collection.find.first result.delete(:_id) result end |
#meta_fact_update(method, ts_begin, ts_end) ⇒ Object
update or insert timestamps for given fact update method
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/puppetdb_query/mongodb.rb', line 130 def (method, ts_begin, ts_end) connection[].find_one_and_update( {}, { '$set' => { last_fact_update: { ts_begin: ts_begin, ts_end: ts_end, method: method }, method => { ts_begin: ts_begin, ts_end: ts_end } } }, { upsert: true } ) end |
#meta_node_properties_update(ts_begin, ts_end) ⇒ Object
update or insert timestamps for node_properties_update
151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/puppetdb_query/mongodb.rb', line 151 def (ts_begin, ts_end) connection[].find_one_and_update( {}, { '$set' => { last_node_properties_update: { ts_begin: ts_begin, ts_end: ts_end } } }, { upsert: true } ) end |
#node_delete(node) ⇒ Object
delete node data for given node name
106 107 108 |
# File 'lib/puppetdb_query/mongodb.rb', line 106 def node_delete(node) connection[nodes_collection].find(_id: node).delete_one end |
#node_facts(node) ⇒ Object
get facts for given node name
66 67 68 69 70 71 |
# File 'lib/puppetdb_query/mongodb.rb', line 66 def node_facts(node) collection = connection[nodes_collection] result = collection.find(_id: node).limit(1).batch_size(1).to_a.first result.delete("_id") if result result end |
#node_properties ⇒ Object
get all nodes and their update dates
55 56 57 58 59 60 61 62 63 |
# File 'lib/puppetdb_query/mongodb.rb', line 55 def node_properties collection = connection[node_properties_collection] result = {} collection.find.batch_size(999).each do |values| id = values.delete('_id') result[id] = values end result end |
#node_properties_update(new_node_properties) ⇒ Object
update node properties
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/puppetdb_query/mongodb.rb', line 111 def node_properties_update(new_node_properties) collection = connection[node_properties_collection] old_names = collection.find.batch_size(999).projection(_id: 1).map { |k| k[:_id] } delete = old_names - new_node_properties.keys data = new_node_properties.map do |k, v| { replace_one: { filter: { _id: k }, replacement: v, upsert: true } } end collection.bulk_write(data) collection.delete_many(_id: { '$in' => delete }) end |
#node_update(node, facts) ⇒ Object
update or insert facts for given node name
93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/puppetdb_query/mongodb.rb', line 93 def node_update(node, facts) connection[nodes_collection].find(_id: node).replace_one(facts, upsert: true) rescue ::Mongo::Error::OperationFailure => e # mongodb doesn't support keys with a dot # see https://docs.mongodb.com/manual/reference/limits/#Restrictions-on-Field-Names # as a dirty workaround we delete the document and insert it ;-) # The dotted field .. in .. is not valid for storage. (57) raise e unless e. =~ /The dotted field / connection[nodes_collection].find(_id: node).delete_one connection[nodes_collection].insert_one(facts.merge(_id: node)) end |
#nodes ⇒ Object
get all node names
49 50 51 52 |
# File 'lib/puppetdb_query/mongodb.rb', line 49 def nodes collection = connection[nodes_collection] collection.find.batch_size(999).projection(_id: 1).map { |k| k[:_id] } end |
#query_facts(query, facts) ⇒ Object
get nodes and their facts that fulfill given mongodb query
37 38 39 40 41 42 43 44 45 46 |
# File 'lib/puppetdb_query/mongodb.rb', line 37 def query_facts(query, facts) fields = Hash[facts.collect { |fact| [fact.to_sym, 1] }] collection = connection[nodes_collection] result = {} collection.find(query).batch_size(999).projection(fields).each do |values| id = values.delete('_id') result[id] = values end result end |
#query_nodes(query) ⇒ Object
get node names that fulfill given mongodb query
31 32 33 34 |
# File 'lib/puppetdb_query/mongodb.rb', line 31 def query_nodes(query) collection = connection[nodes_collection] collection.find(query).batch_size(999).projection(_id: 1).map { |k| k[:_id] } end |