<?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>Eyefodder &#187; Ruby</title>
	<atom:link href="http://eyefodder.com/category/engineering/ruby/feed" rel="self" type="application/rss+xml" />
	<link>http://eyefodder.com</link>
	<description></description>
	<lastBuildDate>Sat, 26 Aug 2017 21:12:17 +0000</lastBuildDate>
	<language>en-US</language>
		<sy:updatePeriod>hourly</sy:updatePeriod>
		<sy:updateFrequency>1</sy:updateFrequency>
	
	<item>
		<title>Code Coverage — a simple Rails example</title>
		<link>http://eyefodder.com/2014/09/code-coverage-setup-rails.html</link>
		<comments>http://eyefodder.com/2014/09/code-coverage-setup-rails.html#comments</comments>
		<pubDate>Thu, 18 Sep 2014 13:13:40 +0000</pubDate>
		<dc:creator><![CDATA[Paul Barnes-Hoggett]]></dc:creator>
				<category><![CDATA[Agile Software Development]]></category>
		<category><![CDATA[Continuous Integration]]></category>
		<category><![CDATA[Engineering]]></category>
		<category><![CDATA[Quality Software]]></category>
		<category><![CDATA[Rails]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Software Craftsmanship]]></category>
		<category><![CDATA[Test Driven Development]]></category>

		<guid isPermaLink="false">http://eyefodder.com/?p=208</guid>
		<description><![CDATA[<p>My tests are my safety net. With them I can refactor with confidence, knowing that I&#8217;m keeping the functionality I intended. With them, I can grow my codebase, knowing that I&#8217;m not introducing regression errors. How do I have confidence that my safety net is good enough? One metric I can use to help with this [&#8230;]</p>
<p>The post <a rel="nofollow" href="http://eyefodder.com/2014/09/code-coverage-setup-rails.html">Code Coverage — a simple Rails example</a> appeared first on <a rel="nofollow" href="http://eyefodder.com">Eyefodder</a>.</p>
]]></description>
				<content:encoded><![CDATA[<p>My tests are my safety net. With them I can refactor with confidence, knowing that I&#8217;m keeping the functionality I intended. With them, I can grow my codebase, knowing that I&#8217;m not introducing regression errors. How do I have confidence that my safety net is good enough? One metric I can use to help with this is code coverage. It answers the question “When I run my tests, how much of my application code executed?”. It&#8217;s a somewhat crude metric—telling me how broad the net is not how strong—but it’s a good place to start. Fortunately, setting it up on a rails project is pretty simple.</p>
<div id="attachment_217" style="width: 550px" class="wp-caption alignnone"><a href="http://upload.wikimedia.org/wikipedia/commons/3/35/Group_of_Circus_Performers_WDL10692.png"><img class="size-large wp-image-217" src="http://eyefodder.com/wp-content/uploads/2014/09/Group_of_Circus_Performers_WDL10692-1024x747.png" alt="circus perfomers in a safety net" width="540" height="393" /></a><p class="wp-caption-text">See how happy people are when they have a safety net?</p></div>
<p><span id="more-208"></span></p>
<h2>Getting Started</h2>
<p>I&#8217;ve made a simple example app that shows code coverage in action. Check out the source code from the <code>code_coverage</code> branch of my <a href="https://github.com/eyefodder/spex" target="_blank">spex</a> repository:</p>
<pre class="brush: bash; gutter: false; title: ; notranslate">

git clone -b code_coverage --single-branch https://github.com/eyefodder/spex.git

</pre>
<p>Now go to the <code>ops</code> directory and run <code>vagrant up</code> To get the virtual machine running. Next, let&#8217;s hop into the virtual machine and run the test suite:</p>
<pre class="brush: bash; gutter: false; title: ; notranslate">
vagrant ssh
...some output...
vagrant@spex:~$ cd /app
vagrant@spex:~$ rspec

</pre>
<p>Now, check out the reports folder. You&#8217;ll see that there is a <code>coverage/rcov</code> folder. Open the index file in the browser and you see an easy to digest code coverage report:<br />
<a href="http://eyefodder.com/wp-content/uploads/2014/09/Screen-Shot-2014-09-17-at-10.08.41-AM.png"><img class="alignnone size-large wp-image-219" src="http://eyefodder.com/wp-content/uploads/2014/09/Screen-Shot-2014-09-17-at-10.08.41-AM-1024x617.png" alt="code coverage report" width="540" height="325" /></a><br />
Pretty nifty huh? You can click on the rows in the table to see each class in more detail, and find out exactly which lines aren&#8217;t being executed:<br />
<a href="http://eyefodder.com/wp-content/uploads/2014/09/Screen-Shot-2014-09-17-at-10.15.28-AM.png"><img class="alignnone size-large wp-image-221" src="http://eyefodder.com/wp-content/uploads/2014/09/Screen-Shot-2014-09-17-at-10.15.28-AM-1024x826.png" alt="code coverage metrics for a single file" width="540" height="435" /></a><br />
Let&#8217;s take a look at how this was all set up&#8230;</p>
<h2>Code Coverage Gems</h2>
<p>First up, we need to add a couple of gems to the <code>Gemfile</code>:</p>
<pre class="brush: ruby; first-line: 59; title: ; notranslate">
  # code_coverage
  gem 'simplecov', :require =&gt; false
  gem 'simplecov-rcov', :require =&gt; false
</pre>
<p>Once we&#8217;ve run a <code>bundle install</code>, our next step is to configure our test suite to generate coverage reports.</p>
<h2>Configuring Code Coverage</h2>
<p>This, again is a pretty simple affair. We need to launch SimpleCov before any application code has run, so we have this code at the top of the <code>spec/spec_helper.rb</code></p>
<pre class="brush: ruby; title: ; notranslate">
if ENV['GENERATE_COVERAGE_REPORTS'] == 'true'
  require 'simplecov'
  require 'simplecov-rcov'
  SimpleCov.start 'rails' do
    coverage_dir ENV['CI_COVERAGE_REPORTS']
  end
  SimpleCov.formatter = SimpleCov::Formatter::RcovFormatter
end
</pre>
<p>There&#8217;s a few things happening here. We have a couple of environment variables that tell us if we should create reports: <code>GENERATE_COVERAGE_REPORTS</code> and if we do, where we should put them: <code>CI_COVERAGE_REPORTS</code>. If you&#8217;ve followed my earlier post on getting <a title="Getting Growl notifications from your Virtual Machine" href="http://eyefodder.com/2014/09/growl-guard-virtual-machine.html">Guard to send Growl</a> notifications, you will know to find these in <code>ops/dotfiles/guest_bash_profile</code> which is a profile automatically generated when we launch the virtual machine with <code>vagrant up</code>. If not, well, now you do!<br />
The next thing you&#8217;ll notice is the <code>SimpleCov.start 'rails'</code> call on line 4. This configures SimpleCov to have a profile that is good for most Rails applications. For example, the <code>spec</code> and <code>config</code> folders are excluded from coverage stats. You can read more about profiles <a href="https://github.com/colszowka/simplecov#profiles">here</a>.<br />
Finally, we tell SimpleCov that we want to format our results with the <code>SimpleCov::Formatter::RcovFormatter</code>. When we get to running our build as part of a <a href="http://en.wikipedia.org/wiki/Continuous_integration">continuous integration</a> process with <a href="http://jenkins-ci.org/">Jenkins</a>, we can use this format to parse results to be viewed in the dashboard.</p>
<h2>Viewing Code Coverage Reports generated on a Guest VM</h2>
<p>The last thing we have to deal with is the fact that the reports are generated on the guest virtual machine. In our <a title="Using Puppet and Vagrant to make a one-click development environment" href="http://eyefodder.com/2014/08/one-click-development-environment.html">existing setup</a>, we use <code>rsync</code> to push code changes from the host to the virtual machine. But this only works one way, and if we add content within the virtual machine you won&#8217;t see them on the host. We solve this with these lines in the <code>Vagrantfile</code></p>
<pre class="brush: ruby; first-line: 21; title: ; notranslate">
  config.vm.synced_folder '../reports', '/reports'
  config.vm.synced_folder &quot;../&quot;, &quot;/app&quot;, type: &quot;rsync&quot;, rsync__exclude: [&quot;.git/&quot;, &quot;ops/*&quot;, &quot;reports/&quot;, &quot;tmp/&quot;, &quot;log/&quot;, &quot;.#*&quot;]
</pre>
<p>What this does is exclude the <code>reports</code> from the main <code>rsync</code> and instead setup a new (regular) shared folder that will map <code>reports</code> to <code>/reports</code> on the virtual machine (note this is a root level folder, not in the <code>/app</code> folder on the guest. This is why we have used an environment variable to tell SimpleCov where to output reports.</p>
<h2>Beware the emperor&#8217;s new code coverage</h2>
<p><a href="http://commons.wikimedia.org/wiki/File:Page_45_illustration_in_fairy_tales_of_Andersen_(Stratton).png"><img class="alignnone size-large wp-image-222" src="http://eyefodder.com/wp-content/uploads/2014/09/emperors_new_clothes-754x1024.png" alt="emperors_new_clothes" width="540" height="733" /></a></p>
<p>One thing to bear in mind is that code coverage really is a very crude metric. There are different types of coverage metrics, and SimpleCov only provides &#8216;C0&#8242; coverage: lines of code that executed. Other types include <a href="http://www.bignerdranch.com/blog/code-coverage-and-ruby-1-9/" target="_blank">branch and path</a> coverage, but as far as I know, there aren&#8217;t any tools for these in Ruby. Let me show you an example of where this falls down:</p>
<p><a href="http://eyefodder.com/wp-content/uploads/2014/09/Screen-Shot-2014-09-17-at-10.15.28-AM.png"><img class="alignnone size-large wp-image-221" src="http://eyefodder.com/wp-content/uploads/2014/09/Screen-Shot-2014-09-17-at-10.15.28-AM-1024x826.png" alt="code coverage metrics for a single file" width="540" height="435" /></a></p>
<p>If you look at this report, we can see that the <code>some_method_with_conditionals</code> gets called, but only the <code>say_yes</code> path (lines 12 and 13) executes, and we never confirm that &#8216;no&#8217; gets sent if we pass <code>false</code> to the method. So far, so good, until we look at <code>some_method_with_ternary</code>. This is basically the same method refactored to be more compact, and with the same tests run against it. Yet we are told it is totally covered. So is the metric even still useful?</p>
<p>I still think code coverage is a valuable metric, if only to show you where there are holes in your test suite. If you go in with this knowledge and understanding the limitations, then you will be better equipped to maintain the quality of your app over time.</p>
<h2>Code Coverage is a temporal metric</h2>
<p>The last thing I want to mention about code coverage is that it&#8217;s useful to understand how your coverage changes over time. Particularly if you are managing a team of developers, it provides a quick warning if developers are slipping on their test writing. If you have a Continuous Integration machine, you can track these sort of metrics over time, which can really help you get a sense of where things are headed.<br />
In my next post I&#8217;ll show how to set up your very own CI machine with just a few clicks&#8230;</p>
<p>The post <a rel="nofollow" href="http://eyefodder.com/2014/09/code-coverage-setup-rails.html">Code Coverage — a simple Rails example</a> appeared first on <a rel="nofollow" href="http://eyefodder.com">Eyefodder</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://eyefodder.com/2014/09/code-coverage-setup-rails.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Getting Growl notifications from your Virtual Machine</title>
		<link>http://eyefodder.com/2014/09/growl-guard-virtual-machine.html</link>
		<comments>http://eyefodder.com/2014/09/growl-guard-virtual-machine.html#comments</comments>
		<pubDate>Sun, 07 Sep 2014 19:07:17 +0000</pubDate>
		<dc:creator><![CDATA[Paul Barnes-Hoggett]]></dc:creator>
				<category><![CDATA[Agile Software Development]]></category>
		<category><![CDATA[Engineering]]></category>
		<category><![CDATA[Quality Software]]></category>
		<category><![CDATA[Rails]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Software Craftsmanship]]></category>
		<category><![CDATA[Test Driven Development]]></category>

		<guid isPermaLink="false">http://eyefodder.com/?p=197</guid>
		<description><![CDATA[<p>As I develop I have Guard running in the background, executing my tests when things change. But I often don&#8217;t have the Terminal window front and centre, so I like to have Growl notifications for my test results. Setting up Growl to push notifications from the Virtual Machine to the host is a little tricky, so here&#8217;s a [&#8230;]</p>
<p>The post <a rel="nofollow" href="http://eyefodder.com/2014/09/growl-guard-virtual-machine.html">Getting Growl notifications from your Virtual Machine</a> appeared first on <a rel="nofollow" href="http://eyefodder.com">Eyefodder</a>.</p>
]]></description>
				<content:encoded><![CDATA[<p>As I develop I have <a title="Getting Started with Guard and RSpec" href="http://eyefodder.com/2014/09/guard-and-rspec.html">Guard running in the background</a>, executing my tests when things change. But I often don&#8217;t have the Terminal window front and centre, so I like to have Growl notifications for my test results. Setting up Growl to push notifications from the Virtual Machine to the host is a little tricky, so here&#8217;s a simple example to show how to do it.</p>
<div id="attachment_199" style="width: 544px" class="wp-caption alignnone"><a href="http://en.wikipedia.org/wiki/Old_Ephraim"><img class="size-full wp-image-199" src="http://eyefodder.com/wp-content/uploads/2014/09/Ursus_arctos_Dessin_ours_brun_grand.jpg" alt="Growl isn't as hairy as 'Old Ephraim' but is probably more useful" width="534" height="336" /></a><p class="wp-caption-text">Growl isn&#8217;t as hairy as &#8216;old Ephraim&#8217; but is probably more useful</p></div>
<p><span id="more-197"></span></p>
<h2>Getting Started</h2>
<p>To get started and see Growl notifications, we need to do a little more than normal. Let&#8217;s start by checking out the source code:</p>
<pre class="brush: bash; gutter: false; title: ; notranslate">

git clone -b guard_growl --single-branch https://github.com/eyefodder/spex.git

</pre>
<h3>Prepping Growl</h3>
<p>Download <a href="http://growl.info/" target="_blank">Growl</a> if you don&#8217;t already have it. Once it&#8217;s installed, open up the preferences pane and click on the &#8216;network&#8217; panel. Check the box marked &#8216;Listen for incoming notifications&#8217; and enter a password:<br />
<a href="http://eyefodder.com/wp-content/uploads/2014/09/Screen-Shot-2014-09-06-at-11.06.13-AM.png"><img class="alignnone size-large wp-image-201" src="http://eyefodder.com/wp-content/uploads/2014/09/Screen-Shot-2014-09-06-at-11.06.13-AM-1024x1006.png" alt="Screen Shot 2014-09-06 at 11.06.13 AM" width="540" height="530" /></a></p>
<h3>Prepping Vagrant</h3>
<p>The next thing we need to do is install a plugin for Vagrant that will allow us to execute a script <em>on the host machine</em> when we boot our virtual machine. I&#8217;ll explain what that is below the fold, but for now, make sure that you have Vagrant 1.6.4 or above (hint, type <code>vagrant -v</code> to find out) and update if necessary. Next, install the <a href="https://github.com/emyl/vagrant-triggers" target="_blank">Vagrant Triggers</a> plugin by entering this on the command line:</p>
<pre class="brush: bash; gutter: false; title: ; notranslate">
$ vagrant plugin install vagrant-triggers
</pre>
<h3>Bringing the virtual machine up</h3>
<p>Next, run <code>vagrant up</code> as usual. When it runs, it creates a file <code>ops/dotfiles/guest_bash_profile</code>:</p>
<pre class="brush: bash; title: ; notranslate">
# Edit the following values with your own values

# Growl password set in Growl &gt; Preferences &gt; Network
export GROWL_PASSWORD=enter_growl_password

# The following entries are automatically generated
# Do not edit unless you know what you are doing!
# They are regenerated each time the virtual machine is rebooted
export HOST_IP=10.0.1.28

</pre>
<p>Go ahead and enter your Growl password. Now we&#8217;re good to go. run <code>vagrant rsync-auto</code> to keep things in sync and in another window, then <code>vagrant ssh</code> into the machine.</p>
<h3>Hey presto! Growl notifications</h3>
<p>So let&#8217;s get up and running! When you are ssh&#8217;d into the host machine, fire up Guard with <code>cd /app &amp;&amp; bundle exec guard -p</code>. Make a change to your code and when the tests run you should see a notification:<br />
<a href="http://eyefodder.com/wp-content/uploads/2014/09/Screen-Shot-2014-09-06-at-11.52.50-AM.png"><img class="alignnone size-large wp-image-202" src="http://eyefodder.com/wp-content/uploads/2014/09/Screen-Shot-2014-09-06-at-11.52.50-AM-1024x414.png" alt="Growl Notification" width="540" height="218" /></a><br />
You&#8217;re good to go now, but read on if you&#8217;d like to understand how all this stuff works&#8230;</p>
<h2>How it works</h2>
<p>There are a lot of moving parts to get this thing going. I&#8217;m going to work from the Guardfile back, and then the Vagrantfile forwards. I guess we&#8217;ll meet somewhere in the middle so bear with me.</p>
<h3>Guardfile changes</h3>
<p>If you look in the Guardfile you will see a few new lines of code at the top:</p>
<pre class="brush: ruby; first-line: 15; title: ; notranslate">
host_ip = ENV['HOST_IP']
growl_pass = ENV['GROWL_PASSWORD']
PLACEHOLDER_GROWL_PASS='enter_growl_password'

if host_ip.blank? || growl_pass.blank? || (growl_pass==PLACEHOLDER_GROWL_PASS)
  puts 'host notifcations off: you must set HOST_IP and GROWL_PASSWORD in ops/dotfiles/guest_bash_profile'
  notification :off
else
  notification :gntp, :sticky =&gt; false, :host =&gt; ENV['HOST_IP'], :password =&gt; ENV['GROWL_PASSWORD']
end
</pre>
<p>Line 23 tells Guard to use GNTP for notifications (assuming HOST_IP and GROWL_PASSWORD have been set). It&#8217;s basically a means for sending Growl notifications over a network. You&#8217;ll need to add the GNTP gem, so we&#8217;ve added this to our Gemfile:</p>
<pre class="brush: ruby; first-line: 49; title: ; notranslate">
  # guard_growl
  gem 'ruby_gntp'
</pre>
<p>We can see that this system relies on a couple of environment variables being set. We got a clue that these get set in <code>ops/dotfiles/guest_bash_profile</code>, let&#8217;s see how that file gets created, and how we get that linked into the guest Virtual Machine.</p>
<h3>Creating the guest bash profile</h3>
<p>For this setup to work, we need two environment variables set: GROWL_PASSWORD and HOST_IP. The script that creates the file is <code>ops/setup_guest_bash_profile</code>. This script does a few things, so let&#8217;s step through it:</p>
<h4>Create a profile file if it doesn&#8217;t exist</h4>
<p>The first thing we want to do is create a profile file if one doesn&#8217;t exist already:</p>
<pre class="brush: bash; first-line: 42; title: ; notranslate">
# make a profile file if it doesn't exist
bash_file=dotfiles/guest_bash_profile
touch $bash_file
</pre>
<p>Next, we want to add some comments to the file. As we will run this script <em>every time the machine boots up</em> we only really want to add these comments if they don&#8217;t already exist. The <code>add_comments_once</code> function does this by checking for a match, and only adding the comment if it isn&#8217;t already in the file:</p>
<pre class="brush: bash; first-line: 7; title: ; notranslate">
add_comment_once(){
  comment=$1
  grep -q &quot;$comment&quot; $bash_file
  if [ $? -ne 0 ]
  then
    echo &quot;# $comment&quot; &gt;&gt; $bash_file
  fi
}
</pre>
<h4>Adding a bash entry for users to change</h4>
<p>Next thing we want to do is add environment variables for users to change. But as this code is run multiple times, we actually have three scenarios:</p>
<ul>
<li>An entry doesn&#8217;t yet exist in the file: create it</li>
<li>An entry exists but hasn&#8217;t been changed from the default: tell user to change it</li>
<li>A user has entered their own value: don&#8217;t do a thing!</li>
</ul>
<p>The <code>add_bash_entry</code> function achieves this:</p>
<pre class="brush: bash; first-line: 16; title: ; notranslate">
add_bash_entry(){
  token=$1
  default=$2
  change_message=&quot;You need to enter your $1 in $bash_file&quot;
  entry_comment=$3

  grep -q $token= $bash_file
  if [ $? -eq 0  ]
  then
    grep -q $default $bash_file
    if [ $? -eq 0 ]
    then
      # entry exists, but it's set to default value from this script
      echo $change_message
    fi
  else
    # variable entry doesn't exist
    echo $change_message
    echo $entry_comment
    echo &quot;&quot; &gt;&gt; $bash_file
    echo &quot;# $entry_comment&quot; &gt;&gt; $bash_file
    echo &quot;export $token=$default&quot; &gt;&gt; $bash_file
    echo &quot; &quot; &gt;&gt; $bash_file
  fi
}
</pre>
<p>The function gets three arguments: the token <code>GROWL_PASSWORD</code>, the default value <code>enter_growl_password</code>, and a message to explain to the user what to do. If the token is found (lines 22 &amp; 23) then it looks for the default value (lines 25 &amp; 26) and prints a change message if it&#8217;s there. If the token isn&#8217;t found (lines 32-38) then we write that into the file.</p>
<h4>Adding the HOST_IP as a system generated value</h4>
<p>The next thing we want to do is add the host system&#8217;s IP address to the file so that Growl knows where to send notifications:</p>
<pre class="brush: bash; first-line: 65; title: ; notranslate">
sed -i '' '/HOST_IP/d' $bash_file
# grab current host IP
ipaddr=`ifconfig | grep &quot;inet &quot; | grep -v 127.0.0.1 | cut -d ' ' -f2`
# and write to file
echo &quot;export HOST_IP=$ipaddr&quot; &gt;&gt; $bash_file
</pre>
<p>This script deletes any existing lines with <code>HOST_IP</code> in them (line 65), then uses a little bash trickery to find the current host ip. I found out how to do it from this <a href="http://www.askdavetaylor.com/how_do_i_figure_out_my_ip_address_on_a_mac/" target="_blank">post</a>, although I needed to change the delimiter from <code>\ </code> to <code>' '</code> (that&#8217;s from an escaped space character to a space in quotes. Finally we write this out to our <code>guest_bash_profile</code> file. The next step is getting this script to run when we want it.</p>
<h3>Running the profile setup script when Vagrant starts</h3>
<p>Thanks to the <a href="https://github.com/emyl/vagrant-triggers" target="_blank">Vagrant Triggers</a> plugin, this is a really simple affair. We just add the following to our Vagrantfile:</p>
<pre class="brush: ruby; first-line: 37; title: ; notranslate">
  config.trigger.before [:up, :reload], :stdout =&gt; true do
    run &quot;sh ./setup_guest_bash_profile.sh&quot;
  end
</pre>
<p>This will run our script every time we call <code>vagrant up</code> or <code>vagrant reload</code>, ensuring that our host ip address is always up to date in the file. The <em>last</em> piece of the puzzle is to make sure we use this file to actually set environment variables on the guest machine.</p>
<h3>Linking the guest bash profile to the guest virtual machine</h3>
<p>This is a relatively simple two-part process. First thing we do is share the <code>ops/dotfiles</code> directory on the virtual machine:</p>
<pre class="brush: ruby; first-line: 20; title: ; notranslate">
  config.vm.synced_folder 'dotfiles', '/dotfiles'
</pre>
<p>Secondly, we want that file symlinked in the guest machine to <code>~/.bash_profile</code>. I created a new Puppet class to achieve this. Check out <code>ops/puppet/modules/spex/dotfile_symlink.pp</code>:</p>
<pre class="brush: ruby; first-line: 4; title: ; notranslate">
class spex::dotfile_symlink{
  file { '/home/vagrant/.bash_profile':
    ensure =&gt; link,
    target =&gt; '/dotfiles/guest_bash_profile'
  }
}
</pre>
<p>Super simple here. We tell puppet to <code>ensure</code> that <code>/home/vagrant/.bash_profile</code> is a symlink to <code>/dotfiles/guest_bash_profile</code>. In <code>ops/puppet/manifests/default.pp</code> we simply include the class with the others:</p>
<pre class="brush: ruby; first-line: 6; title: ; notranslate">
include spex::base_packages
include spex::postgres_setup
include spex::ruby_setup
include spex::dotfile_symlink
</pre>
<p>Now we have everything wired up and ready to go!</p>
<h3>Conclusion</h3>
<p>This wraps up my example for getting Growl notifications from Guard into the host machine. Although there are a bunch of steps to jump through, once it&#8217;s working I hope you&#8217;ll find it a pretty robust solution.</p>
<p>The goal in all of this is to shorten feedback loops when you develop. This process should give you some confidence that when your code changes the right tests run. The power of this is greatest when you are confident that your tests cover enough of your application such that you will know when you break things. Our next step is to look into the breadth of your tests and setting up code coverage metrics for your app&#8230;</p>
<p>The post <a rel="nofollow" href="http://eyefodder.com/2014/09/growl-guard-virtual-machine.html">Getting Growl notifications from your Virtual Machine</a> appeared first on <a rel="nofollow" href="http://eyefodder.com">Eyefodder</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://eyefodder.com/2014/09/growl-guard-virtual-machine.html/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Using Spring with RSpec and Guard to speed up testing</title>
		<link>http://eyefodder.com/2014/09/using-spring-with-rspec-and-guard.html</link>
		<comments>http://eyefodder.com/2014/09/using-spring-with-rspec-and-guard.html#comments</comments>
		<pubDate>Thu, 04 Sep 2014 23:50:58 +0000</pubDate>
		<dc:creator><![CDATA[Paul Barnes-Hoggett]]></dc:creator>
				<category><![CDATA[Agile Software Development]]></category>
		<category><![CDATA[Engineering]]></category>
		<category><![CDATA[Rails]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Software Craftsmanship]]></category>
		<category><![CDATA[Test Driven Development]]></category>

		<guid isPermaLink="false">http://eyefodder.com/?p=180</guid>
		<description><![CDATA[<p>In my last post I showed you how to setup Guard and RSpec so you can automatically run tests when things change. Now lets get things cooking on gas by using the Spring application preloader. This will mean that your app&#160;framework will only have to load once, and tests will be super zippy. Setting up [&#8230;]</p>
<p>The post <a rel="nofollow" href="http://eyefodder.com/2014/09/using-spring-with-rspec-and-guard.html">Using Spring with RSpec and Guard to speed up testing</a> appeared first on <a rel="nofollow" href="http://eyefodder.com">Eyefodder</a>.</p>
]]></description>
				<content:encoded><![CDATA[<p>In my last post I showed you how to setup <a title="Getting Started with Guard and RSpec" href="http://eyefodder.com/2014/09/guard-and-rspec.html">Guard and RSpec</a> so you can automatically run tests when things change. Now lets get things cooking on gas by using the Spring application preloader. This will mean that your app&nbsp;framework will only have to load once, and tests will be super zippy. Setting up Spring with RSpec is simple and has a huge effect on test running speeds.</p>
<p><div style="width: 360px" class="wp-caption alignnone"><a href="http://www.vh1.com/celebrity/2011-09-29/the-10-greatest-movie-shoes-of-all-time/"><img class="" src="http://www.vh1.com/celebrity/bwe/images/2011/09/Inspector-Gadget-Spring-Shoes-1317315905.jpg" alt="" width="350" height="232" /></a><p class="wp-caption-text">Things are zippier on Spring(s)</p></div><br />
<span id="more-180"></span></p>
<h2>Getting the sample code for Spring with RSpec</h2>
<p>As with all of the examples, I&#8217;ve setup the simplest possible example to show setting up Spring with RSpec. Go ahead and checkout the code here:</p>
<pre class="brush: bash; gutter: false; title: ; notranslate">

git clone -b rspec_guard_spring --single-branch https://github.com/eyefodder/spex.git

</pre>
<p>Once you have checked out the code, jump into the <code>ops</code> directory and <code>vagrant up</code>to bring the VM up. When it&#8217;s up and running, get synching running by entering <code>vagrant rsync-auto</code> and then in a new terminal window enter the following:</p>
<pre class="brush: bash; gutter: false; title: ; notranslate">
vagrant ssh
cd /app
bundle exec guard -p
</pre>
<p>And that is basically it! Now when you run your tests, you will be taking advantage of Spring! Read on to see how to get this working&#8230;</p>
<h2>How we got it working</h2>
<p>So to get this working we did a few things. First off, we added the spring commands for rspec with the <a href="https://github.com/jonleighton/spring-commands-rspec" target="_blank">spring-commands-rspec</a> gem:</p>
<pre class="brush: ruby; first-line: 46; title: ; notranslate">
  # rspec_guard_spring
  gem 'spring-commands-rspec'
</pre>
<p>Next, we let spring create the binstub for us (in the example we already did this, so you will see the outputted file at <code>bin/rspec</code>:</p>
<pre class="brush: bash; gutter: false; title: ; notranslate">
pbh$ spring binstub --all
* bin/rake: spring already present
* bin/rspec: generated with spring
* bin/rails: spring already present
</pre>
<p>Lastly, we need to update the command that Guard uses to run RSpec in the <code>Guardfile</code>:</p>
<pre class="brush: ruby; first-line: 14; title: ; notranslate">
guard :rspec, cmd: 'bin/rspec' do
  ...
end
</pre>
<h2>The &#8216;gem pristine&#8230;&#8217; warning</h2>
<p>When your specs run you will probably see an error that talks about running <code>gem pristine --all</code> to speed up loading:</p>
<pre class="brush: bash; gutter: false; title: ; notranslate">
Warning: Running `gem pristine --all` to regenerate your installed gemspecs (and deleting then reinstalling your bundle if you use bundle --path) will improve the startup performance of Spring.
</pre>
<p>I did a little digging on this and found the gems that are causing the issue using the code posted in this <a href="http://http://stackoverflow.com/questions/19726167/how-do-i-get-rid-of-spring-warning-running-gem-pristine-all" target="_blank">answer</a>:</p>
<pre class="brush: bash; gutter: false; title: ; notranslate">
vagrant@spex:/app$ ruby -e 'p Gem::Specification.stubs.reject(&amp;:stubbed?).reject(&amp;:default_gem?).map(&amp;:name)'
[&quot;bigdecimal&quot;, &quot;io-console&quot;, &quot;json&quot;, &quot;minitest&quot;, &quot;psych&quot;, &quot;rake&quot;, &quot;rdoc&quot;, &quot;test-unit&quot;]
</pre>
<p>A little more <a href="https://github.com/rails/spring/issues/320#issuecomment-50658111" target="_blank">digging</a> told me that these gems are probably system Ruby packages that appear as gems thanks to the <code>rubygems-integration</code>. Long story short, you have to live with with this warning message for now. Although startup performance might be affected, overall performance each time you run RSpec should still be great! Now you have Spring with RSpec running, our next post will show you how to set up notifications with Growl so you don&#8217;t have to keep your terminal window visible while you code.</p>
<p>The post <a rel="nofollow" href="http://eyefodder.com/2014/09/using-spring-with-rspec-and-guard.html">Using Spring with RSpec and Guard to speed up testing</a> appeared first on <a rel="nofollow" href="http://eyefodder.com">Eyefodder</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://eyefodder.com/2014/09/using-spring-with-rspec-and-guard.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Getting Started with Guard and RSpec</title>
		<link>http://eyefodder.com/2014/09/guard-and-rspec.html</link>
		<comments>http://eyefodder.com/2014/09/guard-and-rspec.html#comments</comments>
		<pubDate>Thu, 04 Sep 2014 22:23:40 +0000</pubDate>
		<dc:creator><![CDATA[Paul Barnes-Hoggett]]></dc:creator>
				<category><![CDATA[Agile Software Development]]></category>
		<category><![CDATA[Engineering]]></category>
		<category><![CDATA[Quality Software]]></category>
		<category><![CDATA[Rails]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Software Craftsmanship]]></category>
		<category><![CDATA[Test Driven Development]]></category>

		<guid isPermaLink="false">http://eyefodder.com/?p=177</guid>
		<description><![CDATA[<p>As I build out an application I want to ensure it&#8217;s behaving as I intend it. RSpec is a great framework for testing Ruby code, and is the tool I use most for my testing. But tests are pretty useless if you don&#8217;t run them, and rather than manually run tests when I change things, [&#8230;]</p>
<p>The post <a rel="nofollow" href="http://eyefodder.com/2014/09/guard-and-rspec.html">Getting Started with Guard and RSpec</a> appeared first on <a rel="nofollow" href="http://eyefodder.com">Eyefodder</a>.</p>
]]></description>
				<content:encoded><![CDATA[<p>As I build out an application I want to ensure it&#8217;s behaving as I intend it. <a href="http://rspec.info/" target="_blank">RSpec</a> is a great framework for testing Ruby code, and is the tool I use most for my testing. But tests are pretty useless if you don&#8217;t run them, and rather than manually run tests when I change things, I use <a href="http://guardgem.org/" target="_blank">Guard</a> and RSpec together. Here&#8217;s the simplest possible example for setting it up.</p>
<div id="attachment_186" style="width: 550px" class="wp-caption alignnone"><a href="http://en.wikipedia.org/wiki/Yeomen_Warders"><img class="size-large wp-image-186" src="http://eyefodder.com/wp-content/uploads/2014/09/beefeater-757x1024.jpg" alt="&quot;Detroit Publishing Co. - A Yeoman of the Guard (N.B. actually a Yeoman Warder), full restoration&quot; by Adam Cuerden, Detroit Publishing Company - http://www.loc.gov/pictures/item/2002696943/. Licensed under Public domain via Wikimedia Commons - http://commons.wikimedia.org/wiki/File:Detroit_Publishing_Co._-_A_Yeoman_of_the_Guard_(N.B._actually_a_Yeoman_Warder),_full_restoration.jpg#mediaviewer/File:Detroit_Publishing_Co._-_A_Yeoman_of_the_Guard_(N.B._actually_a_Yeoman_Warder),_full_restoration.jpg" width="540" height="730" /></a><p class="wp-caption-text">Guard and RSpec : rather fancy</p></div>
<p><span id="more-177"></span><a href="http://guardgem.org/" target="_blank">Guard</a> is a command line tool that responds to filesystem change events. You can use it to do all sorts of stuff using one of the <a href="https://github.com/guard" target="_blank">many plugins</a> that have been built for it. In our simple example we are going to use it to trigger rspec tests when we change code in our app.</p>
<h2>Getting Started</h2>
<p>For this we are going to grab an example from the Spex repository where I keep all my code examples. Clone the branch from github:</p>
<pre class="brush: bash; gutter: false; title: ; notranslate">

git clone -b rspec_guard --single-branch https://github.com/eyefodder/spex.git

</pre>
<h2>Confirming the tests run</h2>
<p>When you have the code cloned, jump into the <code>ops</code> directory and execute <code>vagrant up</code> to bring up the virtual machine that will run our example. As instructed, run <code>vagrant rsync-auto</code>; this will ensure that if you make changes to the code, they will get synced in the virtual machine (which is where Guard will be running). Now, open a new Terminal window, <code>vagrant ssh</code> into the machine and type the following:</p>
<pre class="brush: bash; gutter: false; title: ; notranslate">
cd /app
rspec
</pre>
<p>By doing this, we should see our tests run and get the following output:</p>
<pre class="brush: bash; gutter: false; title: ; notranslate">
vagrant@spex:~$ cd /app
vagrant@spex:/app$ rspec
Run options: include {:focus=&gt;true}

All examples were filtered out; ignoring {:focus=&gt;true}
....

Finished in 0.29975 seconds (files took 1.65 seconds to load)
4 examples, 0 failures

Top 4 slowest examples (0.2972 seconds, 99.1% of total time):
  Static Pages root page has the expected title
    0.27993 seconds ./spec/integration/static_pages_spec.rb:9
  ApplicationHelper #some_method_to_test returns 'result'
    0.00696 seconds ./spec/helpers.lication_helper_spec.rb:4
  Static Pages root page has a link to my blog
    0.00691 seconds ./spec/integration/static_pages_spec.rb:13
  Static Routing root path routes to the static#home path
    0.0034 seconds ./spec/routing/static_routing_spec.rb:5

Top 3 slowest example groups:
  Static Pages
    0.14342 seconds average (0.28684 seconds / 2 examples) ./spec/integration/static_pages_spec.rb:2
  ApplicationHelper
    0.00696 seconds average (0.00696 seconds / 1 example) ./spec/helpers/application_helper_spec.rb:2
  Static Routing
    0.0034 seconds average (0.0034 seconds / 1 example) ./spec/routing/static_routing_spec.rb:2

Randomized with seed 13496
</pre>
<p>So what happens if we break something? Go ahead and change the code in <code>app/helpers/application_helper.rb</code>:</p>
<pre class="brush: ruby; first-line: 2; title: ; notranslate">
  def some_method_to_test
    'resultd'
  end
</pre>
<p>Now when we run <code>rspec</code> again we see that four tests have run and one failed as expected. But really, given we just changed one file, did we have to run all the tests? And did we have to manually re-run the tests? The answers to these is no; Guard and Rspec together are pretty awesome&#8230;</p>
<h2>Guard and Rspec together</h2>
<p>To get things up and running, type in the following:</p>
<pre class="brush: bash; gutter: false; title: ; notranslate">
bundle exec guard -p
</pre>
<p>You should see something like this:</p>
<pre class="brush: bash; gutter: false; title: ; notranslate">
vagrant@spex:/app$ bundle exec guard -p
21:18:32 - INFO - Guard is using NotifySend to send notifications.
21:18:32 - INFO - Guard is using TerminalTitle to send notifications.
21:18:32 - INFO - Guard::RSpec is running
21:18:32 - INFO - Guard is now watching at '/app'
[1] guard(main)&gt; 
</pre>
<p>If you hit the <code>Enter</code> key, you&#8217;ll see all your tests run again. This is pretty awesome right? Now you can save yourself having to type <code>rspec</code>. But wait, that&#8217;s not all. Go ahead and change your <code>application_helper.rb</code> file back the way it was. Now you should see something like this:</p>
<pre class="brush: bash; gutter: false; title: ; notranslate">
21:22:13 - INFO - Running: spec/helpers/application_helper_spec.rb
Run options: include {:focus=&gt;true}

All examples were filtered out; ignoring {:focus=&gt;true}
.

Finished in 0.0103 seconds (files took 1.99 seconds to load)
1 example, 0 failures

Top 1 slowest examples (0.00878 seconds, 85.2% of total time):
  ApplicationHelper #some_method_to_test returns 'result'
    0.00878 seconds ./spec/helpers.lication_helper_spec.rb:4

Randomized with seed 15725


[1] guard(main)&gt; 
</pre>
<p>Pretty sweet huh? Basically, anytime a file changes Guard figures out what test or tests we need to run. This helps to keep my test suite running lean and fast; aiding rather than slowing down development. Let&#8217;s take a look at what we have added in our example to make things work.</p>
<h2>How this was setup</h2>
<p>First off we added two gems to the <code>Gemfile</code>:</p>
<pre class="brush: ruby; first-line: 42; title: ; notranslate">
group :development do
  # rspec_guard
  gem 'guard-rspec', require: false
end

group :development, :test do
  # rspec_guard
  gem 'rspec-rails', '~&gt; 3.0.0'
end
</pre>
<p>Next, we have written some specs which you will find in the <code>spec</code> folder. I&#8217;m not going to go into detail on the tests I&#8217;ve written, but they should be pretty legible and cover just enough to demonstrate what we need to about Guard. Check out <code>spec/helpers/application_helper.rb</code>, <code>spec/integration/static_pages_spec.rb</code>, and <code>spec/routing/static_routing_spec.rb</code>. The <code>spec/rails_helper.rb</code> and <code>spec/spec_helper.rb</code> files are the ones generated by the <code>rspec-rails</code> gem when we run <code>rails generate rspec:install</code>.</p>
<h3>The Guardfile</h3>
<p>The real magic of getting Guard and RSpec working together comes from the <code>Guardfile</code> which is used to tell Guard what to watch for, and then what to do with it. I&#8217;ve modified the file generated by <code>guard init rspec</code> to demonstrate some common usage patterns based on the way I test. Watch statements follow simple regular expressions to determine when to fire. As you build up your app, you will want to keep this file updated as you may need to introduce new patterns. Let&#8217;s take a look through some examples in the file:</p>
<h4>Run any spec that changes</h4>
<p>Any file that is in the <code>spec</code> folder and ends with &#8216;_spec.rb&#8217; will get run if it changes:</p>
<pre class="brush: ruby; first-line: 15; title: ; notranslate">
  # run a spec file if it changes
  watch(%r{^spec/.+_spec\.rb$})
</pre>
<h4>Re-run the spec if the helper files change</h4>
<p><code>spec_helper.rb</code> and <code>rails_helper.rb</code> are files that RSpec uses to setup your specs. So if they change for any reason, it would affect all the tests.</p>
<pre class="brush: ruby; first-line: 19; title: ; notranslate">
  # re-run if spec helper or rails helper changes
  watch('spec/spec_helper.rb')  { &quot;spec&quot; }
  watch('spec/rails_helper.rb')  { &quot;spec&quot; }
</pre>
<h4>Run the matching spec if something changes in the app folder</h4>
<p>As a good catch all, if a file changes in the <code>app</code> folder we want to run the corresponding spec:</p>
<pre class="brush: ruby; first-line: 25; title: ; notranslate">
  # run a file matching same path with _spec at the end
  # eg /app/models/foo.rb will run /spec/models/foo_spec.rb
  watch(%r{^app/(.+)\.rb$}) { |m| &quot;spec/#{m[1]}_spec.rb&quot; }
</pre>
<h4>Re-run the suite if support files change</h4>
<p>I put RSpec <a href="https://www.relishapp.com/rspec/rspec-core/docs/example-groups/shared-examples" target="_blank">shared examples</a> and <a href="https://www.relishapp.com/rspec/rspec-expectations/v/3-0/docs/custom-matchers/define-matcher" target="_blank">custom matchers</a> into the <code>support</code> folder, so if anything in here changes I&#8217;ll need to re-run the full suite:</p>
<pre class="brush: ruby; first-line: 33; title: ; notranslate">
  watch(%r{^spec/support/(.+)\.rb$})  { &quot;spec&quot; }
</pre>
<h4>Integration tests for views and controllers</h4>
<p>I have found it simpler to test my views and controllers by creating integration tests that mimic user behaviour (although the tests you see aren&#8217;t great examples of that, we&#8217;ll be building out better ones in a future example on using <a href="https://github.com/jnicklas/capybara" target="_blank">Capybara</a>). For this to work, I have a naming structure such that specs related to the <code>foo</code> controller (or views) live in <code>spec/integration/foo_pages_spec.rb</code>. If a controller is pluralized, that is handled well too, so that <code>app/controllers/things_controller.rb</code> will trigger <code>spec/integration/thing_pages_spec.rb</code>:</p>
<pre class="brush: ruby; first-line: 53; title: ; notranslate">
  watch(/^app\/controllers\/(.+)_controller.rb/) do |m|
    [&quot;spec/integration/#{m[1].singularize}_pages_spec.rb&quot;,
     &quot;spec/routing/#{m[1]}_routing_spec.rb&quot;]
  end
</pre>
<p>And if a view file changes, we run the corresponding integration test using this: </p>
<pre class="brush: ruby; first-line: 44; title: ; notranslate">
  # if something within a view folder changes, run that spec
  # eg app/views/static/anyfile runs /spec/integration/static_pages_spec.rb
  watch(%r{^app/views/(.+)/}) do |m|
      &quot;spec/integration/#{m[1].singularize}_pages_spec.rb&quot;
  end
</pre>
<p>If any of the overall layouts change, it could affect any of our integration tests, so we should re-run them all:</p>
<pre class="brush: ruby; first-line: 42; title: ; notranslate">
  watch(%r{^app/views/layouts/(.*)}) { 'spec/integration' }
</pre>
<h4>Abstract classes</h4>
<p>Lastly, you will inevitably end up having abstract classes in your app. If these change you probably want to run all the specs for classes that inherit from it. I only included an example for the <code>application_controller.rb</code> changing but you can extrapolate from here:</p>
<pre class="brush: ruby; first-line: 39; title: ; notranslate">
  watch('app/controllers/application_controller.rb')  { &quot;spec/integration&quot; }
</pre>
<h3>A word on Guard with polling</h3>
<p>The more astute amongst you will notice that Guard is started up with a <code>-p</code> option. This option forces Guard to poll for changes rather than listen for filesystem change notifications. The reason is that with our setup using rsync to synchronize changes in the virtual machine causes Guard to not always respond to changes. Basically an <a href="https://github.com/guard/guard/issues/495" target="_blank">earlier issue</a> with Guard-Rspec was solved by having rspec only run when it sees a file as modified rather than added. However, with rsync it seems that the <a href="https://github.com/guard/listen" target="_blank">listen</a> gem sometimes think the file has been added rather than modified. I&#8217;m trying to <a href="https://groups.google.com/forum/#!topic/guard-dev/-tx6yncq1wA" target="_blank">figure out a resolution</a> for this, but in the meantime, polling it is! </p>
<h3>But wait, there&#8217;s more!</h3>
<p>This super-simple example should help you get up and running with Guard and RSpec. With the setup here you would find that every time the specs run the Rails application has to load up. This makes every test run take at least a few seconds. In earlier versions of Rails I used to use <a href="https://coderwall.com/p/jxrhag" target="_blank">Spork</a> to pre-load the framework, but now Rails comes with <a href="https://github.com/rails/spring" target="_blank">Spring</a> which is much easier to setup. In my next post I&#8217;ll walk you through it; until then happy testing!</p>
<p>The post <a rel="nofollow" href="http://eyefodder.com/2014/09/guard-and-rspec.html">Getting Started with Guard and RSpec</a> appeared first on <a rel="nofollow" href="http://eyefodder.com">Eyefodder</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://eyefodder.com/2014/09/guard-and-rspec.html/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Compiling Ruby from source for your development environment</title>
		<link>http://eyefodder.com/2014/08/compiling-ruby-for-dev-environment.html</link>
		<comments>http://eyefodder.com/2014/08/compiling-ruby-for-dev-environment.html#comments</comments>
		<pubDate>Sun, 31 Aug 2014 20:45:32 +0000</pubDate>
		<dc:creator><![CDATA[Paul Barnes-Hoggett]]></dc:creator>
				<category><![CDATA[Engineering]]></category>
		<category><![CDATA[Rails]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Software Craftsmanship]]></category>

		<guid isPermaLink="false">http://www.eyefodder.com/?p=161</guid>
		<description><![CDATA[<p>For most of us, downloading the development package of Ruby for your platform will suffice. For the curious, or those needing a Ruby version that doesn&#8217;t have a pre-built package available you have to resort to compiling ruby from source code. Fortunately, as we have built ourselves a clean development environment using Vagrant, this is [&#8230;]</p>
<p>The post <a rel="nofollow" href="http://eyefodder.com/2014/08/compiling-ruby-for-dev-environment.html">Compiling Ruby from source for your development environment</a> appeared first on <a rel="nofollow" href="http://eyefodder.com">Eyefodder</a>.</p>
]]></description>
				<content:encoded><![CDATA[<p>For most of us, downloading the development package of Ruby for your platform will suffice. For the curious, or those needing a Ruby version that doesn&#8217;t have a pre-built package available you have to resort to compiling ruby from source code. Fortunately, as we have built ourselves a clean development environment using Vagrant, this is actually a pretty simple task!<br />
<div id="attachment_163" style="width: 660px" class="wp-caption aligncenter"><a href="http://en.wikipedia.org/wiki/Kit_houses_in_North_America#mediaviewer/File:1916_Sterling_Homes_plan_The_Vernon.jpg"><img src="http://eyefodder.com/wp-content/uploads/2014/08/1916_Sterling_Homes_plan_The_Vernon.jpg" alt="Advertisement for a kit home" width="650" height="829" class="size-full wp-image-163" /></a><p class="wp-caption-text">Whilst most of us are fine with buying an existing home, some of us want to build our own; and so it is with Ruby</p></div><br />
<span id="more-161"></span></p>
<h2>Getting started</h2>
<p>For this example we are just going to build a machine and install Ruby, and not worry about installing a Rails app just like we did for our basic example. To get started, checkout the source code from github:</p>
<pre class="brush: bash; gutter: false; title: ; notranslate">

git clone -b building_ruby_from_source --single-branch https://github.com/eyefodder/spex.git

</pre>
<h2>Getting the source code for compiling Ruby</h2>
<p>So, the first step is to figure out the url for grabbing the source code for the version you need. In the example code, we&#8217;re using the latest stable version of Ruby: 2.1.2, but that maybe different by the time you read this. Best thing to do is go check out the <a href="http://ftp.ruby-lang.org/pub/ruby/">Ruby ftp site</a> and find the tarball you need. Our package can be found at <code>http://ftp.ruby-lang.org/pub/ruby/2.1/ruby-2.1.2.tar.gz</code>. Now check out the code in <code>build_ruby_from_source.sh:</code></p>
<pre class="brush: bash; first-line: 11; title: ; notranslate">
  rm -rf /opt/vagrant_ruby
  echo 'downloading'
  curl --remote-name http://ftp.ruby-lang.org/pub/ruby/2.1/ruby-2.1.2.tar.gz &gt;/dev/null 2&gt;&amp;1
  tar zxf ruby-2.1.2.tar.gz &gt;/dev/null 2&gt;&amp;1
  cd ruby-2.1.2/
  echo 'configure'
  ./configure &gt;/dev/null 2&gt;&amp;1
  echo 'make (this could take a while, while I make ruby, you should make tea...)'
  make  &gt;/dev/null 2&gt;&amp;1
  echo 'install'
  make install &gt;/dev/null 2&gt;&amp;1
</pre>
<p>If you&#8217;re going to be compiling a different version of Ruby, then go ahead and change the url in line 13, along with the commands on line 14 and 15. Let&#8217;s understand what this is doing step-by-step:</p>
<ol>
<li><code>rm -rf /opt/vagrant_ruby</code> removes the Ruby installation that comes with the base Vagrant box</li>
<li><code>curl --remote-name http://ftp.ruby-lang.org/...</code> downloads the ruby package with the same name as on the ftp site</li>
<li><code>tar zxf ruby-2.1.2.tar.gz</code> unzip the file and jump into the created directory before&#8230;</li>
<li>Compiling the code using <code>./configure</code>, <code>make</code> and finally <code>make install</code></li>
</ol>
<p>Compilation can take a good while, but trust that your box will get there eventually. The last step after compiling Ruby is to <code>gem install bundler</code>. We could have done this through Puppet, but as we need to have Ruby installed first, it is simpler just to install it here.</p>
<h2>Idempotence, and saving time</h2>
<p>In my post on setting up a base rails environment I talk about the need for Idempotence; that is, ensuring your scripts do no harm if they are run multiple times. Strictly speaking, the code above for compiling Ruby can be run multiple times without harm, and is idempotent. However, the build process takes so long that it&#8217;s really worth doing a check here and only installing if actually necessary. You see that in action in the following code:</p>
<pre class="brush: bash; first-line: 6; title: ; notranslate">
if [ &quot;$(ruby -e 'print RUBY_VERSION')&quot; = '2.1.2' ]
  then
  echo &quot;ruby already installed, skipping&quot;
else
  echo &quot;Installing ruby 2.1.2&quot;
</pre>
<p>This does a check against the current installed Ruby version, and skips installation if we can.</p>
<p>The post <a rel="nofollow" href="http://eyefodder.com/2014/08/compiling-ruby-for-dev-environment.html">Compiling Ruby from source for your development environment</a> appeared first on <a rel="nofollow" href="http://eyefodder.com">Eyefodder</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://eyefodder.com/2014/08/compiling-ruby-for-dev-environment.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Building A Pristine Rails Virtual Machine</title>
		<link>http://eyefodder.com/2014/08/building-pristine-rails-virtual-machine.html</link>
		<comments>http://eyefodder.com/2014/08/building-pristine-rails-virtual-machine.html#comments</comments>
		<pubDate>Thu, 28 Aug 2014 16:53:45 +0000</pubDate>
		<dc:creator><![CDATA[Paul Barnes-Hoggett]]></dc:creator>
				<category><![CDATA[Agile Software Development]]></category>
		<category><![CDATA[Engineering]]></category>
		<category><![CDATA[Rails]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Software Craftsmanship]]></category>

		<guid isPermaLink="false">http://www.eyefodder.com/?p=156</guid>
		<description><![CDATA[<p>A (thankfully) long time ago in a galaxy far far away I developed web apps in Flash. And when I had to target different versions I had to go through a whole rigamarole of uninstalling and reinstalling plugins. Fast forward to now and I&#8217;m often working on projects in Ruby. I&#8217;ve found RVM and gemsets [&#8230;]</p>
<p>The post <a rel="nofollow" href="http://eyefodder.com/2014/08/building-pristine-rails-virtual-machine.html">Building A Pristine Rails Virtual Machine</a> appeared first on <a rel="nofollow" href="http://eyefodder.com">Eyefodder</a>.</p>
]]></description>
				<content:encoded><![CDATA[<p>A (thankfully) long time ago in a galaxy far far away I developed web apps in Flash. And when I had to target different versions I had to go through a whole rigamarole of uninstalling and reinstalling plugins. Fast forward to now and I&#8217;m often working on projects in Ruby. I&#8217;ve found <a href="http://rvm.io/" target="_blank">RVM</a> and <a href="http://rvm.io/gemsets" target="_blank">gemsets</a> to be a lifesaver for managing multiple environments on different projects. But as the environment gets more complicated I&#8217;ve started to want to have each project live in splendid isolation, in their own Virtual Machine. My <a title="Using Puppet and Vagrant to make a one-click development environment" href="http://www.eyefodder.com/2014/08/one-click-development-environment.html" target="_blank">last post</a> described getting a bare-bones Ruby development environment up and running. This post details taking that a little further, and getting a Rails Virtual Machine up and running.</p>
<div id="attachment_157" style="width: 550px" class="wp-caption aligncenter"><a href="http://en.wikipedia.org/wiki/Hobo"><img class="wp-image-157" src="http://eyefodder.com/wp-content/uploads/2014/08/Hobos2-715x1024.jpg" alt="Vagrants on Rails" width="540" height="773" /></a><p class="wp-caption-text">These are some Vagrants on the Rails. Read on for Rails on Vagrants&#8230;</p></div>
<p><span id="more-156"></span></p>
<h2>Vagrant powered Rails Virtual Machine</h2>
<p>In the <a title="Using Puppet and Vagrant to make a one-click development environment" href="http://www.eyefodder.com/2014/08/one-click-development-environment.html" target="_blank">last post</a>, I showed how the initial machine was installed and configured with the packages we&#8217;d need for development. In essence we get as far as having the right version of ruby and installing bundler which we will use to grab all out other dependencies. Now we have everything we need to get up and running with our rails app, we will execute one more script to fully provision our box. Check out the <code>base_rails_app</code> branch to get started:</p>
<pre class="brush: bash; gutter: false; title: ; notranslate">

git clone -b base_rails_app --single-branch https://github.com/eyefodder/spex.git

</pre>
<p>If you look inside the <code>VagrantFile</code> you will see that now instead of running <code>post_up_message.sh</code> we have renamed it to <code>prep_rails_app.sh</code> and added a few commands. Let&#8217;s take a look:</p>
<pre class="brush: ruby; first-line: 6; title: ; notranslate">
cd /app
echo 'installing app dependencies'
bundle install
echo 'creating databases (if needed)'
rake db:create
echo 'performing migrations'
rake db:migrate
echo 'seeding the db'
rake db:seed
</pre>
<p>So, after jumping into the <code>/app</code> directory (which is the guest machine path to our root development directory), we do the following:</p>
<ol>
<li><code>bundle install</code> — to install / update any gems specified in the <code>Gemfile</code></li>
<li><code>rake db:create</code> — to create any databases specified in <code>database.yaml</code></li>
<li><code>rake db:migrate</code> — to execute any pending migrations</li>
<li><code>rake db:seed</code> — to populate the database with any data specified in <code>db/seeds.rb</code></li>
</ol>
<p><img class="aligncenter size-large wp-image-158" src="http://eyefodder.com/wp-content/uploads/2014/08/Screen-Shot-2014-08-28-at-2.01.15-PM-1024x882.png" alt="Rails Virtual Machine Start Screen" width="540" height="465" /><br />
And that in essence is it! When you run <code>vagrant up</code>, you can be sure you have the environment up and running and the app in the newest state ready to develop on. All you need to do to fire up the server is the following:</p>
<pre class="brush: bash; gutter: false; title: ; notranslate">

vagrant ssh
cd /app
rails s

</pre>
<p>Now your app will be accessible on <code>http://localhost:3001</code> and you can start developing. One thing to note is that in the <code>Vagrantfile</code>, we have set the script to run as <code>privileged: false</code>. Scripts run by Vagrant execute as the root user by default (useful for installing software). But for running bundler and rake tasks, this is a bad thing. So we just run the script as the non-privileged <code>vagrant</code> user and we&#8217;re good to go. You&#8217;ll also notice that the script is set to <code>run: 'always'</code> which means it will run every time we <code>vagrant up</code>. This one needs a little more explanation</p>
<h2>Why prep&#8217; the Rails application every time?</h2>
<p>When I have been working on projects with other people, I&#8217;ve found that the most common issues people have when running the app are usually caused by not installing required gems or not performing a migration that someone else has committed. What this script helps to do is mitigate that by ensuring that every time you bring the machine up, those regular updating tasks are performed. It&#8217;s worked really well for me but does come with one caveat: you have to make sure your scripts are idempotent.</p>
<h2>Idempotent what?</h2>
<p>When I first started using Puppet I was introduced to a new term &#8211; that of <a href="http://en.wikipedia.org/wiki/Idempotence" target="_blank">idempotence</a>. What it basically means is that after you have applied commands once, they have no further effect. For example, if you run <code>rake db:create</code> multiple times, no harm is done. Same goes for <code>bundle install</code> and <code>rake db:migrate</code>. However, for rake <code>db:seed</code> you have to be a little more careful in how you write your seed code.</p>
<h2>Beware multiple seeding</h2>
<p>So the only real thing to watch out for in all this is that you write your <code>seeds.rb</code> in such a way that if you run it multiple times you don&#8217;t end up with multiple database entries. Gratuitous use of <code>first_or_create!</code> should help here. For example, instead of:</p>
<pre class="brush: ruby; first-line: 1; gutter: false; title: ; notranslate">
Thing.create(property1: 'foo', property2: 'bar')
</pre>
<p>go for something like this:</p>
<pre class="brush: ruby; first-line: 1; gutter: false; title: ; notranslate">
Thing.where(property1: 'foo').first_or_create!(property1: 'foo', property2: 'bar')
</pre>
<p>This makes for more cautious code anyway, which regardless of if you&#8217;re using this process is generally a good thing.</p>
<h2>What&#8217;s next</h2>
<p>Once I had the Rails Virtual Machine up and running things were (almost) plain sailing. In an upcoming post I&#8217;ll talk about testing using <a title="Getting Started with Guard and RSpec" href="http://eyefodder.com/2014/09/guard-and-rspec.html">Guard and RSpec</a>; as well as how to get notifications from the Rails Virtual Machine to trigger <a href="http://growl.info/" target="_blank">Growl</a> notifications on your desktop.</p>
<p>The post <a rel="nofollow" href="http://eyefodder.com/2014/08/building-pristine-rails-virtual-machine.html">Building A Pristine Rails Virtual Machine</a> appeared first on <a rel="nofollow" href="http://eyefodder.com">Eyefodder</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://eyefodder.com/2014/08/building-pristine-rails-virtual-machine.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using Puppet and Vagrant to make a one-click development environment</title>
		<link>http://eyefodder.com/2014/08/one-click-development-environment.html</link>
		<comments>http://eyefodder.com/2014/08/one-click-development-environment.html#comments</comments>
		<pubDate>Tue, 26 Aug 2014 22:36:58 +0000</pubDate>
		<dc:creator><![CDATA[Paul Barnes-Hoggett]]></dc:creator>
				<category><![CDATA[Agile Software Development]]></category>
		<category><![CDATA[Engineering]]></category>
		<category><![CDATA[Rails]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Software Craftsmanship]]></category>

		<guid isPermaLink="false">http://www.eyefodder.com/?p=151</guid>
		<description><![CDATA[<p>Keeping a development environment clean and tidy can be a bitch. When you are working on multiple projects across different platforms it can get messy really fast. And if you&#8217;re managing a team of people and need them all to run your app locally things can get tricky too. Recently I&#8217;ve setup a process so [&#8230;]</p>
<p>The post <a rel="nofollow" href="http://eyefodder.com/2014/08/one-click-development-environment.html">Using Puppet and Vagrant to make a one-click development environment</a> appeared first on <a rel="nofollow" href="http://eyefodder.com">Eyefodder</a>.</p>
]]></description>
				<content:encoded><![CDATA[<p>Keeping a development environment clean and tidy can be a bitch. When you are working on multiple projects across different platforms it can get messy really fast. And if you&#8217;re managing a team of people and need them all to run your app locally things can get tricky too. Recently I&#8217;ve setup a process so that a pristine development environment can be spun up for a project in glorious isolation. And the best thing is, once you have it set-up, you can spin up new environments in just one command!</p>
<div id="attachment_152" style="width: 550px" class="wp-caption aligncenter"><img class="size-large wp-image-152" src="http://eyefodder.com/wp-content/uploads/2014/08/Reess_Cyclopaedia_Polygraph-1024x717.png" alt="Rees's Polygraph" width="540" height="378" /><p class="wp-caption-text">Write once, deploy many times has been a goal for rather a long time</p></div>
<p><span id="more-151"></span></p>
<h2>Vagrant, Puppet and VirtualBox, the three amigos</h2>
<div id="attachment_153" style="width: 746px" class="wp-caption aligncenter"><img class="size-full wp-image-153" src="http://eyefodder.com/wp-content/uploads/2014/08/three_amigos.jpg" alt="the three amigos" width="736" height="541" /><p class="wp-caption-text">Vagrant, Puppet and VirtualBox—the lesser known nicknames of the three amigos</p></div>
<p>In order to get this utopian ideal working, we are going to setup a virtual machine, and configure it just the way we want it for our project. There are a few great tools we&#8217;re going to use to get there. The first of these is <a href="https://www.virtualbox.org" target="_blank">VirtualBox</a>. It&#8217;s open source virtualization software from Oracle and is a great way to manage virtual machines on your desktop. Next up is <a href="vagrantup.com" target="_blank">Vagrant</a>, which is designed to allow you to describe the machine (or machines) you need to run your project and how you want it networked. Finally <a href="puppetlabs.com" target="_blank">Puppet</a> is used for managing the actual configuration of the machine. Although this might seem complicated, it&#8217;s actually really simple, especially as I&#8217;ve got an example set up that you can use.</p>
<h2>Before you begin</h2>
<p>You just need two things downloaded before you start. I develop on a Mac, so my instructions are based on that, but in theory this stuff should work on Windows or Linux distros. Hit me up in the comments if you have issues. So without further ado &#8211; go get yourself the following:</p>
<ol>
<li><a href="https://www.virtualbox.org/wiki/Downloads">VirtualBox</a></li>
<li><a href="https://www.vagrantup.com/downloads.html">Vagrant</a></li>
</ol>
<p>In case you&#8217;re wondering, we don&#8217;t need to install Puppet as it will be installed on the virtual machine.</p>
<h2>O.K. Go!</h2>
<p>The simplest way to show you how to do this is to clone the repo I setup to demonstrate the process. I have a github repo setup to demonstrate simple standalone examples. You can grab the whole thing of you like, but to follow this example check out the <code>base_ruby_environment</code> branch. If that&#8217;s all you want, you can type the following command:</p>
<pre class="brush: bash; gutter: false; title: ; notranslate">

git clone -b base_ruby_environment --single-branch https://github.com/eyefodder/spex.git

</pre>
<p>When you&#8217;ve grabbed the repo, <code>cd</code> into the <code>ops</code> folder and type <code>vagrant up</code> Now wait. First time round it will take about ten minutes or so as the environment is downloaded and configured. And then—well, that&#8217;s actually it! You now have, sitting there and ready to use, a brand spanking new virtual machine, with sqlite, postgres, git and a bunch of development libraries, ready for you to go do your worst.</p>
<h2>Exploring your new VM</h2>
<p>I&#8217;ll explain everything that was setup in a minute, but first, let&#8217;s have a dig around. When the VM loads up you&#8217;ll see a welcome message, which tells you what to do. Basically you need to tell vagrant to keep a folder in sync between the host machine and your new guest. Typing <code>vagrant rsync-auto</code> will do this for you. There are other ways of doing this, but as I explain below, this is the best I&#8217;ve found for development. Next, to hop into the actual machine, we simply type <code>vagrant ssh</code> and vagrant opens up an ssh connection into the new dev environment. Simples! If you navigate to the <code>/app</code> folder inside your VM, you&#8217;ll see a facsimile of the repo. The ops folder itself isn&#8217;t copied into the virtual machine though.</p>
<p>You can now go off and develop to your heart&#8217;s content. In my next post I&#8217;ll show you how to set up a rails app running in the virtual machine. If you&#8217;d like to understand what went on in setting the VM up, then read on&#8230;</p>
<h2>How the one-click development environment was setup step-by-step</h2>
<p>The rest of this post details how the VM gets to the finished state. It&#8217;s certainly not essential reading, but you might find it interesting, or even essential for configuring / debugging. We will be skipping between Vagrant (defining the type of box that we are running) and Puppet (configuring the box to suit our needs).</p>
<h3>Vagrant: The Vagrantfile</h3>
<p>This is basically the configuration file for the virtual machine. You can check out full reference material on the vagrant file <a href="https://docs.vagrantup.com/v2/vagrantfile/index.html">here</a>, but below I&#8217;ll walk you through the simple example we have going.</p>
<h4>Vagrant Box and Hostname</h4>
<p>The <code>config.vm.box</code> parameter tells Vagrant the box that the machine will be brought up against. This is either the name of a box you have already installed, or the name of a box in Vagrant Cloud. This is a place where other boxes can be shared. In fact, you will see that in our Vagrantfile, we are using a box I created— <code>eyefodder/precise64-utf8</code>. This is an Ubuntu 12.04 install that I made 2 minor changes to. The first was to update Puppet to 3.6.2 as there are significant improvements over the version that ships with the default box (2.7.x). The second change is to set the default locale on the box to <code>en_US.UTF-8</code> I had a hell of a time getting postgres setup and found that in order for it to work with default rails encoding of UTF8, you have to set the locale for the machine to be UTF8 <em>and</em> restart before installing postgres. This extra step seemed counter to my desire to get a development box up and running in one command so I made the change, packaged up the box, and posted it on Vagrant Cloud for all to share.</p>
<p>The hostname is simply the name the machine will be given. You can change it to whatever you like.</p>
<h4>Networking</h4>
<p>Networking is pretty simple in our example, and the syntax makes things pretty easy to follow. For our box, we expose a couple of ports: port 3000 on the guest is forwarded to 3001 on the host, and port 22 to 2222. For more details on other networking options, check out the docs <a href="https://docs.vagrantup.com/v2/networking/index.html">here</a>.</p>
<h4>Synced Folders</h4>
<p>Synced folders allow you to work in your familiar desktop environment, yet have the files you&#8217;re working on magically appear in the virtual machine. Syncing can happen via a number of methods, and I&#8217;ve included two to give you a sense of it. The default when working with VirtualBox machines is a VirtualBox shared folder. This is a two-way sync: if you add files to the folder within the VM, they will appear on your desktop and vice versa. We use this for managing our Puppet install.<br />
The second type we use is rsync. What this does is use rsync to copy files into the virtual machine. It&#8217;s a one-way operation and in order to keep things in sync, we need to tell vagrant to run the process vagrant rysnc-auto to keep watching for changes. Why use this when other synched folder mechanisms seem so much simpler? The reasons are two-fold. First off, I&#8217;ve found the performance when running a server to be <em>much</em> better. Secondly, and this one is critical for me: when I&#8217;m developing, I use <a href="https://github.com/guard/guard">Guard</a> to watch for file changes and use that to trigger selective running of my tests. Rsync seems to be the only way to get file change events triggered in the guest machine to kick off Guard. The net impact is just that I have to run that extra vagrant rsync-auto command, and I can live with that&#8230;</p>
<h4>VirtualBox Specific Configuration</h4>
<p>Vagrant is a nice layer on top of many virtualization providers. And for the most part, as we&#8217;ve seen above, the differences between platforms are abstracted away. But for fine tuning, you sometimes need to provide provider specific configuration options. In our example, we are setting the maximum memory to 1GB and setting the name that appears in the VirtualBox GUI to &#8216;<code>spex</code>&#8216;. You can see all the other config options for VirtualBox <a href="https://docs.vagrantup.com/v2/virtualbox/configuration.html">here</a>.</p>
<h3>Provisioning the Box</h3>
<p>Provisioning allows you to install the software you need to use your machine for your particular use case in an automated manner. Vagrant gives you a number of options for doing this, including simple shell scripts and popular configuration management systems like <a href="http://www.getchef.com/solutions/configuration-management/">Chef</a> and <a href="http://puppetlabs.com/">Puppet</a>. For our example, we are going to use a shell script to install some modules required by puppet, then stick with puppet for the rest of the configuration.</p>
<h4>The shell script</h4>
<p>The first thing that is run is a shell script named <code>install_puppet_modules.sh</code> This does a few things that get us ready for the main event of using Puppet to configure the machine:</p>
<ol>
<li>Update the package list on the machine with apt-get update</li>
<li>Update Puppet (if one is available) with gem update puppet</li>
<li>Install the <a href="https://forge.puppetlabs.com/puppetlabs/apt">APT</a> puppet module with <code>puppet module install puppetlabs-apt</code></li>
<li>Install the <a href="https://forge.puppetlabs.com/puppetlabs/postgresql">postgresql</a> puppet module with <code>puppet module install puppetlabs-postgresql</code></li>
</ol>
<p>A couple of things to note about the puppet module installation. Note we explicitly set the path the modules are getting installed into using <code>--modulepath /etc/puppet/modules/</code>. This is so we explicitly know where to check to see if they have been installed already. The goal of a config script like this is that you can run it multiple times without issue. I&#8217;ve also found that as I get to use more puppet modules, it&#8217;s helpful to look at the source code, and the place we installed the modules just so happens to be one of our shared folders. Neat huh? Now we have these modules (and their dependencies automatically) installed, we can get to the meat of configuration: the Puppet file.</p>
<h3>Puppet</h3>
<p>Puppet is great, but there&#8217;s quite a learning curve to it. I&#8217;m not going to go into tonnes of detail here; and there is great free <a href="https://puppetlabs.com/download-learning-vm">training material</a> out there. I&#8217;ll walk you through what I&#8217;ve used puppet for with our simple example though. To follow the trail, check out ops/puppet/manifests/default.pp as that&#8217;s our entry point into the configuration:</p>
<pre class="brush: ruby; title: ; notranslate">
Exec {
  path =&gt; &quot;/usr/bin:/usr/local/bin:/bin:/usr/local/sbin:/usr/sbin:/sbin:/opt/vagrant_ruby/bin&quot;
}
include spex::base_packages
include spex::postgres_setup
include spex::ruby_setup
</pre>
<p>We see three include statements here, and they each refer to a different stage of configuration of our development environment. You will find each class in the <code>ops/puppet/modules/spex</code> folder. I&#8217;ll cover each of them in turn:</p>
<h4>Base Packages</h4>
<p>If you take a look inside <code>ops/puppet/modules/spex/base_packages.pp</code> you&#8217;ll see the general structure for declaring what packages you want to be present on your machine:</p>
<pre class="brush: ruby; first-line: 8; title: ; notranslate">
  package { &quot;build-essential&quot;:
    ensure =&gt; installed,
  }
</pre>
<p>The puppet DSL is pretty simple to grok once you&#8217;ve seen some examples. Here, we tell puppet the name of the package we want, then tell it we want to ensure that it&#8217;s &#8216;installed&#8217;. The rest of the file does the same for other dev packages we want.</p>
<h4>Postgres Setup</h4>
<p>A little more complex is the setup of Postgres. The first block: <code>trust_local_traffic</code>, adds some rules to the postgres <a href="http://www.postgresql.org/docs/9.1/static/auth-pg-hba-conf.html">authentication</a> file to allow us to connect within our dev environment pretty easily. Basically the declaration:</p>
<pre class="brush: ruby; first-line: 5; title: ; notranslate">
  postgresql::server::pg_hba_rule{ 'trust_unix_all':
    type        =&gt; 'local',
    database    =&gt; 'all',
    user        =&gt; 'all',
    auth_method =&gt; 'trust',
    order       =&gt; '00001',
  }
</pre>
<p>generates an entry in the authentication file entry like this:</p>
<pre class="brush: ruby; first-line: 1; title: ; notranslate">
# Rule Name: trust_unix_all
# Description: none
# Order: 00001
local all all   trust
</pre>
<h4>Ruby Setup</h4>
<p>Last but by no means least, we need to have Ruby up and running. This of course assumes you&#8217;re going to develop in Ruby; if you&#8217;re using another language, you can use this as a guide. The first thing we do is to add the <a href="http://www.ubuntuupdates.org/ppa/brightbox_ruby_ng_experimental">package repository</a> that hosts Ruby packages for Ubuntu releases from 10.04 onwards:</p>
<pre class="brush: ruby; first-line: 7; title: ; notranslate">
  apt::ppa{'ppa:brightbox/ruby-ng-experimental':
    before =&gt; Package['ruby'],
  }
</pre>
<p>Next, we use <a href="https://docs.puppetlabs.com/hiera/1/">Hiera</a> to grab a configuration variable for the package we actually want to install. Hiera is a simple way to separate code from configuration when using puppet. We could have used it more in the setup of the app, but for now, suffice it to say that config variables are kept in <code>/ops/puppet/hieradata/common.yaml</code>. In there you will see that we are installing the <code>ruby2.1-dev</code> package. You could swap this out for a <a href="http://www.ubuntuupdates.org/ppa/brightbox_ruby_ng_experimental">different package</a> if you needed it. The ruby package is installed just like we saw in the <code>base_packages.pp</code> file.<br />
Finally, we install bundler:</p>
<pre class="brush: ruby; first-line: 15; title: ; notranslate">
package {'bundler':
    ensure  =&gt; 'installed',
    require =&gt; Package['ruby'],
    provider =&gt; 'gem',
  }
</pre>
<p>Note that we tell Puppet that bundler is being installed using <code>gem</code> as a provider rather than the default <code>apt-get</code>. This shows how simple it is to define the packages we want as well as where they come from. The last thing I want to show about this class is how we specify an order of execution. Usually with Puppet, we let it decide the most efficient way to execute the configuration of the machine. But sometimes things have to happen in a specific order. So here, the addition of the repository is specified to execute <code>before</code> ruby is installed; and that before bundler is installed, we <code>require</code> ruby to be present.</p>
<h3>And Finally&#8230;</h3>
<p><img src="http://eyefodder.com/wp-content/uploads/2014/08/Screen-Shot-2014-08-27-at-6.45.02-PM-1024x832.png" alt="Development Environment success message" width="540" height="438" class="aligncenter size-large wp-image-154" /><br />
Finally, once Puppet has done its magic, we run a simple shell script <code>post_up_message.sh</code> to give you a bit more detail about what to do next. In my next post about using the environment for Rails development, we&#8217;ll use this for a little more heavy lifting, but for now, go exploring!</p>
<p>The post <a rel="nofollow" href="http://eyefodder.com/2014/08/one-click-development-environment.html">Using Puppet and Vagrant to make a one-click development environment</a> appeared first on <a rel="nofollow" href="http://eyefodder.com">Eyefodder</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://eyefodder.com/2014/08/one-click-development-environment.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
