I've been looking at a way to deploy my app using capistrano. I currently am hosting a small private repo on github and a local server to try deploying my test. I come upon a problem and an error message below.
I have done the following
Generate an ssh key on server and added it sucessfully to deploy keys in repo and tested(git#github.com)
Generate an ssh key on client and add it sucessfully to deploy keys in repo
setup a private repository. And have account deployer with rights to deploy
configed deploy.rb and production rb to follow mimic many other templates out there.
I still cannnot figure why it is giving me an error like this
DEBUG [a5554d3d] Command: /usr/bin/env chmod +x /tmp/App/git-ssh.sh
INFO [a5554d3d] Finished in 0.020 seconds with exit status 0 (successful).
INFO [b1517df1] Running /usr/bin/env git ls-remote --heads git#github.com:aceofw
ings/App.git as deploy#192.168.1.84
DEBUG [b1517df1] Command: ( GIT_ASKPASS=/bin/echo GIT_SSH=/tmp/App/git-ssh
.sh /usr/bin/env git ls-remote --heads git#github.com:aceofwings/App.git )
DEBUG [b1517df1] Permission denied (publickey).
DEBUG [b1517df1] fatal: Could not read from remote repository.
DEBUG [b1517df1]
DEBUG [b1517df1] Please make sure you have the correct access rights
DEBUG [b1517df1] and the repository exists.
(Backtrace restricted to imported tasks)
cap aborted!
SSHKit::Runner::ExecuteError: Exception while executing as deploy#192.168.1.84:
git exit status: 128
git stdout: Nothing written
git stderr: Permission denied (publickey).
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
SSHKit::Command::Failed: git exit status: 128
git stdout: Nothing written
The Deploy.rb file
###############Deploy.rb##################
# config valid only for current version of Capistrano
lock '3.4.0'
set :repo_url, 'git#github.com:aceofwings/App.git'
set :application, 'App'
set :user, 'deploy'
#set :pty, true
# Default branch is :master
# ask :branch, `git rev-parse --abbrev-ref HEAD`.chomp
# Default value for linked_dirs is []
# set :linked_dirs, fetch(:linked_dirs, []).push('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
Production.rb
server '192.168.1.84', user: 'deploy', roles: %w{app db web}
#set :stage, :production
role :app, %w{deploy#192.168.1.84}
role :web, %w{deploy#192.168.1.84}
role :db, %w{deploy#192.168.1.84}
set :ssh_options, {
forward_agent: false,
auth_methods: %w(password),
password: 'Deploy4Real',
user: 'deploy'
}
I had a similar problem. It turns out that the SSH agent was not running. I found this out looking at the Capistrano documentation: http://capistranorb.com/documentation/getting-started/authentication-and-authorisation/
Running this command:
$ ssh-add -l
showed me that my public key was not added to the agent so I had to add it:
$ ssh-add
Identity added: /Users/me/.ssh/id_rsa (/Users/me/.ssh/id_rsa)
After this my Capistrano deployment worked.
You should set in production.rb:
forward_agent: true
I was having the same problem with Capistrano 3.4. With Capistrano 3.2.1 it seemed to ignore this parameter.
You can use capistrano-ssh-doctor to troubleshoot possible config problems.
I think your capistrano may have some old files in its git cache, probably from renaming the repository or something like that. Try to delete the cached-copy folder in the shared folder on the server, so capistrano will pull your repo again.
Related
I'm using Windows 10 and Cygwin, and I am setting up Capistrano 3 for deployment to my production environment. I believe I have everything set up correctly, but I can't figure out how to push my local repo to my production server. I don't have my repo on GitHub or another such site and would like to keep it local. I receive the following error message when I run cap production deploy.
SSHKit::Runner::ExecuteError: Exception while executing as
myappuser#myappname.website.com: git exit status: 128 git stdout:
Nothing written git stderr: fatal: No remote configured to list refs
from.
SSHKit::Command::Failed: git exit status: 128 git stdout: Nothing
written git stderr: fatal: No remote configured to list refs from.
Tasks: TOP => git:check (See full trace by running task with --trace)
The deploy has failed with an error: Exception while executing as
myappuser#myappname.website.com: git exit status : 128 git stdout:
Nothing written git stderr: fatal: No remote configured to list refs
from.
Here's my config\deploy.rb file:
# config valid only for current version of Capistrano
lock '3.4.0'
set :application, 'myappname
set :deploy_user, 'myappuser'
set :repo_url, "file:///C:/Users/me/Documents/repo/myappname.git"
# setup rvm.
set :rbenv_type, :user
set :rbenv_ruby, '2.1.5-p273'
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 :assets_roles, [:app]
# Default value for :scm is :git
set :scm, :git
# Default value for :linked_files is []
set :linked_files, %w{config/database.yml config/application.yml}
# Default value for linked_dirs is []
set :linked_dirs, %w{bin log tmp/pids tmp/cache tmp/sockets vendor/bundle public/system}
# Default value for keep_releases is 5
set :keep_releases, 5
# which config files should be copied by deploy:setup_config
# see documentation in lib/capistrano/tasks/setup_config.cap
# for details of operations
set(:config_files, %w(
nginx.conf
application.yml
database.yml
unicorn.rb
unicorn_init.sh
))
# which config files should be made executable after copying
# by deploy:setup_config
set(:executable_config_files, %w(
unicorn_init.sh
))
# files which need to be symlinked to other parts of the
# filesystem. For example nginx virtualhosts, log rotation
# init scripts etc. The full_app_name variable isn't
# available at this point so we use a custom template {{}}
# tag and then add it at run time.
set(:symlinks, [
{
source: "nginx.conf",
link: "/etc/nginx/sites-enabled/{{full_app_name}}"
},
{
source: "unicorn_init.sh",
link: "/etc/init.d/unicorn_{{full_app_name}}"
},
{
source: "log_rotation",
link: "/etc/logrotate.d/{{full_app_name}}"
}
])
namespace :deploy do
# compile assets locally then rsync
after :finishing, 'deploy:cleanup'
end
All of the answers I found didn't have a solution or were for Capistrano 2, so I am wondering if the options have changed. Any help would be greatly appreciated!
EDIT
I've made some progress, but it's still not reading my local repo. Here's the error message:
SSHKit::Command::Failed: git exit status: 128 git stdout: Nothing
written git stderr: fatal: '/C:/Users/me/Documents/repo/myappname.git'
does not appear to be a git repository fatal: Could not read from
remote repository.
Please make sure you have the correct access rights and the repository
exists.
I've tried all different path combinations for my repo. My git repo is at the following path: C:/Users/me/Documents/repo/myappname. Is there a specific way to reference my local repo?
Unfortunately, this is not how Capistrano is designed to work. The basic assumption Capistrano makes is that your project resides in a repository that the server can access (almost always this is a Git repo hosted at e.g. Bitbucket or GitHub).
Capistrano pulls your source code by running git commands on the server. The server has no way to see your local repository, which is why it doesn't work.
There are some plugins that try to change Capistrano's behavior to allow for the push style deployment you are looking for. Search GitHub for "capistrano copy" and try them out. This one could be what you're looking for:
https://github.com/ydkn/capistrano-git-copy
I have been using capistrano-scm-copy to do my deploys via SCP upload from my dev box to AWS. Note of caution - this is like running with scissors. It's nice to be able to do a quick hotfix deploy, verily it in production and the do git commit. But you need to be really careful to not forget the commit part.
I am modifying a functional Capistrano script trying to speed it up since my home internet upload speed is horrendous. I have a git server (not github), and a ubuntu dev server, when I run cap deploy it currently grabs the latest from my git repo and makes a local copy then uploads that to my ubuntu server and restarts passenger without an issue. My capistrano code contains...
set :deploy_via, :copy
But if I try to use this instead...
set :deploy_via, :remote_cache
I get this error...
** transaction: start
* ←[32m2014-03-13 08:43:36 executing `deploy:update_code'←[0m
updating the cached checkout on all servers
←[33mexecuting locally: "git ls-remote ssh://gitadmin#sub.example.com/opt/git/hub/app.git master"←[0m
gitadmin#sub.example.com's password:
←[2;37mcommand finished in 6880ms←[0m
* ←[33mexecuting "if [ -d /srv/www/app/shared/cached-copy ]; then cd /srv/www/app/shared/cached-copy && git fetch -q origin && git fetch --tags -q origin && git reset
-q --hard efe4a94f5a4f1354edb0f4b516e9ea1d627e5101 && git clean -q -d -x -f; else git clone -q -b master ssh://gitadmin#sub.example.com/opt/git/hub/app.git /s
rv/www/app/shared/cached-copy && cd /srv/www/app/shared/cached-copy && git checkout -q -b deploy efe4a94f5a4f1354edb0f4b516e9ea1d627e5101; fi"←[0m
servers: ["12.34.56.78"]
[12.34.56.78] executing command
** ←[31m[12.34.56.78 :: err] Permission denied, please try again.←[0m
** ←[31m[12.34.56.78 :: err] Permission denied, please try again.←[0m
** ←[31m[12.34.56.78 :: err] Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).←[0m
** [12.34.56.78 :: err] fatal: The remote end hung up unexpectedly
It prompts for the git password - gitadmin#sub.example.com's password:
which I enter and it proceeds ok, then it seems to error out on the next command above. I'm guessing I need to setup some ssh keys somehow? Again this is not hosted on github, the repo and dev server are two separate boxes on my company servers. Locally I am running windows 7. If I try adding...
set :ssh_options, { :forward_agent => true }
default_run_options[:pty] = true
I get the same distance but this time instead of automatically saying permission denied, it prompts for the gitadmin password, I enter it correctly and it says "permission denied".
It would help if you posted your full capistrano scipt (minus any passwords, ip addresses) but I suspect that you need to introduce your server to the remote repository.
You don't need deploy_via option at all
To introduce your server to your repo ssh into your server and from there ssh into the repository service, there should be documentation on the url to use for this. You would get a access denied message but the point is that this process adds ssh public key to your server
It might also be worth watching Ryan Bates Railscast on deploying, he uses github but the process is pretty similar for any remote repo
http://railscasts.com/episodes/335-deploying-to-a-vps
Pay attention to how he introduces his server to github, like I say, your service should provide you with instructions for similar introduction
There is a revised (pro) cast that you would need a subscription for but Ryan is on an extended break (possibly indefinitely right now) so a subscription would be extremely good value right now and well worth thinking about.
The revised (pro) cast on capstrano, how to set variables etc... is here http://railscasts.com/episodes/133-capistrano-tasks-revised
This is a working script for one of my apps just replace xxx and paths as you see fit, I assume you have all the start and restart stuff already set up but this should point you to at least the minimum settings needed. I say this because you possibly have other setting that you don't need, but as you haven't posted your script it is impossible to tell.
require "bundler/capistrano"
server "146.185.182.228", :web, :app, :db, primary: true
set :application, "xxx"
set :user, "xxxx"
# adjust if you are using RVM, remove if you are not
set :rvm_type, :user
set :rvm_ruby_string, 'ruby-2.0.0-p353'
set :ssh_options, {:forward_agent => true}
default_run_options[:pty] = true
# file paths
set :repository, "git#bitbucket.org:xxxx.git"
set :deploy_to, "/home/#{user}/apps/#{application}"
# set :scm, :git # You can set :scm explicitly or Capistrano will make an intelligent guess based on known version control directory names
# Or: `accurev`, `bzr`, `cvs`, `darcs`, `git`, `mercurial`, `perforce`, `subversion` or `none`
set :scm, :git
set :use_sudo, false
set :rails_env, :production
set :password, "xxxxxxx"
#role :web, "your web-server here" # Your HTTP server, Apache/etc
#role :app, "your app-server here" # This may be the same as your `Web` server
#role :db, "your primary db-server here", :primary => true # This is where Rails migrations will run
#role :db, "your slave db-server here"
# if you want to clean up old releases on each deploy uncomment this:
after "deploy:restart", "deploy:cleanup"
# 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
%w[start stop restart].each do |command|
desc "#{command} unicorn server"
task command, roles: :app, except: {no_release: true} do
run "cd #{release_path} && bundle install"
run "/etc/init.d/unicorn_#{application} #{command}"
run "#{sudo} service nginx #{command}"
end
end
task :setup_config, roles: :app do
sudo "ln -nfs #{current_path}/config/nginx.conf /etc/nginx/sites-enabled/#{application}"
sudo "ln -nfs #{current_path}/config/init_unicorn.sh /etc/init.d/unicorn_#{application}"
sudo "ln -nfs #{current_path}/config/sidekiq.conf /etc/init/sidekiq.conf"
run "mkdir -p #{shared_path}/config"
end
after "deploy:setup", "deploy:setup_config"
desc "Make sure local git is in sync with remote."
task :check_revision, roles: :web do
unless `git rev-parse HEAD` == `git rev-parse origin/master`
puts "WARNING: HEAD is not the same as origin/master"
puts "Run `git push` to sync changes."
exit
end
end
before "deploy", "deploy:check_revision"
after "deploy:update_code", "deploy:symlink_shared"
end
I'm using the Capistrano gem and this tutorial to deploy my application to my remote server. Everything works fine until the end of the cap deploy command. I'm receiving this error:
** [ps123456.dreamhostps.com :: out] sh: myapp.git/releases/20130916201449/REVISION: No such file or directory
command finished in 2266ms
*** [deploy:update_code] rolling back
* executing "rm -rf myapp.git/releases/20130916201449; true"
servers: ["ps123456.dreamhostps.com"]
[ps123456.dreamhostps.com] executing command
command finished in 254ms
failed: "sh -c 'git clone --depth 1 ssh://username#ps123456.dreamhostps.com/~/myapp.git myapp.git/releases/20130916201449 && cd myapp.git/releases/20130916201449 && git checkout -b deploy 497af4d996358f8d1f42dc9658e276ee8d9fa64f && git submodule init && git submodule sync && export GIT_RECURSIVE=$([ ! \"`git --version`\" \\< \"git version 1.6.5\" ] && echo --recursive) && git submodule update --init $GIT_RECURSIVE && rm -Rf myapp.git/releases/20130916201449/.git && (echo 497af4d996358f8d1f42dc9658e276ee8d9fa64f > myapp.git/releases/20130916201449/REVISION)'" on ps123456.dreamhostps.com
Here is my deploy.rb:
require 'bundler/capistrano'
set :user, 'username'
set :domain, 'ps123456.dreamhostps.com'
set :applicationdir, "myapp.git"
default_run_options[:pty] = true
set :scm, 'git'
set :repository, "ssh://username#ps123456.dreamhostps.com/~/myapp.git"
set :git_enable_submodules, 1 # if you have vendored rails
set :branch, 'master'
set :git_shallow_clone, 1
set :scm_verbose, true
# roles (servers)
role :web, domain
role :app, domain
role :db, domain, :primary => true
# deploy config
set :deploy_to, applicationdir
set :deploy_via, :export
# additional settings
default_run_options[:pty] = true # Forgo errors when deploying from windows
# Passenger
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
What is going wrong?
It's hard to say exactly, but it's clear that your releases/#{revision} dir is missing by the time you try to push the git revision into the file REVISION.
One likely issue that should probably get fixed is your :deploy_to dir is the same directory on the same machine as the git repo you are cloning from. Try this instead:
In your deploy.rb
set :deploy_to, "/home/#{user}/app/#{application}"
Then make sure to run this the first time:
cap deploy:setup
Then feel free to:
cap deploy
After a lot of head scratching, oddly enough, it was cap deploy:setup that was my downfall. I am still unsure as to what was going on, but I have thoroughly tested and verified that deleting the deployment directory and redeploying the application without running cap deploy:setup will work.
For some reason, running cap deploy:setup, even after successful deploys, will cause permission errors and causes cap deploy to be unable to create and write to directories.
Here's my Capistrano file:
set :application, "mydemoapp"
set :repository, "git#bitbucket.org:sergiotapia/mydemoapp.git"
set :scm, :git
set :user, "serg" # The user on the VPS server.
set :use_sudo, false
set :deploy_to, "/opt/nginx/html/#{application}"
set :deploy_via, :remote_cache
set :keep_releases, 1
set :rails_env, "production"
set :migrate_target, :latest
role :web, "192.168.1.1" # Your HTTP server, Apache/etc
role :app, "192.168.1.1" # This may be the same as your `Web` server
role :db, "192.168.1.1", :primary => true # This is where Rails migrations will run
The IP addresses above are obviously fake, I'm using the IP for my VPS
server in real life.
I ran this command, to verify that BitBucket is allowing me to access the repository:
sergiotapia#jarvis1:~/.ssh$ ssh -T hg#bitbucket.org
conq: logged in as sergiotapia.
You can use git or hg to connect to Bitbucket. Shell access is disabled.
However, running cap deploy again, I get:
[198.211.101.99] executing command
** [192.168.1.1:: err] Permission denied (publickey).
** [192.168.1.1:: err] fatal: The remote end hung up unexpectedly
command finished in 1026ms
*** [deploy:update_code] rolling back
* executing "rm -rf /opt/nginx/html/demoapp/releases/20130601025656; true"
I tried using git clone from my SSH terminal, manually and it worked fine as expected. My project cloned to a folder properly.
It seems Capistrano is running sudo git clone instead of git clone. This is verified by myself running a sudo git clone in the same folder manually on my SSH. Same Permission denied (publickey) error.
Why isn't my Capistrano setting for :use_sudo, false being used?
I'm a newbie, so apologies if this is a silly question, but can anyone help me decipher this deploy error? I've finally managed to get 'cap deploy:setup' and 'cap deploy:check' to run without errors. Now, I'm stuck at 'cap deploy:update'. Judging from the error output, it looks like there's a problem with my deploy file, but I have no idea what the problem might be. The shell output and deploy file are below. Any help would be very much appreciated!
Some background: I'm deploying to a friend's server which is running passenger. We're using ruby 1.9.2-p290, rails 3.1.3, capistrano 2.11.2, and MacOS 10.6.
Here's a sample of the deploy error output:
my-MacBook-Pro:projectfolder myuser$ cap deploy:update
* executing `deploy:update'
** transaction: start
* executing `deploy:update_code'
updating the cached checkout on all servers
executing locally: "git ls-remote ssh://git#server.example.ca/usr/local/git_root/arbiterapi.git master"
*** [deploy:update_code] rolling back
* executing "rm -rf /usr/local/www/sites/project.example.ca/releases/20120330191423; true"
servers: ["project.example.ca"]
[project.example.ca] executing command
[project.example.ca] rvm_path=/usr/local/rvm /usr/local/rvm/bin/rvm-shell 'ruby-1.9.2-p290#project' -c 'rm -rf /usr/local/www/sites/project.example.ca/releases/20120330191423; true'
command finished in 663ms
/Users/myuser/.rvm/gems/ruby-1.9.2-p290#project/gems/capistrano-2.11.2/lib/capistrano/recipes/deploy.rb:107:in ``': No such file or directory - git ls-remote ssh://git#server.example.ca/usr/local/git_root/project.git master (Errno::ENOENT)
from /Users/myuser/.rvm/gems/ruby-1.9.2-p290#project/gems/capistrano-2.11.2/lib/capistrano/recipes/deploy.rb:107:in `block in run_locally'
from /Users/myuser/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/benchmark.rb:310:in `realtime'
from /Users/myuser/.rvm/gems/ruby-1.9.2-p290#project/gems/capistrano-2.11.2/lib/capistrano/recipes/deploy.rb:106:in `run_locally'
from /Users/myuser/.rvm/gems/ruby-1.9.2-p290#project/gems/capistrano-2.11.2/lib/capistrano/recipes/deploy.rb:44:in `block (3 levels) in load'
from /Users/myuser/.rvm/gems/ruby-1.9.2-p290#project/gems/capistrano-2.11.2/lib/capistrano/recipes/deploy.rb:96:in `with_env'
from /Users/myuser/.rvm/gems/ruby-1.9.2-p290#project/gems/capistrano-2.11.2/lib/capistrano/recipes/deploy.rb:44:in `block (2 levels) in load'
from /Users/myuser/.rvm/gems/ruby-1.9.2-p290#project/gems/capistrano-2.11.2/lib/capistrano/recipes/deploy/scm/git.rb:227:in `query_revision'
... etc.
The rest of the error output points to the capistrano folder as well. The output seems too long to include in its entirety. But I can provide the rest if it might help!
Here's the deploy file:
$:.unshift(File.expand_path('./lib', ENV['rvm_path'])) # Add RVM's lib directory to the load path.
require "rvm/capistrano" # Load RVM's capistrano plugin.
set :application, "Project"
set :scm, "git"
set :repository, "ssh://git#server.example.ca/usr/local/git_root/project.git"
set :user, "deploy"
#set :rvm_bin_path, "/usr/local/rvm/bin"
set :rvm_ruby_string, "ruby-1.9.2-p290#project"
set :normalize_asset_timestamps, false
ssh_options[:forward_agent] = false
set :branch, "master"
set :deploy_via, :remote_cache
set :deploy_to, "/usr/local/www/sites/project.example.ca/"
set :use_sudo, false
set :domain, 'project.example.ca'
role :app, domain
role :web, domain
role :db, domain, :primary => true
The error is the line
No such file or directory - git ls-remote ssh://git#server.example.ca/usr/local/git_root/project.git master (Errno::ENOENT)
So it is failing at checkout out the repository. Why do you have the ssh prefix? When specifying a user (git#), you shouldn't have a protocol as well - the repository should just be set to
git#server.example.ca/usr/local/git_root/project.git
or whatever the actual URL is for git repository for the project.
FYI - neither deploy:setup nor deploy:check actually checkout the code, which is why those two work.