I am new to Capistrano and need some help with setting it up. After I running command cap deploy it runs successfully with no errors, but it does not update anything on remote server only creates additional folders inside my_app directory with some files and also creates folder called current and placing all updates from github inside this folder. Here is my deploy.rb file
lock '3.3.3'
set :application, 'my_app'
set :repo_url, 'git#github.com:me/my_app.git'
set :branch, "master"
set :deploy_via, :remote_cache
set :user, "me"
set :stages, ["production"]
set :use_sudo, false
set :deploy_to, '/var/www/my_app'
namespace :deploy do
after :finishing, 'deploy:cleanup', "deploy:update_code"
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
What is wrong! How can I make Capistrano to update files and content of the files in my_app folder on the remote server? Thank you very much for the help!!!
Solution for this problem was simple. When you use Capistrano, you have to point your server to use folder "current". For example I use apache passenger server with rails app, so in my config file I have to specify path DocumentRoot /var/www/my_app/current/public
Related
When we deploy to our server, everything deploys fine, however we get the error "Gemfile not found" right after. At first I thought this was nginx not starting, but if I restart the box, the error goes away and the application works perfectly. We are trying to determine why this is occurring and how to fix it. As of right now, I am not sure where to begin and nothing I seem to research on the "google" has turned up answers.
The breakdown of the server setup and deployment:
rails 4
rbenv
ruby 2.1.0
capistrano v3
passenger
server build with chef
The Capfile:
require 'pry'
# Load DSL and Setup Up Stages
require 'capistrano/setup'
# Includes default deployment tasks
require 'capistrano/deploy'
# Includes 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/tree/master/assets
# https://github.com/capistrano/rails/tree/master/migrations
#
# require 'capistrano/rvm'
require 'capistrano/rbenv'
# require 'capistrano/chruby'
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 }
The deploy.rb:
set :application, 'api'
set :scm, :git
set :repo_url, 'git#github.com:PlacewiseMedia/API.git'
set :branch, 'develop'
set :deploy_to, '/home/apps/api'
set :deploy_via, :remote_cache
set :keep_releases, 10
set :user, 'deploy'
set :use_sudo, false
set :rbenv_type, :system
set :rbenv_ruby, '2.1.0'
set :rbenv_path, '/opt/rbenv'
namespace :deploy do
desc 'Restart application'
task :restart_application do
on roles(:app), in: :sequence, wait: 5 do
spacer("Setting up restart file")
execute "mkdir -p #{release_path}/tmp ; touch #{release_path}/tmp/restart.txt"
spacer("Restarting the nginx service")
execute "sudo service nginx restart"
spacer()
end
end
desc 'Run Migrations'
task :update_database do
on roles(:app), in: :sequence, wait: 5 do
within(release_path) do
with rails_env: fetch(:rails_env) do
spacer("Updating the database")
execute :rake, "db:migrate", "--trace"
spacer()
end
end
end
end
desc 'Create application symlinks'
task :shared_links do
on roles(:app), in: :sequence, wait: 5 do
spacer("Creating application symlinks")
execute "rm #{release_path}/config/database.yml"
execute "ln -s #{shared_path}/config/database.yml #{release_path}/config/database.yml"
execute "ln -s #{shared_path}/config/secrets.yml #{release_path}/config/secrets.yml"
execute "ln -s #{shared_path}/bin/passenger #{release_path}/bin/passenger"
spacer()
end
end
after 'deploy:updated', 'deploy:shared_links'
after :finishing, 'deploy:update_database'
after :finishing, 'deploy:restart_application'
after :finishing, 'deploy:cleanup'
end
namespace :setup do
desc 'Copy the secrets.yml and database.yml files'
task config: ['config/secrets.yml', 'config/database.yml'] do |t|
on roles(:all) do
execute "mkdir -p #{shared_path}/config"
t.prerequisites.each do |file|
upload! file, "#{shared_path}/config"
end
end
end
end
def spacer(desc = nil)
puts "-----------------------------------------------------------------------------"
if desc
puts desc
puts "-----------------------------------------------------------------------------"
end
end
The error:
UPDATE 09/25 : 02:10pm PST
After working with https://hackhands.com/ we discovered that multiple instances of nginx are running as shown:
I can work to kill the services and restart it but it seems like something may not be configured correctly on the server via chef or our cap deployment. If I restart the box things work as stated, but we also tried killing the nginx services. We discovered that works as well. Our dev ops team is working on this, but we are still perplexed how this has occurred or how to repair it.
UPDATE 09/26 : 11:06am PST
I found where the config comes from on the passenger spinup, if you
look in /etc/service/ you will see the folders for the apps that are
on the server. Look at the run file in the folder you're interested
in and you'll see the passenger config. That fires off a ruby .bin/passenger start process, which then fires off the /tmp nginx
process which is hanging on app restart. I've tried restarting all in
different combos, the one that seems to work is killing the nginx
process, then running sudo killall ruby to respawn the new app...
not ideal
So the update our DevOps team did to address this issue revolves around the way passenger spawns nginx. The config that it was using was the older spawn-lv2 from previous versions, and I changed it to spawn for the current version 4. What this seems to do is to stop creating new directories in /tmp that were referenced by the runit script but would fail because the old version was still running. Now it looks like the updates are done in the current /tmp directory instead, so it doesn't matter if the process is still running.
This update was done to the rackbox default attribute in chef: default["rackbox"]["default_config"]["passenger_runit"]["spawn_method"] = "smart"
I'm trying to deploy my rails app, until now hostet at Heroku, now at DigitalOcean using Capistrano. I've created a "1-Click-Rails-Application", which creates a blank rails app, so when I open the remote server in my webbrowser, the typical rails welcome screen (index.html) shows up. When I ssh to the remote directory, the path to the rails app is:
/home/rails/
inside of the rails directory are the typical rails folders like controllers etc. So I thought that the correct deploy_to path should be:
set :deploy_to, "/home/rails/"
I've seen so many different directory suggestions that I really can't figure out what could be right. I had
set :deploy_to, "var/www/#{application}"
as well, which didn't seem to work either.
I'm glad I've managed to upload my local app to the new vpn server without any errors at all. I want to avoid using a git repo to save the extra costs for a private git repo and push it directly from my computer. The problem is, after a
cap production deploy:cold
which does a lot and runs through without any errors, doesn't seem to upload anything. At least I can't find any of "my" files on the server. Well, I'm really happy that I got this far but don't understand why my config isn't working. I hope someone can help. Here is my deploy.rb from the config directory. (I'm using rvm.)
require 'capistrano/ext/multistage'
require "bundler/capistrano"
require "rvm/capistrano"
set :application, "myApp"
set :user, "root"
set :port, 22
set :deploy_to, "home/rails/"
set :repository, "."
set :scm, :none
set :deploy_via, :copy
set :checkout, :export
set :use_sudo, false
#set :rvm_ruby_string, "ruby-2.0.0p195##{application}"
set :rvm_type, :user
set :rvm_type, :system
server "xx.xxx.xx.xx", :app, :web, :db, :primary => true
after "deploy", "deploy:migrate"
I'm using Rails 3.2.13 and Ruby 2.0.0. Thanks a lot!
Update:
I was originally following a railscast capistrano deployment tutorial to get my head around this. Thus I created the deploy folder inside the config folder with a production.rb and a staging.rb inside.
Long story short, I've found "my" rails app, inside a var/www/xx.xxx.xx.xx/current/ directory on the server. The path is specified inside the production.rb which looks like this:
server "xx.xxx.xx.xx", :app, :web, :db, :primary => true
set :deploy_to, "/var/www/xx.xxx.xx.xx"
I could now change the path above to /home/rails but the actual rails app was inside the additional folder named current. How do I have to write the path so that there is no current directory? at least not there?
set :deploy_to, "home/rails/"
The correct way is:
set :deploy_to, "/home/rails/"
and for var path:
set :deploy_to, "/var/www/#{application}"
I got this working with the latest version of Capistrano: 3.2.1 and 3.1.0
Perhaps this was fixed in a patch?
Here are the relevant parts of my files:
Gemfile:
group :development do
gem 'capistrano-rails'
end
Capfile:
require 'capistrano/setup'
# Includes default deployment tasks
require 'capistrano/deploy'
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 }
config/deploy.rb
set :deploy_to, '/var/www/wrong_stage_folder'
# make sure this value is ignored.
config/deploy/staging.rb
set :deploy_to, '/var/www/appname_stage'
config/deploy/production.rb
set :deploy_to, '/var/www/appname_prod'
Run check: cap staging deploy:check --trace
INFO [2618043b] Running /usr/bin/env mkdir -pv /var/www/appname_stage/shared /var/www/appname_stage/releases on example.server.com
Works as expected, same for production.
I need to understand why capistrano doesn't create the folder current. I'm using the following command : cap deploy:setup, cap deploy:check, cap deploy
But when i check in my app directory, i don't current folder.
This my deploy.rb
# Execute "bundle install" after deploy, but only when really needed
require 'bundler/capistrano'
# Automatically precompile assets
load "deploy/assets"
# RVM integration
require "rvm/capistrano"
# Application name
set :application, "app"
# Application environment
set :rails_env, :production
# Deploy username and sudo username
set :user, "ubuntu"
default_run_options[:pty] = true
ssh_options[:forward_agent] = true
#We don't want to use sudo (root) - for security reasons
set :use_sudo, false
#Target ruby version
set :rvm_ruby_string, '1.9.3-p374'
#System-wide RVM installation
set :rvm_type, :user
#We use sudo (root) for system-wide RVM installation
set :rvm_install_with_sudo, true
#git is our SCM
set :scm, :git
#Use github repository
set :repository, "git#github.com:.../CM.git"
#master is our default git branch
set :branch, "master"
#Deploy via github
set :deploy_to, "/var/www/app/#{application}"
set :deploy_via, :remote_cache
#We have all components of the app on the same server
server "125.156.125.125", :app, :web, :db, :primary => true
namespace :deploy do
task :restart, :roles => :app, :except => { :no_release => true } do
run "#{try_sudo} touch #{File.join(current_path,'tmp','restart.txt')}"
end
task :symlink_shared do
run "ln -nfs #{shared_path}/config/database.yml #{release_path}/config/database.yml"
run "ln -nfs #{shared_path}/assets #{release_path}/public/assets"
end
task :assets do
system "rsync -vr --exclude='.DS_Store' public/assets #{user}##{application}:# {shared_path}/"
end
end
after 'deploy:update_code', 'deploy:symlink_shared'
I don't understand where is the error, if someone can i help me ?
Thanks you
It could fail due to folder permissions also.
If the option
set :use_sudo, false
was not present when the first time cap was deployed, the current folder have sudo as the owner. When the user is changed, it might not have sufficient permissions to update the link.
I deleted the symbolic link and ran
cap deploy:create_symlink
This updated the symbolic link for me.
Capistrano creates a current symlink (not directory) as one of the last steps in it's deployment cycle, generally right before the application server gets sent a start/restart command. It cannot create that symlink before deploying as there is nothing to symlink to (no checkouts in /releases).
If it's still not creating the symlink, check your capistrano deploy logs for an error, it won't create the symlink if it has an error before making it to that point. And if there is an error, please post it in your question.
I am not sure where exactly the problem is located but Capistrano takes about 5 minutes to deploy an almost empty project.
Can you tell me if I am doing something wrong or is it usual?
I am using:
Capistrano 2.9.0
Rails 3.1.3
Github Repository
not too slow server (4 cores, 1 GB memory)
ngix, passenger
Here is the output I am getting:
https://gist.github.com/1632009
Capfile
load 'deploy' if respond_to?(:namespace) # cap2 differentiator
# Uncomment if you are using Rails' asset pipeline
load 'deploy/assets'
Dir['vendor/gems/*/recipes/*.rb','vendor/plugins/*/recipes/*.rb'].each { |plugin| load(plugin) }
load 'config/deploy' # remove this line to skip loading any of the default tasks
deploy.rb
# -*- encoding : utf-8 -*-
require "bundler/capistrano"
set :user, 'rubys'
set :domain, 'example.com'
set :application, 'EXAMPLE'
# adjust if you are using RVM, remove if you are not
$:.unshift(File.expand_path('./lib', ENV['rvm_path']))
require "rvm/capistrano"
set :rvm_ruby_string, '1.9.2'
#set :rvm_type, :user
# file paths
set :repository, "git#github.com:GITHUBREPO/ashop.git"
set :deploy_to, "/apps/#{application}"
# using a local git repository on the server you are deploying to.
set :deploy_via, :remote_cache
set :copy_exclude, [ '.git' ]
# distribute your applications across servers (the instructions below put them
# all on the same server, defined above as 'domain', adjust as necessary)
role :app, domain
role :web, domain
role :db, domain, :primary => true
set :deploy_via, :remote_cache
set :scm, 'git'
set :branch, 'master'
set :scm_verbose, false
set :use_sudo, false
set :rails_env, :production
namespace :deploy do
desc "cause Passenger to initiate a restart"
task :restart do
run "touch #{current_path}/tmp/restart.txt"
end
end
EDIT
Networkspeed workplace:
http://www.speedtest.net/result/1714391142.png
Speed Github - Server: ~ 300KiB
Capistrano is probably slow for a bunch of reasons. One is that it opens a new remote shell to your server for every run in your deploy.rb file.
This can be amended a bit by using ssh master channels, which will cause capistrano to actually reuse ssh connections, which means less network overhead.
Here's an article on ruby deployment that mentions ssh master channels: http://alexyoung.org/2011/05/17/deployment/
Another reason is that it copies your entire codebase to a new directory for every deploy.
This is not strictly necessary when using git, and github has a wonderful article on how to "fix" this: https://github.com/blog/470-deployment-script-spring-cleaning
Im trying to deploy my application using Capistrano, but I get this error message:
`deploy:setup' is only run for servers matching {:except=>{:no_release=>true}}, but no servers matched
When running this command:
bundle exec cap deploy:setup
Here is my deploy.rb file.
set :application, "example.com"
set :repository, "git#github.com:username/repo.git"
set :use_sudo, false
set :scm, :git
set :web, application
set :app, application
set :db, application
set :branch, "master"
set :user, "webmaster"
set :deploy_to, "/opt/www/#{application}"
set :deploy_via, :remote_cache
set :domain, application
set :port, 2222
set :bundler_cmd, "bundle install --deployment --without=development,test"
ssh_options[:paranoid] = false
namespace :deploy do
task :start do ; end
task :stop do ; end
task :restart_stalker do
run "cd #{deploy_to}/current && thor stalker:kill && stalker:init"
end
task :restart, :roles => :app, :except => { :no_release => true } do
run "cd #{deploy_to}/current && touch tmp/restart.txt"
end
after "bundler_cmd", "deploy:restart_stalker"
end
I'm using Rails 3.
You need to define some roles. E.g.:
role :app, 'myapphostname'
role :web, 'mywebhostname'
It seems you used "set" instead of "role", but you should confirm this before making the change.
Most people are probably using multistage with capistrano so you wouldnt put your roles in the deploy.rb, so if you have added environment specific roles in config/deploy/#env_name.rb then make sure to add these in your config/deploy.rb
set :stages, %w(#env_name1, #env_name2...)
require 'capistrano/ext/multistage'
and make sure the capistrano-ext gem is installed.
Seems that you've already set up your server with bundle exec cap deploy:setup.
If that's the case you should now run bundle exec cap deploy.
I'm going to leave an answer here that helped me that when none of the suggested answers here or elsewhere could help me - I spent days researching this issue before I found a fix.
Make sure that if using multistage that the environment specific config files (e.g. config/deploy/environment.rb) are the only files in the config/deploy directory. I had an environment, dev that I was unable to deploy too, turned out there somehow was a complete empty config/deploy/dev file that was getting loaded instead of my config/deploy/dev.rb file, causing every deployment to that environment fail with the posted error.