Opscode Chef makes operations fun

Years ago, I did some time helping companies plan and build networks and computer infrastructure. Since then, in several companies, I’ve done my share of operations and IT work, but more recently I’ve found that I prefer to delegate that work to someone who finds it more interesting. I’ll be honest; I find the ever-changing complexity of systems configuration to be a frustrating, never-ending learning problem. (This particularly applies to mail server management.)

Over the past month I’ve been playing with Chef and the Opscode Platform and found that I absolutely love learning a new framework that gives me new development tools for solving an existing problem. This means that my argument, above, is entirely irrational. Let me put that another way: Chef is damn cool. Continue reading “Opscode Chef makes operations fun”

Twitter as a free Asynchronous Message Queue

This has been spinning in the back of my mind for a couple weeks now, but seeing as how Twitter uses a message queue internally and is available for free, it seems that you could use it to have disparate (separately homed) applications communicate to each other using Twitter accounts to send messages. The messages are preserved in order, although limited to 140 characters and not secured.

So I checked around and I see that I’m not the only person who has considered this. Todd Clayton’s post even goes into some detail about how to build such a service, how to support larger messages (by splitting or with external tools like TwitPic).

Mostly, though, it’s a Gedankenexperiment. Clearly it’s just a solution looking for a problem.

Pulled Pork a Hundred Ways: Recipe 1

I have made pulled pork a few times; usually with a slow cooker and barbecue sauce. It works, but I wanted something better; and something that I can call my own recipe. I thought I’d try a number of different approaches and keep track of what I put in each time and how it tasted. So here’s the inaugural attempt. I don’t own a smoker, so this is (and likely all future attempts will be) a braised pork recipe with BBQ sauce.

The Up-shot

This was not my best pulled pork. It ended up good, but not mouthwatering and, honestly, a little dry.

The pork loin roast I used was two pieces, which helped keep the lard and pork fat hot enough to brown. However, it was trimmed almost to no fat. I think extra fat would have helped the flavor. I browned the fatty side first to render off more fat for browning. If the roast had not already been trimmed I would have just trimmed it myself and used the fat for browning, rather than lard.

Next attempt, I might try brining the roast first. Either by patting with salt and letting it sit at room temperature for an hour, soaking in a brine in the fridge over night (then reducing the amount of liquid in the sauce) or dry-rubbing with some of the spices and salt and letting it refrigerate over night.

I also found the sauce had pretty much lost all character. I think adding something like beer, wine or vinegar during reduction would brighten it up. Of course, this isn’t a very sweet sauce; so perhaps some brown sugar would make it more enjoyable.

The Recipe

2 tablespoons lard
1 boneless, lean pork loin roast (3½ pounds), in two pieces

½ cup onion, coarsely chopped
4 cloves garlic, crushed

1 cup Jack Daniels whiskey
½ cup apple cider vinegar
juice of ½ lemon (about 2 tablespoons)
1 teaspoon cumin, ground
1 teaspoon fennel seed, whole
1 teaspoon coriander seeds, whole
1 teaspoon peppercorns, whole
1 tablespoon paprika
½ teaspoon dried mustard
2 teaspoons Worcestershire sauce
1 cup water
1-½ teaspoons salt
½ medium tomato

Preheat oven to 250 degrees.

Heat a heavy-bottomed, oven-proof Dutch oven over medium-high heat. When it’s hot, add the lard and melt until clear, but not smoking.

Brown each pork roast piece individually on all sides starting with the fatty side. Remove and set aside as pieces are browned.

Lower the heat on the fat and add onions, stirring until translucent. Add garlic and cook for 30 seconds.

Pour Jack Daniels into the pan and deglaze, being careful to keep the temperature low enough not to boil. Add the rest of the sauce ingredients except a ½ teaspoon salt and the tomato and stir. Cover the casserole, and bring the mixture to a simmer. Immediately add the roast pieces, fat side up, and keep over medium heat until the liquid is simmering.

Transfer the covered casserole to the oven and cook until the roast can be pulled apart with a fork, about two hours, turning meat halfway through cooking.

Remove the pork to a bowl. Strain the sauce, reserving the liquid to cool. Add the solids, a little liquid, the remaining ½ teaspoon salt and the half tomato to a blender and blend until smooth.

Using a pair of forks, split the pork into strands.

When the broth has cooled, skim fat off the top.

Bring the broth to a simmer, whisk in the puree from the blender and simmer until reduced to half the volume. Pour this sauce into the pork and mix thoroughly. Continue to mix, periodically, until you serve to ensure that the pork absorbs as much of the liquid as possible.

Subversion Revision in Android App Version with Eclipse

As developers, we like to know exactly what code our customers are running when they report a bug. For years, across several companies and many projects, we’ve put the source control revision number into the reported version number. This works well and is a common practice. Here’s how we’re doing it for Android apps.

Continue reading “Subversion Revision in Android App Version with Eclipse”

Location (GPS) and Automated Testing on Android

So I’ve been contributing to the NPR Android App, specifically by building a test suite and tests for outstanding bugs. While my specific interest is in the audio playback, I took on some sticky issues around the location lookup and found that testing location services isn’t as straightforward as I thought. So here’s a quick primer with some notes to get you started.

Getting the device’s location

Android devices can get location from the (cellular) network or the GPS chip (if they have them). Some devices may support other providers in the future. The emulator only has a GPS provider (emulated of course) registered on start-up.

The quickest way to get the location of the device is to call LocationManager#getLastKnownLocation.

LocationManager lm =(LocationManager) getSystemService(Context.LOCATION_SERVICE);
Location location = null;

List providers = lm.getAllProviders();
for (String provider : providers) {
  Location loc = lm.getLastKnownLocation(provider);
  if (loc != null) {
    location = loc;
    break;
  }
}

Note this call is synchronous but in order to not block the thread during a lookup, it just looks for a saved location from the given provider. Therefore, it does not actually do a network or GPS location lookup.

For rapid response, this is what you want, and most of the time that will work well in your apps. Some other application (like Google Maps) has probably already found the location recently. However, when testing the application, it’s very likely the emulator will not have a location and this call will return null (which, according to the SDK docs, means there is no provider but it can also mean the provider hasn’t located the device yet).

To locate the device, you’ll have to set up a listener. There’s a small likelihood that your app is the first (location-based) app your customer started after turning on the phone. In that case it would be the same problem — no location. So what you probably want to do is have your initial activity launch a listener on one or more providers to record a location. This is asynchronous so it doesn’t block your app. Here’s an example of how to do that.

  private static final int MSG_CANCEL_LOCATION_LISTENERS = 2;
  private Handler handler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
      switch (msg.what) {
      case MSG_CANCEL_LOCATION_LISTENERS:
        cancelLocationListeners();
        break;
      }
    }
  };

  // This is public so that we can inspect if for testing
  public List locationListeners =  new ArrayList();

 /**
   * On start up, launch a location listener for each service. We need to do
   * this in order to ensure that getLastKnownLocation
   * will always find a value.
   */
  private void lauchLocationListeners() {
    LocationManager lm =
        (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    List providers = lm.getAllProviders();
    for (String provider : providers) {
      LocationListener listener = new LocationListener() {

        @Override
        public void onLocationChanged(Location location) {
          handler.sendEmptyMessage(MSG_CANCEL_LOCATION_LISTENERS);
        }

        @Override
        public void onProviderDisabled(String provider) {
        }

        @Override
        public void onProviderEnabled(String provider) {
        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
        }

      };
      lm.requestLocationUpdates(provider, 60000, 0, listener);
      locationListeners.add(listener);
    }
  }

  /**
   * Remove all listeners.
   */
  private void cancelLocationListeners() {
    LocationManager lm =
        (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    // Synchronized because there may be multiple listeners running and
    // we don't want them to both try to alter the listeners collection
    // at the same time.
    synchronized (locationListeners) {
      for (LocationListener listener : locationListeners) {
        lm.removeUpdates(listener);
        locationListeners.remove(listener);
      }
    }
  }

Testing the location

The SDK provides methods for mocking location providers and location details for your testing. (You can also change the geo fix with DDMS in Eclipse or through the console, but I’m interested in automated testing here.) It wasn’t very clear from the documentation on how to use them so it took me a while to figure out that all the ‘test’ calls are not necessarily related.

addTestProvider

This method places a new provider (with the name you gave it) in the collection of providers returned by getAllProviders() (and other calls). You cannot add a mock provider with the same name as a provider that already exists, such as “gps”. This will cause an error. The mock provider doesn’t allow you to pass in a class, so it’s not very useful for inspecting actions called on it (which is why I usually use mocks). Instead it just lets you create a provider that will return in response to specific criteria.

The corresponding removeTestProvider removes this entry from the list. You should remove the provider as you clean up your test because otherwise your next test won’t be able to create it again. (The provider is retained in the emulator instance, even if the activity is destroyed and recreated between tests.) Note that you can actually remove any provider, even the built in “gps” one with this call, so be careful. If you do that you’ll have to restart your emulator to get the emulated GPS interface back.

setTestProviderLocation

I think this method should have been called “setTestLocation” because it is not directly related to a “test provider” made in the previous call. You can set a test location, with this call, for any provider. In fact, if all you want to do is test that your application can find a particular location, then set a location on the GPS provider and ignore the ‘addTestProvider’ method completely.

      LocationManager lm = (LocationManager) activity.getSystemService(Context.LOCATION_SERVICE);
      Location location = new Location(providerName);
      location.setLatitude(latitude);
      location.setLongitude(longitude);
      location.setTime(System.currentTimeMillis());
      lm.setTestProviderLocation(providerName, location);

Be sure to set the time on your location if you want the provider to think it’s a new update.

Also, all the testing requires that you have permission for mock location requested in your application under test (not in the test project). Add this to your manifest.

<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION"/>

If you are testing on a device, you’ll need to go into Settings | Applications | Developement | Allow Mock Locations and enable it, which is not set by default. This setting may not exist on all devices (but there’s a workaround).

setTestProviderEnabled

By now you should be getting the picture. This method doesn’t enable your test provider, it sets a value that indicates to your application that a provider is enabled whether it actually is or not.

Hopefully this will help some other folks who were confused about the terms in the SDK.

Testing Challenges

As mentioned above the SDK is not very conducive to dependency injection. What I’d like to do is inject a real mock provider object (one I create in my test) into the provider list for the app and when the app asks the GPS provider for a location I know the app is working. This isn’t possible as it’s currently built so I have to write silly tests with threads to start the activity and check when it collects and removes its listeners. Not very clean (and really testing the wrong thing). In many places in the SDK, I’d like to be able to inject mock objects into the system, but this isn’t thoroughly supported yet.

The second challenge is that you can’t access the location manager without access to the activity because it’s returned by the context of the activity. This makes it impossible to, say, inject a mock provider or location before the activity starts, which you might like to do in the case of, oh, looking up location in the onCreate call.

If anyone has any suggestions on this, I’d be excited to hear them. Perhaps I’m just missing a basic concept here.

Pesky phonebooks

Who uses phonebooks these days, anyway? I suppose many people do, but I seriously haven’t looked in one in years. It pains me when they drop six pounds of ground and reconstituted timber at my doorstep every few months. Then I read the poorly printed bag that the lastest one came in (oh yeah, timber in plastic!), and guess what, Dex will stop sending you phone books if you ask nicely (and give them all kinds of PII).

Go ahead, do it for yourself and save a tree or two a year.

Then, if you want to be really pedantic, email customercareemail@dexone.com and ask them to delete your records that you just created, which according to their their privacy policy they may or may not do depending on “internal… policies and legal obligations.” Or at least delete the cell phone number that you had to provide because you don’t have a POTS line and the email address that they required. I can’t guarantee that this step won’t mess up the opt-out that you just did, though.

Rhubarb Vodka

We have have some rhubarb in the garden that’s been growing for a few years. This year I was brainstorming ideas of what to do with it (besides pie, crisp, cobbler and compote) and thought, What if I infuse some vodka? After all, the rhubarb Dry Soda is pretty good. Wouldn’t that make a nice flavor for cocktails.

I’m not the first to think of this, and found lots of people’s recipes to follow. One suggested making a compote and then infusing vodka with that, but I didn’t like the idea of adding all that sugar. Most others just stuck the rhubarb in the vodka and infused it for a day or a week. I thought I’d run an experiment and take tasting notes as it infused.

I started with one large stalk of rhubarb, well cleaned, and a fifth of Stoli. I put this all in a Ball jar and let it sit.

Day 1 Uck. Super sour and sort of ‘green’. A very light hint of rhubarb and a strong ‘hint’ of the alcohol. Granted I wouldn’t normally taste Stoli straight. On ice it wasn’t much better.

Day 2 Still really sour. Concerned that this may not be something that anyone wants to drink. Did I waste a bottle of vodka?

Day 3 Hmm. Well, it’s not as sour, so I have hope. I can start to taste the rhubarb-ness and not so much the vodka-ness. On ice, helps mellow the vodka some.

Day 4 Now we’re talking. Tastes like rhubarb pie. Rich and somewhat sweet rhubarb flavor. Still sour, but that rhubarb-y sweetness without the sugar. Mixed one-to-one with vanilla vodka (had to find some way to use that up) and topped with soda over ice. Now that’s a yummy pie-like summer cocktail. Great for a picnic dinner in the garden.

Day 5 Much like yesterday — rhubarb pie. Yeah, it’s sour, but in a good way. I think it’s time to decant this, put it in a nice clean bottle and make a label for it.

New Windows for the Bathroom

Nick and I pulled the windows in the 2nd floor bathroom a couple weeks ago. We also rebuilt the wall to stabilize it and to add in a third window. Having gone through all the options for replacing the windows, I’m really excited about this new plan. Nick did an outstanding job refinishing the existing windows, and built a matching third. Reusing the windows will help maintain the historic nature of the house and will look great.

The existing windows are “awning” style windows that tip in from the top. They are simple, and actually can be completely removed from the frame for cleaning (or to get out in an emergency). I love the simple design of these and am happy to be able to keep them. Putting in vinyl windows, while supposedly more efficient and water resistant would have looked bad and cost more. In fact, in pricing replacement windows, I’ve found that they run about $1000 per windows whether you go for super energy efficient ones or fancy all wood ones. Anything less than either would be a waste of money and at that cost it would take about 40 years to pay off the replacement in utility savings. Low-e, double-glazed glass may be great for new construction, where the house is much more insulated, but just repairing the old windows and sealing the frames can get you noticeable increase in efficiency at a fraction of the cost of replacement. Fine Homebuilding even posted an article and blog post about this.

Road Trip Romania

In the second week of my Romanian travels, we left the farm, hit the road and drove up to the mountains and around to the major cities of central Romania. Our path included Cluj-Napoca, Sighişoara, Braşov, Buşteni, Sinaia ans Sibiu. I took a lot more pictures on this part of the trip, mostly because I enjoy pictures of architecture. There were many stiles influenced by the old german presence, the communist government and the gypsies.

In much of western Romania, but also in other parts, gypsy houses, churches and buildings are decorated with tin roofs. You can see a extreme example of this on the the mansions in the first picture below. Most of the other buildings that face the street in villages, while colorful, were less ostentatious. The second image and the last image in the top row were typical of what I saw of the villages built by german settlers. These villages were built 500 years ago when the Austro-Hungarian empires included parts of what is now western Romania. The germans lived here until 1983 when Ceauşescu expelled them back to Germany. The villages were then repopulated by rural Romanians and Gypsies.

Sighişoara, third picture on the top row, is an old castle town that flows out of the castle walls and down to the river. We stayed there the second night of the trip is a small apartment rented by a very friendly couple, who cooked us breakfast and did our laundry before we set out the next day.

We set out for the mountains passing through Braşov, which, like Cluj, is full of block apartments (second picture in the bottom row). These felt typically communist to me and it seemed out of place that while smaller houses were replaced with undecorated, run-down blocks, the churches remained intact and updated. The blocks look marxist-communist but the churches bely that there was full cultural control.

Our final day we lunched in Sibiu, the most german of german cities. Sibiu was awarded “cultural center of Europe” in 2007 and given the work that was done in the last ten years to restore it, you can see why. In the third picture on the bottom row, you can glimpse the buildings surrounding the piaţa — they reminded me of Prague more than anything else.

Along the way, we saw many Romanians participating in a favorite national weekend pastime: Picnicking. As you can see in the final photo, people drive their car out to the country and set up a picnic for the day. While this reminded me of beaches here in the States, it was odd to see people parked in country locales where there may have been a running stream but they were nowhere near it. It makes me question why I feel that a picnic needs to be near running water or an overlook of some kind.

Transilvania: Eight Days on the Farm

Last year, before meeting L in Prague, I spent a week on my friend Roger’s “sheep farm with no sheep” in the remote mountains of Transilvania. This spring, Roger invited me and 40 of his closest friends to come to the farm and celebrate his 40th birthday. Sure, there are lots of other places I’d love to travel to, and I often don’t want to go back to a place right away, but given an opportunity to travel — and an opportunity to see friends — I decided to go. Two weeks in Romania was an excellent holiday.

What a beautiful place. In May, last year, everything was green, but there was not much for fresh vegetables on the farm or in the market (at least not local vegetables). We subsisted on lamb, potatoes, onions and carrots. This year, in June, was much different. Roger put us all up at a pension with room and board. He and a friend has purchased and slaughtered a calf before the event, so our meals were veal, sausage, tomatoes, cucumbers, cheese, bread, eggs, etc. Being early spring, we had fresh, fresh salads, picked straight from the garden. I had outstanding soups in the village and later when we traveled around central Romania. Soups are clearly a Romanian specialty.

Not that beer, sausage and ţuica — hard liquor from apples, plums or pears — aren’t also a staple in the diet.

One of the popular Romanian culinary delights is Mici; a loose sausage of random meets that, when grilled, become more tastey the more beer you drink. This, along with many sausages and veal cutlets made for a wonderful picnic lunch on the farm on the second day of celebration.

For those of us traveling from abroad, who stayed somewhat longer than the weekend, we enjoyed great hikes around the Apuseni mountains, and good food and drink. One day, on the walk into the village, we stopped to talk to the local distiller and see how ţuica is made. The liquor starts as fruit in the fall. Farmers harvest their fruit and put it in the cellar to keep over winter. Any fruit that rots they collect and in the spring all the rotten, fermented fruit (grubs and all) goes into a large vat (an old 50-gallon oil drum, in this case) to be cooked. The entire distillery is portable, so the distiller will travel from farm to farm to distill ţuica for each farmer from his own crop of fruit, whether apples, pears or plum. The mash is double distilled creating a liquor between 70 and 130 proof. This is blended and often watered to bring it down to something between 80 and 100 proof.

There were varying qualities and flavors available, from local farms and from distilleries in Maramureş, which is known for making some of the best ţuica (or palinka, the Hungarian word, as east-central Romania was once part of Hungary and still is home to many ethnic Hungarians). The alcohol wasn’t as clean as a vodka, but much cleaner than a “brandy” (the English word often used to describe it). The fruit flavors were subtle, but apparent. Given the choice, though, I’ll take my gin or aquavit.