From a77d1a191419a3f1f468707aefa69abd689c3e4b Mon Sep 17 00:00:00 2001 From: Emilien Macchi Date: Sat, 16 Jan 2016 08:29:11 -0500 Subject: [PATCH] (MODULES-2983) Enable IPv6 in mongodb provider MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This patch allows to run mongodb providers on ipv6 networks. * When ipv6 is enabled in MongoDB configuration, add --ipv6 when running MongoDB Client [1]. * When bindip is '::0', ip_real will be '[::1]' to be consistent with ipv4 logic. [1] https://docs.mongodb.org/manual/reference/program/mongo/#cmdoption--ipv6 Co-Authored-By: Javier Peña Co-Authored-By: Sofer Athlan Guyot --- lib/puppet/provider/mongodb.rb | 39 ++++++++++++++++++++++++++++++---- 1 file changed, 35 insertions(+), 4 deletions(-) diff --git a/lib/puppet/provider/mongodb.rb b/lib/puppet/provider/mongodb.rb index 6bbc350b6..bdde83d04 100644 --- a/lib/puppet/provider/mongodb.rb +++ b/lib/puppet/provider/mongodb.rb @@ -28,6 +28,22 @@ def self.get_mongod_conf_file file end + def self.ipv6_is_enabled + file = get_mongod_conf_file + config = YAML.load_file(file) + if config.kind_of?(Hash) + ipv6 = config['net.ipv6'] + else # It has to be a key-value store + config = {} + File.readlines(file).collect do |line| + k,v = line.split('=') + config[k.rstrip] = v.lstrip.chomp if k and v + end + ipv6 = config['ipv6'] + end + ipv6 + end + def self.get_conn_string file = get_mongod_conf_file # The mongo conf is probably a key-value store, even though 2.6 is @@ -55,8 +71,11 @@ def self.get_conn_string if bindip first_ip_in_list = bindip.split(',').first - if first_ip_in_list.eql? '0.0.0.0' + 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 @@ -80,7 +99,13 @@ def self.db_ismaster if mongorc_file cmd_ismaster = mongorc_file + cmd_ismaster end - out = mongo(['admin', '--quiet', '--host', get_conn_string, '--eval', cmd_ismaster]) + has_ipv6 = ipv6_is_enabled + if has_ipv6 + ipv6 = '--ipv6' + else + ipv6 = '' + end + out = mongo(['admin', '--quiet', ipv6, '--host', get_conn_string, '--eval', cmd_ismaster]) out.gsub!(/ObjectId\(([^)]*)\)/, '\1') out.gsub!(/ISODate\((.+?)\)/, '\1 ') out.gsub!(/^Error\:.+/, '') @@ -118,13 +143,19 @@ def self.mongo_eval(cmd, db = 'admin', retries = 10, host = nil) cmd = mongorc_file + cmd end + has_ipv6 = ipv6_is_enabled + if has_ipv6 + ipv6 = '--ipv6' + else + ipv6 = '' + end out = nil retry_count.times do |n| begin if host - out = mongo([db, '--quiet', '--host', host, '--eval', cmd]) + out = mongo([db, '--quiet', ipv6, '--host', host, '--eval', cmd]) else - out = mongo([db, '--quiet', '--host', get_conn_string, '--eval', cmd]) + out = mongo([db, '--quiet', ipv6, '--host', get_conn_string, '--eval', cmd]) end rescue => e Puppet.debug "Request failed: '#{e.message}' Retry: '#{n}'"