Search code examples
mongodbchef-infrachef-recipecookbookmongo-shell

Opening Mongo Shell using Chef


I am using chef scripts to install mongo, configure the replication and run some db initialize scripts (load("initializeScript.js"))

Mongo gets installed without any issue now but for configuring the replica members I need open mongo shell and run commands like:

rs.initiate() and rs.add("SERVER DETAILS")

I am trying to do it this way

 mongo_shell_command = 'mongo -ssl --sslPEMKeyFile ' +node['mongo']['ssl']['dir'] +node['hostname'] + '.pem --sslCAFile ' + node['mongo']['ssl']['dir'] + node['mongo']['cert'] + ' --sslAllowInvalidHostnames'

execute 'mongo_shell' do
        command mongo_shell_command
    end

    execute 'mongo_replicaSet' do
        command "mongo --eval rs.execute()"
        only_if { ::File.exist?(node['mongo']['config']) }
    end
execute 'initialize_db' do
             command "mongo --eval \"load(\"initializeScript.js\")\""
            cwd node['mongo']['script']['dir']
        end

But this fails.

Is there any other way to open mongo shell using chef and execute these commands


Solution

  • After some R & D, I figured the way to do so.

    1. No need to specifically open mongo shell.
    2. use --eval in the end and directly run the command.
    3. Need to provide delay between resources because the initialization takes a few seconds in mongo.