简体   繁体   中英

Capistrano + Whenever gems - bin/rails: Permission denied

I have a production environment configured that deploys and works as it should. Although I have encountered an ancillary problem that I can't figure out.

I am running the Whenever gem to execute a couple of cron jobs, the Whenever gem capistrano implementation has them deploying correctly and the scripts are executing. Although inside whenever.log I receive the following output:

/bin/bash: bin/rails: Permission denied

The script is using a runner to update from a RSS feed, this has worked without incident on a previous production deployment, although I deployed to the new server using Capistrano.

I searched around and found this question , although each time I deploy, I have to make bin/rails executable (due to datestamp deployment from Capistrano). Is there a way to make Capistrano make the file executable for me at deploy? Or is there some inherent security risk with making bin/rails executable?

I was able to solve my problem with the following (namespace including restart included for brevity):

namespace :deploy do

 desc 'Restart application'
  task :restart do
    on roles(:app), in: :sequence, wait: 5 do
      # Passenger restart mechanism
      execute :mkdir, '-p', "#{ release_path }/tmp"
      execute :touch, current_path.join('tmp/restart.txt')
    end
  end

  after :publishing, :restart

  after :restart, :x_bin_rails do
    on roles(:web), in: :groups, limit: 3, wait: 10 do
      within release_path do
        execute :chmod, "u+x bin/rails"
      end
    end
  end

end

So my solution was to make the bin/ folder a shared folder

#config/deploy.rb
set :linked_dirs, fetch(:linked_dirs, []).push('bin')

Then on your next deploy, the bin/ folder will be symlinked to shared/bin

You just need to go to this directory once, move the executables from a previous distribution

cp /your_deploy_path/releases/PREV_RELEASE/bin/* /your_deploy_path/shared/bin

... and make sure you chmod ug+x shared/bin/*

Also in my case, I was running on an Amazon EC2-linux machine and for some reasons the bin files were generated with ruby.exe instead of just ruby . Make sure you check those files and remove the .exe extension if you're not running Windows.

EDIT - I am no longer sure about the previous answer as I have ran into bugs with rails + capistrano + whenever with that setup. The following did work however for Rails 5 + Capistrano > 3.6.1

  1. Regenerate most up to date bins (you might have to do something like

     # bundle config --delete bin # Might have to do that rake rails:update:bin # git add bin # If your bin dir was in gitignore, remove it from there and commit it 
  2. remove bin from the linked dirs (so the opposite of my original solution) and add the extra line in deploy.rb

     set :bundle_binstubs, nil 
  3. Deploy. From here it would see the bin files were generated with appropriate -x permissions and correctly referenced the boot file

See this SO for reference

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM