How to use different user for a server in Capistrano? - ruby-on-rails

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}

Related

How to create roles and run capistrano tasks for specific roles?

Here is my production.rb in config/deploy
Instance Details
server '198.61.179.237', :web, :app, :db, primary: true
server '198.61.228.160', :file_server
# Rails Environment
set :rails_env, 'production'
And from deploy.rb
namespace :check do
task :function_1, :roles => :web do
puts 'function_1'
end
task :function_2, :roles => :file_server do
puts 'filesssss'
end
end
But when I try doing
cap HOSTS=198.61.228.160 production check:function_2
cap HOSTS=198.61.228.160 production check:function_1
cap HOSTS=198.61.179.237 production check:function_2
cap HOSTS=198.61.179.237 production check:function_1
Everyone of them gives respective output. But as per the declaration
function_1 should work only for :role => :web and similarly function_2 should work only for :role => :file_server.
Where I am going wrong ?
What is the correct way to approach ?
I believe what you want is cap HOSTFILTER=198.61.228.160 function_2 or cap HOSTFILTER=198.61.179.237 function_1
It's because the HOSTFILTER checks for the intersect of all the servers with the functions role and the server you're looking for. A great explanation can be found here by Pete Hodgson
Also we can see this because of the manual:
$ cap -H
HOSTS
Execute the tasks against this comma-separated list of hosts.
Effectively, this makes the host(s) part of every roles.
HOSTFILTER
Execute tasks against this comma-separated list of host,
but only if the host has the proper role for the task.
HOSTROLEFILTER
Execute tasks against the hosts in this comma-separated list of roles,
but only if the host has the proper role for the task.
ROLES
Execute tasks against this comma-separated list of roles. Hosts which
do not have the right roles will be skipped.

Deploying rails app to EC2 using capistrano

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.

Rails3 ActiveRecord migrations on concurrent deploy

How do you ensure your migrations are run only once, when you deploy to multiple machines at the same time ?
What I have to do right now is select one machine to run the migrations when I do have a change of that sort. Ideally, deployment is brainless and the process would take care of that for me.
My idea currently is to have the migrator look for schemas to migrate and acquire a lock if it has something to do. If the lock is already acquired, it skips the migration alltogether. Reading the ActiveRecord code it doesn't seem to support such idea so it would need some patching.
What's your idea ?
Are you using Capistrano? You can specify a list of database servers and mark one as Primary. Migrations will only be run on that server:
role :app, 'example.com.com'
role :web, 'example.com'
role :db, 'db01.example.com', :primary => true
role :db, 'db02.example.com'
role :db, 'db03.example.com'
EDIT: the :db role is not intended to be used for a separate database server which is not running Rails application code. This probably isn't your setup.

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

Rails, Slicehost, Capistrano - Deployment port issues

I'm getting a port error when I try to deploy my app via:
cap deploy:cold
The error:
ssh: connect to host domain.com port 22: Connection refused
My deploy.rb (replaced sensitive info where appropriate):
set :user, 'user'
set :domain, 'domain.com'
set :application, "App Name"
# file paths
set :repository, "user#domain.com:git/appname.git"
set :port, 9728
set :deploy_to, "/home/ruby/public_html/appname"
# distribute your applications across servers (the instructions below put them)
# all on the same server, definied above as 'domain', adjust as necessary
role :web, domain
role :app, domain
role :db, domain, :primary => true
# you might need to set this if you aren't seeing password prompts
# default_run_options[:pty] = true
# As Capistrano executes in a non-interactive mode and therefore doesn't cause
# any of your shell profile scripts to be run, the following might be needed
# if (for example) you have locally installed gems or applications. Note:
# this needs to contain the full values for the variables set, not simply
# the deltas.
# miscellaneous options
set :deploy_via, :remote_cache
set :scm, :git
set :branch, 'master'
set :scm_verbose, true
set :use_sudo, false
# task which causes Passenger to initiate a restart
namespace :deploy do
task :restart do
run "touch #{current_path}/tmp/restart.txt"
end
end
# optional task to reconfigure databases
after "deploy:update_code", :configure_database
desc "copy database.yml into the current release path"
task :configure_database, :roles => :app do
do_config = "#{deploy_to}/config/database.yml"
run "cp #{db_config} #{release_path}/config/database.yml"
end
I understand the error, but can't see where to make the change. I figured :set port, would handle it.
Have you tried putting the port in your repository path ? Something like ssh://user#domain.com:9728/git/appname.git
I think Capistrano only uses the port you specify in set :port to log in your remote servers.
Here's what works: you must have
set :port, port#
set :repository, "ssh://#{user}#IPAddress:#{port}/~/path/to/git
Or also try:
Here's a possible fix. add ssh_options[:port] = port# May work
Sorry to state the obvious, but:
Is the port actually open on the server?
Have you changed it to something else?
Is it accessible from the outside?
Is your firewall blocking it?
please try it
server "domain:9728", :web, :app, :db

Resources