puppetdb_query 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c33cfa1bbb82f1324f6716de9d17ed70308b35d2
4
- data.tar.gz: 51b845298b9ded7a278ccf96481c96f2b287c936
3
+ metadata.gz: fc5ffd8e14e1d52cd2ba1709657cacfc0e4f4587
4
+ data.tar.gz: 971dc5b43bd77ddf56aa29147e9a1c39d9fae961
5
5
  SHA512:
6
- metadata.gz: 5f6c17658656b5452215c8f297bcd76ab4a48c1305e04b23e36e4fb9f1a504c5d0a9abe8256767b8295e8fb211a7d71c4ac5ccd35c8ae1b9acadb93c3f44cb35
7
- data.tar.gz: 3eaeae64fda4338cf502e9bd2b4a304d24e6f975ff9c5310e74c0b91d6cc989d4fd5fee062f9a4df4ea6aa34a6bc2457a4b01b4c7a449121e8ba271d7eac2c3a
6
+ metadata.gz: e44f81a09dbccdcc4d52df43410b7eba1417462bc677fb2e0c4710f504556acbcd215f4d4c9aac5d3f89ef81966c0d2b00ba5a270e17fce2e0b8acc5997ee481
7
+ data.tar.gz: 4c9435bb7fd94357c68ab8d1b7728a1e8a32da7f09f8dc8b3733b2450a391464aac53574514afef0a34cd25dfb22d2e888d9cc7c0987b08515d532a485cdb3fe
data/Gemfile CHANGED
@@ -4,6 +4,7 @@ gemspec
4
4
 
5
5
  # only for local testing but not needed for spec tests
6
6
  group :test do
7
+ gem 'mongo', '>=2.2.0' # if you want to work with a mongodb
7
8
  gem 'ruby-puppetdb', '=1.5.3' if RUBY_VERSION !~ /^1\./
8
9
  gem 'puppet', '=3.8.7' if RUBY_VERSION !~ /^1\./
9
10
  gem "rubocop", '=0.39.0' if RUBY_VERSION =~ /^1\./
data/README.md CHANGED
@@ -13,29 +13,62 @@ You must simply establish a sync job to read the data from your puppetdb and wri
13
13
  Currently the implementation supports only puppetdb api V 4.
14
14
 
15
15
  ## Installation
16
+ The example implementation uses a mongodb (version >= 3.2).
16
17
 
17
18
  Add this line to your application's Gemfile:
18
19
 
19
20
  ```ruby
20
21
  gem 'puppetdb_query'
22
+ gem 'mongo', '>=2.2.0'
21
23
  ```
22
24
 
23
- And then execute:
25
+ and then execute:
24
26
 
25
27
  $ bundle
26
28
 
27
29
  Or install it yourself as:
28
30
 
29
- $ gem install puppetdb_query
31
+ $ gem install puppetdb_query
32
+ $ gem install mongo
30
33
 
31
- ## Usage within program
34
+ ## Usage
35
+
36
+ First you have to sync your puppetdb data with your mongodb.
37
+ You can accomplish this by calling the following code every 5 minutes.
38
+
39
+ ```ruby
40
+ #!/bin/ruby
41
+
42
+ require 'mongo'
43
+ require 'puppetdb_query'
44
+
45
+ include PuppetDBQuery::Logging
46
+ logger.level = Logger::INFO
47
+ ::Mongo::Logger.logger = logger
48
+
49
+ begin
50
+ MONGO_HOSTS = ['puppetdb-mongo.example.com:27017']
51
+ MONGO_OPTIONS = { database: 'puppetdb', user: 'ops', password: 'very secret' }
52
+ connection = ::Mongo::Client.new(MONGO_HOSTS, MONGO_OPTIONS)
53
+ mongodb = PuppetDBQuery::MongoDB.new(connection)
54
+ puppetdb = PuppetDBQuery::PuppetDB.new('puppetdb-querynodes.example.com')
55
+
56
+ sync = PuppetDBQuery::Sync.new(puppetdb, mongodb)
57
+ sync.sync(5, 10)
58
+ rescue
59
+ logger.error $!
60
+ end
61
+ ```
62
+
63
+
64
+ Now you can query nodes and facts like this:
32
65
 
33
66
  ```ruby
34
67
  require "mongo"
35
68
  require "puppetdb_query"
36
69
  require "pp"
37
70
 
38
- MONGO_HOSTS = ['mongo.myhost.org:27017']
71
+ MONGO_HOSTS = ['puppetdb-mongo.example.com:27017']
39
72
  MONGO_OPTIONS = { database: 'puppetdb', user: 'ops', password: 'very secret' }
40
73
 
41
74
  pm = PuppetDBQuery::MongoQuery.new(MONGO_HOSTS, MONGO_OPTIONS)
@@ -51,10 +51,21 @@ module PuppetDBQuery
51
51
  collection.find.batch_size(999).projection(_id: 1).map { |k| k[:_id] }
52
52
  end
53
53
 
54
+ # get all nodes and their update dates
55
+ def node_properties
56
+ collection = connection[node_properties_collection]
57
+ result = {}
58
+ collection.find.batch_size(999).each do |values|
59
+ id = values.delete('_id')
60
+ result[id] = values
61
+ end
62
+ result
63
+ end
64
+
54
65
  # get facts for given node name
55
66
  def node_facts(node)
56
67
  collection = connection[nodes_collection]
57
- result = collection.find(_id: node).limit(999).batch_size(999).to_a.first
68
+ result = collection.find(_id: node).limit(1).batch_size(1).to_a.first
58
69
  result.delete("_id") if result
59
70
  result
60
71
  end
@@ -70,6 +81,14 @@ module PuppetDBQuery
70
81
  result
71
82
  end
72
83
 
84
+ # get meta informations about updates
85
+ def meta
86
+ collection = connection[meta_collection]
87
+ result = collection.find.first
88
+ result.delete(:_id)
89
+ result
90
+ end
91
+
73
92
  # update or insert facts for given node name
74
93
  def node_update(node, facts)
75
94
  connection[nodes_collection].find(_id: node).replace_one(facts, upsert: true)
@@ -93,7 +112,17 @@ module PuppetDBQuery
93
112
  collection = connection[node_properties_collection]
94
113
  old_names = collection.find.batch_size(999).projection(_id: 1).map { |k| k[:_id] }
95
114
  delete = old_names - new_node_properties.keys
96
- collection.insert_many(new_node_properties.map { |k, v| v.dup.tap { v[:_id] = k } })
115
+ data = new_node_properties.map do |k, v|
116
+ {
117
+ replace_one:
118
+ {
119
+ filter: { _id: k },
120
+ replacement: v,
121
+ upsert: true
122
+ }
123
+ }
124
+ end
125
+ collection.bulk_write(data)
97
126
  collection.delete_many(_id: { '$in' => delete })
98
127
  end
99
128
 
@@ -16,6 +16,7 @@ module PuppetDBQuery
16
16
 
17
17
  def sync(minutes = 5, seconds = 10)
18
18
  logger.info "syncing puppetdb nodes and facts started"
19
+
19
20
  Timeout.timeout(60 * minutes - seconds) do
20
21
  updater = PuppetDBQuery::Updater.new(source, destination)
21
22
 
@@ -1,3 +1,3 @@
1
1
  module PuppetDBQuery
2
- VERSION = "0.0.4".freeze
2
+ VERSION = "0.0.5".freeze
3
3
  end
@@ -20,7 +20,6 @@ Gem::Specification.new do |spec|
20
20
  spec.require_paths = ["lib"]
21
21
  spec.post_install_message = " Sitting quietly, doing nothing, spring comes, and grass grows by itself."
22
22
 
23
- spec.add_dependency "mongo"
24
23
  spec.add_development_dependency "rake"
25
24
  spec.add_development_dependency "rspec"
26
25
  spec.add_development_dependency "rubocop" #, "0.43.0" # matches with .rubocop.yml
metadata CHANGED
@@ -1,29 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: puppetdb_query
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michael Meyling
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-10-02 00:00:00.000000000 Z
11
+ date: 2016-10-03 00:00:00.000000000 Z
12
12
  dependencies:
13
- - !ruby/object:Gem::Dependency
14
- name: mongo
15
- requirement: !ruby/object:Gem::Requirement
16
- requirements:
17
- - - ">="
18
- - !ruby/object:Gem::Version
19
- version: '0'
20
- type: :runtime
21
- prerelease: false
22
- version_requirements: !ruby/object:Gem::Requirement
23
- requirements:
24
- - - ">="
25
- - !ruby/object:Gem::Version
26
- version: '0'
27
13
  - !ruby/object:Gem::Dependency
28
14
  name: rake
29
15
  requirement: !ruby/object:Gem::Requirement