ruby-on-railsrvmchef-infraunicornrunit

Start Unicorn with Runit and User's RVM


I'm deploying my Rails App servers with Chef. Have just swapped to RVM from a source install of Ruby (because I was having issues with my deploy user).

Now I have my deploy sorted, assets compiled and bundler's installed all my gems.

The problem I have is supervising Unicorn with Runit..

RVM is not installed as root user - only as my deploy user has it, as follows:

$ rvm list
rvm rubies    
=* ruby-2.0.0-p247 [ x86_64 ]

I can manually start Unicorn successfully from my deploy user. However, it won't start as part of runit.

My run file looks like this. I have also tried the solution in this SO question unsuccessfully..

#!/bin/bash
cd /var/www/html/deploy/production/current
exec 2>&1
exec chpst -u deploy:deploy /home/deploy/.rvm/gems/ruby-2.0.0-p247/bin/unicorn -E production -c config/unicorn_production.rb

If I run it manually, I get this error:

/usr/bin/env: ruby_noexec_wrapper: No such file or directory

I created a small script (gist here) which does run as root. However, if I call this from runit, I see the workers start but I get two processes for runit and I can't stop or restart the service:

Output of ps:

1001     29062     1  0 00:08 ?        00:00:00 unicorn master -D -E production -c /var/www/html/deploy/production/current/config/unicorn_production.rb                                                                                                                    
1001     29065 29062  9 00:08 ?        00:00:12 unicorn worker[0] -D -E production -c /var/www/html/deploy/production/current/config/unicorn_production.rb                                                                                                                 
root     29076   920  0 00:08 ?        00:00:00 su - deploy -c cd /var/www/html/deploy/production/current; export GEM_HOME=/home/deploy/.rvm/gems/ruby-2.0.0-p247; /home/deploy/.rvm/gems/ruby-2.0.0-p247/bin/unicorn -D -E production -c /var/www/html/deploy/production/current/config/unicorn_production.rb
1001     29083 29076  0 00:08 ?        00:00:00 -su -c cd /var/www/html/deploy/production/current; export GEM_HOME=/home/deploy/.rvm/gems/ruby-2.0.0-p247; /home/deploy/.rvm/gems/ruby-2.0.0-p247/bin/unicorn -D -E production -c /var/www/html/deploy/production/current/config/unicorn_production.rb

What should I do here? Move back to monit which worked nicely?


Solution

  • your run file is doing it wrong, you are using the binary without setting the environment, for that purpose you should use wrappers:

    rvm wrapper ruby-2.0.0-p247 --no-links unicorn
    

    To simplify the script use alias so it does not need to be changed when you decide which ruby should be used:

    rvm alias create my_app_unicorn ruby-2.0.0-p247
    

    And change the script to:

    #!/bin/bash
    cd /var/www/html/deploy/production/current
    exec 2>&1
    exec chpst -u deploy:deploy /home/deploy/.rvm/wrappers/my_app_unicorn/unicorn -E production -c config/unicorn_production.rb
    

    This will ensure proper environment is used for execution of unicorn and any time you want change ruby used to run it just crate alias to a new ruby.