Deploying rails app to EC2 using capistrano - ruby-on-rails

So I'm going through this tutorial http://blog.grio.com/2012/07/how-to-deploy-your-web-app-to-amazon-ec2-using-capistrano.html to deploy a rails app to EC2, and I'm confused about a few things in deploy.rb. In the default version of the file, I have:
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"
However, previously I was hosting on heroku where I was assigned a url. Since this will be a facebook app, I don't want to deal with getting a url, so is there anything I can do to get one automatically like I did on heroku? Also, what is the idea behind this, ie what is it doing?
set :deploy_to, "/var/www/myapp"

When you run Capistrano, you are running it from your local machine.
The:
"your web-server here"
is for the IP address of the remote server you are deploying your app to.
The:
set :deploy_to
is specifying the folder on the server you want.

Related

How to use different user for a server in Capistrano?

I want to set the user for one of the roles to a different one in my Capistrano file. How do I do that? Right now the user is set to "sshadmin" for all the servers.
set :user, "sshadmin"
server "198.227.6.30", :app
server "192.9.1.17", :web
server "192.9.31.9", :db #I want this to use the user "sshadmin2"
In Capistrano v3 You can use this directive:
role :app, %w{sshadmin#198.227.6.30}
role :web, %w{sshadmin#192.9.1.17}
role :db, %w{sshadmin2#192.9.31.9}
From the Capistrano documentation
Servers can be defined in a bunch of ways, the following shows defining two servers, one where we set the username, and another where we set the port. These host strings are parsed and expanded out in to the equivalent of the server line after the comment:
role :web, %w{hello#world.com example.com:1234}
# ...is the same as doing...
server 'world.com' roles: [:web], user: 'hello'
server 'example.com', roles: [:web], port: 1234
Applied to your case
role :db, %w{sshadmin2#192.9.31.9}

deploy similar ruby on rails code to multiple server

I have a rails app looking good on my localhost. Now I want to deploy it to multiple server (one load balancer, and two application server to be exact, with possible increase in the future), and somehow I'm lost. This would be my first time deploying a web by myself, so I'm sorry for my lack of knowledge.
I want all application server to run exactly same code.
And when I create a new content, I want the new content to be stored on each server's database instance (MySQL). So when I took down one server for maintenance and updating, the rest of the server could serve users with exact same content. I've read that capistrano could help me with this, but somehow I managed to get lost in learning how to do this. So, how should I proceed from here? How should the capistrano recipe look like, and do I have to tweak database.yml in my rails also?
Thank you very much for help.
You can use roles to deploy the same application to multiple servers.
Assuming you're using the multistage extension, define the roles in production.rb:
server1 = 'appserver1.tld'
server2 = 'appserver2.tld'
server3 = 'webserver1.tld'
role :app, server1, server2
role :web, server3
The web server will run on servers specified by the :web role.
The app layer will run on servers specified by the :app role.
If you run migrations or other DB operations during deploy, you should also specify a server under the :db role. For example:
role :db, 'dbserver.tld', :primary => true
You may have multiple DB servers, but by specifying one as the primary server capistrano will only run DB operations on that server.
In your deploy.rb, you can also create tasks that run only for certain roles. For example:
task :restart, :roles => :app, :except => { :no_release => true } do
run "touch #{current_path}/tmp/restart.txt"
end
In the above example, :except => { :no_release => true } means that it will only run if at least one release exists on the server being deployed to.
This wiki article may be of further help to you.

Cannot deploy an app with git to EC2

I have set up amazon EC2 instance (working) and Github repo (working well).
When I try to deploy the app to EC2 with using Github repo, I get this error:
** [IP] Permission denied (publickey).
** [IP] fatal: Could not read from remote repository.
**
** Please make sure you have the correct access rights
** and the repository exists.
What am I missing? I can deploy an app to EC with using copy, but not with using github.
What do I need to set up the publickey?
EDIT: SETUP
set :application, "project_name"
set :user, 'username'
set :password, "password"
set :domain, "IP.amazonaws.com"
set :deploy_to, "/path_to_directory"
set :use_sudo, false
role :web, domain
role :app, domain
role :db, domain, :primary => true
set :assets_role, [:app]
default_run_options[:pty] = true
set :repository, "git#github.com:user/repo.git"
set :scm, "git"
Thank you
You haven't set up your SSH keys between EC2 and Github correctly based on the error message, but given the information you've provided, it's not possible to provide suggestions.
Question - have you set up a deploy key on the EC2 server? I'm assuming that you are trying to deploy from the EC2 server and not your local machine using SSH Agent Forwarding. Can you do a git pull from the EC2 server to the github account? Can you ssh to git#github.com (e.g. "ssh -T git#github.com")?
The links below can help you setup a deploy key or figure out another way to manage deployment.
https://help.github.com/articles/deploying-with-capistrano
https://help.github.com/articles/managing-deploy-keys
One of the best ways to debug access privileges when beginning to use Capistrano is to try accessing your server without it.

Running migration on server when deploying with capistrano

I'm trying to deploy my rails application with capistrano, but I'm having some trouble running my migrations. In my development environment I just use sqlite as my database, but on my production server I use MySQL.
The problem is that I want the migrations to run from my server and not my local machine, as I am not able to connect to my database from a remote location.
My server setup:
A debian box running ngnix, passenger, mysql and a git repository.
What is the easiest way to do this?
update:
Here's my deploy script: (i replaced my actual domain with example.com)
set :application, "example.com"
set :domain, "example.com"
set :scm, :git
set :repository, "git#example.com:project.git"
set :use_sudo, false
set :deploy_to, "/var/www/example.com"
role :web, domain
role :app, domain
role :db, "localhost", :primary => true
after "deploy", "deploy:migrate"
When I run cap deploy, everything is working fine until it tries to run the migration.
Here's the error I'm getting:
** [deploy:update_code] exception while rolling back: Capistrano::ConnectionError, connection failed for: localhost (Errno::ECONNREFUSED: Connection refused - connect(2))
connection failed for: localhost (Errno::ECONNREFUSED: Connection refused - connect(2)))
This is why I need to run the migration from the server and not from my local machine.
Any ideas?
Try to add
after "deploy", "deploy:migrate"
in your config/deploy.rb file. This will run a migration on your server upon a successful deployment of your project.
Have you added your deploying user as a mysql user on the server? I reckon the localhost is the server referring to itself not your local machine.
Also you haven't defined your user in your deploy script:
set :user, "deploy_user_name"
role :web, domain
role :app, domain
role :db, domain, :primary => true

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.

Resources