This repository has been archived by the owner on Jul 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
mongo_backend.rb
68 lines (54 loc) · 1.75 KB
/
mongo_backend.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
require_relative '../../config/config'
require 'mongo'
require 'yaml'
module Automaton
class MongoBackend
include Mongo
def initialize
config = Automaton::Configure::config
# Connect to Mongo
hosts = config[:mdb_hosts].split(',')
host,port = hosts[0].split(':')
client = (config[:replicaset] == 'yes') ? MongoReplicaSetClient.new(hosts) : MongoClient.new(host, port)
database = client[config[:database]]
# Authenticate, if set
database.authenticate(config[:username], config[:password]) unless config[:username].to_s.empty?
# Set collection names
@node_collection = database[config[:nodecollection]]
@fact_collection = database[config[:factcollection]]
# Set and Ensure Indexes on node name
[ @node_collection, @fact_collection ].each do |collection|
collection.create_index('node', :unique => true)
collection.ensure_index('node', :unique => true)
end
end
def determine_collection(type)
case type
when 'node'
@node_collection
when 'fact'
@fact_collection
else
msg('warn',"#{type} is not supported")
end
end
def msg(severity, message)
Automaton::Log::msg(severity, message)
end
def find(name, type)
return determine_collection(type).find_one(:node => name)
end
def find_facts(name)
@fact_collection.find_one(:node => name)
end
def add(name, data, type)
return determine_collection(type).insert(data)
end
def update(name, data, type)
return determine_collection(type).update({ :_id => name["_id"] }, data)
end
def remove(name, type)
return determine_collection(type).remove(name)
end
end
end