rails deployment using capistrano & torquebox error 502 - ruby-on-rails

I'm running a rails app using JRuby 1.7.4 (1.9.3p392) with Torquebox and trying to deploy to Digital ocean VPS, when I run 'cap deploy' everything runs perfectly fine, however when i try to go to visit the site i still see "502 Bad Gateway nginx/1.1.19
This is my deploy.rb file
`require 'torquebox-capistrano-support'
require 'bundler/capistrano'
# SCM
# Update this part to match your server and location of your application code.
server "000.000.00.000", :web, :app, :primary => true
set :repository, 'git#github.com:jrgrammont/EmployToy.git'
set :branch, "master"
set :ssh_options, { :forward_agent => true }
set :user, "torquebox"
set :scm, :git
set :scm_verbose, true
set :use_sudo, false
set :deploy_to, '/opt/apps/employtoy.com'
set :torquebox_home, "/opt/torquebox/current"
set :jboss_init_script, "/etc/init.d/jboss-as-standalone"
set :rails_env, 'development'
set :app_context, "/"
set :app_ruby_version, '1.9'
set :application, 'employtoy.com'
default_environment['JRUBY_OPTS'] = '--1.9'
default_environment['PATH'] = '/opt/torquebox/current/jboss/bin:/opt/torquebox/current/jruby/bin:/usr/lib64/qt- 3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin:/root/bin:/root/bin'
after 'deploy:update_code', 'deploy:assets:precompile'
after 'deploy:update', 'deploy:resymlink', 'deploy:nginx'
before 'deploy:finalize_update', 'deploy:assets:symlink'
default_run_options[:pty] = true # Must be set for the password prompt from git to work
set :deploy_via, :remote_cache
namespace :deploy do
desc "relink db directory"
#if you use sqlite
task :resymlink, :roles => :app do
run "mkdir -p #{shared_path}/db; rm -rf #{current_path}/db && ln -s # {shared_path}/db #{current_path}/db && chown -R torquebox:torquebox #{current_path}/db"
end
# This is a weird part. I've found that asset complation with JRuby can really hog up the memory, which can cause the application to crash
# when doing a deploy. So, I like to compile the assets locally and SCP them to the server instead.
# Just make sure you've commented out the load deploy/assets in your Capfile.
namespace :assets do
# If you want to force the compilation of assets, just set the ENV['COMPILE_ASSETS']
task :precompile, :roles => :web do
force_compile = ENV['COMPILE_ASSETS']
begin # on first deploys, there is no current_revision so an error gets raised. in this case we want to just compile assets and upload them.
from = source.next_revision(current_revision)
rescue
force_compile = true
end
if ( force_compile) or (capture("cd #{latest_release} && #{source.local.log(from)} vendor/assets/ lib/assets/ app/assets/ | wc -l").to_i > 0 )
run_locally("rake assets:clean && rake assets:precompile")
run_locally "cd public && tar -jcf assets.tar.bz2 assets"
top.upload "public/assets.tar.bz2", "#{shared_path}", :via => :scp
run "cd #{shared_path} && tar -jxf assets.tar.bz2 && rm assets.tar.bz2"
run_locally "rm public/assets.tar.bz2"
run_locally("rake assets:clean")
else
logger.info "Skipping asset precompilation because there were no asset changes"
end
end
task :symlink, roles: :web do
run ("rm -rf #{latest_release}/public/assets &&
mkdir -p #{latest_release}/public &&
mkdir -p #{shared_path}/assets &&
ln -s #{shared_path}/assets #{latest_release}/public/assets")
end
end
end

I just ran the tutorial again...not really sure, but it looks like Capistrano is not starting Torquebox.
Try ssh'ing into the box and starting it with:
sudo service jboss-as-standalone start
also look at the torquebox log at /var/log/jboss-as/console.log .. is that throwing any errors?

Related

capistrano deployment unable to bundle install gem with no internet connection

I am deploying my application to a new server which has everything installed.
I am using the following capistrano deploy.rb:
require "capistrano/ext/multistage"
require "bundler/capistrano"
set :default_environment, {
'ORACLE_HOME' => "/opt/oraclient/64/11.2.0.2/",
'LD_LIBRARY_PATH' => "$ORACLE_HOME/lib:/usr/local/lib",
'PATH' => "/opt/ruby/bin:$PATH:$ORACLE_HOME/bin"
}
SECURE_FILES = ['database.yml', 'ldap.yml', 'initializers/secret_token.rb']
set :application, "myapp"
set :use_sudo, false
set :scm, :git
set :repository, "ssh://git#hostname:7999/web/myapp.git"
set :user, "webuser"
set :deploy_via, :remote_cache
after "deploy:update_code", "custom:create_symlinks", "custom:assets_precompile", "custom:miscellaneous"
after "deploy", "deploy:migrate"
after "deploy", "deploy:cleanup"
namespace :deploy do
desc "Restarting mod_rails with restart.txt"
task :restart, :roles => :app, :except => { :no_release => true } do
run "touch #{current_path}/tmp/restart.txt"
end
[:start, :stop].each do |t|
desc "#{t} task is a no-op with mod_rails"
task t, :roles => :app do ; end
end
namespace :web do
desc "Enable maintenance mode for apache"
task :enable_maintenance, :role => :web do
run "mkdir -p #{shared_path}/system"
on_rollback { run "rm -f #{shared_path}/system/maintenance.html" }
page = File.read('public/maintenance.html')
put page, "#{shared_path}/system/maintenance.html", :mode => 0644
end
desc "Disable maintenance mode for apache"
task :disable_maintenance, :role => :web do
run "rm -f #{shared_path}/system/maintenance.html"
end
end
end
namespace :custom do
desc "Creating config, bundler-GEMS symlinks"
task :create_symlinks, :roles => :app do
#Secure Configuration File Symlinks
SECURE_FILES.each do |link|
fobj = "#{release_path}/config/#{link}"
run <<-CMD
if [ -e #{fobj} ]; then rm -f #{fobj}; fi;
rm -f #{previous_release}/config/#{fobj};
ln -s #{vormetric_path}/#{application}/#{link} #{fobj};
CMD
end
#Bundler GEM Installation Symlink
shared_bundler_dir = File.join(shared_path, 'bundle')
release_bundler_dir = File.join(current_release, 'vendor/bundle')
run "ln -s #{shared_bundler_dir} #{release_bundler_dir}"
end
desc "Assets Pre-Compilation"
task :assets_precompile, :roles => :app do
run "cd #{current_release} && RAILS_ENV=#{rails_env} bundle exec rake assets:precompile"
end
desc "Miscellaneous Tasks"
task :miscellaneous, :roles => :app do
run "chmod -f +w #{current_release}/db/schema.rb"
end
end
This is box-specific deploy script myhostname.rb:
server "myhostname", :app, :web, :db, :primary => true
set :deploy_to, "/opt/web/var/myapp"
set :rails_env, "customertest"
set :branch, "staging"
Now the remote box does not have access to internet, but I have all my gems stored under vendor/cache. So it should pick up from there.(vendor/cache has nokigiri under /myapp/current/vendor/cache on remote server)
When i run
cap deploy servername
, i get the following error:
** [out :: myhost] An error occurred while installing nokogiri (1.5.9), and Bundler cannot
** [out :: myhost] continue.
** [out :: myhost] Make sure that `gem install nokogiri -v '1.5.9'` succeeds before bundling.
My remote box where the code is supposed to be deployed has the following folders set up:
/opt/web/var/myapp
/opt/web/var/myapp/current(where all the code is cloned currently)
/opt/web/var/myapp/releases
/opt/web/var/myapp/shared
I am not sure how it i supposed to pick up and install the gem.
This quote from bundler docs might be relevant (emphasis mine):
http://bundler.io/v1.9/bundle_package.html
By default, if you simply run bundle install after running bundle package, Bundler will still connect to rubygems.org to check whether a platform-specific gem exists for any of the gems in vendor/cache.
This behavior can be avoided by instead running bundle install --local. Note that this requires you to have the correctly platformed version for all of your gems already cached. The easiest way to achieve this is to run bundle package on an identical machine and then check in those vendored gems.
So, in short, you need to pass the --local flag when running bundler in the production server to avoid connecting to rubygems altogether.
The solution for use with capistrano is to set this variable in you deploy.rb file:
# deployment and quiet are used by default, we add the local flag
set :bundle_flags, "--deployment --quiet --local"

Deploy with capistrano error: …/REVISION No such file or directory

I'm using the gem Capistrano ~2.15.5 and deploying my application to my remote server. Everything works fine until the end of the cap prod deploy command. I'm receiving this error:
** [52.74.34.196 :: out] /bin/bash: ebs/apps/CloudTrendz/releases/20150318203140/REVISION: No such file or directory
** [52.74.34.196 :: out]
command finished in 4923ms
*** [deploy:update_code] rolling back
* executing "rm -rf ebs/apps/CloudTrendz/releases/20150318203140; true"
servers: ["52.74.34.196"]
[52.74.34.196] executing command
command finished in 411ms
failed: "/bin/bash --login -c 'git clone -b master git#github.com:prashant4224/CloudTrendz.git ebs/apps/CloudTrendz/releases/20150318203140 && cd ebs/apps/CloudTrendz/releases/20150318203140 && git checkout -b deploy 16cc51ebe1acd97cb489f10313d85f7ae9a69f6b && rm -Rf ebs/apps/CloudTrendz/releases/20150318203140/.git && (echo 16cc51ebe1acd97cb489f10313d85f7ae9a69f6b > ebs/apps/CloudTrendz/releases/20150318203140/REVISION)'" on 52.74.34.196
Here deploy.rb
require "bundler/capistrano"
set :application, "CloudTrendz"
set :repository, "git#github.com:prashant4224/CloudTrendz.git"
set :deploy_to, "ebs/apps/#{application}"
set :applicationdir, "ebs/apps/#{application}"
set :scm, :git # You can set :scm explicitly or Capistrano will make an intelligent guess based on known version control directory names
set :branch, "master"
set :deploy_to, applicationdir
set :deploy_via, :export
set :scm_verbose, true
set :user, "ubuntu"
set :use_sudo, false
set :rails_env, "production"
set :keep_releases, 2
set :precompile_only_if_changed, true
default_run_options[:pty] = true
default_run_options[:shell] = '/bin/bash --login'
ssh_options[:keys] = %w(~/.ssh/prashantec2.pem)
after "deploy:update_code", "deploy:copy_configs"
task :prod do
set :domain, "52.74.34.196"
set :repository, "git#github.com:prashant4224/CloudTrendz.git"
set :local_repository, "git#github.com:prashant4224/CloudTrendz.git"
set :branch, "master"
set :scm_verbose, true
server "52.74.34.196", :app, :web, :db, :primary => true
set :deploy_env, "prod"
"deploy"
end
namespace :deploy do
task :copy_configs, :roles => :app do
run "cp #{release_path}/../../shared/database.yml #{release_path}/config/database.yml"
end
task :migrate, :roles => :app do
run "cd #{release_path} && bundle exec rake db:migrate"
end
task :restart, :roles => :app, :except => { :no_release => true } do
run "#{try_sudo} touch #{File.join(current_path, 'tmp', 'restart.txt')}"
if deploy_env == 'prod'
tag_name = Time.now.strftime("deploy_%Y_%m_%d_%H_%M")
system "git tag -a -m 'Deployment on prod' #{tag_name}"
system "git push origin #{tag_name}"
if $? != 0
raise "Pushing tag to origin failed"
end
end
end
namespace :assets do
task :precompile, :roles => :web, :except => { :no_release => true } do
logger.info "Skipping asset pre-compilation because there were no asset changes"
end
end
end
after "deploy:update", "deploy:migrate", "deploy:cleanup"
What is wrong with this code?
Thank you in advance
It looks to me like it is failing when cloning the git repository from Github. Make sure that you've added the remote server's SSH key to the Github repository.
You can verify if this is the problem by SSH'ing into the remote server and attempting to run:
git clone git#github.com:prashant4224/CloudTrendz.git
If this fails, copy the contents of ~/.ssh/id_rsa.pub and then go to https://github.com/prashant4224/CloudTrendz/settings/keys, click "Add deploy key" and paste it in.
If ~/.ssh/id_rsa.pub does not exist on the remote server, run ssh-keygen to create it.

Capistrano deploy - assets precompile error

I am deploying with Capistrano to my new VPS. After the first deploy(cap deploy) everything was OK (site was running), but the second deploy failed on assets:precompile error.
I am running rails 3.2.13, ruby 2.0.0, rvm.
error:
* executing "cd -- /home/rails/releases/20140116121250 && RAILS_ENV=production RAILS_GROUPS=assets bundle exec rake assets:precompile"
servers: ["IP"]
[IP] executing command
*** [err :: IP] bash: line 1: 23406 Killed RAILS_ENV=production RAILS_GROUPS=assets bundle exec rake assets:precompile
command finished in 84187ms
*** [deploy:update_code] rolling back
* executing "rm -rf /home/rails/releases/20140116121250; true"
servers: ["IP"]
[IP] executing command
command finished in 519ms
failed: "rvm_path=/usr/local/rvm /usr/local/rvm/bin/rvm-shell 'default' -c 'cd -- /home/rails/releases/20140116121250 && RAILS_ENV=production RAILS_GROUPS=assets bundle exec rake assets:precompile'" on IP
deploy.rb file:
set :application, "app_name"
set :repository, "git_repository"
role :web, "IP"
role :app, "IP"
role :db, "IP", :primary => true
set :user, "rails"
set :password, "password"
set :use_sudo, false
set :deploy_to, "/home/rails/"
set :deploy_via, :copy
set :normalize_asset_timestamps, false
require 'bundler/capistrano'
require "rvm/capistrano"
set :rvm_type, :system
Capfile
load 'deploy'
# Uncomment if you are using Rails' asset pipeline
load 'deploy/assets'
load 'config/deploy' # remove this line to skip loading any of the default tasks
I am quite new to Capistrano, so please try to explain the solution clearly. Thank You for your support!
It seems as you have low operation memory size on your vpn server. (Vpn supplies without memory-swap for now) thus operation system kills your deploy process.
The solution is to compile assets locally (on your development machine)
Add deploy:assets:precompile task to your deploy.rb file (this is for Capistrano 2)
namespace :deploy do
. . .
namespace :assets do
task :precompile, :roles => :web do
from = source.next_revision(current_revision)
if capture("cd #{latest_release} && #{source.local.log(from)} vendor/assets/ lib/assets/ app/assets/ | wc -l").to_i > 0
run_locally("rake assets:clean && rake assets:precompile")
run_locally "cd public && tar -jcf assets.tar.bz2 assets"
top.upload "public/assets.tar.bz2", "#{shared_path}", :via => :scp
run "cd #{shared_path} && tar -jxf assets.tar.bz2 && rm assets.tar.bz2"
run_locally "rm public/assets.tar.bz2"
run_locally("rake assets:clean")
else
logger.info "Skipping asset precompilation because there were no asset changes"
end
end
end
end
Then just redeploy you app
$ bundle exec cap deploy
wish it helps

Capistrano deploy and assets:precompile too slow

"rake assets:precompile" and "cap deploy" consume extremely high amount of time whenever I call it (usually more then an hour). Sometimes deploy hang at all.
I use Rails 3.2.13, Ruby 1.9.3, OSX and Backbone's Chaplin Framework for frontend.
How can I speed up assets precompile and deploy task and what can cause such freezing?
My deploy.rb config:
# -*- encoding : utf-8 -*-
require 'bundler/capistrano'
load 'deploy/assets'
set :application, "eyelashes"
set :rails_env, "production"
set :repository, "git#github.com:eyelasher/repo.git"
set :scm, :git
set :deploy_via, :checkout
set :ssh_options, { :forward_agent => true }
default_run_options[:pty] = true
server "75.223.145.3", :app, :web, :db, :primary => true
set :bundle_without, [:test]
set :user, 'deployer'
set :deploy_to, "/home/deployer/eyelasher"
set :branch, "master" unless exists?(:branch)
set :use_sudo, false
set :rvm_type, :user
set :rvm_ruby_string, :local
set :deploy_via, :checkout
before 'deploy:setup', 'rvm:create_gemset'
set :unicorn_conf, "#{deploy_to}/current/config/unicorn.rb"
set :unicorn_pid, "#{deploy_to}/shared/pids/unicorn.pid"
after "deploy", "deploy:migrate"
after "deploy", "deploy:cleanup"
require 'rvm/capistrano'
require 'thinking_sphinx/deploy/capistrano'
#after 'deploy:update_code', :roles => :app do
#run "rm -f #{current_release}/config/database.yml"
#run "ln -s #{deploy_to}/shared/config/database.yml #{current_release}/config/database.yml"
#end
namespace :deploy do
task :restart do
run "if [ -f #{unicorn_pid} ]; then kill -USR2 `cat #{unicorn_pid}`; else cd #{deploy_to}/current && bundle exec unicorn -c #{unicorn_conf} -E #{rails_env} -D; fi"
end
task :start do
run "cd #{deploy_to}/current && bundle exec unicorn -c #{unicorn_conf} -E #{rails_env} -D;"
end
task :stop do
run "if [ -f #{unicorn_pid} ] && [ -e /proc/$(cat #{unicorn_pid}) ]; then kill -QUIT `cat #{unicorn_pid}`; fi"
end
end
# Thinking Sphinx typing shortcuts
namespace :ts do
task :conf do
thinking_sphinx.configure
end
task :in do
thinking_sphinx.index
end
task :start do
thinking_sphinx.start
end
task :stop do
thinking_sphinx.stop
end
task :restart do
thinking_sphinx.restart
end
task :rebuild do
thinking_sphinx.rebuild
end
end
# http://github.com/jamis/capistrano/blob/master/lib/capistrano/recipes/deploy.rb
# :default -> update, restart
# :update -> update_code, symlink
namespace :deploy do
desc "Link up Sphinx's indexes."
task :symlink_sphinx_indexes, :roles => [:app] do
run "ln -nfs #{shared_path}/db/sphinx #{release_path}/db/sphinx"
end
task :activate_sphinx, :roles => [:app] do
symlink_sphinx_indexes
thinking_sphinx.configure
#thinking_sphinx.stop
#thinking_sphinx.start
end
task :copy_images do
run "cp -R #{shared_path}/public/images #{release_path}/public/images"
end
before 'deploy:update_code', 'thinking_sphinx:stop'
after 'deploy:update_code', 'deploy:activate_sphinx'
after 'deploy:update_code', 'deploy:copy_images'
end
Maybe I can speedup somehow by making changes in deploy configuration?
If you are deploying to a memory constrained VPS that sounds plausible. I'd recommend precompiling the assets on a a faster machine and checking them in to your repository.
There are various Gems and packages for speeding up asset precompilation by parallesing them, however in your situation that will likely make things MUCH worse!

Problems with sudo when deploying RVM with Capistrano

I would like to use Capistrano to deploy my app in my server
I have the deploy.rb file with the following content:
set :user, "deploy"
set :use_sudo, true
set :deploy_to, "/webapps/#{application}"
#set :deploy_via, :remote_cache #Using this option avoids to do a full repository clone
#RVM
set :rvm_type, :system #System Wide installation
set :rvm_ruby_string, 'ree#K2'
set :rvm_install_shell, :bash
before 'deploy:setup','rvm:install_rvm'
before 'deploy:setup', 'rvm:install_ruby'
# if you're still using the script/reaper helper you will need
# these http://github.com/rails/irs_process_scripts
# 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 "touch #{File.join(current_path,'tmp','restart.txt')}"
end
end
Everything works fine when i set the root user, installs RVM (system-wide installation) and even a Ruby Enterprise, but when I run 'cap deploy:setup' setting the use_sudo option and deploy user, get this:
$ cap deploy:setup
* executing `deploy:setup'
triggering before callbacks for `deploy:setup'
* executing `rvm:install_rvm'
* executing "bash -s stable < <(curl -s https://raw.github.com/wayneeseguin/rvm/master/binscripts/rvm-installer)"
servers: ["myserver.com"]
Password:
[myserver.com] executing command
[myserver.com] bash -c 'bash -s stable < <(curl -s https://raw.github.com/wayneeseguin/rvm/master/binscripts/rvm-installer)'
*** [err :: myserver.com] bash: line 304: /usr/local/rvm/RELEASE: Permission denied
command finished in 564ms
failed: "bash -c 'bash -s stable < <(curl -s https://raw.github.com/wayneeseguin/rvm/master/binscripts/rvm-installer)'" on myserver.com
What I have to do to make this work? Do I have to adjust some settings in sudoers file?
please test with 1.1.0:
gem install rvm-capistrano

Resources