Error in staging environment with tr8n and will_filter

Problem
You are using the tr8n translation engine, which works fine in your local development environment, and you want to use it in an environment called ‘staging’, but after deployment you get the following error in your log files;

NoMethodError (undefined method `[]' for nil:NilClass):                                                                                                                                                                                       
  will_filter (3.1.9) lib/will_filter/config.rb:92:in `user_filters_enabled?'                                                                                                                                                                 
  will_filter (3.1.9) lib/will_filter/extensions/action_controller_extension.rb:34:in `init_will_filter'

Solution

The default configuration file for will_filter includes an environment called ‘stage’ and not ‘staging’.
So you can add the following into the config/will_filter/config.yml file in your application (just afer the stage: line):

staging:
  <<: *defaults

Exporting all the commits from git after a certain date

Problem

You would like to export all the commit comments from your git repository into a text file, after a certain date (ie the last live deployment date).

Solution

Use the git log option with the after parameter and redirect the output to a text file like:

git log --after="2012-05-14" --pretty=oneline >> /path/to/file/for/the/commits/export.txt

Some more useful options can be found here

Adding timezone information in MySQL

Problem
You would like to convert times in MySQL with the CONVERT_TZ function and to be able to use timezone names (ie CE).

Problem

  1. Find out if your MySQL includes timezone information as it is not included by default. Log in and run : select count(*) from mysql.time_zone_name;
  2. If the result is 0 then the timezone information is not included
  3. Log out from your MySQL server on the command prompt and upgrade your server with: mysql_upgrade -p
  4. On the command prompt again load up the table with the data (linux server): mysql_tzinfo_to_sql /usr/share/zoneinfo/ | mysql -u root -p mysql/li>
  5. Login to your MySQL server again and run the first query: select count(*) from mysql.time_zone_name;

SSL testing in rspec

Problem
You have added the force_ssl option in your rails application, and the rspec tests fail with a redirection as ssl is required in the test evnironment.

Solution
Using a solution provided here you can add the following to your rspec test to ensure that ssl is used and the tests pass:

before(:each) do
  request.env["rack.url_scheme"] = "https"
end

Apache rewrite rule to redirect https to http for pages you don’t need https

Problem

You are using your Rails 3.2.x application with Apache and you have setup force_ssl in one of your controllers (ie payments), that you want to force the application to use ssl. But after you set it up, and you go to one of the actions for that controller, every subsequent page you follow still uses the https protocol, even if you don’t want them to.
So you need a way to force the serving of pages in http.

Solution

You can use a redirection in your SSL apache conf file, and specify that when it doesn’t match the sections (controller,actions) you specify it should be redirected to http.
Note that RewriteCond in Apache are by default ANDed when they are in subsequent lines so you can add more conditions and that you can use [OR] if you want to perform an OR logic operation.
So by adding the following in your virtual host for the 443 port (ssl), and apply the changes it should be working as expected:

# Add https to http redirection
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} !^/controller_you_want_ssl.*
RewriteRule (.*)  http://%{HTTP_HOST}%{REQUEST_URI}

Capistrano recipe to restart standalone passenger server

Problem
You want to be able to use different ruby versions with passenger and by following the article here, you have set up your server to server the second version with a standalone passenger version. If you do that you cannot use the ‘touch tmp/restart.txt’ command to restart the standalone passenger server.

Solution
What you would need to do in your config/deploy.rb file, and assuming that your standalone passenger runs on a port (4000) different from the default (3000), is to replace the following:

run "#{try_sudo} touch #{File.join(current_path,'tmp','restart.txt')}"

with

run "cd #{release_path} && passenger stop -p 4000 && passenger start #{current_path} -a 127.0.0.1 -p 4000 -e production -d --pid-file #{current_path}/tmp/pids/passenger.4000.pid --log-file #{current_path}/log/passenger.4000.log"

Find out the repositories permissions in gitolite

Problem

You would like to know what permission and for which repositories you have as a certain user when using gitolite to host your repositories.

Solution

Assuming that your gitolite user is gitolite and you have two different servers (server_a and server_b) with two different users (deploy_a and deploy_b) you can find out the permsissions by running the following:

$server_a/deploy_a: ssh gitolite@git_host info

$server_b/deploy_b: ssh gitolite@git_host info

Capistrano staging/production/demo recipe for precompiling assets

Problem

You have upgraded to rails 3.1.x, 3.2.x and you want capistrano to automatically precompile your assets and do the deployment in your various deployment environments (staging. production, demo etc).

Solution

First add a new directory in your server’s shared folder named assets

mkdir /path/to_your_shared_folder/assets

Then add the following line to your Capfile:

load 'deploy/assets'

And finally have your deploy environment file (deploy/staging.rb | deploy/prodution.rb) as follows:

set :rvm_ruby_string, '1.9.3-p125'
set :rvm_type, :user
set :rvm_bin_path, "$HOME/.rvm/bin"

# Add RVM's lib directory to the load path.
$:.unshift(File.expand_path('./lib', ENV['rvm_path']))

# Load RVM's capistrano plugin
require "rvm/capistrano"


server 'xxx.xxx.xxx.xxx', :app, :web, :db, :primary => true

set :rails_env, :staging

after "deploy:update_code", :precompile_assets
  desc "precompile the assets"
  task :precompile_assets, :roles => :app do
    run "cd #{release_path} && RAILS_ENV=#{rails_env} bundle exec rake assets:precompile"
  end