Class: Puppet::Provider::Mongodb

Inherits:
Puppet::Provider
  • Object
show all
Defined in:
lib/puppet/provider/mongodb.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.auth_enabled(config = nil) ⇒ Object



122
123
124
125
# File 'lib/puppet/provider/mongodb.rb', line 122

def self.auth_enabled(config = nil)
  config ||= mongo_conf
  config['auth'] && config['auth'] != 'disabled'
end

.conn_stringObject



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
# File 'lib/puppet/provider/mongodb.rb', line 79

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

.db_ismasterObject



110
111
112
113
114
115
116
# File 'lib/puppet/provider/mongodb.rb', line 110

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.split(%r{\n}).last.chomp
  res.eql?('true')
end

.ipv6_is_enabled(config = nil) ⇒ Object



44
45
46
47
# File 'lib/puppet/provider/mongodb.rb', line 44

def self.ipv6_is_enabled(config = nil)
  config ||= mongo_conf
  config['ipv6']
end

.mongo_26?Boolean

Returns:

  • (Boolean)


169
170
171
172
# File 'lib/puppet/provider/mongodb.rb', line 169

def self.mongo_26?
  v = mongo_version
  !v[%r{^2\.6\.}].nil?
end

.mongo_4?Boolean

Returns:

  • (Boolean)


178
179
180
181
# File 'lib/puppet/provider/mongodb.rb', line 178

def self.mongo_4?
  v = mongo_version
  !v[%r{^4\.}].nil?
end

.mongo_5?Boolean

Returns:

  • (Boolean)


187
188
189
190
# File 'lib/puppet/provider/mongodb.rb', line 187

def self.mongo_5?
  v = mongo_version
  !v[%r{^5\.}].nil?
end

.mongo_cmd(db, host, cmd) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/puppet/provider/mongodb.rb', line 60

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

.mongo_confObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/puppet/provider/mongodb.rb', line 28

def self.mongo_conf
  config = YAML.load_file(mongod_conf_file) || {}
  {
    'bindip' => config['net.bindIp'],
    'port' => config['net.port'],
    'ipv6' => config['net.ipv6'],
    'allowInvalidHostnames' => config['net.ssl.allowInvalidHostnames'],
    'ssl' => config['net.ssl.mode'],
    'sslcert' => config['net.ssl.PEMKeyFile'],
    'sslca' => config['net.ssl.CAFile'],
    'auth' => config['security.authorization'],
    'shardsvr' => config['sharding.clusterRole'],
    'confsvr' => config['sharding.clusterRole']
  }
end

.mongo_eval(cmd, db = 'admin', retries = 10, host = nil) ⇒ Object

Mongo Command Wrapper



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
# File 'lib/puppet/provider/mongodb.rb', line 128

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

.mongo_versionObject

Mongo Version checker



161
162
163
# File 'lib/puppet/provider/mongodb.rb', line 161

def self.mongo_version
  @mongo_version ||= mongo_eval('db.version()')
end

.mongod_conf_fileObject



20
21
22
23
24
25
26
# File 'lib/puppet/provider/mongodb.rb', line 20

def self.mongod_conf_file
  if File.exist? '/etc/mongod.conf'
    '/etc/mongod.conf'
  else
    '/etc/mongodb.conf'
  end
end

.mongorc_fileObject

Optional defaults file



12
13
14
# File 'lib/puppet/provider/mongodb.rb', line 12

def self.mongorc_file
  "load('#{Facter.value(:root_home)}/.mongorc.js'); " if File.file?("#{Facter.value(:root_home)}/.mongorc.js")
end

.ssl_invalid_hostnames(config = nil) ⇒ Object



55
56
57
58
# File 'lib/puppet/provider/mongodb.rb', line 55

def self.ssl_invalid_hostnames(config = nil)
  config ||= mongo_conf
  config['allowInvalidHostnames']
end

.ssl_is_enabled(config = nil) ⇒ Object



49
50
51
52
53
# File 'lib/puppet/provider/mongodb.rb', line 49

def self.ssl_is_enabled(config = nil)
  config ||= mongo_conf
  ssl_mode = config.fetch('ssl')
  !ssl_mode.nil? && ssl_mode != 'disabled'
end

Instance Method Details

#db_ismasterObject



118
119
120
# File 'lib/puppet/provider/mongodb.rb', line 118

def db_ismaster
  self.class.db_ismaster
end

#mongo_26?Boolean

Returns:

  • (Boolean)


174
175
176
# File 'lib/puppet/provider/mongodb.rb', line 174

def mongo_26?
  self.class.mongo_26?
end

#mongo_4?Boolean

Returns:

  • (Boolean)


183
184
185
# File 'lib/puppet/provider/mongodb.rb', line 183

def mongo_4?
  self.class.mongo_4?
end

#mongo_5?Boolean

Returns:

  • (Boolean)


192
193
194
# File 'lib/puppet/provider/mongodb.rb', line 192

def mongo_5?
  self.class.mongo_5?
end

#mongo_eval(cmd, db = 'admin', retries = 10, host = nil) ⇒ Object



156
157
158
# File 'lib/puppet/provider/mongodb.rb', line 156

def mongo_eval(cmd, db = 'admin', retries = 10, host = nil)
  self.class.mongo_eval(cmd, db, retries, host)
end

#mongo_versionObject



165
166
167
# File 'lib/puppet/provider/mongodb.rb', line 165

def mongo_version
  self.class.mongo_version
end

#mongorc_fileObject



16
17
18
# File 'lib/puppet/provider/mongodb.rb', line 16

def mongorc_file
  self.class.mongorc_file
end