Capistrano + Git + DreamHost - ruby-on-rails

I'm trying to deploy my rails application by using Passenger and Capistrano on Dreamhost. I'm using Git as a version control and we bought an account from GitHub.
I have installed all required gems, Passenger and Capistrano in my local machine and I have cloned the repository of my project from GitHub in my local machine as wel.
According to Dreamhost support, they have Passenger, Ruby, Rails and etc on their server as well.
I'm currently following this article http://github.com/guides/deploying-with-capistrano for my deployment.
The following is my deploy.rb.
default_run_options[:pty] = true
ssh_options[:forward_agent] = true
# be sure to change these
set :user, 'gituser'
set :domain, 'github.com'
set :application, 'MyProjectOnGit'
#git#github.com:MyProjectOnGit.git
# the rest should be good
set :repository, "git#github.com:MyProjectOnGit.git"
set :deploy_to, "/ruby.michaelsync.net/"
set :deploy_via, :remote_cache
set :scm, 'git'
set :branch, 'master'
set :git_shallow_clone, 1
set :scm_verbose, true
set :use_sudo, false
set :git_enable_submodules, 1
server domain, :app, :web
role :db, domain, :primary => true
set :ssh_options, { :forward_agent => true }
namespace :deploy do
task :restart do
run "touch #{current_path}/tmp/restart.txt"
end
end
When I run "cap deploy", I'm getting the error below.
[deploy:update_code] exception while rolling back: Capistrano::ConnectionError, connection failed for: github.com (Net::SSH::AuthenticationFailed: gituser) connection failed for: github.com (Net::SSH::AuthenticationFailed: gituser)
Thanks in advance..

don't worry, you'll get it working in the end, I used to use the very same setup as yourself.... i.e. Dreamhost/Passenger/Capistrano/Git (and at one time, SVN) - it can be quite frustrating
Some things for you to do:
1) Read the following two articles by John Nunemaker # railstips.com - I used to refer to them every single time I had to setup a server on Dreamhost (the second one is the most important but the first link gives you some tips that are well worth following)
1.1) http://railstips.org/blog/archives/2008/11/23/gitn-your-shared-host-on/
1.2) http://railstips.org/blog/archives/2008/12/14/deploying-rails-on-dreamhost-with-passenger/
2) I think github is complaining about "gituser" - you do appear to set your username to "gituser" in your capfile - i would change that to your own name
3) you've got your domain down as github.com - again, this should be your own domain name and not github.... From what I recall..
4) start using heroku
good luck - hope this helps, let us know if it does or not....
cheers

You use your private url to clone your repository. Try with public clone URL
git://github.com/Myproject.git

i did to do sudo ln -s /opt/ruby/bin/packet_worker_runner /usr/bin/packet_worker_runner” and it solved the problem..
Thanks.

Related

Capistrano Pointing to Old Release That No Longer Exists

I have altered this question quite a bit based on changing circumstances.
My deploy.rb file:
set :rails_env "production"
set :branch, "master"
set :keep_releases, 1
set :app_server, "ip address"
set :db_server, "ip address"
server app_server, :app, :web
role :db, db_server, :primary => true
set :deploy_to, "/var/www"
set :user, :cool_user
set :repository, "git account"
set :scm, "git"
set :scm_username, "my username"
set :scm_passphrase, "password"
ssh_options[:forward_agent] = true
after 'deploy:restart', 'deploy:cleanup'
after 'deploy:update', 'deploy:symlink'
When I run deploy everything looks fine in the console, no errors. But when I visit the site, it's pointing an old release that no longer exists in the server.
For example, when I visit the /pricing page I receive this error...
Unknown action
The action 'pricing' could not be found for DirectController
On another page, I receive this error...
Template is missing
Missing template layouts/anonymous
I have no idea how to point capistrano to the most recent version. Any help is appreciated! Thanks.
If I were to reboot the server then I receive this error.
Sprockets::FileNotFound in Home#index
couldn't find file 'angular'
(in /var/www/releases/20140428003009/app/assets/javascripts/application.js:21)
UPDATE:
I've solved the problem by moving my angular_js gem out of the assets group and into the all group in my bundler. I also did an apache restart from within the server instead of rebooting from the hosting website. Thanks for all the help though!

Why did I have to upload my private key to my server to get Capistrano to work with GitHub?

From what I've read, including this article on GitHub itself, by using ssh agent forwarding, I should not have needed to upload my private id_rsa key to my server in order for Capistrano to connect to GitHub. Yet, until I uploaded it, cap deploy would fail trying to pull from GitHub saying "Permission denied (publickey)".
A little about my setup: My server is a shared Dreamhost server. I'm on Windows using the mysysgit Git Bash as my CLI. I previously set up passwordless ssh login to the Dreamhost server by copying my public key to authorized_keys. I can push to GitHub from my dev. Another symptom was that I could ssh into GitHub from my box, but from the server it gave the same error as above. That tipped me off about uploading my private key to resolve the issue. But it shouldn't have been necessary as I understand it.
Here's my deploy.rb:
default_run_options[:pty] = true
set :domain, "myactualdomain"
set :user, "myusernameeverywhere"
# source
set :scm, :git
set :repository, "git#github.com:#{user}/#{myreponame}.git"
set :branch, "master"
ssh_options[:forward_agent] = true
set :deploy_via, :remote_cache
set :scm_verbose, true
# deployment
set :use_sudo, false
set :deploy_to, "/home/#{user}/#{domain}"
role :web, "#{domain}"
role :app, "#{domain}"
role :db, "#{domain}", :primary => true
Why did I have to upload my private key? Is there a way to handle this where that isn't necessary?
Agent forwarding is handled by the net-ssh gem. Did you try to update it to >=1.1.0 ?

Deploying a Rails App to Multiple Servers using Capistrano - Best Practices

I have a rails application that I need to deploy to 3 servers - machine1.com, machine2.com and machine3.com. I want to be able to deploy it to all machines at once and each machine individually. Can someone help me out with a skeleton Capistrano config file / recipe? Should it all be in deploy.rb or should I break it out in machine1.rb, etc?
I thought I was on the right track getting Capistrano to take in command line arguments, but it choked when I tried set the roles within the namespaces. I'd pass in 'hosts=1,2,3' as an argument and set the role:app/web/db to "machine#{host}.com" after splitting on the command and going into an each do |host| {}...
Anyway, other than creating 4 different deploy.rb files and renaming it before running cap:deploy each time, I'm stumped. I'd like to be able to do the following:
cap deploy:machine1:latest_version_from_svn
cap deploy:all_machines:latest:version_from_svn
Just don't know if it should all be in deploy.rb split up with namespaces or if it should be broken into multiple *deploy**.rb files.
It should all go in one file. Here's an example:
set :application, "my-app"
set :repository, "git#git.my-git-host.com:my-app.git"
set :keep_releases, 5
set :deploy_via, :remote_cache
set :git_enable_submodules, true
set :scm, :git
set :user, 'your-user-here'
set :deploy_to, "/var/www/staging.mydomain.com"
set :branch, 'staging'
set :rails_env, 'staging'
role :web, "machine1.mydomain.com", "machine2.mydomain.com", "machine3.mydomain.com"
role :app, "machine1.mydomain.com", "machine2.mydomain.com", "machine3.mydomain.com"
role :db, "db.mydomain.com"
# ...
You'll see that only one db server was specified. This is the machine the migrations will be run from. If you only have one database (99.9% chance of the answer to that question being YES), then make sure to only provide one.
I had to use a slightly different syntax.
role :app, %w{s01.foobaz.com s02.foobaz.com}, user: 'deployer'
role :web, %w{s01.foobaz.com s02.foobaz.com}, user: 'deployer'

Capistrano install directory problem

I'm doing my first Rails deployment and using capistrano, following my host's directions:
I have the domain plantality.com.
I've created the gws folder for my app.
I've installed with capistrano and followed all the wiki instructions I could find.
public_html is pointing to gws/public but capistano has installed my app to gws/current
I've double checked that my deploy.rb paths are correct.
I've created a symlink between gws/public and public_html (I tried one between gws/current/public and public_html but it didn't help.
I'm trying to use Passenger if it makes any difference.
Here is my deploy.rb:
set :user, 'plantali'
set :scm_username, 'solent'
set :scm_password, '<removed>'
set :svnserver, 'plantality.sourcerepo.com'
set :application, "gws"
set :repository, "http://#{svnserver}/plantality/gws/gws"
set :server, 'plantality.com'
set :applicationdir, 'gws'
set :use_sudo, false
set :keep_releases, 5
set :scm, :subversion
role :web, "plantality.com" # Your HTTP server, Apache/etc
role :app, "plantality.com" # This may be the same as your `Web` server
role :db, "plantality.com", :primary => true # This is where Rails migrations will run
#role :db, ""
set :deploy_to, "/home/#{user}/#{applicationdir}"
set :group_writeable, false
Capistrano deploys releases into timestamped directories under the releases directory and creates a symbolic link named current that points to the root of the Rails application within the latest release directory.†
Therefore the Rails root of your application is /home/<user>/gws/current/ and Passenger needs to be configured to serve the application from there accordingly. Set the Apache DocumentRoot in the virtual host that Passenger is using to /home/<user>/gws/current/public and restart Passenger.
† Incidentally, this is how Capistrano can easily roll back a bad release—it simply recreates the symlink to point to the previous timestamped release.

Capistrano and Git, Ruining my life. "Unable to resolve revision for [HEAD] on repository ..."

I searched all of the relevant Capistrano issues, but couldn't find something that even elucidated anything here for me.
git version 1.6.4.2
Capistrano v2.5.10
Basically, when I run my cap deploy.rb script, it connects to the server, starts executing the deploy:update task, then in the deploy:update_code task:
*** [deploy:update_code] rolling back
* executing "rm -rf /home/user_name/public_html/project_name/releases/20091223094358; true"
servers: ["project_name.com"]
It fails with the following error:
/Library/Ruby/Gems/1.8/gems/capistrano-2.5.10/lib/capistrano/recipes/deploy/scm/git.rb:231:in `query_revision': Unable to resolve revision for 'master' on repository 'ssh://git#slice_ip:path_to_git_repository'. (RuntimeError)
Here's my deploy script, I've tried including and omitting:
set :branch 'master'
I also just thought my path to the repository was off, but i've tried just about every permutation (absolute, not absolute, .git suffix, no suffix). There's definitely a bare git repository at the path i'm pointing to.
**I do have multiple projects being hosted on one slice. The other projects is also a rails project, but is running SVN. Capistrano deployments work fine.
Any pointers in the right direction or any ideas would help reduce the amount of drinking I am planning on doing if I can't figure this out. (Paths / IPs obfuscated, dont hack me bro!)
set :application, "project1"
set :user, "username"
set :repository, "ssh://git#67.24.9.133/home/git/project1.git"
set :branch, "master"
set :port, 696969
set :deploy_to, "/home/username/public_html/#{application}"
set :scm, :git
role :app, application
role :web, application
role :db, application, :primary => true
# deployment via remote client (workstation)
set :deploy_via, :copy
set :runner, user
# mod_rails
namespace :deploy do
desc "Restarting mod_rails with restart.txt"
task :restart, :roles => :app, :except => { :no_release => true } do
run "touch #{current_path}/tmp/restart.txt"
end
[:start, :stop].each do |t|
desc "#{t} task is a no-op with mod_rails"
task t, :roles => :app do ; end
end
end
This was the most relevant post (extremely relevant even), but I couldn't really figure out what they were saying the fix is. I'm pretty new with git / capistrano configs.
https://capistrano.lighthouseapp.com/projects/8716/tickets/56-query_revision-unable-to-resolve-revision-for-head-on-repository
Ok I seemed to have fixed it.
Basically, since I have 2 separate repositories on the remote server, I think the "git" user was failing because I hadn't registered an ssh keypair for the git user. That explains why one of my deploy.rb scripts was working properly, while this one wasn't.
In the link I posted in the question, one of the commenters pointed out the issue:
https://capistrano.lighthouseapp.com/projects/8716/tickets/56-query%5Frevision-unable-to-resolve-revision-for-head-on-repository
Note this error is also displayed if
you are using multiple github keys per
http://capistrano.lighthouseapp....
and you do not have these keys and a
corresponding entry in your
.ssh/config on the workstation you're
running the deploy from. so the
ls-remote is run locally. is there a
way to reference the repository at
github.com for this request while the
remote deploy uses
git#github-project1:user/project1.git
Also, see the following link for more details, since the whole ssh issue would apply even if you're not using github.
http://github.com/guides/multiple-github-accounts
Both your workstation and your server must be able to reach the repository at the address specified, if not then you may have to set :local_repository to how you access it from your workstaion, and :repository to be how your servers should access it.
For me Capistrano deployments with Git only seem to work when setting set :copy_cache, true
I've only used capistrano with git once, but never used or seen the use of ssh:// in the repository definition.
Try using set :repository, "git#67.24.9.133/home/git/project1.git" instead
Make sure the branch you are deploying from exists.
set :branch, "upgrade-to-2013.4.3"
is not equal to
set :branch, "upgrade-to-2013.3.4"

Resources