<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>overwatering &#187; rails</title>
	<atom:link href="http://www.overwatering.org/blog/category/rails/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.overwatering.org/blog</link>
	<description>Random musings on fish, books and occasionally programming.</description>
	<lastBuildDate>Mon, 07 Nov 2011 07:06:02 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Automatic Deployment for Rails</title>
		<link>http://www.overwatering.org/blog/2008/12/automatic-deployment-for-rails/</link>
		<comments>http://www.overwatering.org/blog/2008/12/automatic-deployment-for-rails/#comments</comments>
		<pubDate>Sun, 07 Dec 2008 11:31:00 +0000</pubDate>
		<dc:creator>giles</dc:creator>
				<category><![CDATA[comp. sci.]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[recipe]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://www.overwatering.org/blog/2008/12/automatic-deployment-for-rails/</guid>
		<description><![CDATA[For the Rails applications we&#8217;re building at work, as well as all the
standard continuous integration features, we also automatically deploy
our applications. That is, every time we submit code a central server
is automatically updated with a new release. Before running tests.

We&#8217;re pretty happy with this set up. It&#8217;s already found a couple of
bugs in some plugins [...]]]></description>
			<content:encoded><![CDATA[<p>For the Rails applications we&#8217;re building at work, as well as all the
standard continuous integration features, we also automatically deploy
our applications. That is, every time we submit code a central server
is automatically updated with a new release. Before running tests.</p>

<p>We&#8217;re pretty happy with this set up. It&#8217;s already found a couple of
bugs in some plugins we&#8217;re using. More on that in an upcoming
post. Here&#8217;s how we made our automatic deployment work. We&#8217;re using
Capistrano for our deployment scripts, we&#8217;re deploying to Phusion
Passenger running under Apache on FreeBSD and our continuous
integration server runs an Ant script.</p>

<p>These instructions describe how to set up a Apache 2.2 web server with
Phusion Passenger on FreeBSD; the Ant script to automatically deploy
and how to configure a Rails app to be deployed like this.</p>

<p>This will give you two new environments for your apps: DEVTEST and
UAT. UAT is a user acceptance testing environment, our system testers
and analysts use and own this environment. We don&#8217;t automatically
deploy to here, we release to here. DEVTEST is the environment we
automatically deploy to.</p>

<h3>Setting up Your Server</h3>

<h4>Installing Phusion Passenger</h4>

<p>Installing Phusion Passenger on a FreeBSD server is no different to
installing anywhere else:</p>

<p><pre><code>$ sudo gem install passenger
$ sudo passenger-install-apache2-module
</code></pre></p>

<h4>Configuring Apache</h4>

<p>At the end of the second step, the installer tells you to add some
config to the end of your Apache config. On FreeBSD, edit this with:</p>

<p><pre><code>$ sudoedit /usr/local/etc/apache22/httpd.conf
</code></pre></p>

<p>And then add the following at the end:</p>

<p><pre><code>LoadModule passenger_module /usr/local/lib/ruby/gems/1.8/gems/passenger-2.0.3/ext/apache2/mod_passenger.so
PassengerRoot /usr/local/lib/ruby/gems/1.8/gems/passenger-2.0.3
PassengerRuby /usr/local/bin/ruby18
NameVirtualHost *:80
&lt;VirtualHost *:80&gt;
    ServerName devtest.example.com
    ServerAlias devtest
    DocumentRoot /usr/local/www/rails/devtest
    &lt;Directory "/usr/local/www/rails/devtest"&gt;
        Options FollowSymLinks
        AllowOverride None
        Order allow,deny
        Allow from all
    &lt;/Directory&gt;
    RailsEnv "devtest"
&lt;/VirtualHost&gt;
&lt;VirtualHost *:80&gt;
    ServerName uat.example.com
    ServerAlias uat
    DocumentRoot /usr/local/www/rails/uat
    &lt;Directory "/usr/local/www/rails/uat"&gt;
        Options FollowSymLinks
        AllowOverride None
        Order allow,deny
        Allow from all
    &lt;/Directory&gt;
    RailsEnv "uat"
&lt;/VirtualHost&gt;
</code></pre></p>

<p>Unless you want to use two different servers for the two environments,
you&#8217;ll need to use named virtual hosts, and ask your friendly
administrator to add CNAME records to your DNS server pointing devtest
and uat at the same physical server. They&#8217;ll know what you mean.</p>

<h4>Create a Local User</h4>

<p>You&#8217;ll need a local user on your server. This is the user that will
run the automatic deployments.</p>

<p><pre><code>$ sudo adduser
Username: deploy-robot
Full name: Deployment Robot
Uid (Leave empty for default):
Login group [deploy-robot]:
Login group is deploy-robot. Invite deploy-robot into other groups? []: www
Login class [default]:
Shell (sh csh tcsh zsh nologin) [sh]: 
Home directory [/home/deploy-robot]:
Use password-based authentication? [yes]:
Use an empty password? (yes/no) [no]:
Use a random password? (yes/no) [no]:
Enter password:
Enter password again:
Lock out the account after creation? [no]:
Username   : deploy-robot
Password   : ****
Full Name  : Deployment Robot
Uid        : 1001
Class      :
Groups     : 
Home       : /home/deploy-robot
Shell      : /usr/local/bin/sh
Locked     : no
OK? (yes/no): yes
adduser: INFO: Successfully added (deploy-robot) to the user database.
Add another user? (yes/no): no
Goodbye!
</code></pre></p>

<h4>Deployment Directories</h4>

<p>Set up the directories to hold your applications.</p>

<p><pre><code>$ sudo mkdir -p /usr/local/www/rails/devtest
$ sudo mkdir -p /usr/local/www/rails/uat
</code></pre></p>

<p>These are the web roots for each of the environments, but applications
will not be deployed here. Instead, symlinks will be created from here
to where the applications are actually deployed.</p>

<p><pre><code>$ sudo mkdir -p /usr/local/app/rails/devtest
$ sudo mkdir -p /usr/local/app/rails/uat
</code></pre></p>

<p>These last two directories, and everything under them should be owned
by the deployment user you created above.</p>

<p><pre><code>$ sudo chown -R deploy-robot:www devtest uat
</code></pre></p>

<h4>Gems</h4>

<p>Finally, there are some gems you&#8217;ll need installed on the target
deployment server. Some of these depend on FreeBSD ports.</p>

<p><pre><code>$ cd /usr/ports/comms/ruby-termios
$ sudo make install clean
</code></pre></p>

<p>And then just a couple of gems.</p>

<p><pre><code>$ sudo gem install termios
$ sudo gem install capistrano
</code></pre></p>

<p>And that&#8217;s it for initial server configuration. There will be some
more configuration when first deploying an application.</p>

<h3>Preparing Your Application</h3>

<h4>Capistrano Config</h4>

<p>Capify your application:</p>

<p><pre><code>$ cd app
$ capify .
</code></pre></p>

<p>Edit your capistrano rules in <code>deploy.rb</code>. You&#8217;ll want them to look
something like the following. These rules use no source control
system to get the code. Our continuous integration server takes care
of checking out the code, so it&#8217;s easier to deploy from the local code
copy. And, this way we can be sure each deployment only contains one
changelist.</p>

<p><pre><code># Overall config
set :use_sudo, false
&#35; Application config
set :application, "app-name"
set :default_env, "production"
set :rails_env, ENV['RAILS_ENV'] || default_env
&#35; Deployment source and strategy
set :deploy_to, "/usr/local/app/rails/#{rails_env}/#{application}"
set :deploy_via, :copy
set :scm, :none
set :repository,  "."
&#35; Target servers
set :default_server, "localhost"
set :dest_server, ENV['SERVER'] || default_server
role :app, dest_server
role :web, dest_server
role :db,  dest_server, :primary =&gt; true
&#35; Phusion Passenger specific restart task
namespace :deploy do
    desc "Restart Application"
    task :restart, :roles =&gt; :app do
        run "touch #{current_path}/tmp/restart.txt"
    end
end
</code></pre></p>

<h4>Environment Configuration</h4>

<p>Set up the two new environments for your application.</p>

<p><pre><code>$ cp config/environments/production.rb config/environments/devtest.rb
$ cp config/environments/production.rb config/environments/uat.rb
</code></pre></p>

<p>Somewhere inside both those files you&#8217;ll need to set the
<code>RAILS_RELATIVE_URL_ROOT</code> as the application will be running at a
sub-URI on your server and Rails needs to know that. Something like:</p>

<p><pre><code>ENV['RAILS_RELATIVE_URL_ROOT'] = "/app-name"
</code></pre></p>

<p>The two new environments will also need to be described in your
<code>database.yml</code> file. This of course depends on your specific database
server setup, so I&#8217;ll leave that bit to you.</p>

<h4>Server-side Application Setup</h4>

<p>Apache needs to know about the applications, and there needs to be
symlinks from the web root to the application deployment folder. This
setup only needs to be done once for each application.</p>

<p>To add the application to Apache, edit
<code>/usr/local/etc/apache22/httpd.conf</code> again, and in the <code>VirtualHost</code>
section for the devtest environment, add a line like the following:</p>

<p><pre><code>RailsBaseURI /app-name
</code></pre></p>

<p>Now, set up the symlink:</p>

<p><pre><code>$ ln -s /usr/local/app/rails/devtest/app-name/current/public /usr/local/www/rails/devtest/app-name
</code></pre></p>

<p>And you&#8217;re done with the application configuration.</p>

<h3>Ant Deployment Scripts</h3>

<p>Our company has an in-house continuous integration server. We&#8217;d be too
embarrassed at cocktail parties if we didn&#8217;t have our own. Yes, yes, I
know this is completely ridiculous. And to make it even worse, it only
runs Ant scripts. Sigh. Anyway, here&#8217;s how you make Ant automatically
deploy an application to devtest.</p>

<p>In a file called <code>definitions.xml</code>:</p>

<p><pre><code>&lt;project name="definitions_rake"&gt;
    &lt;macrodef name="rake"&gt;
        &lt;attribute name="app" /&gt;
        &lt;attribute name="target" /&gt;
        &lt;element name="variables" optional="true" /&gt;
        &lt;sequential&gt;
            &lt;exec executable="rake" dir="@{app}" failonerror="true"&gt;
                &lt;arg value="@{target}" /&gt;
                &lt;variables /&gt;
            &lt;/exec&gt;
        &lt;/sequential&gt;
    &lt;/macrodef&gt;
    &lt;macrodef name="capistrano"&gt;
            &lt;attribute name="app" /&gt;
            &lt;attribute name="environment" /&gt;
            &lt;attribute name="task" /&gt;
            &lt;sequential&gt;
                &lt;exec executable="cap" dir="@{app}" failonerror="true"&gt;
                    &lt;env key="RAILS_ENV" value="@{environment}" /&gt;
                    &lt;env key="SERVER" value="${project.server}" /&gt;
                    &lt;arg value="@{task}" /&gt;
                    &lt;arg value="-s" /&gt;
                    &lt;arg value="user=${project.user}" /&gt;
                    &lt;arg value="-s" /&gt;
                    &lt;arg value="password=${project.password}" /&gt;
                &lt;/exec&gt;
            &lt;/sequential&gt;
    &lt;/macrodef&gt;
    &lt;macrodef name="deploy"&gt;
        &lt;attribute name="app" /&gt;
        &lt;attribute name="environment" /&gt;
        &lt;sequential&gt;
            &lt;capistrano app="@{app}" environment="@{environment}" task="deploy:setup" /&gt;
            &lt;capistrano app="@{app}" environment="@{environment}" task="deploy:migrations" /&gt;
        &lt;/sequential&gt;
    &lt;/macrodef&gt;
    &lt;macrodef name="test"&gt;
        &lt;attribute name="app" /&gt;
        &lt;sequential&gt;
            &lt;rake app="@{app}" target="db:migrate" /&gt;
            &lt;rake app="@{app}" target="test" /&gt;
            &lt;rake app="@{app}" target="spec" /&gt;
        &lt;/sequential&gt;
    &lt;/macrodef&gt;
&lt;/project&gt;
</code></pre></p>

<p>Ant macros, while quite insane, are generally a better way to define
new tasks than the complete insanity of trying to write a whole Ant
plugin in Java. These macros define low-level tasks to run <code>rake</code> and
<code>capistrano</code> tasks, and then use these to build up higher level tasks
like test and deploy. All these tasks assume that Ant has been run
from the directory immediately above your Rails app directory.</p>

<p>In a file called <code>project.properties</code>, set your server, user name and
password. Having the password here is unfortunate, but it is a local
account, with limited privileges on an internal server. Your call.</p>

<p><pre><code>user=deploy-robot
password=deploy-robot-password
server=deployment-server
</code></pre></p>

<p>In a file called <code>build.xml</code>:</p>

<p><pre><code>&lt;project name="aegean" default="build"&gt;
    &lt;import file="./definitions.xml" /&gt;
    &lt;property file="project.properties" prefix="project" /&gt;
    &lt;!-- Sample application.
         To add a new application:
         1. Copy the following targets.
         2. Replace 'depot' with your Rails app name.
         3. Add the 'app name' target as a dependency of the target 'build'.
    &lt;target name="depot.deploy.devtest"&gt;
           &lt;deploy app="depot" environment="devtest" /&gt;
    &lt;/target&gt;
    &lt;target name="depot.test"&gt;
           &lt;test app="depot" /&gt;
    &lt;/target&gt;
    &lt;target name="depot" depends="depot.deploy.devtest, depot.test" /&gt;
    --&gt;
    &lt;target name="example.deploy.devtest"&gt;
           &lt;deploy app="example" environment="devtest" /&gt;
    &lt;/target&gt;
    &lt;target name="example.test"&gt;
           &lt;test app="example" /&gt;
    &lt;/target&gt;
    &lt;target name="example" depends="example.deploy.devtest, example.test" /&gt;
    &lt;target name="build" depends="example" /&gt;
&lt;/project&gt;
</code></pre></p>

<p>The large comment block is just helpful for other developers trying to
add another application. From here, to try this out:</p>

<p><pre><code>$ ant
</code></pre></p>

<p>It should run the deployment, and then run the test suites. If that works as you expect, then just configure your continuous integration server to run Ant over that file on every submit.</p>

<p>Hopefully this is of use to someone. Though this is how our
environment is configured, I have written this all from memory, so I
might have missed a critical step somewhere. Please let me know if
there&#8217;s anything that needs to be changed.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.overwatering.org/blog/2008/12/automatic-deployment-for-rails/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

