I am writing one capistrano script to deploy a PHP application here is my basic script
set :default_stage, "staging"
ssh_options[:forward_agent] = true
server "capistrano.myserver.com", :app, :web, :db, :primary => true
set :application, "PHP_App_capified"
set :scm, :git
set :repository, '.'
default_run_options[:pty] = true
set :user, 'capi5784'
set :deploy_to, "/home/user/public_html"
set :deploy_via, :copy
set :use_sudo, false
set :copy_exclude, [".git", ".DS_Store", ".gitignore", ".gitmodules", "Capfile", "config/deploy.rb"]
when I run the cap deploy I am getting the following error
failed: "sh -c 'cd /home/myserver/public_html/releases && tar xzf /tmp/20130808120301.tar.gz && rm /tmp/20130808120301.tar.gz'" on capistrano.myserver.com
Any suggestions what I am doing wrong ?
Two things:
Can you ssh into capistrano.myserver.com from your machine?
I think you need to enable use_sudo.
set :use_sudo, true
You would need sudo access to delete something from /tmp
Related
I am trying to set up Capistrano to deploy a website on a remote server. When I run the following command:
cap production deploy
I get the following error:
cap aborted!
SSHKit::Runner::ExecuteError: Exception while executing on host domain.com: Authentication failed for user #domain.com
Here is my deploy.rb file in the config directory of my Rails app:
...
set :application, 'my-app'
set :repo_url, 'git#bitbucket.org:karns/my-app.git'
set :deploy_to, "/var/www/my-app"
# Default value for :scm is :git
set :scm, :git
set :branch, 'master'
set :user, "deploy"
set :use_sudo, false
set :rails_env, "production"
set :deploy_via, :copy
server "domain.com", roles: [:app, :web, :db], :primary => true
...
To my understanding, deploy is supposed to be the user on the remote server.
What am I missing? Why does the error say "Authentication failed for user #domain.com"? Why wouldn't it say "deploy#domain.com"? If you need any other code, please let me know.
The documentation seems a little sparse on what this means.
I'm trying to set up an app deployed by a multistage capistrano script.
EDIT: I am trying to deploy the same app, twice to the same server. The only real difference (other than the git branches) is I want to deploy each copy to a different folder. The first instance is a staging where I can test the app in the exact same environment as the second instance, which is a production instance. Is capistrano able to do this?
I ran the staging deployment without any issues. However, when I run any tasks specifying my production stage, (such as deploy:setup, in this case) I receive the following error:
`deploy:setup' is only run for servers matching {:except=>{:no_release=>true}}, but no servers matched
Here's my Deploy.rb
require "rvm/capistrano"
require "bundler/capistrano"
require "capistrano/ext/multistage"
#server "direct.measuremyho.me", :web, :app, :db, primary: true
set :stages, %w{staging production} # Set staging and production environment
set :default_stage, "staging" # Use staging environment as the default one to prevent accidentally deploying to production
set :application, "mmh"
set :user, "mmh"
set :deploy_via, :remote_cache
#set :deploy_to, "/var/www/#{application}"
set :use_sudo, false
set :keep_releases, 3
set :scm, "git"
set :repository, "git#localhost:#{application}.git"
set :local_repository, "git#direct.measuremyho.me:#{application}.git"
#set :branch, "master"
default_run_options[:pty] = true
ssh_options[:forward_agent] = true
after "deploy:update_code", "deploy:migrate"
after "deploy", "deploy:cleanup"
namespace :deploy do
%w[start stop].each do |command|
desc "#{command} nginx server"
task command, roles: :app, except: {no_release: true} do
sudo "#{try_sudo} service nginx #{command}"
end
end
desc "restart passenger server"
task :restart, roles: :app, except: { no_release: true } do
run "#{try_sudo} touch #{File.join(current_path,'tmp','restart.txt')}"
end
task :setup_config, roles: :app do
# link the nginx config file in the app
# sudo "ln -nfs #{current_path}/config/nginx.conf "\
# "/opt/nginx/conf/nginx.conf"
# make the shared rails config directory
run "mkdir -p #{shared_path}/config"
# ftp the database.yml file to that directory
put File.read("config/database.yml"), "#{shared_path}/config/database.yml"
# make the shared uploads directory
run "mkdir -p #{shared_path}/uploads"
# tell the user to edit database.yml
puts "==> IMPORTANT!!! Now edit database.yml in "\
"#{shared_path}/config <==="
end
after "deploy:setup", "deploy:setup_config"
task :symlink_config, roles: :app do
run "ln -nfs #{shared_path}/config/database.yml #{release_path}/config/database.yml"
run "rm -rf #{release_path}/public/uploads"
run "ln -nfs #{shared_path}/uploads #{release_path}/public/"
end
after "deploy:finalize_update", "deploy:symlink_config"
end
My staging.rb
set :application_directory, "staging"
set :rails_env, "staging"
set :main_server, 'direct.measuremyho.me'
set :branch do
default_tag = `git tag`.split("\n").last
tag = Capistrano::CLI.ui.ask "Tag to deploy (make sure to push the tag first): [#{default_tag}] "
tag = default_tag if tag.empty?
branch = "release/#{tag}"
branch
end
# Do not modify
# Set up the server
server "#{main_server}", :web, :app, :db, :primary => true
set :deploy_to, "/var/www/mmh/#{application_directory}"
My production.rb
set :application_directory, "production"
set :rails_env, "production"
set :main_server, 'direct.measuremyho.me'
set :branch, "master"
# Do not modify
# Set up the server
server "#{main_server}", :web, :app, :db, :primary => true
set :deploy_to, "/var/www/mmh/#{application_directory}"
My question is, why is this being set? Additionally, what can I do to avoid setting this variable so I can use the deploy commands.
Let me know if I've missed any pertinent information.
I have solved the problem for now by replacing the following line:
server "#{main_server}", :web, :app, :db, :primary => true
with:
server "#{main_server}", :web, :app, :db, :primary => true, :no_release => false
in my production.rb file.
However, this is a hacky solution, and I'd like to understand how I should properly deploy a rails app twice to the same server, for staging and production purposes. Or alternatively, why I should not do this, and what the alternatives are. So, I have left the question unanswered.
I am attempting to expand on this helpful answer by providing a tutorial that includes its steps, as well as setting up a self-hosted git repository instead of github and deployment scripts for staging and production versions of the app on the same server. So this question answers a pivotal piece of this process. I'm trying to get an idea of what the best practices are for a situation like this.
Comments welcomed; I'll add them to this answer.
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.
My working deployment script is failing with upload via sftp failed on XX.XXX.XXX.XXX: Net::SFTP::StatusException (Net::SFTP::StatusException write /tmp/20130610114941.tar.gz (4, "failure")) error.
here is my deployment script
require "rvm/capistrano"
require "bundler/capistrano"
set :rvm_type, :user
set :rvm_ruby_string, 'ruby-2.0.0-p0'
set :user, "deployer"
set :application, "myapp"
set :deploy_to, "/home/#{user}/apps/#{application}"
set :bundle_dir, "/home/#{user}/.rvm/gems/ruby-2.0.0-p0"
server "XX.XXX.XXX.XXX", :web, :app, :db, primary: true
set :use_sudo, false
set :deploy_via, :copy
set :repository, "."
set :copy_exclude, %w[.git log tmp .DS_Store]
set :scm, :none
default_run_options[:pty] = true
after "deploy", "deploy:cleanup" # keep only the last 5 releases
namespace :deploy do
task :start do
;
end
task :stop do
;
end
task :restart, roles: :app, except: {no_release: true} do
run "touch #{deploy_to}/current/tmp/restart.txt"
end
end
Any idea?
Thanks
Edit: Just checked and my disk space is full. Obviously this is my problem. An after update hook to prevent this?
From the comments:
check available disk space on the server for /tmp
df /tmp
free up space if necessary
alternatively configure capistrano to use a different folder to deploy to (How do I change the temporary directory that Capistrano uses?)
you may want to look at removing old releases if you are not already doing so
cap deploy:cleanup
i cant get capistrano to fully deploy my rails app to my dreamhost VPS..
as far as i can tell its halting at creating the release directory..
$ cap deploy
i get
failed: "sh -c 'cd /home/gasloggr/gasloggr.com/releases/20120824064241 && bundle install --gemfile /home/gasloggr/gasloggr.com/releases/20120824064241/Gemfile --path /home/gasloggr/gasloggr.com/shared/bundle --deployment --quiet --without development test'" on gasloggr.com
for troubleshooting purposes i ran what was in quotes on the server itself and i received..
bash: cd: /home/gasloggr/gasloggr.com/releases/20120824064241: No such file or directory
a quick ls -alh of the releases dir, and guess what... its empty.
My deploy.rb file
require 'bundler/capistrano'
default_run_options[:pty] = false
ssh_options[:forward_agent] = true
set :use_sudo, false
set :user, "gasloggr"
set :application, "gasloggr.com"
set :repository, "git#github.com:gorelative/GasLoggr.git"
set :scm, :git
set :branch, 'master'
set :git_shallow_clone, 1
set :deploy_via, :copy
set :copy_compression, :bz2
set :rails_env, 'production'
set :deploy_to, "/home/gasloggr/#{application}"
role :web, "#{application}" # Your HTTP server, Apache/etc
role :app, "#{application}" # This may be the same as your `Web` server
role :db, "#{application}", :primary => true # This is where Rails migrations will run
# role :db, "your slave db-server here"
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
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
i have tried removing :deploy_via as well as all of the below:
set :deploy_via, :remote_cache
set :deploy_via, :copy
Please try again following the methods outlined here: http://wiki.dreamhost.com/Capistrano
This specific part may be of the most help, mentioning cap deploy:setup : http://wiki.dreamhost.com/Capistrano#Deployment_with_Capistrano