0
votes

I am new to chef and I'm using sc-mongodb, and I can't get this to work. Is there a better way of doing replication for MongoDB with chef?

I was able to get the default recipe working

include_recipe "sc-mongodb::default"

But when I tried to do replication for mongo, I started getting some weird errors.

include_recipe "sc-mongodb::replicaset"

Error:

 ================================================================================
   Recipe Compile Error in /tmp/kitchen/cache/cookbooks/c_mongo/recipes/default.rb
   ================================================================================

   Net::HTTPServerException
   ------------------------
   400 "Bad Request"

   Cookbook Trace:
   ---------------
     /tmp/kitchen/cache/cookbooks/sc-mongodb/definitions/mongodb.rb:236:in `block in from_file'
     /tmp/kitchen/cache/cookbooks/sc-mongodb/recipes/replicaset.rb:36:in `from_file'
     /tmp/kitchen/cache/cookbooks/c_mongo/recipes/default.rb:54:in `from_file'

   Relevant File Content:
   ----------------------
   /tmp/kitchen/cache/cookbooks/sc-mongodb/definitions/mongodb.rb:

   229:      notifies :run, 'ruby_block[config_sharding]', :immediately if new_resource.is_mongos && new_resource.auto_configure_sharding
   230:      # we don't care about a running mongodb service in these cases, all we need is stopping it
   231:      ignore_failure true if new_resource.name == 'mongodb'
   232:    end
   233:
   234:    # replicaset
   235:    if new_resource.is_replicaset && new_resource.auto_configure_replicaset
   236>>     rs_nodes = search(
   237:        :node,
   238:        "mongodb_cluster_name:#{new_resource.cluster_name} AND "\
   239:        'mongodb_is_replicaset:true AND '\
   240:        "mongodb_config_mongod_replication_replSetName:#{new_resource.replicaset_name} AND "\
   241:        "chef_environment:#{node.chef_environment}"
   242:      )
   243:
   244:      ruby_block 'config_replicaset' do
   245:        block do

   System Info:
   ------------
   chef_version=13.8.5
   platform=centos
   platform_version=7.4.1708
   ruby=ruby 2.4.3p205 (2017-12-14 revision 61247) [x86_64-linux]
   program_name=chef-client worker: ppid=28997;start=00:31:33;
   executable=/opt/chef/bin/chef-client


   Running handlers:
   [2018-03-27T00:31:35+00:00] ERROR: Running exception handlers
   Running handlers complete
   [2018-03-27T00:31:35+00:00] ERROR: Exception handlers complete
   Chef Client failed. 0 resources updated in 01 seconds
   [2018-03-27T00:31:35+00:00] FATAL: Stacktrace dumped to /tmp/kitchen/cache/chef-stacktrace.out
   [2018-03-27T00:31:35+00:00] FATAL: Please provide the contents of the stacktrace.out file if you file a bug report
   [2018-03-27T00:31:35+00:00] ERROR: 400 "Bad Request"
   [2018-03-27T00:31:35+00:00] FATAL: Chef::Exceptions::ChildConvergeError: Chef run process exited unsuccessfully (exit code 1)

I have tried so many ways to resolve this problem, looking at the issues from the github repository. From the errors, it looks like the attributes aren't getting set, so people are setting them manually. :

# attempt1------------

#node.default['mongodb']['config']['replSet'] = true
#node.default[:mongodb][:cluster_name] = "repl-name"
#include_recipe "sc-mongodb::replicaset"

# attempt2----------

#node.normal['mongodb']['install_method'] = 'mongodb-org'
#node.normal['mongodb']['config']['bind_ip'] = '0.0.0.0'
#node.normal['mongodb']['dbconfig_file'] = '/etc/mongod.conf'
#node.normal['mongodb']['config']['replSet'] = true
#node.normal['mongodb']['is_replicaset'] = true
#node.normal['mongodb']['cluster_name'] = 'scribe'
#node.normal['mongodb']['replSet'] = 'scribe'
#node.normal['mongodb']['is_shard'] = false
#include_recipe "sc-mongodb::replicaset"

#attempt3------------
#node.default[:mongodb][:cluster_name] = "cluster_name"
#include_recipe "sc-mongodb::replicaset"

#attempt4------------

#if node['mongodb']['config']['replSet'].nil?
#  node.default['mongodb']['config']['replSet'] = "repl-name"
#end
#include_recipe "sc-mongodb::replicaset"

#attempt5-------------

#https://github.com/sous-chefs/mongodb/issues/167
#node.default['mongodb']['config']['mongod']['replication']['replSetName'] = "rs-name"
#include_recipe "sc-mongodb::replicaset"

This one gives me a different error:

#attempt6-----------
node.default['mongodb']['config']['mongod']['replication']['replSetName']= 'rs_default'
node.default['mongodb']['cluster_name'] = 'cluster'
node.default['mongodb']['auto_configure']['replicaset'] = true
include_recipe "sc-mongodb::replicaset"

Stacktrace:

           ================================================================================
       Error executing action `run` on resource 'ruby_block[config_replicaset]'
       ================================================================================

       NoMethodError
       -------------
       undefined method `[]' for nil:NilClass

       Cookbook Trace:
       ---------------
       /tmp/kitchen/cache/cookbooks/sc-mongodb/libraries/mongodb.rb:74:in `configure_replicaset'
       /tmp/kitchen/cache/cookbooks/sc-mongodb/definitions/mongodb.rb:246:in `block (3 levels) in from_file'

       Resource Declaration:
       ---------------------
       # In /tmp/kitchen/cache/cookbooks/sc-mongodb/definitions/mongodb.rb

       244:     ruby_block 'config_replicaset' do
       245:       block do
       246:         MongoDB.configure_replicaset(node, replicaset_name, rs_nodes) unless new_resource.replicaset.nil?
       247:       end
       248:       action :nothing
       249:     end
       250:

       Compiled Resource:
       ------------------
       # Declared in /tmp/kitchen/cache/cookbooks/sc-mongodb/definitions/mongodb.rb:244:in `block in from_file'

       ruby_block("config_replicaset") do
         params {:mongodb_type=>"mongod", :action=>[:enable, :start], :logpath=>"/var/log/mongodb/mongod.log", :configservers=>[], :replicaset=>true, :notifies=>[], :not_if=>[], :name=>"mongod"}
         action [:nothing]
         retries 0
         retry_delay 2
         default_guard_interpreter :default
         block_name "config_replicaset"
         declared_type :ruby_block
         cookbook_name "sc-mongodb"
         recipe_name "replicaset"
         block #<Proc:0x00000003ebdec8@/tmp/kitchen/cache/cookbooks/sc-mongodb/definitions/mongodb.rb:245>
       end

       Platform:
       ---------
       x86_64-linux
1

1 Answers

0
votes

I've had a lot of trouble with this cookbook, you're not alone.

From what I've gathered, you need to run this cookbook multiple times and/or in different configurations depending on what you are trying to achieve or what state your node is in. For example, I believe the auto_configure attribute should only be set for the last node in the set after the others have been cheffed with that set to false. Similarly for their user recipe, mongodb only allows admin coll operations on the primary and so you should ensure this recipe is executed on the designated primary node.

Unfortunately the documentation is not clear and for someone like me new to Chef and Ruby, the src and errors are tricky to interpret. I am still in the process of figuring out this cookbook and can report back if I have something concrete to give you, have you been able to get this working since your post? Sorry I can't be of much more help, you will have to try configurations out with test-kitchen VMs.