capistrano-plugins / capistrano-unicorn-nginx

Capistrano tasks for automatic and sensible unicorn + nginx configuration
MIT License
175 stars 81 forks source link

HOME variable not set in unicorn init script #12

Closed rhomeister closed 10 years ago

rhomeister commented 10 years ago

I'm using capistrano-unicorn-nginx in combination with https://github.com/rvm/rvm1-capistrano3. The problem I'm experiencing is that when I execute cap [env] unicorn:start I get the following error Can not find rvm in '/.rvm'.

See https://github.com/rvm/rvm1-capistrano3/issues/43 for a full discussion. The problem seems to be that the $HOME variable in the init script is blank at the start of the script.

One solution I came up with is to change https://github.com/bruno-/capistrano-unicorn-nginx/blob/master/lib/generators/capistrano/unicorn_nginx/templates/unicorn_init.erb#L18 to something like:

CMD="cd $APP_ROOT && HOME=/home/<%= fetch(:user) %> <%= bundle_unicorn("-D -c", fetch(:unicorn_config), "-E", fetch(:stage)) %>"

Not sure if this is the right way to do this, but I don't know if there is another way to get the HOME variable in this init script.

FYI: I'm using the Ubuntu 14.04 x64 image from DigitalOcean.

bruno- commented 10 years ago

Hi, sorry for the late response. Mpapis already opened #11 so let's discuss this issue there.

rhomeister commented 10 years ago

11 has not fixed the issue. I tested this on DigitalOcean and found that the script does not enter the else branch where @mpapis made his changes. (https://github.com/mpapis/capistrano-unicorn-nginx/blob/6ec0cfaa782ce35302215e1dc4fd1ec2213dd0dd/lib/generators/capistrano/unicorn_nginx/templates/unicorn_init.erb#L37)

I'm going to put back my original suggestion, which at least works on Ubuntu 14.04:

CMD="cd $APP_ROOT && HOME=/home/$AS_USER <%= bundle_unicorn("-D -c", fetch(:unicorn_config), "-E", fetch(:stage)) %>"

mpapis commented 10 years ago

you still can use the export HOME; true "${HOME:=$(cd . ; pwd;)}" part of my fix - hardcoding /home will not work everywhere

rhomeister commented 10 years ago

OK, I've tried many things, but I can't get this to work using your method. My last attempt was to replace https://github.com/mpapis/capistrano-unicorn-nginx/blob/6ec0cfaa782ce35302215e1dc4fd1ec2213dd0dd/lib/generators/capistrano/unicorn_nginx/templates/unicorn_init.erb#L18 by:

CMD="export HOME; true "${HOME:=$(cd . ; pwd;)}" ; cd $APP_ROOT && <%= bundle_unicorn("-D -c", fetch(:unicorn_config), "-E", fetch(:stage)) %>"

Unfortunately, this doesn't work. The output is:

Couldn't reload, starting 'export HOME; true / ; cd [DEPLOYMENT_PATH]/current && [DEPLOYMENT_PATH]/rvm1scripts}/rvm-auto.sh . bundle exec unicorn -D -c [DEPLOYMENT_PATH]/shared/config/unicorn.rb -E staging' instead
Can not find rvm in '//.rvm'.

The only way I've been able to get this to work so far is to use the line in my previous comment. Please advise.

mpapis commented 10 years ago

a bit longer bt should be more reliable:

CMD="export HOME; true "${HOME:=$(getent passwd "$USER" | cut -d: -f6;)}" ; 
cd $APP_ROOT && <%= bundle_unicorn("-D -c", fetch(:unicorn_config), "-E", fetch(:stage)) %>"
rhomeister commented 10 years ago

Still no luck:

Couldn't reload, starting 'export HOME; true  ; cd [DEPLOY_PATH]/current && [DEPLOY_PATH]/rvm1scripts}/rvm-auto.sh . bundle exec unicorn -D -c [DEPLOY_PATH]/shared/config/unicorn.rb -E staging' instead 
Can not find rvm in '/.rvm'.
rhomeister commented 10 years ago

One sec: shouldn't $USER be $AS_USER? Trying this now.

rhomeister commented 10 years ago

This works. Thanks @mpapis! Could you please check my pull request? https://github.com/capistrano-plugins/capistrano-unicorn-nginx/pull/20

rhomeister commented 10 years ago

@mpapis Thanks again for helping me fix this issue. However, as we'd say in Dutch "this would not win a beauty contest", i.e. this is a bit of a hack. I'd like it if we could get rid of this somehow. Would the same problem occur when using the capistrano-rbenv or capistrano-rvm? I'm trying to figure out where the real cause of this problem lies, so we can fix this properly.

@bruno- and I had a short discussion about this and we both feel the same.

mpapis commented 10 years ago

the real problem is su -l not setting HOME and this script not accounting for it check:

not setting HOME in su -l is not compatible with what login does (man login):

... The value for $HOME, $USER, $SHELL, $PATH, $LOGNAME, and $MAIL are set according to the appropriate fields in the password entry. ...

please open bug for debian/ubuntu for not conforming to standards

next cron does that too (man cron):

... Several environment variables are set up automatically by the cron(8) daemon. SHELL is set to /bin/sh, and LOGNAME and HOME are set from the /etc/passwd line of the crontab´s owner. ...

finally my guess is the HOME could be missing from the environment that is used to invoke the script, I would need more details to help you with finding what fails there

genericlady commented 9 years ago

For me I had to change $AS_USER to $USER to get it to work even though in the init script $AS_USER is assigned the same name as the environment variable $USER.