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



146
147
148
149
# File 'lib/puppet/provider/mongodb.rb', line 146

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

.db_ismasterObject



132
133
134
135
136
137
138
139
140
# File 'lib/puppet/provider/mongodb.rb', line 132

def self.db_ismaster
  cmd_ismaster = 'db.isMaster().ismaster'
  if mongorc_file
    cmd_ismaster = mongorc_file + cmd_ismaster
  end
  db = 'admin'
  res = mongo_cmd(db, get_conn_string, cmd_ismaster).to_s.chomp()
  res.eql?('true') ? true : false
end

.get_conn_stringObject



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

def self.get_conn_string
  config = get_mongo_conf
  bindip = config.fetch('bindip')
  if bindip
    first_ip_in_list = bindip.split(',').first
    case first_ip_in_list
    when '0.0.0.0'
      ip_real = '127.0.0.1'
    when /\[?::0\]?/
      ip_real = '::1'
    else
      ip_real = first_ip_in_list
    end
  end

  port = config.fetch('port')
  shardsvr = config.fetch('shardsvr')
  confsvr = config.fetch('confsvr')
  if port
    port_real = port
  elsif !port and (confsvr.eql? 'configsvr' or confsvr.eql? 'true')
    port_real = 27019
  elsif !port and (shardsvr.eql? 'shardsvr' or shardsvr.eql? 'true')
    port_real = 27018
  else
    port_real = 27017
  end

  "#{ip_real}:#{port_real}"
end

.get_mongo_confObject



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

def self.get_mongo_conf
  file = get_mongod_conf_file
  # The mongo conf is probably a key-value store, even though 2.6 is
  # supposed to use YAML, because the config template is applied
  # based on $::mongodb::globals::version which is the user will not
  # necessarily set. This attempts to get the port from both types of
  # config files.
  config = YAML.load_file(file)
  config_hash = Hash.new
  if config.kind_of?(Hash) # Using a valid YAML file for mongo 2.6
    config_hash['bindip'] = config['net.bindIp']
    config_hash['port'] = config['net.port']
    config_hash['ipv6'] = config['net.ipv6']
    config_hash['ssl'] = config['net.ssl.mode']
    config_hash['sslcert'] = config['net.ssl.PEMKeyFile']
    config_hash['sslca'] = config['net.ssl.CAFile']
    config_hash['auth'] = config['security.authorization']
    config_hash['shardsvr'] = config['sharding.clusterRole']
    config_hash['confsvr'] = config['sharding.clusterRole']
  else # It has to be a key-value config file
    config = {}
    File.readlines(file).collect do |line|
      k,v = line.split('=')
      config[k.rstrip] = v.lstrip.chomp if k and v
    end
    config_hash['bindip'] = config['bind_ip']
    config_hash['port'] = config['port']
    config_hash['ipv6'] = config['ipv6']
    config_hash['ssl'] = config['sslOnNormalPorts']
    config_hash['sslcert'] = config['sslPEMKeyFile']
    config_hash['sslca'] = config['sslCAFile']
    config_hash['auth'] = config['auth']
    config_hash['shardsvr'] = config['shardsvr']
    config_hash['confsvr'] = config['confsvr']
  end

  config_hash
end

.get_mongod_conf_fileObject



22
23
24
25
26
27
28
29
# File 'lib/puppet/provider/mongodb.rb', line 22

def self.get_mongod_conf_file
  if File.exists? '/etc/mongod.conf'
    file = '/etc/mongod.conf'
  else
    file = '/etc/mongodb.conf'
  end
  file
end

.ipv6_is_enabled(config = nil) ⇒ Object



70
71
72
73
# File 'lib/puppet/provider/mongodb.rb', line 70

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

.mongo_24?Boolean

Returns:

  • (Boolean)


199
200
201
202
# File 'lib/puppet/provider/mongodb.rb', line 199

def self.mongo_24?
  v = self.mongo_version
  ! v[/^2\.4\./].nil?
end

.mongo_cmd(db, host, cmd) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/puppet/provider/mongodb.rb', line 81

def self.mongo_cmd(db, host, cmd)
  config = get_mongo_conf

  args = [db, '--quiet', '--host', host]
  args.push('--ipv6') if ipv6_is_enabled(config)

  if ssl_is_enabled(config)
    args.push('--ssl')
    args += ['--sslPEMKeyFile', config['sslcert']]

    ssl_ca = config['sslca']
    unless ssl_ca.nil?
      args += ['--sslCAFile', ssl_ca]
    end
  end

  args += ['--eval', cmd]
  mongo(args)
end

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

Mongo Command Wrapper



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

def self.mongo_eval(cmd, db = 'admin', retries = 10, host = nil)
  retry_count = retries
  retry_sleep = 3
  if mongorc_file
    cmd = mongorc_file + cmd
  end

  out = nil
  retry_count.times do |n|
    begin
      if host
        out = mongo_cmd(db, host, cmd)
      else
        out = mongo_cmd(db, get_conn_string, cmd)
      end
    rescue => e
      Puppet.debug "Request failed: '#{e.message}' Retry: '#{n}'"
      sleep retry_sleep
      next
    end
    break
  end

  if !out
    raise Puppet::ExecutionFailure, "Could not evaluate MongoDB shell command: #{cmd}"
  end

  ['ObjectId','NumberLong'].each do |data_type|
    out.gsub!(/#{data_type}\(([^)]*)\)/, '\1')
  end
  out.gsub!(/^Error\:.+/, '')
  out
end

.mongo_versionObject

Mongo Version checker



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

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

.mongorc_fileObject

Optional defaults file



10
11
12
13
14
15
16
# File 'lib/puppet/provider/mongodb.rb', line 10

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

.ssl_is_enabled(config = nil) ⇒ Object



75
76
77
78
79
# File 'lib/puppet/provider/mongodb.rb', line 75

def self.ssl_is_enabled(config=nil)
  config ||= get_mongo_conf
  ssl_mode = config.fetch('ssl')
  ssl_mode.nil? ? false : ssl_mode != 'disabled'
end

Instance Method Details

#db_ismasterObject



142
143
144
# File 'lib/puppet/provider/mongodb.rb', line 142

def db_ismaster
  self.class.db_ismaster
end

#mongo_24?Boolean

Returns:

  • (Boolean)


204
205
206
# File 'lib/puppet/provider/mongodb.rb', line 204

def mongo_24?
  self.class.mongo_24?
end

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



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

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

#mongo_versionObject



195
196
197
# File 'lib/puppet/provider/mongodb.rb', line 195

def mongo_version
  self.class.mongo_version
end

#mongorc_fileObject



18
19
20
# File 'lib/puppet/provider/mongodb.rb', line 18

def mongorc_file
  self.class.mongorc_file
end