jump to navigation

Run a single cookbook recipe with chef-solo November 25, 2009

Posted by John Dewey in Chef, Code, Example, Rake.
1 comment so far

I generally build my systems with chef-solo cookbooks. I do not want to maintain a central server for recipes. However, I’m getting to the point where I’d like to share my cookbooks from one solo project to another. Anyways, I
wanted to run an individual recipe of a given cookbook. The following gist allows for this. Although, it may not be necessary, since cookbooks are generally idempotent. Seemed like a good idea earlier in the evening, meh.


Running Sinatra with Phusion Passenger April 19, 2009

Posted by John Dewey in Passenger, Rack, Ruby, Sinatra.
1 comment so far

Here is a config.ru for use with Sinatra. It also includes a logging section for your app. This configuration works on Sinatra ( Quite a few of the examples out there use the deprecated Sinatra::Application.default_options vs set.


Updated config.ru with Blake’s suggestions.

Syncing bundle_fu assets to s3 for Cloudfront April 2, 2009

Posted by John Dewey in Amazon, CDN, Rake, Vlad.
1 comment so far

I wanted to automatically upload my bundle_fu assets to Amazon’s s3, and use their Cloudfront CDN for SnGs. I came up with the following rake task, which can be hooked into Vlad.

It’s as simple as adding the following to production.rb, and running the rake task ‘cloud:upload’.

ActionController::Base.asset_host = "cloudfront_url"

Find missing RSpec view tests March 16, 2009

Posted by John Dewey in Code, Rake, RSpec.

I created a quick little rake task which finds missing RSpec view tests. It assumes the extension is .haml, however, this can be overridden with the EXTENSION environment variable.

rake spec:missing_view_templates
EXTENSION=erb rake spec:missing_view_templates 

using geminstaller with vlad the deployer February 28, 2009

Posted by John Dewey in Configuration, Deployment, Example, Rake, Vlad.
1 comment so far

I really really really like vlad the deployer. I recently switched my apps over to geminstaller vs Rails’ config.gem.

This gist adds in geminstaller functionality to vlad’s update task.

Am I rspec-tarded? December 29, 2008

Posted by John Dewey in Code, Example, RSpec, Ruby.
1 comment so far

So I want to test the xml declaration and xhtml doctype of my application layout.

I ended up with this pastie.
Does anyone know of a way to accomplish this using rspec‘s have_tag/with_tag? I’m not too fond of the regexp solution in the pastie.

Simple_admin met a Pieman November 7, 2008

Posted by dougmcinnes in Code, Plugins & Gems, Rails, Rake, Ruby.
Tags: , , , , ,

Simple_admin is a Ruby on Rails plugin I created at the Los Angeles Times that morphed into a collabrative effort between Dewey, Reid and myself as it traveled between projects and functionality was added.  I’ve just released it to our latimes github account: http://github.com/latimes/simple_admin/tree/master

The basic premise is to give a simple way of managing login usernames and passwords without a database.  The data is stored in a YAML file in the application with the passwords encrypted by String’s crypt() method.

To add users to the file there’s an included rake task: admin:add_user.  The username and password are passed as parameters:

rake admin:add_user username=mrwalrus password=mahbukkit

Rake will append the user to the login.yaml file or create a new one.  The default location for this file is config/admin/login.yml but can be overridden by setting the LOGIN_FILE environment variable in your application.  If you set the LOGIN_FILE differently in your different rails environment files you can have different usernames and passwords for development and production.

There’s also a rake task for adding multiple users at the same time from a text file list, giving them all random passwords.

To get the plugin to actually use Basic HTTP authentication to ask for usernames and passwords add this to your application controller:

include SimpleAdmin
before_filter :check_basic_http_credentials

Like all filters you can add conditions:

before_filter :check_basic_http_credentials, :only => :login

When including SimpleAdmin in your controller you also get access to the authenticate(username, password) method which can be used for custom login pages.  For example:

def login
  if authenticate(params[:username], params[:password])
    session[:admin] = true
    redirect_to main_page

Also included in the plugin is some code for marking different servers as “admin” servers and a way for a rails application to check to see if he’s an admin or not and change its behavior.  We used this on one application running on multiple boxes so we could turn off page caching on the admin-marked boxes so the administration WYSIWYG pages wouldn’t be cached and sent to non-admin users (that would be a big oops).

Adding vi keybindings to irb, script/console, and mysql October 19, 2008

Posted by John Dewey in Rails, Ruby.

I loves me my vi, and sun keyboard mappings (although it drives people crazy when they peer on my macbook).

Add the following on OS X 10.5:

john@emopop:~> cat .editrc
bind -v

This will work for OS X 10.4 and other operating systems:

john@emopop:~> cat .inputrc 
set editing-mode vi

Hyphenated XML tags in Builder October 13, 2008

Posted by John Dewey in ActiveRecord, Example, Rails, Ruby.

I typically use to_xml when building a shallow representation of my model in XML. It becomes hella difficult to maintain the format of my XML when nesting numerous levels deep. Sometimes you gotta use a Builder.

If you would like your tags to be hyphenated (like the to_xml default), here is a nice trick:

xml.__send__('hyphenated-tag-name'.to_sym) do
  xml.tag "data"

OpenURI returns two different objects September 24, 2008

Posted by John Dewey in Code, Ruby.
1 comment so far

I am unfortunate in using ImageMagick to watermark images passed into a service. However, I noticed occasional nils when trying to read images returned by OpenURI.


Interesting… OpenURI returns a StringIO object rather than Tempfile when data on the other end exceeds 10240 bytes.

StringMax = 10240
def <<(str)
  @io << str
  @size += str.length
  if StringIO === @io && StringMax < @size
    require 'tempfile'
    io = Tempfile.new('open-uri')
    Meta.init io, @io if @io.respond_to? :meta
    io << @io.string
    @io = io

There are several approaches to this problem, but I opted for the easy one. I simply added the following to my model to force a Tempfile.

# OpenURI::Buffer::StringMax = 10240
OpenURI::Buffer::StringMax = 0

Since this particular application is not under active development, and Rails _only_ uses OpenURI for ‘script/plugin’ and a couple generators — I didn’t loose too much sleep.