jtimberman's Code Blog

Chef, Ops, Ruby, Linux/Unix. Opinions are mine, not my employer's (CHEF).

Chef 0.10's Knife Plugins

Earlier today^Wyesterday, Opscode previewed a new feature of the upcoming Chef 0.10, Knife Plugins. I’ve been excited about this feature since Dan DeLeo first created the ticket for it. I already created the “grep” plugin that Dan illustrates on the blog post, and it is quite useful already (and I only have a few systems!).

A few months ago, I wrote a silly library that will retrieve the list of Canonical’s released AMIs. They make this available as a text file per distribution codename (lucid, maverick, etc), and my library supports that. For even greater silliness, but perhaps usefulness, I wrote a knife plugin to retrieve the AMI data. The code is really simple.

The run method processes the name_args (see the Knife Plugin Documentation) for the distribution and optionally a type. Lets see an example of what this means.

First, usage:

% knife ec2 amis ubuntu -h
knife ec2 amis ubuntu DISTRO [TYPE]
    -s, --server-url URL             Chef Server URL
    -k, --key KEY                    API Client Key
        --color                      Use colored output
    -c, --config CONFIG              The configuration file to use
        --defaults                   Accept default values for all questions
    -e, --editor EDITOR              Set the editor to use for interactive commands
    -E, --environment ENVIRONMENT    Set the Chef environment
    -F, --format FORMAT              Which format to use for output
        --no-color                   Don't use colors in the output
    -n, --no-editor                  Do not open EDITOR, just accept the data as is
    -u, --user USER                  API Client Username
        --print-after                Show the data after a destructive operation
    -V, --verbose                    More verbose output. Use twice for max verbosity
    -v, --version                    Show chef version
    -y, --yes                        Say yes to all prompts for confirmation
    -h, --help                       Show this message

The name_args we can pass are DISTRO and TYPE, TYPE being optional. The rest of the options are built into knife and available to every sub-command. Let’s pick a distro. My favorite these days is lucid:

% knife ec2 amis ubuntu lucid
ap_northeast_large:      ami-580fa459
ap_northeast_large_ebs:  ami-5e0fa45f
ap_northeast_small:      ami-4c0fa44d
ap_northeast_small_ebs:  ami-5c0fa45d
ap_southeast_large:      ami-c692ec94
ap_southeast_large_ebs:  ami-f092eca2
ap_southeast_small:      ami-2492ec76
ap_southeast_small_ebs:  ami-f292eca0
eu_west_large:           ami-631f2b17
eu_west_large_ebs:       ami-3d1f2b49
eu_west_small:           ami-a11e2ad5
eu_west_small_ebs:       ami-311f2b45
us_east_large:           ami-fa01f193
us_east_large_ebs:       ami-3202f25b
us_east_small:           ami-7000f019
us_east_small_ebs:       ami-3e02f257
us_west_large:           ami-e1bfefa4
us_west_large_ebs:       ami-f5bfefb0
us_west_small:           ami-19bfef5c
us_west_small_ebs:       ami-ebbfefae

For the DISTRO, I can use any of the codenames that Canonical makes available, including karmic, lucid, maverick and even natty at the time of this writing. The value on the left is the TYPE. Lets see what happens when I pass one of those:

% knife ec2 amis ubuntu lucid us_west_small

This returns just the AMI. We can feed this into an environment variable or just copy and paste it into a “knife ec2 server create” command for the “-I ami” option.

A note about the naming, I classed it under the ec2 sub-command space since it is most relevant there. Further, I used “amis ubuntu” rather than “ubuntu amis” because theoretically other plugins could be written to retrieve other platforms. There’s a couple bugs I know about already and will fix them later. Enjoy!