-
-
Notifications
You must be signed in to change notification settings - Fork 448
/
Copy pathmongodb.rb
47 lines (39 loc) · 1.25 KB
/
mongodb.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
require File.expand_path(File.join(File.dirname(__FILE__), '..', 'mongodb'))
Puppet::Type.type(:mongodb_database).provide(:mongodb, parent: Puppet::Provider::Mongodb) do
desc 'Manages MongoDB database.'
defaultfor kernel: 'Linux'
def self.instances
require 'json'
pre_cmd = 'try { rs.secondaryOk() } catch (err) { rs.slaveOk() }'
dbs = JSON.parse mongo_eval(pre_cmd + ';printjson(db.getMongo().getDBs())')
dbs['databases'].map do |db|
new(name: db['name'],
ensure: :present)
end
end
# Assign prefetched dbs based on name.
def self.prefetch(resources)
dbs = instances
resources.keys.each do |name|
provider = dbs.find { |db| db.name == name }
resources[name].provider = provider if provider
end
end
def create
if db_ismaster
mongo_eval('db.dummyData.insert({"created_by_puppet": 1})', @resource[:name])
else
Puppet.warning 'Database creation is available only from master host'
end
end
def destroy
if db_ismaster
mongo_eval('db.dropDatabase()', @resource[:name])
else
Puppet.warning 'Database removal is available only from master host'
end
end
def exists?
!(@property_hash[:ensure] == :absent || @property_hash[:ensure].nil?)
end
end