@@ -16,39 +16,71 @@ def get_options_from_hash_config(config)
1616 # - sslMode is "requireSSL"
1717 # - Parameter --sslPEMKeyFile is set
1818 # - Parameter --sslCAFile is set
19- result << "--ssl --host #{ Facter . value ( :fqdn ) } " if [ 'allowSSL' , 'preferSSL' , 'requireSSL' ] . include? config [ 'net.ssl.mode' ] || !config [ 'net.ssl.PEMKeyFile' ] . nil? || !config [ 'net.ssl.CAFile' ] . nil?
19+ result << "--ssl --host #{ Facter . value ( :fqdn ) } " if config [ 'net.ssl.mode' ] == 'requireSSL' || !config [ 'net.ssl.PEMKeyFile' ] . nil? || !config [ 'net.ssl.CAFile' ] . nil?
2020 result << "--sslPEMKeyFile #{ config [ 'net.ssl.PEMKeyFile' ] } " unless config [ 'net.ssl.PEMKeyFile' ] . nil?
2121 result << "--sslCAFile #{ config [ 'net.ssl.CAFile' ] } " unless config [ 'net.ssl.CAFile' ] . nil?
22- result << "--sslAllowInvalidHostnames" if config [ 'net.ssl.allowInvalidHostnames' ] == true
2322 # use --tls and --host if:
2423 # - tlsMode is "requireTLS"
2524 # - Parameter --tlsCertificateKeyFile is set
2625 # - Parameter --tlsCAFile is set
27- result << "--tls --host #{ Facter . value ( :fqdn ) } " if [ 'allowTLS' , 'prefeTLS' , 'requireTLS' ] . include? config [ 'net.tls.mode' ] || !config [ 'net.tls.certificateKeyFile' ] . nil? || !config [ 'net.tls.CAFile' ] . nil?
26+ result << "--tls --host #{ Facter . value ( :fqdn ) } " if config [ 'net.tls.mode' ] == 'requireTLS' || !config [ 'net.tls.certificateKeyFile' ] . nil? || !config [ 'net.tls.CAFile' ] . nil?
2827 result << "--tlsCertificateKeyFile #{ config [ 'net.tls.certificateKeyFile' ] } " unless config [ 'net.tls.certificateKeyFile' ] . nil?
2928 result << "--tlsCAFile #{ config [ 'net.tls.CAFile' ] } " unless config [ 'net.tls.CAFile' ] . nil?
30- result << "--tlsAllowInvalidHostnames" if config [ 'net.tls.allowInvalidHostnames' ] == true
3129
3230 result << '--ipv6' unless config [ 'net.ipv6' ] . nil?
3331
3432 result . join ( ' ' )
3533end
3634
35+ def get_options_from_keyvalue_config ( file )
36+ config = { }
37+ File . readlines ( file ) . map do |line |
38+ k , v = line . split ( '=' )
39+ config [ k . rstrip ] = v . lstrip . chomp if k && v
40+ end
41+
42+ result = [ ]
43+
44+ result << "--port #{ config [ 'port' ] } " unless config [ 'port' ] . nil?
45+ # use --ssl and --host if:
46+ # - sslMode is "requireSSL"
47+ # - Parameter --sslPEMKeyFile is set
48+ # - Parameter --sslCAFile is set
49+ result << "--ssl --host #{ Facter . value ( :fqdn ) } " if config [ 'ssl' ] == 'requireSSL' || !config [ 'sslcert' ] . nil? || !config [ 'sslca' ] . nil?
50+ result << "--sslPEMKeyFile #{ config [ 'sslcert' ] } " unless config [ 'sslcert' ] . nil?
51+ result << "--sslCAFile #{ config [ 'sslca' ] } " unless config [ 'sslca' ] . nil?
52+ # use --tls and --host if:
53+ # - tlsMode is "requireTLS"
54+ # - Parameter --tlsCertificateKeyFile is set
55+ # - Parameter --tlsCAFile is set
56+ result << "--tls --host #{ Facter . value ( :fqdn ) } " if config [ 'tls' ] == 'requireTLS' || !config [ 'tlscert' ] . nil? || !config [ 'tlsca' ] . nil?
57+ result << "--tlsCertificateKeyFile #{ config [ 'tlscert' ] } " unless config [ 'tlscert' ] . nil?
58+ result << "--tlsCAFile #{ config [ 'tlsca' ] } " unless config [ 'tlsca' ] . nil?
59+
60+ result << '--ipv6' unless config [ 'ipv6' ] . nil?
61+
62+ result . join ( ' ' )
63+ end
64+
3765def get_options_from_config ( file )
3866 config = YAML . load_file ( file )
39- get_options_from_hash_config ( config )
67+ if config . is_a? ( Hash ) # Using a valid YAML file for mongo 2.6
68+ get_options_from_hash_config ( config )
69+ else # It has to be a key-value config file
70+ get_options_from_keyvalue_config ( file )
71+ end
4072end
4173
4274Facter . add ( 'mongodb_is_master' ) do
4375 setcode do
44- if %w[ mongo mongod ] . all? { |m | Facter ::Util ::Resolution . which m }
76+ if %w[ mongosh mongod ] . all? { |m | Facter ::Util ::Resolution . which m }
4577 file = mongod_conf_file
4678 if file
4779 options = get_options_from_config ( file )
4880 e = File . exist? ( '/root/.mongoshrc.js' ) ? 'load(\'/root/.mongoshrc.js\'); ' : ''
4981
5082 # Check if the mongodb server is responding:
51- Facter ::Core ::Execution . exec ( "mongosh --quiet #{ options } --eval \" #{ e } EJSON.stringify (db.adminCommand({ ping: 1 }))\" " )
83+ Facter ::Core ::Execution . exec ( "mongosh --quiet #{ options } --eval \" #{ e } printjson (db.adminCommand({ ping: 1 }))\" " )
5284
5385 if $CHILD_STATUS. success?
5486 Facter ::Core ::Execution . exec ( "mongosh --quiet #{ options } --eval \" #{ e } db.isMaster().ismaster\" " )
0 commit comments