threetee's posterous

threetee's posterous

threetee  //  

Jan 20 / 5:49am

Describing EC2 instance AZ placement with Knife

With Chef's knife utility, it's easy to get information about your EC2 environment's node distribution across availability zones:

For example, to list nodes and their AZ placement:

$ knife exec -E 'nodes.transform("*:*") {|n| puts "#{n.name}: #{n.ec2.placement_availability_zone}"}' | sort
node1: us-east-1b
node2: us-east-1c
node3: us-east-1b
node4: us-east-1a

Or to count AZ placements:

$ knife exec -E 'nodes.transform("*:*") {|n| puts "#{n.ec2.placement_availability_zone}"}' | awk ' { for (i=1;i<=NF;i++) count[$i]++ } END { for (i in count) print count[i], i }' $* | sort -r
2 us-east-1b
1 us-east-1a
1 us-east-1c

Gathering and summarizing AZ data in this fashion can help you verify that your application is properly distributed across AZs. It also can come in handy when purchasing reserved instances. You can of course modify the search query from *:* to something more targeted. And since there's always more than one way to do it, please feel free to share any alternative methods in the comments.

Here's a gist for the above; I'll update it with any future tweaks or additional commands:

Filed under  //  AWS   Chef   ec2   knife  
Dec 16 / 8:31am

Opscode Chef Cookbook: Zerigo

I had a need to update DNS records in Zerigo automatically via Chef, so I wrote a Chef cookbook that provides LWRPs that allow easy interaction with Zerigo in my recipes. You can grab it from the Opscode community site at http://community.opscode.com/cookbooks/zerigo, or from Github at https://github.com/threetee/chef-cookbook-zerigo.

Usage:

include_recipe "zerigo"

zerigo_zone "create a zone" do
  domain  "test.com"
  email   "hostmaster@test.com"

  zerigo_email          "zerigo-account@somewhere.com"
  zerigo_token          "zerigo_api_token"

  action :create
end

zerigo_record "create a record" do
  name  "test"
  value "16.8.4.2"
  type  "A"

  zone_id               "test.com"   # Zerigo-hosted domain name
  zerigo_email          "zerigo-account@somewhere.com"
  zerigo_token          "zerigo_api_token"

  action :create
end

 

Filed under  //  Chef   DNS   Zerigo  
Oct 6 / 5:13am

Generating SHA256 checksums on OS X

I do a lot of work with Chef, and its remote_file resource can optionally use a SHA256 checksum to verify the integrity of the remote files it retrieves. Unfortunately, on OS X it's not immediately obvious how to generate SHA256 sums. Here are a couple ways that I found:

shasum

Probably the easiest way, since I believe the shasum binary is installed by default on OS X:

$ shasum -a 256 <filename>

I created a sha256 alias by putting this into my .bash_profile:

alias sha256='shasum -a 256'

md5deep

It's like md5sum on steroids, and it includes the sha256sum binary. Use Homebrew or grab the source at http://md5deep.sourceforge.net/ and compile/install manually (I recommend the former). Once it's installed, calculate your sums:

$ sha256deep somefile

f2ca1bb6c7e907d06dafe4687e579fce76b37e4e93b7605022da52e6ccc26fd2 /Users/edennis/somefile

 

Filed under  //  Chef   SHA256  
Mar 10 / 10:50pm

Streaming tweets with node.js and socket.io

I was learning me some node.js and came across a useful twitter streaming example: https://github.com/andregoncalves/twitter-nodejs-websocket (thanks to @andregoncalves for putting it up). I added support for non-websocket browsers via socket.io and static file serving capabilities via node-static, and here's the result: https://github.com/threetee/twitter-nodejs-socketio.

Jan 8 / 5:50am

Slow Query Logging on Amazon RDS - a HOWTO

Amazon RDS instances have slow query logging disabled by default. Here is how to enable slow query logging and access the slow query data on your RDS instance using the RDS command-line tools:

  1. Install the Amazon RDS command-line tools and configure your environment according to the documentation: http://aws.amazon.com/developertools/2928
  2. Create a parameter group:
    $ rds-create-db-parameter-group sweet-parameter-group -f mysql5.1 -d "This is a totally sweet database parameter group"
  3. Place your DB instance in that parameter group:
    $ rds-modify-db-instance my-instance --db-parameter-group-name sweet-parameter-group --apply-immediately
  4. Modify the parameter group to turn on slow query logging:
    $ rds-modify-db-parameter-group sweet-parameter-group --parameters "name=slow_query_log, value=ON, method=immediate" --parameters "name=long_query_time, value=1, method=immediate" --parameters "name=min_examined_row_limit, value=100, method=immediate"

    If you like, you can verify that your settings were made properly:
    $ rds-describe-db-parameters sweet-parameter-group

  5. Reboot your DB instance:
    $ rds-reboot-db-instance my-instance

After following the above steps, you will be able to access the slow query log by querying the mysql.slow_log table.

Filed under  //  AWS   Amazon   HOWTO   MySQL   RDS  
Dec 7 / 9:02pm

Using ActiveResource with Rails nested resources

I ran into a problem with ActiveResource not saving nested resources. All the details are on this StackOverflow post:

http://stackoverflow.com/questions/4383592/rails-cant-save-a-nested-resource-via-activeresource

And here's the solution, for future reference:

Models

class Asset < ActiveRecord::Base
   has_many :interfaces
   accepts_nested_attributes_for :interfaces
   alias interfaces_attributes interfaces

   def to_xml(options = {})
     super(options.merge({:methods => :interfaces_attributes}))
   end
end

class Interface < ActiveRecord::Base
   belongs_to :asset
end

Relevant Asset Controller Actions

def show
   @asset = Asset.find(params[:id])

   respond_to do |format|
   format.html # show.html.erb
   format.xml { render :xml => @asset }
   end
end

def update
   @asset = Asset.find(params[:id])

   respond_to do |format|
   if @asset.update_attributes(params[:asset])
     format.html { redirect_to(@asset, :notice => 'Asset was successfully updated.') }
     format.xml { head :ok }
   else
     format.html { render :action => "edit" }
     format.xml { render :xml => @asset.errors, :status => :unprocessable_entity }
   end
end

Nov 13 / 6:24pm

The Two Things about Computer Programming - The Fishbowl

Brett pointed to The Two Things, a page that can be best summed up by quoting:

A few years ago, I was chatting with a stranger in a bar. When I told him I was an economist, he said, “Ah. So… what are the Two Things about economics?”

“Huh?” I cleverly replied.

“You know, the Two Things. For every subject, there are really only two things you really need to know. Everything else is the application of those two things, or just not important.”

“Oh,” I said. “Okay, here are the Two Things about economics. One: Incentives matter. Two: There’s no such thing as a free lunch.”

Ever since that evening, I’ve been playing the Two Things game.

In The Two Things about the Two Things, it's noted that people are unlikely to agree what the two things are, especially when it comes to computing. So I'm going to cheat. Here are my four two by two things:

Computer Programming:

  1. Every problem can be solved by breaking it up into a series of smaller problems.
  2. The computer will always do exactly what you tell it to.

Software Engineering:

  1. Writing the code is the easy part. Writing it so someone else can understand it later is the important part.
  2. Make it work, then make it elegant, then make it fast.

This has me thinking about the Two Things for the various things I enjoy.

Oct 12 / 1:47am

Scalr gem - updated for 2.0 API

I needed to do some work with the Scalr API, and came across this gem: http://github.com/mhodgson/scalr. Unfortunately, at the time I found that gem, it was written for a pre-2.0 Scalr API, which didn't help me for my project, so I updated it to 2.0 and sent a pull request to the original author. By the time you read this, the original will probably be updated already, but the code is also available at http://github.com/threetee/scalr/tree/api-2.0.

Oct 9 / 10:41pm

iTwango

A group-buy site (a la Groupon) that I worked on recently has launched. Go check it out:
http://itwango.com/