0

I've been doing Ruby on Rails development with ElasticSearch between two machines and its starting to get a little annoying. My usual workflow is:

git pull
bundle install
rake db:migrate (or rake db:setup depending)
rails server
elasticsearch -f -D myconfig.xml
rake environment tire:import CLASS=MyObject FORCE=true

Is there anyway I can add all of these commands to some type of start up script in Rails to bring them all into one place? It would make bringing up a dev environment a lot easier on me everytime I switch machines.

3 Answers 3

3

The best way I've found is to use the Foreman gem to kickstart your project and associated processes.

Sign up to request clarification or add additional context in comments.

1 Comment

This thing is AWESOME! Thanks for the info!
2

It looks like you should do this in your deployment using Capistrano. Here is an example config/deploy.rb file:

[basic parts omitted]

after "deploy", "bundler:bundle_install"
after "bundler:bundle_install", "db:db_migrate"
after "deploy:db_migrate", "deploy:elastic_search_indexing"

namespace :bundler do
  desc 'call bundle install'
  task :bundle_install do
    run "cd #{deploy_to}/current && bundle install"
  end
end

namespace :db do
  desc 'fire the db migrations'
  task :db_migrate do
    run "cd #{deploy_to}/current && bundle exec rake db:migrate RAILS_ENV=\"production\""
  end
end

namespace :elasticsearch do
  desc 'run elasticsearch indexing via tire'
  task :index_classes do
    run "cd #{deploy_to}/current && bundle exec rake environment tire:import CLASS=YourObject FORCE=true "
  end
end

[rest omitted]

Make sure you have a config file on the target machine (Linux) in /etc/elasticsearch/elasticsearch.yml with contents like:

cluster:
   name:   elasticsearch_server

network:
   host:   66.98.23.12

And the last point to mention is that you should create an initializer config/initializers/tire.rb:

if Rails.env == 'production'
  Tire.configure do
    url "http://66.98.23.12:9200"
  end
end

As you can see, this is the exact same IP address, but only used for the production environment. I assume that you access elasticsearch locally (in development mode) via localhost. elasticsearch is connection per default to

 http://0.0.0.0:9200

A good starting point and also in depth help is provided by awesome Ryan Bates and his Railscasts http://railscasts.com/episodes?utf8=%E2%9C%93&search=capistrano

Comments

0

Whats keeping you from putting it in a bash script? And put the script inside your RAILS_APP_HOME/scripts folder?

#!/bin/sh
git pull
bundle install
rake db:migrate 
rails server
elasticsearch -f -D myconfig.xml
rake environment tire:import CLASS=MyObject FORCE=true

3 Comments

I guess nothing really. I was just curious if there was a ruby on rails specific way people do it
In a batch script these won't work, because rails server would need to finish before elasticsearch … could start.
@Gareth I ran into this issue the other day when I started implementing the foreman gem. It seems to run everything async and the indexing commands run before the elasticsearch is officially started. I looked around for a few hours and couldnt find a way to run the command synchronously.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.