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



130
131
132
133
# File 'lib/puppet/provider/mongodb.rb', line 130

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

.conn_stringObject



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

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'
                '127.0.0.1'
              when %r{\[?::0\]?}
                '::1'
              else
                first_ip_in_list
              end
  end

  port = config.fetch('port')
  cluster_role = config.fetch('clusterRole')
  port_real = if port
                port
              elsif cluster_role.eql?('configsvr')
                27_019
              elsif cluster_role.eql?('shardsvr')
                27_018
              else
                27_017
              end

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

.db_ismasterObject



119
120
121
122
123
124
# File 'lib/puppet/provider/mongodb.rb', line 119

def self.db_ismaster
  cmd_ismaster = 'db.isMaster().ismaster'
  db = 'admin'
  res = mongosh_cmd(db, conn_string, cmd_ismaster).to_s.split(%r{\n}).last.chomp
  res.eql?('true')
end

.ipv6_is_enabled(config = nil) ⇒ Object



41
42
43
44
# File 'lib/puppet/provider/mongodb.rb', line 41

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

.mongo_confObject



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

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),
    'tlsallowInvalidHostnames' => config['net.tls.allowInvalidHostnames'] || config.fetch('net', {}).fetch('tls', {}).fetch('allowInvalidHostnames', nil),
    'tls' => config['net.tls.mode'] || config.fetch('net', {}).fetch('tls', {}).fetch('mode', nil),
    'tlscert' => config['net.tls.certificateKeyFile'] || config.fetch('net', {}).fetch('tls', {}).fetch('certificateKeyFile', nil),
    'tlsca' => config['net.tls.CAFile'] || config.fetch('net', {}).fetch('tls', {}).fetch('CAFile', nil),
    'auth' => config['security.authorization'] || config.fetch('security', {}).fetch('authorization', nil),
    'clusterRole' => config['sharding.clusterRole'] || config.fetch('sharding', {}).fetch('clusterRole', nil),
  }
end

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

Mongo Command Wrapper



136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/puppet/provider/mongodb.rb', line 136

def self.mongo_eval(cmd, db = 'admin', host = nil)
  cmd = mongoshrc_file + cmd if mongoshrc_file

  out = nil
  begin
    out = mongosh_cmd(db, host, cmd)
  rescue StandardError => e
    raise Puppet::ExecutionFailure, "Could not evaluate MongoDB shell command: #{cmd}, with: #{e.message}"
  end

  Puppet::Util::MongodbOutput.sanitize(out)
end

.mongo_versionObject

Mongo Version checker



154
155
156
# File 'lib/puppet/provider/mongodb.rb', line 154

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

.mongod_conf_fileObject



22
23
24
# File 'lib/puppet/provider/mongodb.rb', line 22

def self.mongod_conf_file
  '/etc/mongod.conf'
end

.mongosh_cmd(db, host, cmd) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/puppet/provider/mongodb.rb', line 62

def self.mongosh_cmd(db, host, cmd)
  config = mongo_conf

  host = conn_string if host.nil? || host.split(':')[0] == Facter.value(:fqdn) || host == '127.0.0.1'

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

  if tls_is_enabled(config)
    args.push('--tls')
    args += ['--tlsCertificateKeyFile', config['tlscert']]

    tls_ca = config['tlsca']
    args += ['--tlsCAFile', tls_ca] unless tls_ca.nil?

    args.push('--tlsAllowInvalidHostnames') if tls_invalid_hostnames(config)
    args.push('--tlsAllowInvalidCertificates') if tls_invalid_certificates(config)
  end

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

.mongoshrc_fileObject

Optional defaults file



14
15
16
# File 'lib/puppet/provider/mongodb.rb', line 14

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

.tls_invalid_certificates(config = nil) ⇒ Object



57
58
59
60
# File 'lib/puppet/provider/mongodb.rb', line 57

def self.tls_invalid_certificates(config = nil)
  config ||= mongo_conf
  config['tlsallowInvalidCertificates']
end

.tls_invalid_hostnames(config = nil) ⇒ Object



52
53
54
55
# File 'lib/puppet/provider/mongodb.rb', line 52

def self.tls_invalid_hostnames(config = nil)
  config ||= mongo_conf
  config['tlsallowInvalidHostnames']
end

.tls_is_enabled(config = nil) ⇒ Object



46
47
48
49
50
# File 'lib/puppet/provider/mongodb.rb', line 46

def self.tls_is_enabled(config = nil)
  config ||= mongo_conf
  tls_mode = config.fetch('tls')
  !tls_mode.nil? && tls_mode != 'disabled'
end

Instance Method Details

#conn_stringObject



115
116
117
# File 'lib/puppet/provider/mongodb.rb', line 115

def conn_string
  self.class.conn_string
end

#db_ismasterObject



126
127
128
# File 'lib/puppet/provider/mongodb.rb', line 126

def db_ismaster
  self.class.db_ismaster
end

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



149
150
151
# File 'lib/puppet/provider/mongodb.rb', line 149

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

#mongo_versionObject



158
159
160
# File 'lib/puppet/provider/mongodb.rb', line 158

def mongo_version
  self.class.mongo_version
end

#mongoshrc_fileObject



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

def mongoshrc_file
  self.class.mongoshrc_file
end