Tag Archives: ruby

Opscode Chef Xtra: Obtaining network interface data

It is somewhat of a challenge to obtain interface information out of the data Ohai makes available for network interfaces. For information only about inet addresses, the following works: node_addresses = {} node[:network][:interfaces].each do |iface, vals| vals[:addresses].each do |addr, h| next unless h[’family’] == ‘inet’ && !addr.match(’127.0.0.1′) iface_data = Hash.new iface_data = h.dup iface_data[’address’] = [...]

Getting a little crazy with FileEdit

In case there is any doubt, you can go nuts with Chef::Util::FileEdit. If one is using search_file_replace, internally it is simply: new_contents << ((method == 1) ? replace : line.gsub!(exp, replace)) Meaning if need be, I can do something silly: ruby_block ‘fix remi.repo’ do action :nothing block do f = Chef::Util::FileEdit.new(’/etc/yum.repos.d/remi.repo’) f.search_file_replace(/\$releasever/, ‘%i’ % major) [...]

Opscode Chef Xtra: A Deletable Template via a Definition

While there is no delete action recognized by the Chef template resource, it is possible to fake it using a definition. For example, a definition for managing a configuration file for the multifaceted DNS server dnsmasq might look like the following: define :dnsmasq_conf, :enable => true, :template => ‘dnsmasq.conf.erb’ do include_recipe ‘dnsmasq’   conffile = [...]

Opscode Chef Xtra: Achieving Idempotence in execute Resource

The certainty of outcome offered by other Chef resources is notably lacking from the execute Resource, for Chef has no way of knowing the consequences of the provided shell script fragment. Fortunately, it’s possible to ensure idempotent behavior with the appropriate application of care. As an example, perhaps one needs to load several database dump [...]

Merge on the fly in development with juicer

Finally upgrading to the latest juicer, I found my prior hack no longer worked. I decided to revisit my solution and replace it with something more effective. While I still overrode a method, I dispatched the temporary file I was using previously. Ultimately this ought to be Rack middleware. Perhaps next time. class JuicerController < [...]

Exclusive favorite association for ActiveRecord has_many

It’s not uncommon in my application to maintain an exclusively preferred selection out of a collection on an object. For example: class Person < ActiveRecord::Base has_many :employers, :class_name => ‘Employee’ end   class Company < ActiveRecord::Base has_many :employees end   class Employee < ActiveRecord::Base belongs_to :person belongs_to :company   named_scope :favorite, {:conditions => {:favorite => [...]

Failing fast, Ruby style

While building up the server side RPC methods for client side calls from ExtJS’s new Direct RPC framework, I thought it would be useful to fail fast whenever the client caller violated the interface contract. Enter fail fast. def submit(params) action = params.delete(:action) id = params.delete(:id) assert(action) {|v| [’update’, ‘create’].include?(v)} … Further up the stack, [...]

Beware the Class.new dragons

When using this neat snippet from Dr Nic for testing a plugin I use internally, I came across the behavior described by Avdi. The proposed solution works great! A single line modification to Dr Nic’s class ensures it works with class_inheritable_accessor from ActiveSupport. def create_class(class_name, superclass, &block) klass = Class.new superclass klass.instance_eval &block Object.const_set class_name, [...]

juicer and dynamic merges

While a hack, a dirty way of getting Juicer to merge JavaScript files at every load is the following, which I’m running under a custom Webrick setup: # Shamelessly cheat so Juicer cannot close its copy of STDOUT module Juicer LOGGER.instance_eval do def close ; end end end   # Later, far away in a [...]

Managing Systems with Chef: Sandbox Play

Chef is an awesome tool for centralized management of systems and resources. Chef recipes are best consumed in a sandbox, first, before being deployed. Below, I describe a simple sandbox setup using VirtualBox and Debian Lenny. Using VirtualBox, define a new virtual machine. I used the business card ISO to install a bare minimum Debian, [...]