I am using:
rails 4.2
unicorn server
nginx web server
capistrano for deployment.
If I am adding a new gem to gemfile it's not reflecting in the application. I tried to check a gem by using Gem.loaded_specs["koala"].full_gem_path but its not showing anywhere. I can see gem being bundled in the log and deployment gets completed successfully. But somewhere in between, I can see one error in Capistrano logs.
NOTE: Bundler is already installed.
cd /home/deploy/bloom/releases/20170516105043 && RAILS_ENV=dev bundle exec honeybadger deploy --environment dev --revision 08e4726 --repository git#bitbucket.org:appster/bloom-ruby.git --user arvindmehra
DEBUG[1450b9f0] **bash: bundle: command not found**
Here is my capfile:
require 'capistrano/setup'
require 'capistrano/deploy'
require 'capistrano/bundler'
require 'capistrano/honeybadger'
require 'capistrano/rails/assets'
require 'capistrano/rails/migrations'
require 'capistrano/rvm'
require 'whenever/capistrano'
Dir.glob('lib/capistrano/tasks/*.rake').each { |r| import r }
Here is my deployment environment script from dev.rb
set :branch, 'dev'
set :keep_releases, 3
server '66.128.61.239',
user: 'deploy',
roles: %w{web app db},
ssh_options: {
user: 'deploy', # overrides user setting above
keys: %w(~/.ssh/id_rsa),
forward_agent: false,
#auth_methods: %w(publickey)
password: 'password'
}
namespace :deploy do
%w[start stop restart].each do |command|
desc "#{command} unicorn server"
task command do
on roles(:app), in: :sequence, wait: 1 do
execute "/etc/init.d/bloom-ruby #{command}"
end
end
end
after :publishing, :restart
after :restart, :clear_cache do
on roles(:web), in: :groups, limit: 3, wait: 10 do
# Here we can do anything such as:
# within release_path do
# execute :rake, 'cache:clear'
# end
end
end
end
Here is my deploy.rb
# config valid only for current version of Capistrano
lock '3.3.3'
set :application, 'bloom'
set :repo_url, 'git#bitbucket.org:appster/bloom-ruby.git'
set :deploy_to, '/home/deploy/bloom'
#set :linked_files, fetch(:linked_files, []).push('config/database.yml', 'config/secrets.yml')
# Define which type of RVM the server is using
set :rvm_type, :user
set :rvm_ruby_version, '2.2.2#bloom'
# Default value for :linked_files is []
set :linked_files, %w{config/database.yml config/secrets.yml config/settings.yml config/providers.yml config/stripe.yml}
# Default value for linked_dirs is []
set :linked_dirs, %w{log tmp/pids tmp/cache tmp/sockets vendor/bundle public/system public/identicons public/uploads public/images}
Deployment is in production mode.
RAILS_ENV=production bundle install
Related
I'm struggling to get a decent understanding of capistrano. I want to run rails commands in production but it seems that the corresponding binstub is nowhere to be found. As a matter of fact, I have the current/ and shared/ directories under my app name, but none of both has a bin/ directory with a rails binstub.
I'm also a complete newbie at building capistrano tasks. I found out this gem for example to run rails c with capistrano, but it requires the rails binstub in the current/bin directory, which of course I don't have.
EDIT: I tried the capistrano-rails-console gem but even if I add the ssh_options like this, I end up with:
00:00 rails:console
01 ~/.rvm/bin/rvm default do bundle exec rails console production
Usage:
rails new APP_PATH [options]
Options:
-r, [--ruby=PATH] # Path to the Ruby binary of your choice
# Default: /home/ubuntu/.rvm/rubies/ruby-2.3.1/bin/ruby
...
as if any binstub is not recognized.
I also followed the answers of this question, but none of the approaches seems to work for me. I run the app on Linux so iterm is not an option for me, and the GitHub snippets linked in the other answers either end up with:
cap aborted!
NameError: undefined local variable or method `current_task' for #<SSHKit::Backend::Netssh:0x00000001f15800>
Did you mean? current_path
or:
00:00 rails:console
Connecting with <my_username>#<my_host>
bash: bundle: command not found
Connection to <my_host> closed.
So I believe it's an rvm problem, but I totally don't know how to cope with it.
I noticed that, when I run cap production deploy, following commands are run among others:
~/.rvm/bin/rvm default do bundle exec rake assets:precompile
~/.rvm/bin/rvm default do bundle exec rake db:migrate
but if I run them on my production server, I get the following response:
Could not locate Gemfile or .bundle/ directory
For your reference, here's my config/deploy.rb:
set :scm, :git
set :repo_url, '<git_repo>'
set :application, '<app_name>'
set :user, '<production_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)}/#{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 :branch, :master
# set :format, :pretty
# set :log_level, :debug
# set :keep_releases, 5
## Linked Files & Directories (Default None):
set :linked_files, %w{config/application.yml config/database.yml config/secrets.yml}
set :linked_dirs, %w{bin log tmp/pids tmp/cache tmp/sockets vendor/bundle public/system public/uploads}
# Bonus! Colors are pretty!
def red(str)
"\e[31m#{str}\e[0m"
end
# Figure out the name of the current local branch
def current_git_branch
branch = `git symbolic-ref HEAD 2> /dev/null`.strip.gsub(/^refs\/heads\//, '')
puts "Deploying branch #{red branch}"
branch
end
# Set the deploy branch to the current branch
set :branch, current_git_branch
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 :assets do
desc "compile assets locally and upload before finalize_update"
task :deploy do
%x[bundle exec rake assets:clean && bundle exec rake assets:precompile]
ENV['COMMAND'] = " mkdir '#{release_path}/public/assets'"
invoke
upload '/#{app_dir}/public/assets', "#{release_path}/public/assets", {:recursive => true}
end
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
task :fix_absent_manifest_bug do
on roles(:web) do
within release_path do execute :touch,
release_path.join('public', fetch(:assets_prefix), 'manifest-fix.temp')
end
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
after :updating, 'deploy:fix_absent_manifest_bug'
end
# ps aux | grep puma # Get puma pid
# kill -s SIGUSR2 pid # Restart puma
# kill -s SIGTERM pid # Stop puma
and my Capfile:
# Load DSL and set up stages
require 'capistrano/setup'
# Include default deployment tasks
require 'capistrano/deploy'
# Include tasks from other gems included in your Gemfile
#
# For documentation on these, see for example:
#
# https://github.com/capistrano/rvm
# https://github.com/capistrano/rbenv
# https://github.com/capistrano/chruby
# https://github.com/capistrano/bundler
# https://github.com/capistrano/rails
# https://github.com/capistrano/passenger
#
# require 'capistrano/rvm'
# require 'capistrano/rbenv'
# require 'capistrano/chruby'
# require 'capistrano/bundler'
# require 'capistrano/rails/assets'
# require 'capistrano/rails/migrations'
# require 'capistrano/passenger'
require 'capistrano/setup'
require 'capistrano/deploy'
require 'capistrano/rails'
require 'capistrano/bundler'
require 'capistrano/rvm'
require 'capistrano/puma'
require 'capistrano/rails/collection'
# Load custom tasks from `lib/capistrano/tasks` if you have any defined
Dir.glob('lib/capistrano/tasks/*.rake').each { |r| import r }
Am I missing something? Thanks in advance
Solved with the help of this Upwork freelancer.
The solution steps were:
remove the binstubs locally
set :bundle_binstubs, nil in config/deploy.rb
remove the bin directory from the :linked_dirs list (adding also /bin in .gitignore)
push the changes and run cap production deploy
recreate the binstubs with rake rails:update:bin
comment out the set :bundle_binstubs, nil line
add the bin directory in :linked_dirs again
modify the config/deploy.rb file like this:
namespace :deploy do
task :regenerate_bins do
on roles(:web) do
within release_path do
execute :bundle, 'exec rake rails:update:bin'
end
end
end
...
...
after :finishing, :regenerate_bins
...
uncomment set :bundle_binstubs, nil and remove bin from :linked_dirs once more
push changes and deploy
After this, the binstubs are found in the current/bin directory instead of the shared/bin one (in Rails 4 and 5)
I can't figure out how to solve this problem. Capistrano didn't work correctly. So can't deploy my app.
Here's the error.
$ bundle exec cap staging deploy
(Backtrace restricted to imported tasks)
cap aborted!
Net::SSH::AuthenticationFailed: Authentication failed for user ec2-user#13.112.91.105
Here's config file, named config/deploy.rb
# config valid only for Capistrano 3.1
lock '3.5.0'
set :application, 'dola'
set :repo_url, 'git#ghe.intelligence-dev.com/inolab/eiicon-dola.git'
# Default branch is :master
# ask :branch, proc { `git rev-parse --abbrev-ref HEAD`.chomp }.call
set :branch, 'master'
# Default deploy_to directory is /var/www/my_app
set :deploy_to, '/var/www/dola'
# Default value for keep_releases is 5
# set :keep_releases, 5
set :rbenv_type, :user
set :rbenv_ruby, '2.3.2-p217'
set :rbenv_map_bins, %w{rake gem bundle ruby rails}
set :rbenv_roles, :all
set :linked_dirs, %w{bin log tmp/backup tmp/pids tmp/cache tmp/sockets vendor/bundle}
role :web, %w{13.112.91.105}
namespace :deploy do
desc 'Restart application'
task :restart do
on roles(:app), in: :sequence, wait: 5 do
# Your restart mechanism here, for example:
# 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
# Here we can do anything such as:
# within release_path do
# execute :rake, 'cache:clear'
# end
end
end
end
And here's config/deploy/staging.rb
Simple Role Syntax
# ==================
# Supports bulk-adding hosts to roles, the primary server in each group
# is considered to be the first unless any hosts have the primary
# property set. Don't declare `role :all`, it's a meta role.
role :app, %w{ec2-user#13.112.91.105}
role :web, %w{ec2-user#13.112.91.105}
# Extended Server Syntax
# ======================
# This can be used to drop a more detailed server definition into the
# server list. The second argument is a, or duck-types, Hash and is
# used to set extended properties on the server.
server '13.112.91.105', user: 'ec2-user', roles: %w{web app}, my_property: :my_value
# Custom SSH Options
# ==================
set :stage, :staging
set :rails_env, 'staging'
server '13.112.91.105', user: 'ec2-user',
roles: %w{web app}
set :ssh_options, {
keys: [File.expand_path('~/.ssh/id_rsa_ec2.pem)')]
}
Anyone, please!
Capistrano is trying to establish an SSH session between your computer and the machine to which you are trying to deploy your application - 13.112.91.105 in this case. In order to do that, given your Capistrano configuration, you need to be able to authenticate to the SSH server that is running on 13.112.91.105 as the user ec2-user using your SSH private key, which I'm assuming is ~/.ssh/id_rsa_ec2.pem. For this to happen, your corresponding SSH public key must be listed in the authorized_keys file for ec2-user on the machine 13.112.91.105.
I am using capistrano for deployment, and for some reason my shared/bin folder is empty, and, well it should contain -rails, rake, bundle, setup, spring. now obviously I did something wrong, but as I am new to capistrano, I have no Idea what it is, because it is in my git repository, and as far as I know it copies the entire thing from the repository. since I am not sure wether it is relavent or not, I will just put everything I changed reguarding the capistrano deployment.
here's my deploy.rb
lock '3.4.0'
# application settings
set :application, 'SomeApplication'
set :user, 'someuser'
#set :repo_url, 'git#bitbucket.org:someapp/someappserver.git'
set :rails_env, 'production'
set :use_sudo, false
set :keep_releases, 5
#git settings
set :scm, :git
set :branch, "master"
set :repo_url, "git#bitbucket.org:someapplication/someapplicationserver.git"
set :deploy_via, :remote_cache
set :rvm_ruby_version, '2.2.1'
set :default_env, { rvm_bin_path: '~/.rvm/bin' }
SSHKit.config.command_map[:rake] = "#{fetch(:default_env)[:rvm_bin_path]}/rvm ruby-#{fetch(:rvm_ruby_version)} do bundle exec rake"
# dirs we want symlinked to the shared folder
# during deployment
set :linked_dirs, %w{bin log tmp/pids tmp/cache tmp/sockets vendor/bundle public/system}
set :pg_database, "someapp_production"
set :pg_user, "someapp_production"
set :pg_ask_for_password, true
namespace :deploy do
task :config_nginx do
pre = File.basename(previous_release)
cur = File.basename(release_path)
run "#{sudo} sed 's/#{pre}/#{cur}/g' /etc/nginx/sites-available/default"
end
task :restart_thin_server do
run "cd #{previous_release}; source $HOME/.bash_profile && thin stop -C thin_config.yml"
run "cd #{release_path}; source $HOME/.bash_profile && thin start -C thin_config.yml"
end
task :restart_nginx do
run "#{sudo} service nginx restart"
end
desc 'Restart application'
task :restart do
on roles(:app), in: :sequence, wait: 5 do
# Your restart mechanism here, for example:
# execute :touch, release_path.join('tmp/restart.txt')
#
# The capistrano-unicorn-nginx gem handles all this
# for this example
end
end
after :publishing, :restart
after :restart, :clear_cache do
on roles(:web), in: :groups, limit: 3, wait: 10 do
# Here we can do anything such as:
# within release_path do
# execute :rake, 'cache:clear'
# end
end
end
end
here is my deploy/production.rb
# production deployment
set :stage, :production
# use the master branch of the repository
set :branch, "master"
# the user login on the remote server
# used to connect and deploy
set :deploy_user, "someuser"
# the 'full name' of the application
set :full_app_name, "#{fetch(:application)}_#{fetch(:stage)}"
# the server(s) to deploy to
server 'someserver.cloudapp.net', user: 'someuser', roles: %w{web app db}, primary: true
# the path to deploy to
set :deploy_to, "/home/#{fetch(:deploy_user)}/apps/#{fetch(:full_app_name)}"
# set to production for Rails
set :rails_env, :production
and here is my cap file
require 'capistrano/setup'
# Include default deployment tasks
require 'capistrano/deploy'
# Include tasks from other gems included in your Gemfile
#
# For documentation on these, see for example:
#
# https://github.com/capistrano/rvm
# https://github.com/capistrano/rbenv
# https://github.com/capistrano/chruby
# https://github.com/capistrano/bundler
# https://github.com/capistrano/rails
# https://github.com/capistrano/passenger
#
require 'capistrano/rvm'
# require 'capistrano/rbenv'
# require 'capistrano/chruby'
require 'capistrano/bundler'
require 'capistrano/rails/assets'
require 'capistrano/rails/migrations'
# require 'capistrano/passenger'
require 'capistrano/thin'
require 'capistrano/postgresql'
# Load custom tasks from `lib/capistrano/tasks` if you have any defined
Dir.glob('lib/capistrano/tasks/*.rake').each { |r| import r }
like #emj365 said
just remove bin form your linked_dirs in config/deploy.rb
set :linked_dirs, %w{bin log tmp/pids tmp/cache tmp/sockets vendor/bundle public/system}
#gilmishal Kindly check with this link .. capistrano-deploy-configuration
And keep your eye on directory path where i did mistake many a times
# Default deploy_to directory is /var/www/my_app_name
# set :deploy_to, '/var/www/my_app_name' # This conf is by default
Hope it will solve your problem
We have a production environment for a Rails 4 app with Apache, Phusion Passenger, and Capistrano 3, and a remote bitbucket repository. The Capistrano's "cap production deploy" works well, and executes without errors. But when we go the "current" folder on the remote server, and do a "git log" command, the last commits of our remote repository aren't loaded.
We've tried the "git log" in the main folder of our app, same problem.
Our question is, who can we load the last commits of our repo into the production env ? Isn't Capistrano made to do it by default ?
Any idea of where it could come from ?
Here is the code of our Capfile, deploy.rb and deploy/production.rb files :
Capfile
# Load DSL and Setup Up Stages
require 'capistrano/setup'
# Includes default deployment tasks
require 'capistrano/deploy'
require 'rvm1/capistrano3'
require 'capistrano/bundler'
require 'capistrano/rails/assets'
require 'capistrano/rails/migrations'
# Loads custom tasks from `lib/capistrano/tasks' if you have any defined.
Dir.glob('lib/capistrano/tasks/*.cap').each { |r| import r }
Dir.glob('lib/capistrano/**/*.rb').each { |r| import r }
deploy.rb
lock '3.1.0'
set :application, 'XXXXXXX'
set :deploy_user, 'XXXXXXX'
set :repo_url, 'GIT_REPO_URL.XXXXXXX.git'
set :keep_releases, 5
set :rvm_type, :user
set :rvm_ruby_version, 'ruby-2.1.2'
set :default_env, { rvm_bin_path: '/usr/local/rvm/bin' }
set :bundle_dir, "/usr/local/bin"
set :ssh_options, {:forward_agent => true}
set :linked_files, %w{config/database.yml config/application.yml}
set :linked_dirs, %w{bin log tmp/pids tmp/cache tmp/sockets vendor/bundle public/system}
set :tests, []
set(:config_files, %w(
apache2.conf
database.example.yml
log_rotation
unicorn.rb
unicorn_init.sh
))
set :log_level, :debug
set :pty, true
set :assets_roles, [:app]
# which config files should be made executable after copying
# by deploy:setup_config
set(:executable_config_files, %w(
unicorn_init.sh
))
# files which need to be symlinked to other parts of the
# filesystem. For example nginx virtualhosts, log rotation
# init scripts etc.
set(:symlinks, [
{
source: "apache2.conf",
link: "/etc/apache2/sites-enabled/#{fetch(:full_app_name)}"
},
{
source: "unicorn_init.sh",
link: "/etc/init.d/unicorn_#{fetch(:full_app_name)}"
},
{
source: "log_rotation",
link: "/etc/logrotate.d/#{fetch(:full_app_name)}"
}
])
namespace :deploy do
task :start do ; end
task :stop do ; end
desc 'Restart application'
task :restart do
on roles(:all), in: :sequence, wait: 5 do
# Your restart mechanism here, for example:
execute :touch, release_path.join('restart.txt')
end
end
task :stop_node do
on roles(:all), in: :sequence do
#Stop the node_server
execute "nohup node ./realtime/node_server.js &"
end
end
task :restart_node do
on roles(:all), in: :sequence do
#Restart the node_server
execute "nohup node ./realtime/node_server.js &"
end
end
end
# Bundle install configuration
set :bundle_without, %w{development test}.join(' ')
set :bundle_roles, :all
namespace :bundler do
desc "Install gems with bundler."
task :install do
on roles fetch(:bundle_roles) do
with RAILS_ENV: fetch(:environment) do
within release_path do
execute :bundle, "install", "--without #{fetch(:bundle_without)}"
end
end
end
end
end
before 'deploy:updated', 'bundler:install'
before 'deploy:restart', 'bundler:install'
after 'deploy:updated', 'deploy:publishing'
after 'deploy:restart','deploy:restart_node'
deploy/production.rb
set :stage, :production
set :branch, "REPO_BRANCH"
set :full_app_name, "#{fetch(:application)}_#{fetch(:stage)}"
set :server_name, "XXXXXXX.com www.XXXXXXXX.com"
set :password, ask('Server password', nil)
server 'XXXXXX.com', user: 'XXXXXX', password: fetch(:password), port: 22, roles: %w{web app}, primary: true
set :deploy_to, '/PATH/TO/APP'
set :rails_env, :production
set :environment, "production"
set :unicorn_worker_count, 5
set :enable_ssl, false
Looks like capistrano keeps a repo/ directory in /var/www/:appname/repo which caches the git repo, so if you change the repo capistrano won't auto-update.
Nuking the repo directory did the trick for me
You have set a specific branch for deployment (set :branch, "REPO_BRANCH") and this branch is from the remote git repository. Make sure you have pushed the commits to the right branch of the bitbucket repo.
I want to deploy to production an app to my local server. i'm using capistrano 3.
this is my capfile
require 'capistrano/setup'
require 'capistrano/deploy'
require 'capistrano/rvm'
require 'capistrano/bundler'
require 'capistrano/rails'
#require 'capistrano/rails/migrations'
#require 'capistrano/rails/assets'
# Loads custom tasks from `lib/capistrano/tasks' if you have any defined.
Dir.glob('lib/capistrano/tasks/*.cap').each { |r| import r }
this is my deploy.rb
# config valid only for Capistrano 3.1
lock '3.1.0'
set :application, 'ImpresaZiliani'
set :repo_url, 'francesco#10.0.1.8:repos/impresaziliani.git'
set :branch, 'master'
# Default branch is :master
# ask :branch, proc { `git rev-parse --abbrev-ref HEAD`.chomp }
# Default deploy_to directory is /var/www/my_app
set :deploy_to, '/home/francesco/impresaziliani'
# Default value for :scm is :git
set :scm, :git
set :deploy_user, "francesco"
set :rails_env, "production"
set :keep_releases, 5
namespace :deploy do
desc 'Restart application'
task :restart do
on roles(:app), in: :sequence, wait: 5 do
# Your restart mechanism here, for example:
# 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
# Here we can do anything such as:
# within release_path do
# execute :rake, 'cache:clear'
# end
end
end
end
set :rvm_ruby_version, '2.1.1'
set :default_env, { rvm_bin_path: '~/.rvm/bin' }
SSHKit.config.command_map[:rake] = "#{fetch(:default_env)[:rvm_bin_path]}/rvm ruby-# {fetch(:rvm_ruby_version)} do bundle exec rake"
my database.yml is ok since if i run manually the migrations on the server it works, i have tried with uncommenting the line of capistrano/rails/migrations and assets but nothing changes: when i deploy it runs fine till the bundler install, then without any warning or error, skip to the asset precompiler and doesn't run migrations.
how can i fix this?
thank you
You also need to make the user deploying has the role of db, such as:
server 'you_ip_address', user: 'user_name', roles: %w{web app db}
rake db:migrate is automatic per deploy in capistrano 3
you just need to uncomment #require 'capistrano/rails/migrations' in your Capfile
Both Jude Calimbas and hiveer's answers are more accurate than the accepted answer - the migration task is run automatically as part of the deploy task.
However, their answers do not explain the problem observed. The only thing that occurs to me is that the database.yml file is not explicitly linked in the deploy.rb file. So a line like
set :linked_files, %w{config/database.yml}
would have fixed it.
I know that this is an old question but it would be interesting to know more details from the OP regarding the problem and the fix.