Deploying Rails-application on Ubuntu server using Capistrano gem - ruby-on-rails

I try to deploy my rails application on local virtual machine which runs ubuntu 14 LTS. I use nginx and phusion passenger. Also, I use capistrano gem for deployment.
I added my local ssh key to virtual machine's authorized keys.
Also i have no problem connecting to my virtual machine via ssh like that:
ssh neil#192.168.0.8
However when I write:
bundle exec cap production deploy
I get the following error:
cap aborted!
Net::SSH::Disconnect: connection closed by remote host
EOFError: end of file reached
Tasks: TOP => rbenv:validate
My deploy.rb file:
server "192.168.0.8", port: 80, roles: %i(:web :app :db), primary: true
set :log_level, :debug
set :application, "mySimpleBlog"
set :repo_url, "git#github.com:NeilAlishev/mySimpleBlog.git"
set :user, "neil"
set :linked_files, fetch(:linked_files, []).push("config/database.yml", "config/secrets.yml")
set :linked_dirs, fetch(:linked_dirs, []).push("log", "tmp/pids", "tmp/cache", "tmp/sockets",
"vendor/bundle", "public/system", "public/uploads")
set :deploy_to, "/home/#{fetch(:user)}/src/#{fetch(:application)}"
set :rbenv_type, :user
set :rbenv_ruby, "2.2.4"
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
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
My deploy/production.rb file:
set :stage, :production
server "192.168.0.8", user: "neil", roles: %w(web app db)
role :app, %w(neil#192.168.0.8)
role :web, %w(neil#192.168.0.8)
role :db, %w(neil#192.168.0.8)
My nginx.conf file:
server {
listen 80;
server_name 192.168.0.8;
charset utf-8;
rails_env production;
passenger_enabled on;
root /home/neil/mySimpleBlog/current/public;
}

Change production.rb like this,
role :app, 'address_of_the_server', {
:ssh_options => {
:keys => 'key_path(like ~/.ssh/key_name.pem)'
}
}
role :web, 'address_of_the_server', {
:ssh_options => {
:keys => 'key_path(like ~/.ssh/key_name.pem)'
}
}
role :db, 'address_of_the_server', {
:ssh_options => {
:keys => 'key_path(like ~/.ssh/key_name.pem)'
}
}

Related

Capistrano - Net::SSH::AuthenticationFailed

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.

Why is Capistrano deploying my project in "development" environment?

I have the following files set up for my capistrano configuration. When I deploy my project with the following command it deploys to the server, but the Rails.env is set to 'deployment'. How can I have it properly set to 'production'?
deploy command
cap production deploy
deploy.rb
set :application, 'project'
set :repo_url, 'git#github.com:AccountName/project.git'
set :deploy_to, '/home/deploy/project'
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
config/deploy/production.rb
set :stage, :production
set :branch, 'master'
set :rails_env, 'production'
server '127.0.0.1:4400', user: 'deploy', roles: %w{web app}
nginx.conf
server {
listen 80;
server_name ip_address;
rails_env production;
root /path/to/app/public;
passenger_enabled on;
}
sites-available/project
server {
listen 80 default_server;
server_name ip_address;
passenger_enabled on;
root /path/to/app/public;
}
The problem was that the passenger_app_env was at development. I couldn't figure out which services weren't restarted so I did a reboot of the server. Which presented some other issues, but I'm fairly certain the problem was resolved by changing that variable to passenger_app_env production. Thanks for the help!

Why is Capistrano deploy giving No Matching Host for bundle exec rake db:migrate?

i am deploying with capistrano and I am strugg;ling to find out why it will not run migrations when I try to deploy the site.
here is the whole error:
WARN [SKIPPING] No Matching Host for /usr/bin/env if test ! -d /home/deploy/forge_staging/releases/20140319132005; then echo "Directory does not exist '/home/deploy/forge_staging/releases/20140319132005'" 1>&2; false; fi
WARN [SKIPPING] No Matching Host for bundle exec rake db:migrate
heres my setup:
deploy.rb
lock '3.1.0'
server "xxx.xxx.xxx.xxx"
set :application, "ForgeAndCo"
set :scm, "git"
set :repo_url, "my-repo"
# set :scm_passphrase, ""
set :user, "deploy"
set :use_sudo, false
set :ssh_options, {
forward_agent: true,
port: 14439
}
# files we want symlinking to specific entries in shared.
set :linked_files, %w{config/database.yml}
# dirs we want symlinking to shared
set :linked_dirs, %w{bin log tmp/pids tmp/cache tmp/sockets vendor/bundle public/system}
SSHKit.config.command_map[:rake] = "bundle exec rake" #8
SSHKit.config.command_map[:rails] = "bundle exec rails"
set :branch, ENV["REVISION"] || ENV["BRANCH_NAME"] || "master"
set :keep_releases, 20
namespace :deploy do
desc 'Restart passenger without service interruption (keep requests in a queue while restarting)'
task :restart do
on roles(:app) do
execute :touch, release_path.join('tmp/restart.txt')
unless execute :curl, '-s -k --location localhost | grep "Forge" > /dev/null'
exit 1
end
end
end
after :finishing, "deploy:cleanup"
end
# start new deploy.rb stuff for the beanstalk repo
staging.rb
role :app, %w{deploy#xxx.xxx.xxx.xxx}
role :web, %w{deploy#xxx.xxx.xxx.xxx}
role :db, %w{deploy#xxx.xxx.xxx.xxx}
# Extended Server Syntax
# ======================
# This can be used to drop a more detailed server
# definition into the server list. The second argument
# something that quacks like a hash can be used to set
# extended properties on the server.
# server 'example.com', user: 'deploy', roles: %w{web app}, my_property: :my_value
set :stage, :staging
server "xxx.xxx.xxx.xxx", user: "deploy", roles: %w{web app db}
set :deploy_to, "/home/deploy/forge_staging"
set :rails_env, 'staging' # If the environment differs from the stage name
set :migration_role, 'migrator' # Defaults to 'db'
set :branch, ENV["REVISION"] || ENV["BRANCH_NAME"] || "master"
Is this an issue with roles?
remove from deploy.rb: (add capistrano-bundler and require it in the Capfile)
server "xxx.xxx.xxx.xxx"
SSHKit.config.command_map[:rake] = "bundle exec rake"
SSHKit.config.command_map[:rails] = "bundle exec rails"
set :user, "deploy" # you have it in staging.rb
set :use_sudo, false # not used in cap3
remove from staging.rb:
role :app, %w{deploy#xxx.xxx.xxx.xxx}
role :web, %w{deploy#xxx.xxx.xxx.xxx}
role :db, %w{deploy#xxx.xxx.xxx.xxx}
set :migration_role, 'migrator' # <= this is why you got the error
PS: edit your question and replace the ip with xxx.

Rails ActiveSupport not loaded when deploying via Capistrano

I'm following this guide for deploying a rails app via capistrano: https://github.com/capistrano/capistrano/wiki/2.x-From-The-Beginning
I'm using linode as my VPS. I've done all the initial setup and cap deploy:setup/update/deploy all work. When I go to execute this command in my rails directory: $ rake RAILS_ENV=production db:schema:load. I get undefined method 'minutes' for 90:Fixnum. It seems that activesupport is somehow not installed, yet when I type rails --version, I get Rails 3.2.11. Any insight would be extremely helpful!
Here's my deploy.rb file:
require 'bundler/capistrano'
require "capistrano-rbenv"
set :rbenv_ruby_version, "1.9.3-p392"
set :application, "uganda-coords"
# Deploy from your local Git repo by cloning and uploading a tarball
set :scm, :git
set :repository, "git#github.com:benrudolph/myapp.git"
set :deploy_via, :copy
set :scm_passphrase, "mypassword"
set :branch, "master"
set :deploy_via, :remote_cache
set :rails_env, "production"
set :user, :root
set :deploy_to, "/var/www/#{application}"
set :use_sudo, false
set :ssh_options, { :forward_agent => true }
role :web, "176.58.105.165" # Your HTTP server, Apache/etc
role :app, "176.58.105.165" # This may be the same as your `Web` server
role :db, "176.58.105.165", :primary => true # This is where Rails migrations will run
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
end
Turns out this had to do with using 90.minutes in my application.rb file. I still don't know why this works on dev and not production.

Deployment issues with capistrano

Am following a nice tutorial here and it's really helped wrap my head around some things. Rails apache and rvm all work nicely together. I'm almost finished but am getting stuck on the final part.
Basically I have the deploy file similar to what he has but cant seem to debug what he's looking for.The deploy.rb file looks like this:
#RVM Bootstrap
$:.unshift(File.expand_path('./lib',ENV['rvm_path']))
require 'rvm/capistrano'
set :rvm_ruby_string, '1.9.2-p318'
#bundler bootstrap
require 'bundler/capistrano'
#main details
set :application , "test"
role :web, "test"
role :app, "test"
role :db, "test", :primary => true
#server Details
default_run_options[:pty] = true
ssh_options[:forward_agent] = true
set :deploy_to, "/var/www/test/"
set :deploy_via, :remote_cache
set :user, "passenger"
set :use_sudo, false
# repo details
set :scm, :git
set :scm_username, "passenger"
set :repository, "git#gitserver:test.git"
set :branch, "master"
set :git_enable_submodules, 1
# tasks
namespace :deploy do
task :start, :roles => :app do
run "touch #{current_path}/tmp/restart.txt"
end
task :stop, :roles => :app do
# Do nothing.
end
desc "Restart Application"
task :restart, :roles => :app do
run "touch #{current_path}/tmp/restart.txt"
end
end
When I attempt to deploy the application with capistrano with cap deploy:setup
I get the following error:
* executing `deploy:setup'
* executing "mkdir -p /var/www/test/ /var/www/test/releases /var/www/test/shared /var/www/test/shared/system /var/www/test/shared/log /var/www/test/shared/pids"
servers: ["test"]
connection failed for: test (SocketError: getaddrinfo: Name or service not known)
I've tinkered with it a bit. Rails webrick has no problems starting the rails application so it must be something to do with me deploying to apache. One thing to note is that the application name "app" (because test is reserved in rails) and the domain name is "test".
This mismatch could be causing problems but I have little to no experience so I'm not sure.
Can anyone point me where to debug or what it might be?
Role web, app and db need to be the URL or IP of the server that you're deploying to. Something like this:
task :staging do
set :rails_env, 'staging'
role :app, "example.com"
role :web, "example.com"
role :db, "example.com", :primary => true
end

Resources