1818 mk_resource_methods
1919
2020 def initialize ( resource = { } )
21- Puppet . debug ( "REPLSET: initialise" )
2221 super ( resource )
2322 @property_flush = { }
2423 end
2524
2625 def settings = ( settings )
27- Puppet . debug ( "REPLSET: settings= with #{ settings } " )
2826 @property_flush [ :settings ] = settings
2927 end
3028
3129 def members = ( hosts )
32- Puppet . debug ( "REPLSET: members= with #{ hosts } " )
3330 @property_flush [ :members ] = hosts
3431 end
3532
3633 def self . instances
37- Puppet . debug ( "REPLSET: instances" )
3834 instance = replset_properties
3935 if instance
4036 # There can only be one replset per node
@@ -45,45 +41,38 @@ def self.instances
4541 end
4642
4743 def self . prefetch ( resources )
48- Puppet . debug ( "REPLSET: prefetch" )
4944 instances . each do |prov |
5045 resource = resources [ prov . name ]
5146 resource . provider = prov if resource
5247 end
5348 end
5449
5550 def exists?
56- Puppet . debug ( "REPLSET: exists?" )
5751 @property_hash [ :ensure ] == :present
5852 end
5953
6054 def create
61- Puppet . debug ( "REPLSET: create" )
6255 @property_flush [ :ensure ] = :present
6356 @property_flush [ :members ] = resource . should ( :members )
6457 @property_flush [ :settings ] = resource . should ( :settings )
6558 end
6659
6760 def destroy
68- Puppet . debug ( "REPLSET: destroy" )
6961 @property_flush [ :ensure ] = :absent
7062 end
7163
7264 def flush
73- Puppet . debug ( "REPLSET: flush" )
7465 set_members
7566 @property_hash = self . class . replset_properties
7667 end
7768
7869 private
7970
8071 def db_ismaster ( host )
81- Puppet . debug ( "REPLSET: flush" )
8272 mongo_command ( 'db.isMaster()' , host )
8373 end
8474
8575 def rs_initiate ( conf , master )
86- Puppet . debug ( "REPLSET: in rs_initiate with conf is #{ conf } and master is #{ master } and auth_enabled is #{ auth_enabled } " )
8776 if auth_enabled && auth_enabled != 'disabled'
8877 mongo_command ( "rs.initiate(#{ conf } )" , initialize_host )
8978 else
@@ -92,58 +81,47 @@ def rs_initiate(conf, master)
9281 end
9382
9483 def rs_status ( host )
95- Puppet . debug ( "REPLSET: in rs_status with host is #{ host } " )
9684 mongo_command ( 'rs.status()' , host )
9785 end
9886
9987 def rs_config ( host )
100- Puppet . debug ( "REPLSET: in rs_config with host is #{ host } " )
10188 mongo_command ( 'rs.config()' , host )
10289 end
10390
10491 def rs_add ( host_conf , master )
105- Puppet . debug ( "REPLSET: in rs_add with host_conf is #{ host_conf } and master is #{ master } " )
10692 mongo_command ( "rs.add(#{ host_conf . to_json } )" , master )
10793 end
10894
10995 def rs_remove ( host_conf , master )
110- Puppet . debug ( "REPLSET: in rs_remove with host_conf is #{ host_conf } and master is #{ master } " )
11196 host = host_conf [ 'host' ]
11297 mongo_command ( "rs.remove('#{ host } ')" , master )
11398 end
11499
115100 def rs_reconfig_member ( host_conf , master )
116- Puppet . debug ( "REPLSET: in rs_reconfig_member with host_conf is #{ host_conf } and master is #{ master } " )
117101 mongo_command ( "rsReconfigMember(#{ host_conf . to_json } )" , master )
118102 end
119103
120104 def rs_reconfig_settings ( settings , master )
121- Puppet . debug ( "REPLSET: in rs_reconfig_settings with settings is #{ settings } and master is #{ master } " )
122105 mongo_command ( "rsReconfigSettings(#{ settings . to_json } )" , master )
123106 end
124107
125108 def rs_arbiter
126- Puppet . debug ( "REPLSET: rs_arbiter" )
127109 @resource [ :arbiter ]
128110 end
129111
130112 def rs_add_arbiter ( host , master )
131- Puppet . debug ( "REPLSET: rs_add_arbiter" )
132113 mongo_command ( "rs.addArb('#{ host } ')" , master )
133114 end
134115
135116 def auth_enabled
136- Puppet . debug ( 'REPLSET: in auth_enabled' )
137117 self . class . auth_enabled
138118 end
139119
140120 def initialize_host
141- Puppet . debug ( "REPLSET: initialize_host" )
142121 @resource [ :initialize_host ]
143122 end
144123
145124 def master_host ( members )
146- Puppet . debug ( "REPLSET: in master_host with members is #{ members } " )
147125 members . each do |member |
148126 status = db_ismaster ( member [ 'host' ] )
149127 return status [ 'primary' ] if status . key? ( 'primary' )
@@ -152,30 +130,24 @@ def master_host(members)
152130 end
153131
154132 def self . replset_properties
155- Puppet . debug ( "REPLSET: self.replset_properties" )
156133 conn_string = conn_string # rubocop:disable Lint/SelfAssignment
157134 begin
158135 output = mongo_command ( 'rs.conf()' , conn_string )
159136 rescue Puppet ::ExecutionFailure => e
160- Puppet . debug ( "REPLSET in self.replset_properties rescue with errors #{ e . message } " )
161137 if e . message =~ %r{command replSetGetConfig requires authentication} || e . message =~ %r{not authorized on admin to execute command}
162- Puppet . debug ( "REPLSET in self.replset_properties rescue new try with rs.status()" )
163138 output = mongo_command ( 'rs.status()' , conn_string )
164- Puppet . debug ( "REPLSET in self.replset_properties rescue new try #{ output } " )
165139 if output [ 'members' ]
166140 memb = [ ]
167141 output [ 'members' ] . each do |m |
168142 memb << { 'host' => m [ 'name' ] }
169143 end
170- ret = {
144+ {
171145 name : output [ 'set' ] ,
172146 ensure : :present ,
173147 members : memb ,
174148 #settings: @resource[:settings],
175149 provider : :mongo
176150 }
177- Puppet . debug ( "REPLSET in self.replset_properties rescue result is #{ ret } " )
178- return ret
179151 end
180152 else
181153 nil
@@ -194,13 +166,11 @@ def self.replset_properties
194166 end
195167
196168 def get_hosts_status ( members )
197- Puppet . debug ( "REPLSET: get_hosts_status with #{ members } " )
198169 alive = [ ]
199170 members . select do |member |
200171 host = member [ 'host' ]
201172 Puppet . debug "Checking replicaset member #{ host } ..."
202173 status = rs_status ( host )
203- Puppet . debug ( "REPLSET: get_hosts_status with status #{ status } " )
204174 raise Puppet ::Error , "Can't configure replicaset #{ name } , host #{ host } is not supposed to be part of a replicaset." if status . key? ( 'errmsg' ) && status [ 'errmsg' ] == 'not running with --replSet'
205175
206176 if auth_enabled && status . key? ( 'errmsg' ) && ( status [ 'errmsg' ] . include? ( 'requires authentication' ) || status [ 'errmsg' ] . include? ( 'not authorized on admin' ) || status [ 'errmsg' ] . include? ( 'Authentication failed' ) )
@@ -238,7 +208,6 @@ def get_hosts_status(members)
238208 end
239209
240210 def get_members_changes ( current_members_conf , new_members_conf )
241- Puppet . debug ( "REPLSET: get_ members_changes with #{ current_members_conf } and #{ new_members_conf } " )
242211 # no changes in members config
243212 return [ [ ] , [ ] , [ ] ] if new_members_conf . nil?
244213
@@ -270,16 +239,13 @@ def get_members_changes(current_members_conf, new_members_conf)
270239 end
271240
272241 def get_replset_settings_changes ( current_settings , new_settings )
273- Puppet . debug ( "REPLSET: get_replset_settings_change swith #{ current_settings } and #{ new_settings } " )
274242 new_settings . each do |k , v |
275243 current_settings [ k ] = v
276244 end
277245 current_settings
278246 end
279247
280248 def set_members
281- Puppet . debug ( "REPLSET: set_members" )
282-
283249 if @property_flush [ :ensure ] == :absent
284250 # TODO: I don't know how to remove a node from a replset; unimplemented
285251 # Puppet.debug "Removing all members from replset #{self.name}"
@@ -298,7 +264,6 @@ def set_members
298264 # authentication should be working.
299265 #
300266 Puppet . debug 'Checking for dead and alive members'
301- Puppet . debug ( "REPLSET: set_members @property_flush[:members] is #{ @property_flush [ :members ] } and resource[:members] is #{ resource [ :members ] } " )
302267 if !@property_flush [ :members ] . nil? && !@property_flush [ :members ] . empty?
303268 # Find the alive members so we don't try to add dead members to the replset using new config
304269 alive_hosts , dead_hosts = get_hosts_status ( @property_flush [ :members ] )
@@ -448,11 +413,9 @@ def mongo_command(command, host, retries = 4)
448413 end
449414
450415 def self . mongo_command ( command , host = nil , retries = 4 )
451- Puppet . debug ( "REPLSET: self.mongo_command #{ command } and host #{ host } " )
452416 begin
453417 output = mongo_eval ( "EJSON.stringify(#{ command } )" , 'admin' , retries , host )
454418 rescue Puppet ::ExecutionFailure => e
455- Puppet . debug ( "REPLSET: rescue self.mongo_command error #{ e . message } and #{ output } " )
456419 if e . message =~ %r{no replset config has been received} || e . message =~ %r{Authentication failed}
457420 output = '{}'
458421 else
@@ -465,7 +428,6 @@ def self.mongo_command(command, host = nil, retries = 4)
465428 output = '{}' if output =~ %r{no replset config} || output =~ %r{Authentication failed}
466429 output = '{}' if output == "null\n "
467430 output = '{}' if output == "\n null\n "
468- Puppet . debug ( "REPLSET: self.mongo_command result #{ output } " )
469431
470432 # Parse the JSON output and return
471433 JSON . parse ( output )
0 commit comments