Add a utility for doing master election via etcd.

Sending this for comment, will work on unit tests once people are happy with the code.
I'm getting kinda nitpicky here, but...

I feel like the readability of this function could be greatly improved if you took everything starting at this line and stopping right before the select and moved it into another function. Then you could exit early from error conditions and maybe avoid the many layers of nesting. As a side benefit, this new function might be easier to test.