Help please to configure capistrano for deployment.
I have ssh:
user: User
host: 8.8.8.8:6554
pass: 123
Then i have bitbucket repository git#bitbucket.org:somerepo/code.git
user: Repouser#gmail.com
pass: repopass
I am just need to deploy code from default branch to User#8.8.8.8:8888:/public_html/test/ . On local machine i have ssh key, that allows me to connect without password. But capistrano didn't connect.
There is my config:
lock '3.3.5'
set :application, 'App'
set :scm, :git
set :repo_url, 'git#bitbucket.org:somerepo/code.git'
set :scm_passphrase, ""
set :scm_user, "Repouser#gmail.com"
set :user, 'User'
set :deploy_to, '/public_html/test'
set :app_dir, "/public_html/test"
set :ssh_options, {:forward_agent => true}
role :web, '8.8.8.8:6554'
namespace :deploy do
after :restart, :clear_cache do
on roles(:web), in: :groups, limit: 3, wait: 10 do
end
end
end
Error:
connection closed by remote host
** Invoke deploy:failed (first_time)
** Execute deploy:failed
Step 1: in Gemfile
gem 'capistrano'
gem 'capistrano-bundler'
gem 'capistrano-rails'
Step 2: bundle
Step 3: cap install ## it will generate set of file
Step 4: go in Capfile and paste the following code ## this file will be parallel to your rails application
require 'capistrano/setup'
require 'capistrano/deploy'
require 'capistrano/bundler'
require 'capistrano/rails/assets'
require 'capistrano/rails/migrations'
Dir.glob('lib/capistrano/tasks/*.rake').each { |r| import r }
Step 5: Config/deploy.rb that will be common to both ENV
This file will be shared/common across the application environment
set :application, 'your_app' ## keep in mind that your app dir name will be your_app
set :repo_url, 'git#bitbucket.org:somerepo/code.git'
set :branch, 'master'
set :use_sudo, true
set :deploy_to, '/public_html/test'
set :linked_files, fetch(:linked_files, []).push('config/database.yml')
set :linked_dirs, fetch(:linked_dirs, []).push('bin', 'log', 'tmp/pids', 'tmp/cache', 'tmp/sockets', 'vendor/bundle', 'public/system')
namespace :deploy do
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
Step6: Lets create ENV specific file for now For production Environment
config/deploy/production.rb ## this file will be generate by cap install command that you did earlier no need for this time
do comment all the code except this
role :app, %w{8.8.8.8:6554}
set :ssh_options, {
user: 'User'
}
Step 6: now do ssh to your server ssh User#8.8.8.8:6554
now it will ask for the password ... give password
Step 7: now by default your app will go /var/www/app and here you need to create the folder accordingly But in your case as you set :deploy_to, '/public_html/test' # make sure Dir name is followed by / 'Forward slash' this mistake i did many times
sudo mkdir -p /public_html
sudo mkdir -p /public_html/test
sudo chown User:User /public_html/test # `chown` will change the owner ship so that `User` user can `**Read/Write**`
umask 0002
mkdir /public_html/test/releases ## these are convention
mkdir /public_html/test/shared ## these are convention
sudo chown User:User public_html/test/releases
sudo chown User:User public_html/test/shared
mkdir .ssh
chmod .ssh 007
ssh-keygen -t rsa
and follow the step ## this will generate ssh key
cat .ssh/id_rsa.pub
Now add this key to your repo's go to => setting => deployment keys Button => click on that and add Key. Put the label name any thing you want and paste the ssh key here.
That it from server side
Step8: Now you need to add your ssh key to server
For that do cat ~/.ssh/id_rsa.pub if you have rsa key other wise generate rsa key it is very easy to crate
Step 9: Login to your server using ssh
`vi .ssh/authorized_keys` and paste your local machine rsa key
save and exit
Step 10 : cap -T ## list out all the task
step 11: cap production deploy:check
It will throw an error because database.yml file is not there
For that vi /public_html/test/shared/config/database.yml
development:
adapter: postgresql
database: testdb_cap
pool: 5
timeout: 5000
save and exit
Do again cap production deploy:check
This time would not throw any error
Step 12:
cap production deploy
And That's it
Check this also ruby rake task after deploymewnt
Related
It's 2 days what I am trying to install Capistrano 3 for a Rails 4 app, my hairs already turned grey...
The problem is that I am unable to deploy the code to server (Ubuntu 14, nginx, running on DigitalOcean).
Here's my config:
Gemfile:
gem 'capistrano', '~> 3.1.0'
gem 'capistrano-rails', '~> 1.1.0'
gem 'capistrano-bundler'
gem 'unicorn'
Capfile:
require 'capistrano/setup'
require 'capistrano/deploy'
require 'capistrano/bundler'
require 'capistrano/rails/assets'
require 'capistrano/rails/migrations'
# Load 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:
set :application, 'project'
set :deploy_user, 'deployer'
set :scm, :git
set :repo_url, 'git#bitbucket.org:username/project.git"'
set :pty, true
set :use_sudo, false
set :keep_releases, 5
set :linked_files, %w{config/database.yml}
set :linked_dirs, %w{bin log tmp/pids tmp/cache tmp/sockets vendor/bundle public/system}
set(:symlinks, [
{
source: "nginx.conf",
link: "/etc/nginx/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)}"
},
{
source: "monit",
link: "/etc/monit/conf.d/#{fetch(:full_app_name)}.conf"
}
])
namespace :deploy do
before :deploy, "deploy:check_revision"
before :deploy, "deploy:run_tests"
#after 'deploy:symlink:shared', 'deploy:compile_assets_locally'
after :finishing, 'deploy:cleanup'
before 'deploy:setup_config', 'nginx:remove_default_vhost'
after 'deploy:setup_config', 'nginx:reload'
after 'deploy:setup_config', 'monit:restart'
after 'deploy:publishing', 'deploy:restart'
end
and config/deploy/production.rb:
set :stage, :production
set :branch, "master"
set :full_app_name, "#{fetch(:application)}_#{fetch(:stage)}"
set :server_name, "IP"
server 'IP', user: 'deployer', roles: %w{web app db}, primary: true
set :deploy_to, "/home/#{fetch(:deploy_user)}/apps/#{fetch(:full_app_name)}"
set :rails_env, :production
set :unicorn_worker_count, 5
set :enable_ssl, false
When I am trying to set up capistrano and run cap production deploy:setup_config, I get this output:
/Users/adam/.rvm/gems/ruby-2.0.0-p481/gems/bundler-1.6.5/lib/bundler.rb:301: warning: Insecure world writable dir /usr/local/mysql-5.6.13-osx10.7-x86_64 in PATH, mode 040777
DEBUG [d940e414] Running /usr/bin/env [ -f /etc/nginx/sites-enabled/default ] as deployer#IP
DEBUG [d940e414] Command: [ -f /etc/nginx/sites-enabled/default ]
Text will be echoed in the clear. Please install the HighLine or Termios libraries to suppress echoed text.
deployer#188.166.53.247's password: # HERE I PUT MY PASSWORD, BUT WHEN I TYPE IT, I SEE WHAT I TYPE (THE PASSWORD IS NOT HIDDEN)
DEBUG [d940e414] Finished in 28.324 seconds with exit status 0 (successful).
INFO [cba64b02] Running /usr/bin/env sudo rm /etc/nginx/sites-enabled/default as deployer#IP
DEBUG [cba64b02] Command: /usr/bin/env sudo rm /etc/nginx/sites-enabled/default
DEBUG [cba64b02] [sudo] password for deployer:
And here it stuck - nothing happened. No matter what I do (if I type something/password) -- it stuck here for over 30 minutes.
Could I ask you to help me out with this issue, guys?
Thank you very much in advance, I am not sure how to continue here.
It appears the nginx:remove_default_vhost task you have configured to run before deploy:setup_config is trying to execute rm with sudo. Capistrano 3 requires passwordless sudo to be setup, otherwise you get the symptom you have described. See the authorization section of the documentation.
Setting up the deployer user to execute rm with sudo without a password should get things moving, but before you do that I think it's worth asking: do you really want your deployer user to be able to remove arbitrary files as root without a password? It's not clear to me what the origin of that task is, but IMHO that is more a part of privisioning than of deployment and there are other tools more suited for provisioning than capistrano (chef, puppet, etc.). Or just SSH to your server and remove it. It only needs to be done once.
If you do decide to let your deployer user execute rm with sudo without a password, remember to use visudo and do not edit /etc/sudoers directly.
I am seeing this error when i am trying to deploy my rails app to production
SSHKit::Runner::ExecuteError: Exception while executing on host xxx.xxx.xxx.xx: rake exit status: 1
rake stdout: Nothing written
rake stderr: rake aborted!
NoMethodError: undefined method `fetch' for "QLite version 3.x":String
This error is invoked by th command
Command: cd /home/deploy/myapp/releases/20140616034148 && ( RAILS_ENV=production ~/.rvm/bin/rvm default do bundle exec rake assets:precompile )
capfile
# Load DSL and Setup Up Stages
require 'capistrano/setup'
# Includes default deployment tasks
require 'capistrano/deploy'
require 'capistrano/bundler'
require 'capistrano/rails'
require 'capistrano/rvm'
set :rvm_ruby_version, '2.1.2'
require 'capistrano/rails/assets'
require 'capistrano/rails/migrations'
deploy.rb
lock '3.1.0'
set :application, 'myapp'
set :repo_url, 'git#bitbucket.org:username/myapp.git'
set :deploy_to, '/home/deploy/myapp'
set :branch, "master"
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
# Your restart mechanism here, for example:
execute :touch, release_path.join('tmp/restart.txt')
end
end
production.rb
set :stage, :production
role :app, %w{deploy#xxx.xxx.xxx.xx}
role :web, %w{deploy#xxx.xxx.xxx.xx}
role :db, %w{deploy#xxx.xxx.xxx.xx}
set :password, ask('Server password', nil)
server '107.170.187.98', user: 'deploy', password: fetch(:password), roles: %w{web app}
set :bundle_env_variables, { 'NOKOGIRI_USE_SYSTEM_LIBRARIES' => 1 }
I dont understand what is causing this issue. Can someone plz point me out in right direction.
Check the config/database.yml file you've set up on the production server. Its first line is probably "QLite version 3.x" - this is because it used to be "# SQLite version 3.x", but the initial "# S" has gone missing.
To fix this, just add the "# S" back to the front of it, or delete that line altogether. Once you've done that, your Capistrano deploy should start working again.
I'm guessing that you pasted your config/database.yml file from elsewhere into a Vim instance; I hit exactly the same problem as you did for exactly the same reason. If you don't put Vim into insert mode before you paste, it will paste the entire thing, but will drop the "# S" from the front.
The reason why these three in particular are deleted is because these letters are actually Vim commands. If you aren't in Insert mode, when you paste something, those commands will run. Those three characters, when pasted, do the following in sequence:
# - Moves the cursor to the previous occurrence of the last search. Since we didn't search for anything, nothing happens.
space - Moves the cursor to the right once. Since there's no content in the editor, nothing happens.
S - Deletes the number of lines there are before the S. Since there are no numbers before the S, nothing gets deleted. Once this is done, we enter Insert mode, and the rest of the document is pasted.
Source: http://vimdoc.sourceforge.net/htmldoc/
I'm trying to set up Capistrano deployment for the first time and I wanted to test it on my development directory before attempting it on production. Normally I wouldn't even bother with Capistrano in the dev environment, but I'm having issues when deploying. It seems that Capistrano wants to:
A) run the bundle command: bundle --without development test
and
B) run rake assets:precompile in the development environment. I don't want that. Why would I? Maybe in the 'staging' environment if I ever wanted to do that, but certainly not in development mode.
The biggest hurdle at the moment is the fact that it bundles thinking it's in production mode and thus skips over gems which are required when it DOES correctly use the development environment when precompiling assets.
EDIT: Here's a sample of two scripts which are run - the first scripts run bundler as if we're in the production environment, and the last runs it in development environment (RAILS_ENV=development). We of course get the error because BetterErrors is a gem that's loaded only in the development environment and so it can't find BetterErrors because the call to bundle was in the production environment.
INFO [9797fc64] Running ~/.rvm/bin/rvm default do bundle install --binstubs /home/vps_user/rails_deployments/dev.www/shared/bin --path /home/vps_user/rails_deployments/dev.www/shared/bundle --without development test --deployment --quiet on localhost
DEBUG [9797fc64] Command: cd /home/vps_user/rails_deployments/dev.www/releases/20140217224858 && ~/.rvm/bin/rvm default do bundle install --binstubs /home/vps_user/rails_deployments/dev.www/shared/bin --path /home/vps_user/rails_deployments/dev.www/shared/bundle --without development test --deployment --quiet
INFO [9797fc64] Finished in 1.883 seconds with exit status 0 (successful).
DEBUG [da905ff7] Running /usr/bin/env if test ! -d /home/vps_user/rails_deployments/dev.www/releases/20140217224858; then echo "Directory does not exist '/home/vps_user/rails_deployments/dev.www/releases/20140217224858'" 1>&2; false; fi on localhost
DEBUG [da905ff7] Command: if test ! -d /home/sprvps_userucewo/rails_deployments/dev.www/releases/20140217224858; then echo "Directory does not exist '/home/vps_user/rails_deployments/dev.www/releases/20140217224858'" 1>&2; false; fi
DEBUG [da905ff7] Finished in 0.044 seconds with exit status 0 (successful).
INFO [0562438c] Running ~/.rvm/bin/rvm default do bundle exec rake assets:precompile on localhost
DEBUG [0562438c] Command: cd /home/vps_user/rails_deployments/dev.www/releases/20140217224858 && ( RAILS_ENV=development ~/.rvm/bin/rvm default do bundle exec rake assets:precompile )
DEBUG [0562438c] rake aborted!
DEBUG [0562438c] uninitialized constant BetterErrors
Is there something wrong with my setup? I'm using Capistrano v3+, with Ruby v2.1.0 inside rvm.
Gemfile:
if RUBY_PLATFORM !~ /mingw/
gem 'capistrano-rails'
gem 'capistrano-rvm'
gem 'capistrano-bundler'
end
Capfile:
# Load DSL and Setup Up Stages
require 'capistrano/setup'
# Includes default deployment tasks
require 'capistrano/deploy'
require 'capistrano/rails'
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 }
deploy.rb:
# config valid only for Capistrano 3.1
lock '3.1.0'
set :application, 'website'
set :repo_url, 'git#bitbucket.org:MyUserName/website.git'
set :user, 'vps_user'
set :tmp_dir, '/home/vps_user/tmp'
# Default value for keep_releases is 5
set :keep_releases, 3
SSHKit.config.command_map[:rake] = "bundle exec rake"
SSHKit.config.command_map[:rails] = "bundle exec rails"
# Common directories (usually assets)
set :linked_dirs, %w{ public/assets/emails public/assets/events public/assets/photographs public/assets/updates public/assets/video public/assets/wines }
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
deploy/development.rb
set :branch, :develop
set :stage, :development
set :rails_env, 'development'
set :deploy_to, '/home/vps_user/rails_deployments/dev.www'
server 'localhost', user: 'vps_user', roles: %w{web app}
deploy/production.rb
set :branch, :master
set :stage, :production
set :rails_env, 'production'
set :deploy_to, '/home/vps_user/rails_deployments/www'
server 'localhost', user: 'vps_user', roles: %w{web app}
And I run the deploy command as such: bundle exec cap development deploy
OK so looking at Capistrano::Bundler under 'Usage' I saw an option in there called :bundle_without and it looked promising. So I put set :bundle_without, 'production' inside of my development.rb deploy script and it worked!
This doesn't solve the issue that Capistrano keeps trying to generate precompiled assets but I'm sure there's a solution out there like overriding the rake task or something.
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 deployed my rails 3 app onto the production server using Capistrano. The deployment is correct, the application server is correctly restarted but when I log onto the production server and go into "my_path/current", I cannot run "rails c":
The program 'rails' is currently not installed. You can install it by typing:
sudo apt-get install rails
I checked the gem list and everything seems to be correctly installed though.
My config/deploy.rb file is:
require "bundler/capistrano"
# Add RVM's lib directory to the load path.
$:.unshift(File.expand_path('./lib', ENV['rvm_path']))
# Load RVM's capistrano plugin.
require "rvm/capistrano"
set :rvm_ruby_string, '1.9.3'
set :rvm_type, :user # Don't use system-wide RVM
set :application, "MyApp"
set :repository, "git#github.com:user/myapp.git"
set :scm, :git
set :branch, "master"
set :scm_verbose, true
set :scm_username, "user" # GITHUB user name
set :user, "user" # VPS user name
set :scm_passphrase, "pass" # VPS password
set :deploy_to, "/home/luc/be"
set :use_sudo, false
server "my_server", :web, :app, :db, :primary => true
# start / stop application server
namespace :deploy do
task :start do
invoke_command "cd /home/luc/be/current; bundle exec thin start -C config/thin.config"
end
task :stop do
invoke_command "cd /home/luc/be/current; bundle exec thin stop -C config/thin.config"
end
task :restart do
invoke_command "cd /home/luc/be/current; bundle exec thin restart -C config/thin.config"
end
end
Try bundle exec rails c. Also check what error, if any, is thrown when you visit the page in your browser