Capistrano & Git: Needed a single revision - ruby-on-rails

Capistrano and Git worked properly till I moved my site to another server. My first attempt to deploy went wrong because of an SSH-known-hosts related problem.
Even if I managed to solve this problem, another one raised and I can't deploy any more:
* executing `deploy'
* executing `deploy:update'
** transaction: start
* executing `deploy:update_code'
updating the cached checkout on all servers
executing locally: "git ls-remote collimarco#mywebsite.com:/home/collimarco/git/foto-fiori.git master"
* executing "if [ -d /home/collimarco/mywebsite.com/shared/cached-copy ]; then cd /home/collimarco/mywebsite.com/shared/cached-copy && git fetch origin && git reset --hard 7de27a6a1de7a94508f943596413e5e0f217f57e; else git clone --depth 1 collimarco#mywebsite.com:/home/collimarco/git/foto-fiori.git /home/collimarco/mywebsite.com/shared/cached-copy && cd /home/collimarco/mywebsite.com/shared/cached-copy && git checkout -b deploy 7de27a6a1de7a94508f943596413e5e0f217f57e; fi"
servers: ["mywebsite.com"]
[mywebsite.com] executing command
** [mywebsite.com :: out] * refusing to create funny ref 'remotes/origin/*' locally
** [mywebsite.com :: out] collimarco#mywebsite.com's password:
Password:
** [mywebsite.com :: out]
** [mywebsite.com :: out] fatal: Needed a single revision
command finished
*** [deploy:update_code] rolling back
* executing "rm -rf /home/collimarco/mywebsite.com/releases/20091027191027; true"
servers: ["mywebsite.com"]
[mywebsite.com] executing command
command finished
failed: "sh -c \"if [ -d /home/collimarco/mywebsite.com/shared/cached-copy ]; then cd /home/collimarco/mywebsite.com/shared/cached-copy && git fetch origin && git reset --hard 7de27a6a1de7a94508f943596413e5e0f217f57e; else git clone --depth 1 collimarco#mywebsite.com:/home/collimarco/git/foto-fiori.git /home/collimarco/mywebsite.com/shared/cached-copy && cd /home/collimarco/mywebsite.com/shared/cached-copy && git checkout -b deploy 7de27a6a1de7a94508f943596413e5e0f217f57e; fi\"" on mywebsite.com
My git repository seems intact: why do I get "Needed a single revision"?
Any suggestion is greatly appreciated.
Thanks in advance!
UPDATE: someone suggested to remove the remote cache, how do I do that?
UPDATE: I have renamed shared/cached-copy into shared/cached-copy-old.
Then I have created a shared/cached-copy empty folder.
Now I get "Not a git repository":
http://pastie.org/672254.txt

Check your git versions on the client and the server; I bet you need to update one of them to 1.6.

Related

Amazon EC2, deployment with capistrano, how to? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 5 years ago.
Improve this question
I am trying to deploy application to Amazon EC2 with capistrano.
When I run cap deploy:cold or cap deploy I get:
* 2014-08-27 13:23:39 executing `deploy:cold'
* 2014-08-27 13:23:39 executing `deploy:update'
** transaction: start
* 2014-08-27 13:23:39 executing `deploy:update_code'
executing locally: "git ls-remote git#bitbucket.org:username/repository.git HEAD"
command finished in 1929ms
* executing "git clone -q git#bitbucket.org:username/repository.git /var/www/repository/releases/20140827112340 && cd /var/www/repository/releases/20140827112340 && git checkout -q -b deploy 813ea45938eca0ee8bae9663d66592b917ecf4fb && (echo 813ea45938eca0ee8bae9663d66592b917ecf4fb > /var/www/repository/releases/20140827112340/REVISION)"
servers: ["54.XX.XX.XX"]
[54.XX.XX.XX] executing command
** [54.XX.XX.XX :: err] Permission denied (publickey).
** [54.XX.XX.XX :: err] fatal: Could not read from remote repository.
**
** Please make sure you have the correct access rights
** and the repository exists.
command finished in 505ms
*** [deploy:update_code] rolling back
* executing "rm -rf /var/www/repository/releases/20140827112340; true"
servers: ["54.XX.XX.XX"]
[54.XX.XX.XX] executing command
command finished in 246ms
failed: "sh -c 'git clone -q git#bitbucket.org:username/repository.git /var/www/repository/releases/20140827112340 && cd /var/www/repository/releases/20140827112340 && git checkout -q -b deploy 813ea45938eca0ee8bae9663d66592b917ecf4fb && (echo 813ea45938eca0ee8bae9663d66592b917ecf4fb > /var/www/repository/releases/20140827112340/REVISION)'" on 54.XX.XX.XX
My deploy.rb file:
set :application, "appname"
set :scm, :git
set :repository, "git#bitbucket.org:username/repository.git"
set :user, "ubuntu"
set :deploy_to, "/var/www/repository"
server "54.XX.XX.XX", :app, :primary => true
I have been following this tutorial.
I added SSH key on remote and placed it at in git account ssh-keys.
I have write access to this repository.
How to fix that?
Thanks

Capistrano destination path already exists, not an empty directory

While deploying a Rails app with Capistrano on Ubuntu 14.04, I am getting the following error:
fatal: destination path '/var/www/APP-NAME/repo' already exists and is not an empty directory.
cf5a389e] Running /usr/bin/env [ -f /var/www/rd/repo/HEAD ] on LINODE-INSTANCE-IP
DEBUG[cf5a389e] Command: [ -f /var/www/rd/repo/HEAD ]
DEBUG[cf5a389e] Finished in 0.005 seconds with exit status 1 (failed).
DEBUG[8899b95c] Running /usr/bin/env if test ! -d /var/www/rd; then echo "Directory does not exist '/var/www/rd'" 1>&2; false; fi on LINODE-INSTANCE-IP
DEBUG[8899b95c] Command: if test ! -d /var/www/rd; then echo "Directory does not exist '/var/www/rd'" 1>&2; false; fi
DEBUG[8899b95c] Finished in 0.005 seconds with exit status 0 (successful).
INFO[fc5f524b] Running /usr/bin/env git clone --mirror GIT_REPO_URL /var/www/APP-NAME/repo on LINODE-INSTANCE-IP
DEBUG[fc5f524b] Command: cd /var/www/APP-NAME && ( GIT_ASKPASS=/bin/echo GIT_SSH=/tmp/rd/git-ssh.sh /usr/bin/env git clone --mirror GIT-REPO-URL /var/www/APP-NAME/repo )
DEBUG[fc5f524b] fatal: destination path '/var/www/APP-NAME/repo' already exists and is not an empty directory.
Here are config files:
config/deploy/production.rb
config/deploy.rb
The only reason for this error I can find online is;
same host in more than one role, so that they're racing? For example I mean that you might have the same IP address defined as an :app role host more than once.
Which I guess isn't fitting with the above config files.
I had the same problem. The reason is in double definition of the role and/or server.
Try to remove
server 'SERVER-IP', user: 'USERNAME', roles: %w{app}
in production.rb and
role :app, "SERVER-IP"
in deploy.rb. The latter seems to be just simple syntax while the former - is extended one, so you in fact you declare roles twice (three time to be more precise: 2 in production.rb and 1 in deploy.rb). Hope it helps.
On a related note, I ran into this issue when changing my DB connections file setup on one of my deploys.
In this case, the old structure had one shared file for DB settings, while the newer had two. Even though these were declared in the recipe, I was getting errors when automated deletion of older builds were being done.
To resolve, I just deleted the older builds, and ran the cap [server name] deploy a few times to clear it out, and to verify this was no longer occurring. So far, it's been fine.

Crontab in Amazon Elastic Beanstalk

I am doing a cron tab in AWS - Elastic Beanstalk with Ruby on Rails 3, but I don't know what is wrong.
I have this code in my .ebextensions/default.config
container_commands:
01remove_old_cron_jobs:
command: "crontab -r || exit 0"
02send_test_email:
command: crontab */2 * * * * rake send_email:test
leader_only: true
I receive this error:
Failed on instance with return code: 1 Output: Error occurred during build: Command 02send_test_email failed .
UPDATE 1
I tried next:
crontab.txt
*/2 * * * * rake send_email:test > /dev/null 2>&1
default.config
02_crontab:
command: "cat .ebextensions/crontab.txt | crontab"
leader_only: true
RESULT: No errors, but it does not work.
UPDATE 2
crontab.sh
crontab -l > /tmp/cronjob
#CRONJOB RULES
echo "*/2 * * * * /usr/bin/wget http://localhost/crontabs/send_test_email > /dev/null 2>&1" >> /tmp/cronjob
#echo "*/2 * * * * rake send_email:test > /dev/null 2>&1" >> /tmp/cronjob
crontab /tmp/cronjob
rm /tmp/cronjob
echo 'Script successful executed, crontab updated.'
default.config
02_crontab:
command: "/bin/bash .ebextensions/crontab.sh"
leader_only: true
RESULT: Works with url, but not with rake task.
Updated for 2018
In order to get this to work on the latest version of Elastic Beanstalk, I added the following to my .ebextensions:
.ebextensions/0005_cron.config
files:
"/etc/cron.d/mycron":
mode: "000644"
owner: root
group: root
content: |
56 11 * * * root . /opt/elasticbeanstalk/support/envvars && cd /var/app/current && /opt/rubies/ruby-2.3.4/bin/bundle exec /opt/rubies/ruby-2.3.4/bin/rake send_email:test >> /var/app/current/log/cron.log 2>&1
commands:
remove_old_cron:
command: "rm -f /etc/cron.d/*.bak"
How I got there:
There are four main issues to confront when trying to cron a rake task in AWS EB:
The first hurdle is making sure all of your EB and Rails environment variables are loaded. I beat my head against the wall a while on this one, but then I discovered this AWS forum post (login may be required). Running . /opt/elasticbeanstalk/support/envvars loads all of your environment variables.
Then we need to make sure we cd into the current app directory using cd /var/app/current.
Next we need to know where to find the bundle and rake executables. They are not installed in the normal bin directories, but are located in a directory specific to your ruby version. To find out where your executables are located, ssh into your EB server (eb ssh) and then type the following:
$ cd /var/app/current
$ which bundle
/opt/rubies/ruby-2.3.4/bin/bundle
$ which rake
/opt/rubies/ruby-2.3.4/bin/rake
You could probably guess the directory based on your ruby version, but the above commands will let you know for sure. Based on the above, your can build your rake command as:
/opt/rubies/ruby-2.3.4/bin/bundle exec /opt/rubies/ruby-2.3.4/bin/rake send_email:test
NOTE: If you update your ruby version, you will likely need to update your cron config as well. This is a little brittle. I'd recommend making a note in your README on this. Trust me, six months from now, you will forget.
The fourth thing to consider is logging. I'd recommend logging to the same location as your other rails logs. We do this by tacking on >> /var/app/current/log/cron.log 2>&1 to the end of our command string.
Putting all of this together leads to a cron command string of:
. /opt/elasticbeanstalk/support/envvars && cd /var/app/current && /opt/rubies/ruby-2.3.4/bin/bundle exec /opt/rubies/ruby-2.3.4/bin/rake send_email:test >> /var/app/current/log/cron.log 2>&1
Finally, I referenced the latest AWS documentation to build an .ebextensions config file for my cron command. The result was the .ebextensions/0005_cron.config file displayed at the top of this answer.
I am having the same issue. Though I figured out that the reason that rake task doesn't run correctly on eb is because of RACK_ENV, RAILS_ENV and BUNDLE_WITHOUT
Defaults of eb:
RACK_ENV: production
RAILS_ENV: production
BUNDLE_WITHOUT: test:development
When the cron runs rake task, it runs in development mode, and gives gem not found error, as gems grouped in development are not installed.
you can see this by changing your cron a bitfrom:
*/2 * * * * rake send_email:test > /dev/null 2>&1
to:
*/2 * * * * cd /var/app/current/ && /usr/bin/bundle exec /usr/bin/rake send_email:test > /tmp/cron_log 2>&1
and then checking the /tmp/cron_log file
To know the location of bundle and rake, run
which bundle
which rake
I tried setting RAILS_ENV in command in cron, but that didn't work aswell
One quick fix is to set
BUNDLE_WITHOUT to null
EDIT:
Finally I got it to work,
.ebextensions/.config
files:
"/tmp/cron_jobs" :
mode: "000777"
content: |
1 10 * * * cd /var/app/current/ && RACK_ENV=production rake some:task >> /var/app/current/log/cron_log 2>&1
encoding: plain
container_commands:
01_delete_cron_jobs:
command: "crontab -r -u webapp || exit 0"
02_add_cron_jobs:
command: "crontab /tmp/cron_jobs -u webapp"
leader_only: true
option_settings:
- option_name: RAILS_ENV
value: production
- option_name: RACK_ENV
value: production
Notice the '-u webapp' when removing and adding cron, this will run this cron under user webapp. The above will also run in production mode. And the output will be dumped in log/cron_log file.
If the above wont work then adding 'Bundle exec' before 'rake some:task' might work.
I've seen these used with separate files in .ebextensions, such as:
02send_test_email:
command: "cat .ebextensions/crontab | crontab"
leader_only: true
I haven't gotten around to it yet, but I took note of this along the way. Let us know if this works.
This stackoverflow post has much more information
After Update 1/2:
Cron doesn't know where rake is. Your application runs from /var/app/current, and you need to be running bundle exec rake from that directory.
Elastic beanstalk is horrible with logging errors, to get this right, ssh to the machine and experiment until you have the commands right, then put this back into your cron script. You can even try and re-run some of the eb scripts as found in the logs, then reverse that into your ebextensions files.

Capistrano deploy with gitosis and application server at the same server?

I build gitosis server and stage server on the same VPS server. Clone the repository from gitosis in my local machine or stage server are work well. But cap deploy in local machine always ask me input password as below, I have no idea which password is and I try every password all doesn't work.
And I know could copy local repository with deploy_via: copy, but I prefer build a gitosis server for the other projects.
Any ideas? thanks.
environment
gitosis and stage server ip: 106.187.xxx.xxx (mask some number for security reason)
log
* executing `deploy'
triggering before callbacks for `deploy'
* executing `check:revision'
* executing `deploy:update'
** transaction: start
* executing `deploy:update_code'
updating the cached checkout on all servers
executing locally: "git ls-remote gitosis#106.187.xxx.xxx:foo_project.git master"
command finished in 1105ms
* executing "if [ -d /home/deployer/apps/railsapp/shared/cached-copy ]; then cd /home/deployer/apps/railsapp/shared/cached-copy && git fetch -q origin && git fetch --tags -q origin && git reset -q --hard 07827de89355c5366c4511ee22fdd9c68a31b0be && git clean -q -d -x -f; else git clone -q gitosis#106.187.xxx.xxx:foo_project.git /home/deployer/apps/railsapp/shared/cached-copy && cd /home/deployer/apps/railsapp/shared/cached-copy && git checkout -q -b deploy 07827de89355c5366c4511ee22fdd9c68a31b0be; fi"
servers: ["106.187.xxx.xxx"]
[106.187.xxx.xxx] executing command
** [106.187.xxx.xxx :: out] Password:
Password:
** [106.187.xxx.xxx :: out]
** [106.187.xxx.xxx :: out] Password:
Password:
** [106.187.xxx.xxx :: out]
** [106.187.xxx.xxx :: out] Password:
Password:
** [106.187.xxx.xxx :: out]
** [106.187.xxx.xxx :: out] Permission denied (publickey,keyboard-interactive).
** [106.187.xxx.xxx :: out] fatal: The remote end hung up unexpectedly
deploy.rb
server "106.187.xxx.xxx", :web, :app, :db, primary: true
set :application, "railsapp"
set :user, "deployer"
set :local_user, "joshchang"
set :deploy_to, "/home/#{user}/apps/#{application}"
set :use_sudo, false
set :rails_env, "stage"
set :scm, "git"
set :repository, "gitosis#106.187.xxx.xxx:foo_project.git"
set :deploy_via, :remote_cache
set :branch, "master"
default_run_options[:pty] = true
ssh_options[:forward_agent] = true
Sorry, a little hard to understand the question, but there are two ways to use git within capistrano. The first is to grant the server direct access to the git repository; on GitHub, for example, you have an option to install "deploy keys" -- the public key(s) of the server(s) that need access. So check if gitosis has this option.
But before you do, consider the other approach, which is to pass the git authorization of the user doing the deploy, so when you deploy, you pull as yourself, rather than instructing the server to do so. There are pros and cons to each method, but I think the second method is much easier to manage in the long run.
To use the second method, the machine of the person deploying (running capistrano) needs to 1) have ssh-agent running, and 2) needs to use ssh-add to authorize ssh-agent to use your public key -- it's very secure, and once you have it set up, it's transparent.
When you use the second method, access to git will be the same as it is locally, so shouldn't prompt for password. Otherwise your settings in deploy.rb are fine as is.

Capistrano deploy incomplete

I want to deploy a local application to a production server with Capistrano. However something fails and I don't know how to fix it. The good news is that the /shared/cached-copy is fully created on the remote server. How can I fix the rsync error?
cap deploy
* executing `deploy'
* executing `deploy:update'
** transaction: start
* executing `deploy:update_code'
updating the cached checkout on all servers
executing locally: "git ls-remote git#bitbucket.org:foo/bar.git HEAD"
* executing "if [ -d /path/foo/shared/cached-copy ]; then cd /path/foo/shared/cached-copy && git fetch -q origin && git fetch --tags -q origin && git reset -q --hard e216d5f840f3d78458a3734a3830a4030750c85c && git clean -q -d -x -f; else git clone -q git#bitbucket.org:foo/bar.git /path/foo/shared/cached-copy && cd /path/foo/shared/cached-copy && git checkout -q -b deploy e216d5f840f3d78458a3734a3830a4030750c85c; fi"
servers: ["foo.com"]
[foo.com] executing command
command finished in 33589ms
copying the cached version to /path/foo/releases/20120204063129
* executing "rsync -lrpt --exclude=\".git\" --exclude=\".DS_Store\" --exclude=\".gitignore\" --exclude=\".gitmodules\" /path/foo/shared/cached-copy/ /path/foo/releases/20120204063129 && (echo e216d5f840f3d78458a3734a3830a4030750c85c > /path/foo/releases/20120204063129/REVISION)"
servers: ["foo.com"]
[foo.com] executing command
*** [err :: foo.com] rsync: mkdir "/path/foo/releases/20120204063129" failed: No such file or directory (2)
*** [err :: foo.com] rsync error: error in file IO (code 11) at main.c(587) [Receiver=3.0.8]
*** [err :: foo.com] rsync: connection unexpectedly closed (9 bytes received so far) [sender]
*** [err :: foo.com] rsync error: error in rsync protocol data stream (code 12) at io.c(601) [sender=3.0.8]
command finished in 899ms
*** [deploy:update_code] rolling back
* executing "rm -rf /path/foo/releases/20120204063129; true"
servers: ["foo.com"]
[foo.com] executing command
command finished in 841ms
failed: "sh -c 'rsync -lrpt --exclude=\".git\" --exclude=\".DS_Store\" --exclude=\".gitignore\" --exclude=\".gitmodules\" /path/foo/shared/cached-copy/ /path/foo/releases/20120204063129 && (echo e216d5f840f3d78458a3734a3830a4030750c85c > /path/foo/releases/20120204063129/REVISION)'" on foo.com
My deploy.rb file:
set :application, "FooBar"
set :repository, "git#bitbucket.org:foo/bar.git"
set :scm, :git
set :deploy_to, "/path/foo"
set :deploy_via, :remote_cache
set :copy_exclude, [".git", ".DS_Store", ".gitignore", ".gitmodules"]
set :user, "foobar"
server "foobar.tdl", :app
ssh_options[:forward_agent] = true
It looks like your first error is:
*** [err :: foo.com] rsync: mkdir "/path/foo/releases/20120204063129" failed: No such file or directory (2)
Does your /path/foo/releases directory already exist on the server? Try creating it.

Resources