-
-
Notifications
You must be signed in to change notification settings - Fork 448
/
Copy pathmongodb.rb
186 lines (156 loc) · 5.07 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', '..'))
require 'puppet/util/mongodb_output'
require 'yaml'
require 'json'
class Puppet::Provider::Mongodb < Puppet::Provider
# Without initvars commands won't work.
initvars
commands mongo: 'mongo'
# Optional defaults file
def self.mongorc_file
"load('#{Facter.value(:root_home)}/.mongorc.js'); " if File.file?("#{Facter.value(:root_home)}/.mongorc.js")
end
def mongorc_file
self.class.mongorc_file
end
def self.mongod_conf_file
if File.exist? '/etc/mongod.conf'
'/etc/mongod.conf'
else
'/etc/mongodb.conf'
end
end
def self.mongo_conf
config = YAML.load_file(mongod_conf_file) || {}
{
'bindip' => config['net.bindIp'] || config.fetch('net', {}).fetch('bindIp', nil),
'port' => config['net.port'] || config.fetch('net', {}).fetch('port', nil),
'ipv6' => config['net.ipv6'] || config.fetch('net', {}).fetch('ipv6', nil),
'allowInvalidHostnames' => config['net.ssl.allowInvalidHostnames'] || config.fetch('net', {}).fetch('ssl', {}).fetch('allowInvalidHostnames', nil),
'ssl' => config['net.ssl.mode'] || config.fetch('net', {}).fetch('ssl', {}).fetch('mode', nil),
'sslcert' => config['net.ssl.PEMKeyFile'] || config.fetch('net', {}).fetch('ssl', {}).fetch('PEMKeyFile', nil),
'sslca' => config['net.ssl.CAFile'] || config.fetch('net', {}).fetch('ssl', {}).fetch('CAFile', nil),
'auth' => config['security.authorization'] || config.fetch('security', {}).fetch('authorization', nil),
'shardsvr' => config['sharding.clusterRole'] || config.fetch('sharding', {}).fetch('clusterRole', nil),
'confsvr' => config['sharding.clusterRole'] || config.fetch('sharding', {}).fetch('clusterRole', nil)
}
end
def self.ipv6_is_enabled(config = nil)
config ||= mongo_conf
config['ipv6']
end
def self.ssl_is_enabled(config = nil)
config ||= mongo_conf
ssl_mode = config.fetch('ssl')
!ssl_mode.nil? && ssl_mode != 'disabled'
end
def self.ssl_invalid_hostnames(config = nil)
config ||= mongo_conf
config['allowInvalidHostnames']
end
def self.mongo_cmd(db, host, cmd)
config = mongo_conf
args = [db, '--quiet', '--host', host]
args.push('--ipv6') if ipv6_is_enabled(config)
args.push('--sslAllowInvalidHostnames') if ssl_invalid_hostnames(config)
if ssl_is_enabled(config)
args.push('--ssl')
args += ['--sslPEMKeyFile', config['sslcert']]
ssl_ca = config['sslca']
args += ['--sslCAFile', ssl_ca] unless ssl_ca.nil?
end
args += ['--eval', cmd]
mongo(args)
end
def self.conn_string
config = mongo_conf
bindip = config.fetch('bindip')
if bindip
first_ip_in_list = bindip.split(',').first
ip_real = case first_ip_in_list
when '0.0.0.0'
Facter.value(:fqdn)
when %r{\[?::0\]?}
Facter.value(:fqdn)
else
first_ip_in_list
end
end
port = config.fetch('port')
shardsvr = config.fetch('shardsvr')
confsvr = config.fetch('confsvr')
port_real = if port
port
elsif !port && (confsvr.eql?('configsvr') || confsvr.eql?('true'))
27_019
elsif !port && (shardsvr.eql?('shardsvr') || shardsvr.eql?('true'))
27_018
else
27_017
end
"#{ip_real}:#{port_real}"
end
def self.db_ismaster
cmd_ismaster = 'db.isMaster().ismaster'
cmd_ismaster = mongorc_file + cmd_ismaster if mongorc_file
db = 'admin'
res = mongo_cmd(db, conn_string, cmd_ismaster).to_s.chomp
res.eql?('true')
end
def db_ismaster
self.class.db_ismaster
end
def self.auth_enabled(config = nil)
config ||= mongo_conf
config['auth'] && config['auth'] != 'disabled'
end
# Mongo Command Wrapper
def self.mongo_eval(cmd, db = 'admin', retries = 10, host = nil)
retry_count = retries
retry_sleep = 3
cmd = mongorc_file + cmd if mongorc_file
out = nil
begin
out = if host
mongo_cmd(db, host, cmd)
else
mongo_cmd(db, conn_string, cmd)
end
rescue => e
retry_count -= 1
if retry_count > 0
Puppet.debug "Request failed: '#{e.message}' Retry: '#{retries - retry_count}'"
sleep retry_sleep
retry
end
end
unless out
raise Puppet::ExecutionFailure, "Could not evaluate MongoDB shell command: #{cmd}"
end
Puppet::Util::MongodbOutput.sanitize(out)
end
def mongo_eval(cmd, db = 'admin', retries = 10, host = nil)
self.class.mongo_eval(cmd, db, retries, host)
end
# Mongo Version checker
def self.mongo_version
@mongo_version ||= mongo_eval('db.version()')
end
def mongo_version
self.class.mongo_version
end
def self.mongo_26?
v = mongo_version
!v[%r{^2\.6\.}].nil?
end
def mongo_26?
self.class.mongo_26?
end
def self.mongo_4?
v = mongo_version
!v[%r{^4\.}].nil?
end
def mongo_4?
self.class.mongo_4?
end
end