<?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>Carbon Silk &#187; upstart</title>
	<atom:link href="http://www.carbonsilk.com/tag/upstart/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.carbonsilk.com</link>
	<description>Developing Ideas by James Broad</description>
	<lastBuildDate>Tue, 08 Jan 2013 00:00:38 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.2</generator>
		<item>
		<title>Automated deployment of node.js apps</title>
		<link>http://www.carbonsilk.com/node/deploying-nodejs-apps/</link>
		<comments>http://www.carbonsilk.com/node/deploying-nodejs-apps/#comments</comments>
		<pubDate>Sun, 20 Mar 2011 00:28:11 +0000</pubDate>
		<dc:creator>James Broad</dc:creator>
				<category><![CDATA[node]]></category>
		<category><![CDATA[ci]]></category>
		<category><![CDATA[continuous integration]]></category>
		<category><![CDATA[express]]></category>
		<category><![CDATA[git]]></category>
		<category><![CDATA[Jenkins]]></category>
		<category><![CDATA[JS]]></category>
		<category><![CDATA[Make]]></category>
		<category><![CDATA[Node]]></category>
		<category><![CDATA[npm]]></category>
		<category><![CDATA[Ubuntu]]></category>
		<category><![CDATA[upstart]]></category>

		<guid isPermaLink="false">http://www.carbonsilk.com/?p=9919</guid>
		<description><![CDATA[This article has a simple aim to improve the process of developing and deploying node.js web applications. The Continuous Integration folks will refer to what I am covering as an aptly named Automated Deployment. As you explore this guide you will learn: How to run a node.js script using Upstart. How to manage Jenkins (was [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://farm5.static.flickr.com/4038/4302501803_957d51a01a.jpg" alt="Delivering the goods" /></p>
<p class="clear">This article has a simple aim to improve the process of developing and deploying <a href="http://nodejs.org/">node.js</a> web applications. The Continuous Integration folks will refer to what I am covering as an aptly named <a href="http://en.wikipedia.org/wiki/Continuous_integration#Automate_deployment">Automated Deployment</a>. As you explore this guide you will learn:</p>
<ul>
<li>How to run a node.js script using Upstart.</li>
<li>How to manage Jenkins (was Hudson) with Git.</li>
<li>How to make and use Makefiles.</li>
</ul>
<p>Lets crack on&#8230;</p>
<p>I have detailed an environment scheme below which works well for me. You are free to assign your machine names what you please but following this guide will be easiest if you follow my conventions.</p>
<table>
<thead>
<tr>
<th>Hostname</th>
<th>Description</th>
<th>Operating System</th>
</tr>
</thead>
<tr>
<td><em class="localhost">localhost</em></td>
<td>Your machine you develop from</td>
<td>Mac / Windows / Linux</td>
</tr>
<tr class="odd">
<td><em class="ci-app">ci.app</em></td>
<td>Jenkins and Git Server</td>
<td>Ubuntu</td>
</tr>
<tr>
<td><em class="int-app">int.app</em></td>
<td>node.js application server</td>
<td>Ubuntu</td>
</tr>
</table>
<h3>Getting a working node.js app and environment</h3>
<p>You will need to have an environment to run your app in. To save on resources I prefer using virtual machines hosted on my <em class="localhost">localhost</em>. This also means you are not tied to a network in order to develop (great for coding on the train/in a coffee shop).</p>
<p>You can run node.js apps on your <em class="localhost">localhost</em> but for this article we are just focusing the deployment process to an integration server (<em class="int-app">int.app</em>).</p>
<p>The correct development flow for a node.js app should be that you test on <em class="localhost">localhost</em>, then checkin when you are happy with your changes. The checkin will then trigger a build and automatically get built to <em class="int-app">int.app</em>. We will be in this sweet spot by the end of this article.</p>
<h4>Setting up your virtual machines</h4>
<p>It is fairly straightforward (and free) to get a Linux virtual machine up and running these days. Following the steps on <a href="https://help.ubuntu.com/community/VirtualBox">https://help.ubuntu.com/community/VirtualBox</a> should be a good starting point.<br />
I suggest you set up one machine (<em class="int-app">int.app</em>) first with a barebones configuration and clone it to create <em class="ci-app">ci.app</em>, changing the hostname (<code>sudo hostname newhostname</code>) and getting a new IP address <code>sudo dhclient</code> on the cloned machine.</p>
<h4>Configuring <em class="int-app">int.app</em></h4>
<p>Our initial development centric dependencies are <a href="http://git-scm.com/">Git</a>, <a href="http://nodejs.org/">node.js</a>, <a href="http://expressjs.com/">Express</a> and <a href="http://npmjs.org/">NPM</a>.</p>
<p>Login to <em class="int-app">int.app</em></p>
<pre>
ssh int.app
</pre>
<p>Install Git</p>
<pre>
apt-get install git git-core
</pre>
<p>Install node.js</p>
<pre>
cd /tmp/
git clone https://github.com/joyent/node.git
cd node
./configure
make
make install
</pre>
<p>Install NPM</p>
<pre>
apt-get install curl
curl http://npmjs.org/install.sh | sudo sh
</pre>
<p>Install Express</p>
<pre>
npm install express
</pre>
<p>Now you have everything you need to get a node.js app up and running. Let&#8217;s create our demo app.</p>
<pre>
mkdir -p /data/apps/hello_world
</pre>
<p>Add these contents to <code>/data/apps/hello_world/app.js</code>.</p>
<pre>
var express = require('express')
  , app = express.createServer()

app.get('/', function(req, res){
  res.send([
      '&lt;h1&gt;Hello World&lt;/h1&gt;'
  ].join('\n'));
});

app.listen(3000);
console.log('Express app started on port 3000');
</pre>
<h4>Run your node.js app</h4>
<pre>
node /data/apps/hello_world/app.js
</pre>
<h4>Getting and using the IP address of <em class="int-app">int.app</em></h4>
<p>SSH into <em class="int-app">int.app</em> and run:</p>
<pre>
ifconfig -a
</pre>
<ol>
<li>Grab the IP address from &#8216;inet addr&#8217;. This should look something like: <code>192.188.0.23</code>.</li>
<li>Add this to your <code>/etc/hosts</code> file on your <em class="localhost">localhost</em> like: <code>192.188.0.23 int.app</code></li>
</ol>
<h4>Testing the app</h4>
<p>Now let&#8217;s test if everything is running ok. Open up <a href="http://int.app:3000/">http://int.app:3000/</a>. With any luck you should be seeing the text &#8220;Hello World&#8221;.</p>
<h3>Getting a build process working</h3>
<p>We will be using Git as our source control management tool (<a href="http://en.wikipedia.org/wiki/Source_Code_Management">VCS</a>). Have a read of Joel Spolsky&#8217;s <a href="http://www.joelonsoftware.com/items/2010/03/17.html">distributed VCS post</a> to understand why we are using Git over something like <a href="http://subversion.tigris.org/"><abbr title="Subversion">SVN</abbr></a>.</p>
<h4>Setting up a Git server on <em class="ci-app">ci.app</em></h4>
<p>Git works in a de-centralised way but we need to have a central Git repository that we and other developers can push their changes to and other tools like Jenkins can pull updates from.</p>
<p>On <em class="ci-app">ci.app</em> run:</p>
<pre>
useradd git
su git
sudo mkdir -p /data/git/hello_world.git
sudo chown git -R /data/git/hello_world.git
cd /data/git/hello_world.git
git --bare init
curl http://utsl.gen.nz/git/post-update > /data/git/hello_world.git/hooks/post-update
exit
</pre>
<p>On <em class="localhost">localhost</em> you can now run:</p>
<pre>
git clone ssh://ci.app/data/git/hello_world.git ~/hello_world
cd hello_world
git remote add origin ssh://git@ci.app/data/git/hello_world.git
</pre>
<p>Now you have an empty directory on your local machine where you can develop from. Let&#8217;s add <code>app.js</code> we made earlier to this repository.</p>
<p>Add these contents to <code> ~/hello_world/app.js</code></p>
<pre>
var express = require('express')
  , app = express.createServer()

app.get('/', function(req, res){
  res.send([
      '&lt;h1&gt;Hello World&lt;/h1&gt;'
  ].join('\n'));
});

app.listen(3000);
console.log('Express app started on port 3000');
</pre>
<p>Now lets check it in.</p>
<pre>
git add --all
git commit -m 'Initial checkin' app.js
</pre>
<p>Let&#8217;s push it to our remote Git server</p>
<pre>
git push origin master
</pre>
<h4>Moving the app automatically</h4>
<p>We are going to use <a href="http://www.gnu.org/software/make/">make</a> as a tool to bundle commands. We will use Make to move the application from the Git repository to the <code>/data/apps/hello_world</code> directory.</p>
<p>On <em class="localhost">localhost</em>, add these contents to <code>~/hello_world/Makefile</code></p>
<pre>
install :
	sudo mkdir -p /data/apps/hello_world
	sudo cp ./app.js /data/apps/hello_world/
</pre>
<p>Check this file in and push changes:</p>
<pre>
git add --all
git commit -m 'adding build steps'
git push origin master
</pre>
<p>Now you could run this command and have the app put in the correct location for you:</p>
<pre>
make install
</pre>
<h3>Automating the build</h3>
<p>Jenkins is a very useful tool in the process of <a href="http://en.wikipedia.org/wiki/Continuous_integration">Continuous Integration</a>. At the most basic level Jenkins is a tool to allow you to schedule things to happen based on timings or events such as a code checkin.</p>
<h4>Installing Jenkins</h4>
<p>Let&#8217;s install Jenkins on <em class="ci-app">ci.app</em>.</p>
<pre>
apt-get install jenkins
</pre>
<p>Once installed you should be able to load Jenkins in your browser: at <a href="http://ci.app:8080/">http://ci.app:8080/</a>.</p>
<h4>Adding a new job to Jenkins</h4>
<p>Here you can add a &#8216;job&#8217; by:</p>
<ol>
<li>Selecting &#8216;<a href="http://ci.app:8080/view/All/newJob">New Job</a>&#8216;.</li>
<li>Call the Job name &#8220;Hello World&#8221;.</li>
<li>Select the option &#8216;Build a free-style software project&#8217;.</li>
<li>Save</li>
</ol>
<p>Once created you will be met with a page with a bunch of inputs. We will come back to that in a moment but first we need to add Git support to Jenkins. </p>
<h4>Adding Git support to Jenkins</h4>
<ol>
<li>Go to <a href="http://ci.app:8080/pluginManager/available">http://ci.app:8080/pluginManager/available</a>.</li>
<li>Click on the enable Git Plugin checkbox.</li>
<li>Click install at the bottom of the page.</li>
<li>Return back to your job configuration page (<a href="http://ci.app:8080/job/Hello%20World/configure">http://ci.app:8080/job/Hello World/configure</a>)</li>
<li>Select the Git option under the Source Code Management section.</li>
<li>Add this url into the url field: <code>ssh://git@localhost/data/git/hello_world.git</code></li>
<li>Select the Poll SCM checkbox under Build Triggers and enter: <code>* * * * *</code> (This will make Jenkins check every minute for changes to source files.)</li>
<li>Save the configuration</li>
<li>Click &#8216;build now&#8217; to check everything is working ok.</li>
</ol>
<p>As it stands, the Jenkins job is not being all that useful but we will discover it&#8217;s power later on.</p>
<h3>Deploying your app using Upstart</h3>
<p>Node.js scripts need to be run as daemons for hosting websites, which can make the deployment process a bit fiddly. We can use tools like <a href="http://upstart.ubuntu.com/">Upstart</a> to run and manage node.js scripts.</p>
<h4>Install Upstart</h4>
<pre>
apt-get install upstart
</pre>
<h4>Adding Upstart app configuration</h4>
<p>On <em class="localhost">localhost</em>, add these contents to <code>~/hello_world/hello_world.conf</code></p>
<pre>
#!upstart
description "node.js hello world app server"
author      "James Broad"

start on startup
stop on shutdown

script
    export HOME="/root"

    exec /usr/local/bin/node /data/apps/hello_world/app.js 2>&#038;1 >> /var/log/node.log
end script
</pre>
<p>We will be putting this file into <code>/etc/event.d/hello_world</code> on <em class="int-app">int.app</em> later on. Once in that location you can run <code>sudo start hello_world</code></p>
<h3>Continuously deploying your app</h3>
<p>Now that Jenkins is set up to keep an eye on our files, we can issue it some commands to deploy and run the app.js application.</p>
<p>Let&#8217;s return to <em class="localhost">localhost</em> and add a deployment step to Makefile. This will copy the files needed to install and run our application.<br />
Add these contents to <code>~/hello_world/Makefile</code></p>
<pre>
app_dir = /data/apps/hello_world
temp_install_dir = /tmp/hello_world
deployment_hostname = int.app

install :
	sudo mkdir -p $(app_dir)
	sudo cp ./app.js $(app_dir)/app.js
	sudo cp ./hello_world.conf /etc/event.d/hello_world

deploy :
	# Get rid of old temp installs
	ssh $(deployment_hostname) sudo rm -rf $(temp_install_dir)
	# Copy files over to remote machine
	rsync -r . $(deployment_hostname):$(temp_install_dir)
	# Install our app to the right location
	ssh $(deployment_hostname) cd $(temp_install_dir)\; make install
	ssh $(deployment_hostname) cd $(temp_install_dir)\; make start_app

start_app :
	sudo start  --no-wait -q hello_world
</pre>
<p>Check in and push your changes.</p>
<pre>
git add --all
git commit -m 'adding deployment steps'
git push origin master
</pre>
<h4>Handling environment permissions</h4>
<p>On <em class="int-app">int.app</em> you will NEED to set up your SSH Keys to allow passwordless ssh access and add paswordless sudo rights for the user jenkins so it can carry out tasks that require sudo such as service restarting.</p>
<h4>Passwordless SSH access</h4>
<p>To set up passwordless SSH access for Jenkins, log into <em class="ci-app">ci.app</em> as the user jenkins <code>ssh jenkins@ci.app</code>. Run and complete the prompts:</p>
<pre>
ssh-keygen
</pre>
<p>Copy the output of <code>~/.ssh/id_dsa.pub</code> or <code>~/.ssh/id_rsa.pub</code> (depending on what option you chose) to the file <code>~/.ssh/authorized_keys</code> on <em class="int-app">int.app</em>.</p>
<p>Now SSH to <em class="int-app">int.app</em> and add the jenkins user <code>adduser jenkins</code>, this will be needed for when Jenkins is installing stuff onto <em class="int-app">int.app</em>.</p>
<p>Now we need to log into <em class="ci-app">ci.app</em> as user jenkins to be able to accept the SSH Keys:</p>
<pre>
ssh jenkins@ci.app
ssh jenkins@int.app
exit
exit
</pre>
<h4>Passwordless sudo rights</h4>
<p>For enabling passwordless sudo rights to the jenkins user you will need to run: <code>sudo visudo</code> and append the file with the contents:</p>
<pre>
# Allow Jenkins to run certain commands as sudo without password
jenkins ALL = NOPASSWD: /bin/mkdir, /bin/rm, /bin/cp, /sbin/start, /sbin/stop
</pre>
<h4>Adding shell script stages to Jenkins</h4>
<p>Go to your hello world configuration panel (<a href="http://ci.app:8080/job/Hello%20World">http://ci.app:8080/job/Hello World</a>) and choose &#8216;add build step&#8217; under the Build section.</p>
<ol>
<li>Select &#8216;Execute shell&#8217; and add this line: <code>make deploy</code>.</li>
<li>Save the settings.</li>
<li>Run a build.</li>
</ol>
<p>If everything went according to plan you should be able to load <a href="http://int.app:3000">http://int.app:3000</a> and see the text &#8216;Hello World&#8217;.</p>
<h3>Verifying deployments</h3>
<p>Instead of us having to check the app is running every time a build happens, we can get Jenkins to do that for us by checking a url returns 200 response code.</p>
<p>Lets add a new Freestyle job in Jenkins and call it &#8220;Hello World Monitor&#8221;. Leave everything blank but click the checkbox for Monitor Site and add the url <code>http://int.app:3000/</code>.</p>
<p>We can now return back to our <a href="http://ci.app:8080/job/Hello%20World/configure">Hello World job</a> and check the Build other projects checkbox. Then add <code>Hello World Monitor</code> (it will auto-suggest). Save this configuration and run a build.</p>
<h3>Finally</h3>
<p>If you have made it this far, thanks for reading, hopefully everything went well?</p>
<p>If everything went well, you are in a good place to start adding more Continuous Integration elements such as unit tests, JSLinting and many other automated steps.</p>
<p>There are a number of things I didn&#8217;t go into much depth on such as the node.js app itself, there are plenty of articles out there about how to do this. A good resource worth checking out is <a href="http://howtonode.org/">http://howtonode.org</a>.</p>
<p>Feel free to get in touch if you need any help but be warned I have a busy day job so I may take time to respond.</p>
<p>Image courtesy of <a href="http://www.flickr.com/photos/kulor/4302501803/">Me</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.carbonsilk.com/node/deploying-nodejs-apps/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
	</channel>
</rss>
