capistrano deployment no such file or directory - ruby-on-rails-4

Here is my deploy.rb file
require "bundler/capistrano"
set :rvm_ruby_string, "ruby-2.0.0-p247"
set :rvm_type, :user
set :application, "myproject"
set :repository, "git#bitbucket.org:user/project.git"
set :user, "server-login-name"
set :branch, "master"
set :stages, %w{staging production}
require 'capistrano/ext/multistage'
set :deploy_via, :copy
set :use_sudo, true
set :rvm_install_with_sudo, true
default_run_options[:pty] = true
set :port, 2021
set :scm, :git
role :web, "mydomain.com"
role :app, "mydomain.com"
role :db, "mydomain.com", :primary => true
role :db, "mydomain.com"
after "deploy:update_code","deploy:config_symlink"
set :rvm_type, :system
# If you are using Passenger mod_rails uncomment this:
namespace :deploy do
task :start do ; end
task :stop do ; end
task :restart, :roles => :app, :except => { :no_release => true } do
#run "#{try_sudo} touch #{File.join(current_path,'tmp','restart.txt')}"
end
task :config_symlink do
#run "cp #{release_path}/config/database.yml.example #{release_path}/config/database.yml"
end
end
before 'deploy', 'rvm:install_ruby'
before 'deploy', 'rvm:create_gemset'
My staging.rb and production.rb has the following in separate file
set :deploy_to, "/var/rails_apps/project_stage"
set :deploy_to, "/var/rails_apps/project"
Now I am facing two error
1. triggering before callbacks for `deploy'
the task `rvm:install_ruby' does not exist
If i comment rvm:install_ruby and rvm:create_gemset
2. cannot open: no such file or directory for all my file and folders
I could not find why this is happening. can anyone help

RVM:install_ruby isn't a thing in capistrano-RVM?

Related

File backup for deploying a rails app with capistrano

I am deploying my rails 4 application with capistrano onto an Ubuntu 14.04 server. This is the deploy.rb I am using currently:
server 'ip', port: port, roles: [:web, :app, :db], primary: true
set :repo_url, 'git git'
set :application, 'appname'
set :user, 'user'
set :puma_threads, [4, 16]
set :puma_workers, 0
set :pty, true
set :use_sudo, false
set :stage, :production
set :deploy_via, :remote_cache
set :deploy_to, "/home/#{fetch(:user)}/apps/#{fetch(:application)}"
set :puma_bind, "unix://#{shared_path}/tmp/sockets/#{fetch(:application)}-puma.sock"
set :puma_state, "#{shared_path}/tmp/pids/puma.state"
set :puma_pid, "#{shared_path}/tmp/pids/puma.pid"
set :puma_access_log, "#{release_path}/log/puma.error.log"
set :puma_error_log, "#{release_path}/log/puma.access.log"
set :ssh_options, { forward_agent: true, user: fetch(:user), keys: %w(~/.ssh/id_rsa.pub) }
set :puma_preload_app, true
set :puma_worker_timeout, nil
set :puma_init_active_record, true # Change to false when not using ActiveRecord
## Defaults:
# set :scm, :git
# set :branch, :master
# set :format, :pretty
# set :log_level, :debug
# set :keep_releases, 5
## Linked Files & Directories (Default None):
set :linked_dirs, %w{bin log tmp/pids tmp/cache tmp/sockets vendor/bundle public/uploads}
namespace :puma do
desc 'Create Directories for Puma Pids and Socket'
task :make_dirs do
on roles(:app) do
execute "mkdir #{shared_path}/tmp/sockets -p"
execute "mkdir #{shared_path}/tmp/pids -p"
end
end
before :start, :make_dirs
end
namespace :deploy do
desc "Make sure local git is in sync with remote."
task :check_revision do
on roles(:app) do
unless `git rev-parse HEAD` == `git rev-parse origin/master`
puts "WARNING: HEAD is not the same as origin/master"
puts "Run `git push` to sync changes."
exit
end
end
end
desc 'Initial Deploy'
task :initial do
on roles(:app) do
before 'deploy:restart', 'puma:start'
invoke 'deploy'
end
end
desc 'Restart application'
task :restart do
on roles(:app), in: :sequence, wait: 5 do
invoke 'puma:restart'
end
end
before :starting, :check_revision
after :finishing, :compile_assets
after :finishing, :cleanup
after :finishing, :restart
end
namespace :sake do
desc "Run a task on a remote server."
# run like: cap staging sake:invoke task="a_certain_task"
task :invoke do
on roles(:all) do |h|
execute "cd #{fetch(:deploy_to)}/current && bundle exec rake #{ENV['task']} RAILS_ENV=#{fetch(:rails_env)}"
end
end
end
# ps aux | grep puma # Get puma pid
# kill -s SIGUSR2 pid # Restart puma
# kill -s SIGTERM pid # Stop puma
I had an issue in this file so that my shared/public folder got overwritten and I lost a lot of uploaded images. Due to this loss I am now considering to implement some way of creating a backup outside of the application directory on the server for uploaded files.
Is it possible to create some kind of automated routine that copies the current shared folder to a save space on the server before capistrano deploys the new version?
Regards.
Yes, you probably want to hook into Capistrano's deploy:started step to perform your backup before the new version is deployed.
Define a task that performs your backup:
task :backup_shared_public do
on roles(:all) do
# Very basic backup technique. YMMV!
execute "mkdir -p ~/public_backups"
execute "cp -Rp #{shared_path.join('public')} ~/public_backups/#{Time.now.to_i}"
end
end
And then tell Capistrano to execute it before every deploy:
after "deploy:started", "backup_shared_public"

Keep public/uploads folder of the last version with capistrano deploy

I am using Rails 4 with Capistrano to deploy on an Ubuntu 14.04 server and my users are able to upload files which are stored in /public/uploads. This folder should always be kept for new versions and should never get files from the git repository.
After reading a few guides I followed those steps:
Adding this to my deploy.rb:
set :linked_dirs, %w{bin log tmp/pids tmp/cache tmp/sockets vendor/bundle public/system}
set :linked_dirs, fetch(:linked_dirs) + %w{public/uploads}
Adding this to my /.gitignore:
# Ignore uploads
/public/uploads
Deleting the folder in my current git repository:
git rm -r --cached public/uploads
Now if I run cap deploy:setup I get this error:
Stage not set, please call something such as `cap production deploy`, where production is a stage you have defined.
If I define the stage production the deploy begins but I aborts very early:
cap aborted!
Don't know how to build task 'deploy:setup' (see --tasks)
And if I use my regular command to deploy cap production deploy everything works but the public/uploads/ folder will be renewed as before those changes.
Not sure if I am just missing the setup command or if I made mistakes in setting this up. My deploy.rb looks like this:
# Change these
server 'ip', port: port, roles: [:web, :app, :db], primary: true
set :repo_url, 'git#github.com:blabla/bla.git'
set :application, 'blub'
set :user, 'blib'
set :puma_threads, [4, 16]
set :puma_workers, 0
set :pty, true
set :use_sudo, false
set :stage, :production
set :deploy_via, :remote_cache
set :deploy_to, "/home/#{fetch(:user)}/apps/#{fetch(:application)}"
set :puma_bind, "unix://#{shared_path}/tmp/sockets/#{fetch(:application)}-puma.sock"
set :puma_state, "#{shared_path}/tmp/pids/puma.state"
set :puma_pid, "#{shared_path}/tmp/pids/puma.pid"
set :puma_access_log, "#{release_path}/log/puma.error.log"
set :puma_error_log, "#{release_path}/log/puma.access.log"
set :ssh_options, { forward_agent: true, user: fetch(:user), keys: %w(~/.ssh/id_rsa.pub) }
set :puma_preload_app, true
set :puma_worker_timeout, nil
set :puma_init_active_record, true # Change to false when not using ActiveRecord
## Defaults:
# set :scm, :git
# set :branch, :master
# set :format, :pretty
# set :log_level, :debug
# set :keep_releases, 5
## Linked Files & Directories (Default None):
set :linked_dirs, %w{bin log tmp/pids tmp/cache tmp/sockets vendor/bundle public/system}
set :linked_dirs, fetch(:linked_dirs) + %w{public/uploads}
#set :linked_files, %w{config/database.yml}
namespace :puma do
desc 'Create Directories for Puma Pids and Socket'
task :make_dirs do
on roles(:app) do
execute "mkdir #{shared_path}/tmp/sockets -p"
execute "mkdir #{shared_path}/tmp/pids -p"
end
end
before :start, :make_dirs
end
namespace :deploy do
desc "Make sure local git is in sync with remote."
task :check_revision do
on roles(:app) do
unless `git rev-parse HEAD` == `git rev-parse origin/master`
puts "WARNING: HEAD is not the same as origin/master"
puts "Run `git push` to sync changes."
exit
end
end
end
desc 'Initial Deploy'
task :initial do
on roles(:app) do
before 'deploy:restart', 'puma:start'
invoke 'deploy'
end
end
desc 'Restart application'
task :restart do
on roles(:app), in: :sequence, wait: 5 do
invoke 'puma:restart'
end
end
before :starting, :check_revision
after :finishing, :compile_assets
after :finishing, :cleanup
after :finishing, :restart
end
namespace :sake do
desc "Run a task on a remote server."
# run like: cap staging sake:invoke task="a_certain_task"
task :invoke do
on roles(:all) do |h|
execute "cd #{fetch(:deploy_to)}/current && bundle exec rake #{ENV['task']} RAILS_ENV=#{fetch(:rails_env)}"
end
end
end
# ps aux | grep puma # Get puma pid
# kill -s SIGUSR2 pid # Restart puma
# kill -s SIGTERM pid # Stop puma

Error (upload image) after deploy using capistrano rails 4

Yesterday i tried to deploy my rails app to my own server. For deploy, i have followed gorails tutorial -> https://gorails.com/deploy/ubuntu/14.04. After deployed, finally my rails app has running live. But i have a problem when i try to create room module with image, i get error like this
We're sorry, but something went wrong.
If i create room without image, it success. I'm sure that i can't upload image.
gem 'paperclip'
room.rb
has_attached_file :image, :styles => { :for_index => "336x227>", :thumb => "150x150>" }, :default_url => "/images/:style/missing.png"
validates_attachment_content_type :image, :content_type => /\Aimage\/.*\Z/
deploy.rb
lock '3.1.0'
set :application, 'hotelxxxxx'
set :repo_url, 'git#bitbucket.org:xxxxxx/hotelxxxxx.git'
set :deploy_to, '/home/xxxxxx/hotelxxxxx'
set :linked_files, %w{config/database.yml}
set :linked_dirs, %w{bin log tmp/pids tmp/cache tmp/sockets vendor/bundle public/system}
namespace :deploy do
desc 'Restart application'
task :restart do
on roles(:app), in: :sequence, wait: 5 do
execute :touch, release_path.join('tmp/restart.txt')
end
end
after :publishing, 'deploy:restart'
after :finishing, 'deploy:cleanup'
end
How to fix that?
The first step to troubleshoot this issue would be to look at the Rails log on your server. It should be in the log/production.log file. Having the actual error message in the log would make it a lot easier to help you figure this out.
Finally image uploaded !! I see error from nginx error.log => 'Paperclip::Errors::NotIdentifiedByImageMagick'. After that i try to re-install imagemagick from https://www.enovate.co.uk/web-design-blog/2015/02/25/how-to-install-imagemagick-from-source-on-ubuntu-14.04/
Imagemagick re-install succeed and open config/application.rb in my rails app
config/application.rb
# add line
Paperclip.options[:command_path] = "/usr/local/bin/identify"
Deploy again

Capistrano 3.4 Deployment Problems

I have inherited a Rails project and I am unable to deploy it using the supplied instructions using Capistrano. I am not familiar with capistrano at all.
Whatever I try I always get the following error message
(Backtrace restricted to imported tasks)
cap aborted!
ArgumentError: wrong number of arguments (1 for 2)
Tasks: TOP => production
(See full trace by running task with --trace)
This includes
$ bundle exec cap -T
$ bundle exec cap production deploy
I am following the guide here https://github.com/capistrano/capistrano
I have starred out important info but here is the a copy of the config/deploy/staging.rb
role :app, %w{deploy#**.**.**.**:****}
role :web, %w{deploy#**.**.**.**:****}
role :db, %w{deploy#**.**.**.**:****}
set :branch, 'develop'
set :deploy_to, '/var/www/soreen.staged.ws'
set :ssh_options, {
keepalive: true,
keepalive_interval: 60 #seconds
}
and here is Capfile
# Load DSL and Setup Up Stages
require 'capistrano/setup'
# Includes default deployment tasks
require 'capistrano/deploy'
require 'capistrano/rbenv'
require 'capistrano/bundler'
require 'capistrano/rails/migrations'
require 'capistrano/rails/assets'
require 'capistrano/rails/collection'
# Loads custom tasks from `lib/capistrano/tasks' if you have any defined.
Dir.glob('lib/capistrano/tasks/*.rake').each { |r| import r }
and here is config/deploy.rb
# config valid only for current version of Capistrano
lock '3.4.0'
set :application, 'Soreen'
set :repo_url => "****************"
set :linked_dirs, fetch(:linked_dirs, []).push('log', 'tmp/pids', 'tmp/cache', 'tmp/sockets', 'vendor/bundle', 'public/system')
set :rbenv_type, :user # or :system, depends on your rbenv setup
set :rbenv_ruby, '2.1.2'
set :rbenv_prefix, "RBENV_ROOT=#{fetch(:rbenv_path)} RBENV_VERSION=# {fetch(:rbenv_ruby)} #{fetch(:rbenv_path)}/bin/rbenv exec"
set :rbenv_map_bins, %w{rake gem bundle ruby rails}
set :rbenv_roles, :all # default value
namespace :deploy do
desc 'Restart application'
task :restart do
on roles(:app), in: :sequence, wait: 5 do
# Your restart mechanism here, for example:
execute :mkdir, '-p', "#{ release_path }/tmp"
execute :touch, release_path.join('tmp/restart.txt')
end
end
after :publishing, :restart
after :restart, :clear_cache do
on roles(:web), in: :groups, limit: 3, wait: 10 do
end
end
end
Please help !
You can get a backtrace for the error using the following:
bundle exec cap --backtrace -T
That will give you more information. Likely this is a syntax error in your configuration.

Rails 4, capistrano 3, delayed_job - can't find bin/delayed_job on one of the servers

When i deploy to pre server everything's woking fine. But if i try to deploy to staging server i get this error:
bundler: command not found: bin/delayed_job
file - config/deploy/staging.rb
set :rails_env, 'staging'
set :eager_load, :true
set :unicorn_rack_env, 'staging'
role :app, %w{deploy#x.x.x.x}
role :web, %w{deploy#x.x.x.x}
role :db, %w{deploy#x.x.x.x}
set :rvm_type, :auto # Defaults to: :auto
set :rvm_ruby_version, '2.1.2'
set :rails_env, 'staging'
set :eager_load, :true
role :app, %w{deploy#x.x.x.}
role :web, %w{deploy#x.x.x.}
role :db, %w{deploy#x.x.x.}
set :rvm_type, :auto # Defaults to: :auto
set :rvm_ruby_version, '2.1.2'
set :deploy_to, '/var/www/app'
server 'x.x.x', user: 'deploy', roles: %w{web app} , port: 222
set :unicorn_pid, ->{ "#{deploy_to}/shared/tmp/pids/unicorn.pid" }
set :scm, :git
set :ssh_options, { user: 'superman' }
set :keep_releases, 5
restart of delayed jobs -
namespace :delayed_job do
def args
fetch(:delayed_job_args, "")
end
def delayed_job_roles
fetch(:delayed_job_server_role, :app)
end
desc 'Stop the delayed_job process'
task :stop do
on roles(delayed_job_roles) do
within release_path do
with rails_env: fetch(:rails_env) do
execute :bundle, :exec, :'bin/delayed_job', :stop
end
end
end
end
desc 'Start the delayed_job process'
task :start do
on roles(delayed_job_roles) do
within release_path do
with rails_env: fetch(:rails_env) do
execute :bundle, :exec, :'bin/delayed_job', args, :start
end
end
end
end
desc 'Restart the delayed_job process'
task :restart do
on roles(delayed_job_roles) do
within release_path do
with rails_env: fetch(:rails_env) do
execute :bundle, :exec, :'bin/delayed_job', args, :restart
end
end
end
end
end
Could you please give me a hand in this issue ? Thanks in advance!
UPDATE 1
More info on this error :
lib/capistrano/tasks/delayed_job.rake:33 which is execute :bundle, :exec, :'bin/delayed_job', args, :restart
You should run once on destination server:
bundle exec rails generate delayed_job
This will generate delayed_job executable script in shared/bin
Check if you can see delayed_job on your staging server in #{deploy_to}/shared/bin/
If it's not there, copy it there from your project's bin folder.
In addition to operations in answer by Rider. Following will perform 'bundle exec rails generate delayed_job' on remote if bin/delayed_job is missing there. Put it in config/deploy.rb file.
namespace :delayed_job do
desc "Install Deployed Job executable if needed"
task :install do
on roles(delayed_job_roles) do |host|
within release_path do
# Only install if not already present
unless test("[ -f #{release_path}/#{delayed_job_bin} ]")
with rails_env: fetch(:rails_env) do
execute :bundle, :exec, :rails, :generate, :delayed_job
end
end
end
end
end
before :start, :install
before :restart, :install
end
This is happening because the delayed_job executable is missing on the server. It should exist in the bin directory. To fix this you should generate the executable on your development machine and check it in to source control.
This command will generate the executable for you:
bundle exec rails generate delayed_job
Its better to use Capistrano::DelayedJob Gem
Delayed Job support for Capistrano 3.x
Add this line to your application's Gemfile:
gem 'capistrano3-delayed-job', '~> 1.0'
Require in Capfile to use the default task:
require 'capistrano/delayed-job'
Add this to your deploy.rb file
after 'deploy:published', 'restart' do
invoke 'delayed_job:restart'
end
For more configurable options https://github.com/platanus/capistrano3-delayed-job
Please check if you setup your rails_env system variable correctly.