Mongodb replication


#1

I have mongodb replication script which is not working, getting error. Any idea how to make it work. Appreciate any kind of help. I am trying this on Opsworks Chef 12 but this works fine on Chef 11

=========
instance = search("aws_opsworks_instance", "self:true").first
stack = search("aws_opsworks_stack").first

script 'initiate_replset' do
  subscribes :run, 'service[mongod]'
  interpreter "ruby"
  Chef::Log.info("Running replset initiate script")
  code <<-EOH
	require 'json'
    retries = 0
    until JSON.parse('mongo localhost:27017 --quiet --eval "JSON.stringify(rs.status())"')['ok'] == 1 || retries == 40
	# Make this more readable
	'mongo localhost:27017 --quiet --eval "JSON.stringify(db.adminCommand({"replSetInitiate" : {_id: "#{stack['name']}", members: [{_id: 1, host: "mongodb1.#{stack['name']}.#{node['domain_name']}:27017"},{_id: 2, host: "mongodb1.#{stack['name']}.#{node['domain_name']}:27017"},{_id: 3, host: "mongodb1.#{stack['name']}.#{node['domain_name']}:27017"}]}}))"'
	retries += 1
	sleep(15)
	end
	EOH
  action :nothing
end
===============

Error:=>

`Error executing action `run` on resource 'script[initiate_replset]'`

`================================================================================`
`Mixlib::ShellOut::ShellCommandFailed`
`------------------------------------`
`Expected process to exit with [0], but received '1'`
`---- Begin output of "ruby"  "/tmp/chef-script20180821-3793-1x6gdh9" ----`
`STDOUT: `
`STDERR: /usr/share/ruby/gems/2.0/gems/json-1.8.3/lib/json/common.rb:155:in `parse': 757: unexpected token at 'mongo localhost:27017 --quiet --eval "JSON.stringify(rs.status())"' (JSON::ParserError)`

`from /usr/share/ruby/gems/2.0/gems/json-1.8.3/lib/json/common.rb:155:in `parse'`

`from /tmp/chef-script20180821-3793-1x6gdh9:3:in `<main>'`

`---- End output of "ruby"  "/tmp/chef-script20180821-3793-1x6gdh9" ----`

`Ran "ruby"  "/tmp/chef-script20180821-3793-1x6gdh9" returned 1`

#2

Consider mongodb-lib cookbook if you are planning to go with 12+ Chef.