staging and live app with capistrano - ruby-on-rails

I thought I'd do a simple yet potentially very useful thing, and create another symlink called live, that points to an arbitrary release, and leave current at the head where it usually is:
20120519235508
20120521004833
20120521024312 <-- live
20120521025150
20120521030449 <-- current
I then configured www.mysite.com to hit
live/public
and stage.mysite.com to hit
current/public
Unfortunately both hosts seem to run the same app, and not 2 different apps. I've confirmed the httpd.conf has the correct settings and restarted it. However no change, they're both still running the same app, the app referenced by current/public to be exact.
I don't know if I have a setting wrong, or if something else needs to be restarted, or if this simply can't work as I imagined. I'm using passenger.
Can someone shed some light on this subject, because this configuration would be VERY useful to me for many projects.

Instead of creating an other symlink in the releases directory, I suggest to use the multistage extension. With this extension you can define different stages and add custom configuration to them. So instead of using one deployment directory for both staging and production, use a separate one for each other.
Add these lines to deploy.rb:
require "capistrano/ext/multistage"
set :stages, ["staging", "production"]
set :default_stage, "staging"
Remove the deploy_to variable from deploy.rb. Then create a deploy directory inside config which has files with the stage names. In this case: deploy/staging.rb and deploy/production.rb. The content of staging.rb:
set :rails_env, "staging"
set :deploy_to, "staging/capistrano"
And similarly for production.rb:
set :rails_env, "production"
set :deploy_to, "production/capistrano"
Of course change the paths in deploy_to. Then point staging.example.com to staging/capistrano/current/public and www.example.com to production/capistrano/current/public.
To do a staging deploy, execute cap staging deploy or simple cap deploy (remember, staging was set to default in deploy.rb) and cap production deploy to deploy to production.

Related

Problem with rails environment in app run on JELASTIC

I got already deployed server on JELASTIC. The problem is that if I do a deploy, rails_env in /etc/nginx/ruby.env is setting up by itself on ROOT -- not production as I want to. Then my application crash, because rails cannot see a ROOT env in database.yml. The case is that i do not want to add ROOT on database.yml. I want to keep it as clean as possible so I want to deploy my app in production environment.
When I click on "edit project" i got sth like this:
Which says that context production is already in use (and it is, because THIS app is deployed in production mode, like you can see in previous screen). When i do not choose any application deployment type, i have blank selectbox with ROOT placeholder (which is apparently used as deployment type because rails_env in ruby.env is set like this).
I tried also deploy app from deployment manager:
which allow me to choose environment and deployment type and
tells me that this context is already in use and if I want to redeploy context
but it deploy also as a ROOT and i have to change manually nginx ruby.env and restart nginx to make it work.
Do you have any idea, what am I doing wrong?
Any suggestions?
Two more questions, why deploy hooks does not save (for example it run previous hooks even if i delete them and replace with simple "echo")
And last question is, can i somehow create new deployment type called "staging", becasue as we know "development", "production" and "test" are used to other things like staging. I need staging for other purposes, like for example disabling mailers for client test servers.

Capistrano deploy to different path on same server

I am trying to deploy my application using capistrano. But I want to deploy my application to multiple paths of the same server.For example If for the first run I want to deploy it to below path
set :deploy_to, '/home/a/some_path/
Once completed the first one it should run for the second path that will be
set :deploy_to, '/home/b/some_path/
and so on. Any suggestions how can I achieve this? Right now my single path deployment path is working AOK.
In your config file:
set :deploy_to, ENV["DEPLOY_PATH"]
Then, to deploy, run the command setting the DEPLOY_PATH variable:
DEPLOY_PATH="my/path" cap production deploy
Using capistrano 3.8.2, I monkeypatched lib/capistrano/dsl/paths.rb in my deploy.rb, but then I found that I needed more work to get git wrapper set up right when there where different deploy users.
The result is at: https://gist.github.com/mcr/49e8c7034658120013c1fe49da77c2ac
But, I'm leaving the essence of the content here:
module Capistrano
module DSL
module Paths
def deploy_to
dir = #host.properties.fetch(:deploy_to) || fetch(:deploy_to)
puts "For #{#host.hostname} deploy_to: #{dir}"
dir
end
end
end
end
(You can take the puts out, and shorten it to a one-liner, but I found the extra debug useful)
One then does:
server "server.client1.example.com", user: "client1", roles: %w{app db web}, deploy_to: '/client1/app/foobar'
server "server.client2.example.com", user: "client2", roles: %w{app db web}, deploy_to: '/client2/app/foobar'
where server.client1.example.com and server.client2.example.com are CNAMEs or duplicate A/AAAA records for the same server. This also isolates the question of where each client is to DNS.

ROR deployment: Staging and Development environments

I already have a production instance deployed on the server. Its working well.
Here is what I need to do.
Deploy a staging and Development environment on the server.
I have already created a branch in github to do that.
Config
1) Ruby 1.8.6
2) Rails is being vendored
3) Webserver Nginx and Thin
4) I have already create a file under /usr/local/nginx/sites-enabled and sites-available folders
5) Added yml file under /etc/thin
6) Made edits to the deploy.rb and have added dev.rb under the config and deploy folders
7) Capistrano is being used on the server for deploy
Questions:
How to deploy the dev environment from separate github branch different that production ? Will that reboot/affect the current production environment too ?
I want to make sure the production wont get affected by this. Please provide a list of commands
or tutorials that will help me with this. I am into very early stages of of learning ROR so please be
a little details. Help is very much appreciated.
EDIT:
1) Capify the project by installing the gem locally and running capify locally.
2) Make changes to you deploy.rb under config
3) set :stages with staging and production
4) set :default_stage as staging .. You have to edit this file more to customize your deployment
5) Under config/deploy/ : Create you production and staging ".rb" files. set the branch to master or any specific branch. Set your rails_env to staging in staging.rb and to production in production.rb.
Set deploy_to as xxxpath/staging and xxxpath/production in those appropriate files.
6) cap deploy will deploy in staging as default due to 4)
7) cap production deploy for production
It looks like you're most of the way there. The key will be to ensure that Capistrano deploys each branch to a separate location on the filesystem -- the sites-available document roots should be different (in other words, don't overwrite your production files!).
Two methods, if you have set stable production, staging and development branches, use the method documented here http://help.github.com/deploy-with-capistrano/
You can use this method for one-off branch deployments Using capistrano to deploy from different git branches.
Passenger looks for the file tmp/restart.txt to know when to restart; this is under the application tree so should only affect the specific variant of the site.
Depending on your server's capacity, the production site may suffer a brief performance hit from the restart of another environment. When you are able, you should consider getting a separate server for staging, test, dev, etc.

Capistrano Multistage deploying to wrong directory

Im having some problems with my capistrano setup after updating my gems lately. I have a multistage setup with a production and staging setup.
/config/deploy.rb
# setup multistage
set :stages, %w(testing production)
set :default_stage, "testing"
require 'capistrano/ext/multistage'
/config/deploy/production.rb
# Set deploy path
set :deploy_to, "/var/www/mysite/live"
set :rails_env, "production"
/config/deploy/testing.rb
# Set deploy path
set :deploy_to, "/var/www/mysite/test"
set :rails_env, "test"
Problem is that it seems to ignore my deploy_to setting. It just deploys to the default /u/apps/mysite.
I don't know if it has any relevance, the cause of all of this is a move from apache+passenger to nginx+unicorn. I don't think it has anything to do with that though, as this is just the checkout process.
I stumbled across this while on Stack Overflow. Its an old question but since its flagged as open I'm going to give it a shot.
I think this might be a scope issue with how the Capistrano Instances get loaded.
I notice this syntax doesn't work in the production.rb and test.rb files
set :deploy_to, "/var/www/mysite/live"
But this one does:
set(:deploy_to) { "/var/www/#{application}/live" }
Its a subtle difference but I think the one that works is actually passing the information as a Proc block, whereas the first one is passing it as a string. I have a sneaky suspicion that by the time the Capistrano Instance comes into being that string is no longer present.
This would indicate to me that something is off in your load or require order as you should be able to set the deploy variables in these files. If you can't figure it out you may be able to cheat and surround the deploy/production.rb or deploy/test.rb code with
Capistrano::Configuration.instance.load do
# variables, etc here
end
That would definitely tell you that this file isn't being loaded within the scope of the Capistrano instance.
Also minor point but the files should be in
config/deploy # relative to your Rails app
Not
/config/deploy/ # this is an absolute path off of your root folder
Good Luck. Hopefully you've already solved this issue!
Where are those production.rb and testing.rb located in the project?
Make sure they are under config/deploy.
I eventually solved this by adding the following to my deploy/production.rb and testing.rb
set(:deploy_to) { "/var/www/#{application}/live" }
set(:releases_path) { File.join(deploy_to, version_dir) }
set(:shared_path) { File.join(deploy_to, shared_dir) }
set(:current_path) { File.join(deploy_to, current_dir) }
set(:release_path) { File.join(releases_path, release_name) }
Could be just the order you have it in your deploy.rb? put the require above the stage settings
require 'capistrano/ext/multistage'
# setup multistage
set :stages, %w(testing production)
set :default_stage, "testing"

Can a specify a particular server on the capistrano command line?

I just tried to run
cap deploy:setup
on the command line, but it wanted it to run on just one particular server instead of them all. Is there a way to run a task on just one server from the command line, or do I have to define it that way in the deploy.rb file?
Are you using capistrano-multistage? If not I recommend you do, I believe you can achieve the same with just the deploy.rb but personally I just find it easier this way and it makes this process much neater, especially if you start doing different things in production, staging or other stages.
Basically once you've installed the gem locally you can simply run commands like this:
cap staging deploy:setup
Where the 'staging' part matches one of you stage files (See below).
To get up and running change deploy.rb to something like this:
set :stages, %w(staging production)
set :default_stage, "staging"
require 'capistrano/ext/multistage'
after "deploy", "deploy:cleanup"
Then add a folder named deploy into the config directory of your rails app. In there you can place your separate deployment files, e.g staging.rb and production.rb.

Resources