Search code examples
rubyelasticsearchchef-infrachef-recipe

How to code elasticsearch status checks in ruby with Chef?


I want to accomplish two things: 1) clean out any pointless pid files (if elasticsearch is not running) and then start it, and 2) check that ES has started up before proceeding

Now between what Chef offers out-of-box and what Ruby allows, I can only figure out a pseudo-code like syntax for making it happen but its not going to run so I need some help/advice writing the real thing.

Pseudo-Code For (1):

bash "start it up" do
    user "root"
    only_if { # pretty sure this syntax is all incorrect, any ideas to make it happen?
        (sudo service elasticsearch status).match(/^elasticsearch not running/)
    }
    code <<-EOS
        sudo rm -rf /usr/local/var/run/elasticsearch/*.pid
        sudo service elasticsearch restart
    EOS
end

Pseudo-Code For (2):

bash "wait for it to start up" do
    user "root"
    only_if { # pretty sure this syntax is all incorrect, any ideas to make it happen?
        (sudo service elasticsearch status).match(/^elasticsearch  running with PID/)
    }
    retries 20
    retry_delay 5
    code <<-EOS
        echo "I can now go on with my life..."
    EOS
end

Solution

  • If you wish to ensure a certain particular status before continuing, insert this in a recipe (this is an example and not tested):

    service "elasticsearch" do
      action [ :enable, :start ]
      status_command "/usr/sbin/service elasticsearch status | grep 'running with PID'" 
    end
    

    It's the job of the init script's start command to wait for the service to be actually started.

    Chef docs says:

    There is no reason to use the execute resource to control a service because the service resource exposes the start_command attribute directly, which gives a recipe full control over the command issued in a much cleaner, more direct manner.