Skip to content
This repository has been archived by the owner on Nov 28, 2018. It is now read-only.

Adding support for a remote database #62

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions agent/registration-mongodb/agent/registration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,19 @@ def initialize
@config = Config.instance

@mongohost = @config.pluginconf["registration.mongohost"] || "localhost"
@mongoport = @config.pluginconf["registration.mongoport"] || "27017"
@mongodb = @config.pluginconf["registration.mongodb"] || "puppet"
@mongouser = @config.pluginconf["registration.mongouser"]
@mongopass = @config.pluginconf["registration.mongopass"]
@collection = @config.pluginconf["registration.collection"] || "nodes"
@yaml_dir = @config.pluginconf["registration.extra_yaml_dir"] || false

Log.instance.debug("Connecting to mongodb @ #{@mongohost} db #{@mongodb} collection #{@collection}")
Log.instance.debug("Connecting to mongodb @ #{@mongohost}:#{@mongoport} db #{@mongodb} collection #{@collection}")

@dbh = Mongo::Connection.new(@mongohost).db(@mongodb)
@dbh = Mongo::Connection.new(@mongohost,@mongoport).db(@mongodb)
unless @mongouser.empty?
@auth = @dbh.authenticate(@mongouser, @mongopass)
end
@coll = @dbh.collection(@collection)

@coll.create_index("fqdn", {:unique => true, :dropDups => true})
Expand Down Expand Up @@ -86,7 +92,11 @@ def handlemsg(msg, connection)
before = Time.now.to_f
begin
doc = @coll.find_and_modify(:query => by_fqdn, :update => {'$set' => req}, :new => true)
doc_id = doc['_id']
if doc
doc_id = doc['_id']
else
doc_id = @coll.insert(req, {:safe => true})
end
rescue Mongo::OperationFailure
doc_id = @coll.insert(req, {:safe => true})
ensure
Expand Down
8 changes: 7 additions & 1 deletion agent/registration-mongodb/discovery/mongo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,17 @@ def discover(filter, timeout, limit=0, client=nil)
config = Config.instance

mongohost = config.pluginconf["registration.mongohost"] || "localhost"
mongoport = config.pluginconf["registration.mongoport"] || "27017"
mongodb = config.pluginconf["registration.mongodb"] || "puppet"
mongouser = config.pluginconf["registration.mongouser"]
mongopass = config.pluginconf["registration.mongopass"]
collection = config.pluginconf["registration.collection"] || "nodes"
newerthan = Time.now.to_i - Integer(config.pluginconf["registration.criticalage"] || 3600)

dbh = ::Mongo::Connection.new(mongohost).db(mongodb)
dbh = ::Mongo::Connection.new(mongohost,mongoport).db(mongodb)
unless mongouser.empty?
auth = dbh.authenticate(mongouser, mongopass)
end
coll = dbh.collection(collection)

found = []
Expand Down