Detect initial deploy in openshift deploy hook - ruby-on-rails

I have a rails application on openshift. I want to run rake db:migrate in a deploy hook on openshift, except on first deploy, when I want to run rake db:setup.
This is not necessarily openshift-specific, since deploy hooks are just bash scripts which run when the application is deployed.
Is there any way of knowing if the application has been deployed before or whether the database has already been created from a deploy hook?

I don't think you have to make this distinction. rake db_setup can be called even if the database already exist. See also - How to check if the database exists or not in rails before doing a rake db:setup
Alternatively, you could create a custom rails task. This task could for example try to access the database to check whether it exists. If not you can call db:setup. To learn more about custom rake tasks have a look at this nice video - http://railscasts.com/episodes/66-custom-rake-tasks. Using rake has the benefit that your solution is independent of OpenShift and by using Rake you have access to the Rails environment.

I found a solution in the openshift docs, which is actually not even valid sh. Here is my version.
if echo "use $OPENSHIFT_APP_NAME; show tables" | mysql | grep schema_migrations 2>&1 > /dev/null
then
bundle exec rake db:migrate RAILS_ENV="production"
else
bundle exec rake db:setup RAILS_ENV="production"
fi
If you are using postgres, here is a similar command
if echo "\c $PGDATABASE; \dt" | psql | grep schema_migrations 2>&1 >/dev/null
then
bundle exec rake db:migrate RAILS_ENV="production"
else
bundle exec rake db:setup RAILS_ENV="production"
fi
I'm sure there is a similar environment variable as $PGDATABASE for mysql that you can use instead of $OPENSHIFT_APP_NAME. You can find it by running rhc ssh -a app-name, then running env to get a list of the environment variables.

Related

Automatically running rake tasks when you switch branches in git

Whenever I switch branches I am having to run the commands:
rake db:drop
rake db:migrate
rake db:setup
Is there a way to automatically run these commands in terminal for my application? I am developing a ruby on rails application and have many branches on GitHub. Is there a file I can add to that will automatically run this command?
For example, I will do this "git checkout branch101"
and then I want to run the 3 commands above.
Save this shell script to the file /path/to/repo/.git/hooks/post-checkout, and make it executable.
#! /bin/sh
# Start from the repository root.
cd ./$(git rev-parse --show-cdup)
# drop migrate and set up.
rake db:drop
rake db:migrate
rake db:setup
What you're looking for is something like Guard that runs your test suite whenever code changes in your repo. You should be able to tweak Guard to run your rake tasks instead of tests.
Railscasts has a great screencast explaining the Guard.
After a quick google search, looks like there's a gem available that can run rake tasks for you: guard-rake . Unfortunately, it doesn't seem to be widely used though.

Running RAILS_ENV=production with the system command

I have got a really weird problem with a script that I am running on my server, the details are below. But essentially I am using a script to set up a rails application and I am calling that script from an existing rails application.
In my existing application (the application that is calling the script on the server) I have:-
Spawnling.new do
system "cd ~/ && ~/*/create_site.sh param1 param2 >> /tmp/logger"
end
The create_site.sh script creates a fresh installation of rails using the below:
rails new $DIR --database postgresql
It then does a number of things to set up the application. The issue is that the script seems to run absolutely fine until it gets to the following command:
cd $DIR && RAILS_ENV=production rails g roroacms:install -v && RAILS_ENV=production rake assets:clean && RAILS_ENV=production rake assets:precompile
It is really odd because when I run the top command manually as the root user it seems to run absolutely fine without any issues at all. When I view the logger file at the end of the top command it looks like the below:
Your bundle is updated!
Bundle is installed and up to date
RoroaCMS installation
Installation complete with assets
Server started
When I run this manually it outputs a number of messages between each line where it is running the command. Any ideas on this? I am thinking that it could be something to do with RAILS_ENV as the rails new command runs fine earlier in the script.
Hi I do a fair bit of scripting rails on servers and I always use the format bundle exec rails g rspec:install production so your command would be cd $DIR && bundle exec rails g roroacms:install -v production && bundle exec rake assets:clean production && bundle exec rake assets:precompile production. I dont have a whole lot of reasoning behind it but that format seems to be most reliable across different server environments.

Delayed Job in Rails 4 with Capistrano

I cant figure how to start Delayed Jobs on a dedicated Ubuntu server.
It works fine on my localhost but when I run on my server
sudo RAILS_ENV=production bin/delayed_job restart
I get
sudo: bin/delayed_job: command not found
On top of that, if I run the "rake jobs:work RAILS_ENV=production" command Im getting the following error:
PG::FeatureNotSupported: ERROR: SELECT FOR UPDATE/SHARE is not allowed in subqueries
Apparently theres an issue with my psql version.
Is there any way I can get the script to work? Any effective Capistrano recipes available? All ive found on the web are old recipes for Rails 3 and older versions of capistrano.
Thanks in advance.
EDIT:
I have already bundled install the daemons gem and generated "delayed_job:active_record" on my local machine, then proceded to cap deploy which bundle installed and migrated in the production server.
The bin/delayed_job file exists in the server yet it fails with command not found.
And add this to config/environment.rb:
ENV['RAILS_ENV'] ||= 'production'
Then at your production server:
RAILS_ENV=production rake db:migrate
RAILS_ENV=test production generate delayed_job:active_record && RAILS_ENV=production rake db:migrate
Now after you do that:
RAILS_ENV=production script/delayed_job start
As for Capistrano error you are facing, please try to add the command like:
run "cd #{current_path}; #{sudo} RACK_ENV=production bundle exec #{current_path}/bin/delayed_job start"
You must run this on target server:
bundle exec rails generate delayed_job

Execute rake command inside bash

I am seeing many times the question about execution of bash files inside rake (task) files.
My question is, how to execute a rake command inside the bash file?
I have a migrate.sh file inside each rails app on my server and I'm using a general publish.sh. All of this runs ok.
Then, I have a command like rake tmp:clear assets:clean log:clear RAILS_ENV=production inside each migrate.sh that gives me a rake: command not found error.
Help?
Basically rake is not resolved as the PATH variable is not correct. You can try doing echo $PATH. Also you can create a bash script and provide some environment variables required by rake like this:
#!/bin/bash
GEM_HOME=/home/tuxdna/.gems
SHELL=/bin/bash
USER=tuxdna
PATH=/home/tuxdna/.gems/bin:/usr/lib/ruby/gems/1.8/bin/:/usr/local/bin:/usr/bin:/bin:/usr/bin/X11:/usr/games
GEM_PATH=/home/tuxdna/.gems:/usr/lib/ruby/gems/1.8
cd ~/somesite
export RAILS_ENV=production
bundle exec rake mytask:task1
klashxx's supposition was correct. It was a permissions/profile issue. I had change my user to root to be able to do other previous tasks and found out that my root was not able to run rake tasks.
This will not be an issue on production server though.
Thanks klashxx

How to run seed.rb file on Amazon ec2

Recently I hosted my Ruby on Rails application on Amazon EC2 using Elastic Beanstalk. Everything works fine except my seeds.rb file. My seeds.rb file is not executed at the time of hosting. I am using ActiveAdmin also and I define first admin on my seeds.rb file.
How can I create first admin user on Amazon by rails console? Is there any way to open Rails Console on Amazon EC2 ? I am trying to do this using putty but don't know how to do this. Please give me some pointers..
Are you not supposed to do something like this?
# .ebextensions/bundles_container.config
container_commands:
01-bundle-install:
command: "bundle install"
leader_only: true
02-bundle-db-migrate:
command: "bundle exec rake db:migrate"
leader_only: true
03-bundle-db-seed:
command: "bundle exec rake db:seed RAILS_ENV='staging'"
leader_only: true
You can also pass parameters if needed, or combine all those commands with "cmd1 && cmd2".
You need to create keypair to access the amazon instance(which i think you already have). Make sure that ssh access is enabled in the current selected security group.
You can connect to the amazon instance using
ssh -i path/to/keypair.pub ec2-user#ec2-an-ip-address.compute-1.amazonaws.com
Then cd into the app directory and run bundle exec rake db:seed RAILS_ENV='staging' assuming that you're running the app in staging environment.
In case you're here and the above solutions didn't work for you.
Apart from using the command provided in this answer above by benchwarmer:
https://stackoverflow.com/a/17232607/1216245
I had to run the seed command providing env vars for the master key and all rds settings.
bundle exec rake db:seed RAILS_ENV=production RAILS_MASTER_KEY=<your master key> RDS_HOSTNAME=<your rds hostname> RDS_PASSWORD=<...> RDS_USERNAME=<...> RDS_DB_NAME=<...> RDS_PORT=<...>
And it worked, finally :)
You can check all this in the Configuration panel for your environment in the AWS console (dashboard).

Resources