Capistrano deploy code from bitbucket repository gets "authorization failed" - mercurial

I'm trying to deploy code to an Amazon EC2 instance using Capistrano, a private bitbucket repository, and mercurial.
I successfully ran cap deploy:setup from my development box.
Then I tried to run cap deploy:cold. I was prompted to enter the bitbucket password, which I did correctly, but I got "authorization failed" error message. As a result, the machine to which I was trying to deploy the code could not "clone" the code from the bitbucket repository.
This is what happened in details:
% cap deploy:cold
triggering load callbacks
* 2013-01-10 18:44:07 executing 'production'
triggering start callbacks for 'deploy:cold'
* 2013-01-10 18:44:07 executing 'multistage:ensure'
* 2013-01-10 18:44:07 executing 'deploy:cold'
* 2013-01-10 18:44:07 executing 'deploy:update'
** transaction: start
* 2013-01-10 18:44:07 executing 'deploy:update_code'
updating the cached checkout on all servers
executing locally: "hg log --verbose -r default --template \"{node|short}\""
command finished in 797ms
* executing "if -d /home/ec2-user/my_project/shared/cached-copy ; then hg pull --verbose --repository /home/ec2-user/my_project/shared/cached-copy https://bitbucket_user_id#bitbucket.org/bitbucket_user_id/my_project && hg update --verbose --repository /home/ec2-user/my_project/shared/cached-copy --clean 2ec0193d60ff; else hg clone --verbose --noupdate https://bitbucket_user_id#bitbucket.org/bitbucket_user_id/my_project /home/ec2-user/my_project/shared/cached-copy && hg update --verbose --repository /home/ec2-user/my_project/shared/cached-copy --clean 2ec0193d60ff; fi"
servers: "ec2-123-456-78-999.compute-1.amazonaws.com"
ec2-123-456-78-999.compute-1.amazonaws.com executing command
** ec2-123-456-78-999.compute-1.amazonaws.com :: out http authorization required
** ec2-123-456-78-999.compute-1.amazonaws.com :: out realm: Bitbucket.org HTTP
** ec2-123-456-78-999.compute-1.amazonaws.com :: out user: bitbucket_user_id
** ec2-123-456-78-999.compute-1.amazonaws.com :: out password:
hg password:
** ec2-123-456-78-999.compute-1.amazonaws.com :: out
** ec2-123-456-78-999.compute-1.amazonaws.com :: out abort: authorization failed
command finished in 5410ms
*** deploy:update_code rolling back
* executing "rm -rf /home/ec2-user/my_project/releases/20130110184415; true"
servers: "ec2-123-456-78-999.compute-1.amazonaws.com"
ec2-123-456-78-999.compute-1.amazonaws.com executing command
command finished in 431ms
failed: "sh -c 'if -d /home/ec2-user/my_project/shared/cached-copy ; then hg pull --verbose --repository /home/ec2-user/my_project/shared/cached-copy https://bitbucket_user_id#bitbucket.org/bitbucket_user_id/my_project && hg update --verbose --repository /home/ec2-user/my_project/shared/cached-copy --clean 2ec0193d60ff; else hg clone --verbose --noupdate https://bitbucket_user_id#bitbucket.org/bitbucket_user_id/my_project /home/ec2-user/my_project/shared/cached-copy && hg update --verbose --repository /home/ec2-user/my_project/shared/cached-copy --clean 2ec0193d60ff; fi'" on ec2-123-456-78-999.compute-1.amazonaws.com
This is my Capistrano deploy.rb file
set :application, "my_project"
set :repository, "https://bitbucket_user_id#bitbucket.org/bitbucket_user_id/my_project"
set :keep_releases, 4
set :scm, :mercurial
set :scm_username, "bitbucket_user_id"
default_run_options:pty = true
set :scm_prefer_prompt, :true
set :scm_verbose, :true
set :deploy_to, "/home/ec2-user/#{application}"
set :deploy_via, :remote_cache
set :user, "ec2-user"
set :use_sudo, false
namespace :deploy do
desc "Gracefully restarting unicorn"
task :restart, :roles => :app do
run "sudo /etc/init.d/start_script upgrade"
end
task :start, :roles => :app do
run "sudo /etc/init.d/start_script start"
end
task :stop, :roles => :app do
run "sudo /etc/init.d/start_script stop"
end
task :link, :roles => :app do
run "mkdir -p #{shared_path}/images"
run "ln -nfs #{shared_path}/images #{release_path}/tmp/images"
end
end
I tried this manually on the command line
ssh ec2-user#ec2-123-456-78-999.compute-1.amazonaws.com "hg clone --verbose --noupdate https://bitbucket_user_id#bitbucket.org/bitbucket_user_id/my_project /home/ec2-user/my_project/shared/cached-copy"
and I got (no prompt for bitbucket password)
abort: http authorization required
I tried
ssh -t ec2-user#ec2-123-456-78-999.compute-1.amazonaws.com "hg clone --verbose --noupdate https://bitbucket_user_id#bitbucket.org/bitbucket_user_id/my_project /home/ec2-user/my_project/shared/cached-copy"
and it worked.
I believe Capistrano is using ssh (with pseudo tty turned on by this line in my deploy.rb)
default_run_options[:pty] = true
So it should work, but why it isn't working with Capistrano?
Can someone help with this please?
Thanks much!

I was having a similar issue to this and solved it by looking at this question: Capistrano deploy failed after moving to bitbucket.org
An answerer there suggested that the person check that they have made deployment keys in BitBucket. In your BitBucket repo, click your settings wheel and in the left hand sidebar click "Deployment Keys": Use deployment keys to gain read-only access to this repository.
If you don't know how to set up SSH keys, read BitBucket's guide here: https://confluence.atlassian.com/pages/viewpage.action?pageId=270827678
Hope this helps!

Related

error: cannot open .git/FETCH_HEAD: Permission denied on gitlab-runner

I'm trying to pull code with gitlab-runner with a shell executor.
$ git pull origin master
error: cannot open .git/FETCH_HEAD: Permission denied
ERROR: Job failed: exit status 1
git status seems to work fine but when I try git fetch
$ git fetch
error: cannot open .git/FETCH_HEAD: Permission denied
ERROR: Job failed: exit status 1
This could be because you did not add a deploy key for your gitlab-runner user in your gitlab project, so the runner user does not have access to your gitlab repository.
You should follow theses steps to create the keypair for gitlab-runner user (the shell executor user) https://docs.gitlab.com/ee/ci/ssh_keys/#ssh-keys-when-using-the-shell-executor
Then copy the created public key in /home/gitlab-runner/.ssh/id_rsa.pub
And add it to your gitlab project's deploy key :
Go to you gitlab project
Settings => Repository
Deploy keys
Add a title, past the public key and click "Add key"
Trigger you CI again and the shell executor runner should be able to pull the repository.
I just solved this problem by running this command
sudo chown -R $(whoami) .git/ in my project directory.
You can change $(whoami) to the username of your server. For example sudo chown -R gitlab-runner .git/
Changing the permissions helped!
sudo chown -R .git/

Create a Mercurial alias that runs two commands and takes one paramater

I'm trying to create an alias and allows me to commit my changes and push all changesets for the current branch.
I'm running this from a Windows command prompt.
I've read this question and this question and so far have this:
ci-push = !hg ci -m $1 && hg push -b .
When I try this i get the error:
abort: Commit: The system cannot find the file specified
If I try:
ci-push = !hg ci -m %1 && hg push -b .
then it appears to work (prompts for auth and pushes the commit), but my commit message is:
%1
Is this even possible from a Windows cmd prompt?
On Windows, %USERPROFILE%\mercurial.ini:
[alias]
ll = log -l$1
Testing:
>hg ll
abort: too few arguments for command alias
> hg ll 5
changeset:...
Shell alias (%USERPROFILE%\mercurial.ini):
[alias]
ld = !hg log -r $1 && hg diff -r $1
Testing:
>hg ld 154
changeset: 154:5bb3aba44eab
....
diff -r 5bb3aba44eab ....
P.S.
When using $N with spaces you should use quotes (!hg ci -m "$1" ... in aliases).

.hg not found when performing [hg out]

I create a local repo with
local-host $ hg init ~/test/
and then in the remote host i do a clone
remote-host $ hg clone ssh://user#local-host/test
without any issues.
When im trying to check if there are outgoing changes in the remote repo im getting this error
remote-host $ cd test
remote-host $ hg --verbose out
comparing with ssh://user#local-host/test
running ssh user#local-host 'hg -R test serve --stdio'
searching for changes
no changes found
remote: abort: no repository found in '/home/user' (.hg not found)!
remote: abort: no repository found in '/home/user' (.hg not found)!
If i commit any change in the remote/local repository and push it im getting the error but the change gets pushed.
Both hosts have the same mercurial version.
Any ideas ?
You need to let it know which repository - easiest is:
remote-host $ cd ~/test/
remote-host $ hg --verbose out

Mercurial changeset hook exists with status -1

I am using Mercurial SCM over a LAN using a normal shared folder instead of HTTP and I'm having a problem getting the auto update hook to run.
I have entered this hook as detailed in the FAQ. This installs the hook, but when I push something to the remote repository, I get an error:
added 1 changesets with 1 changes to 1 files
running hook changegroup: hg update >&2
warning: changegroup hook exited with status -1
There is another stackoverflow question similar to this, but it offers no solutions other than it may be a permissions error somewhere.
Has anyone else had this problem and can anyone else shed any more light on this or give me a heads up on where to start fixing this? Thanks.
Is hg in your standard search PATH ?
Replace your .hgrc configuration with a custom script, e.g.
[hooks]
changegroup = /var/tmp/myscript.sh
[ui]
debug = true
(unix) In the /var/tmp/myscript.sh write something like this:
#!/bin/sh
set -e
echo ---------- >>/tmp/myscript.log
set >>/tmp/myscript.log
echo --- >>/tmp/myscript.log
pwd >>/tmp/myscript.log
hg update >>/tmp/myscript.log 2>&1
(unix) Do not forget to mark as executable: chmod a+x /var/tmp/myscript.sh
(windows) The corresponding batch file myscript.bat is:
#echo off
echo ------ >>%TEMP%\myscript.log
set >>%TEMP%\myscript.log
echo --- >>%TEMP%\myscript.log
cd >>%TEMP%\myscript.log
hg update >>%TEMP%\myscript.log 2>&1
(windows) Of course, the line in .hgrc is changegroup = \your\directory\myscript.bat.
Run your hg push command to reproduce the problem.
Check the content of the /tmp/myscript.log file.

"No such repository hgadmin" while installing mercurial-server.

I'm trying to install mercurial-server. After adding my keys to keys/root and refreshing auth, I tried to clone hgadmin-repo but I get the following error:
$ hg clone ssh://hg#<domain>/hgadmin
remote: mercurial-server: no such repository hgadmin
abort: no suitable response from remote hg!
Anyone know what's the problem?
I had this same problem and for me it was a problem with the installation of the hgadmin repository. When I installed the package, I got errors from python saying the mercurial package wasn't installed. I assume that happened when mercurial-server tried to initialize the hgadmin repository. So when I went to checkout the hgadmin respistory, there was no .hg directory:
root#myshost:/var/lib/mercurial-server/repos# cd hgadmin/
root#myshost:/var/lib/mercurial-server/repos/hgadmin# ls -a
. ..
In order to resolve this, I did:
easy_install mercurial
sudo apt-get purge mercurial-server
sudo rm -rf /var/lib/mercurial-server
sudo apt-get install mercurial-server
And then continued on with the directions here:
http://kurtgrandis.com/blog/2010/03/20/gitosis-for-mercurial/
Thanks a lot Randy for exposing the exact issue here.
I struggled with the same problem, and found an alternative approach to solving it (without the need to purge and re-install).
You can initialize the hgadmin repo manually and install the hooks, achieving the same effect as a normal installation. You need to to it as 'hg' user though.
Procedure
The commands worked for my environment (Ubuntu 10.04.4 / Hg 1.4.3)
First initialise a mercurial repository in /var/lib/mercurial-server/repos/hgadmin :
$ sudo su hg
$ cd ~/repos/hgadmin/
$ hg init
Then the only difference I found with a normally initialized hgadmin repo (that I deployed in a VM for comparison) were the hooks in .hg/hgrc file. So open the file :
$ vim .hg/hgrc
and paste this exact content :
# WARNING: when these hooks run they will entirely destroy and rewrite
# ~/.ssh/authorized_keys
[extensions]
hgext.purge =
[hooks]
changegroup.aaaab_update = hg update -C default > /dev/null
changegroup.aaaac_purge = hg purge --all > /dev/null
changegroup.refreshauth = python:mercurialserver.refreshauth.hook
Are you sure your clone command syntax is correct? I see at least two errors in it:
You must put the repo you're cloning (not just the destination)
Just as for push, you must use two slashes before hgadmin:
Example FAILING (missing the source repo and using only one '/' before 'home')
$ hg clone ssh://John#127.0.0.1/home/John/delme
Example FAILING (missing the source repo)
$ hg clone . ssh://John#127.0.0.1/home/John/delme
Example SUCCEEDING:
$ hg clone . ssh://John#127.0.0.1//home/John/delme