The peril of binding your code to implementation details

One always hears about encapsulation of code and hiding implementation details. And it is clearly a best practice that should be followed strictly.

But rarely do you get a good example of what could go wrong.

Recently, I was alerted to a bug in one of my apps. There was some javascript code that was saving geocoding data after a google places query. All of a sudden one parameter (latitude disappeared). Why?

Well the culprit was this code:

$('#latitude').val(place.geometry.location.Xa);
$('#longitude').val(place.geometry.location.Ya);

location.Xa was now null and location.Ya was returning latitude instead of longitude. And longitude was now in location.Za. What went wrong there?

Google changed the internals of the Location object and how long/lat were represented. Why would they do that, you ask? Because those 2 variables are implementation details. They were never officially supported in the documentation. But the code above was binding to those object internals.

I’m pretty sure that Xa and Ya were supported in an earlier version of Google Maps but became deprecated (I am using v3).

Instead, Google provides accessor methods Location#lat() and Location#lng().

The fix was:

$('#latitude').val(place.geometry.location.lat());
$('#longitude').val(place.geometry.location.lng());

You might ask: why did I have the variable access in the first place? A copy/paste job from stackoverflow :P

vagrant nfs and permission issues

I keep noticing an interesting thing with NFS on vagrant. Whenever I create a new directory (for example mkdir invite) through my IDE on the vagrant FS, I get something weird:

vagrant@oneiric64:/vagrant$ ls -rtl application/views/scripts/
ls: cannot access application/views/scripts/invite: No such file or directory
total 4
?????????? ? ? ? ? ? invite
drwxrwxr-x 1 vagrant vagrant 102 2012-10-16 17:04 user
drwxrwxr-x 1 vagrant vagrant 102 2012-10-16 17:04 login
drwxrwxr-x 1 vagrant vagrant 136 2012-10-16 17:04 search
drwxrwxr-x 1 vagrant vagrant 102 2012-10-16 17:04 register
drwxrwxr-x 1 vagrant vagrant 102 2012-10-16 17:04 profile
drwxrwxr-x 1 vagrant vagrant 442 2012-10-16 17:04 partials
drwxrwxr-x 1 vagrant vagrant 136 2012-10-16 17:04 paginator
drwxrwxr-x 1 vagrant vagrant 374 2012-10-16 17:04 me
-rw-rw-r-- 1 vagrant vagrant 3633 2012-10-16 17:04 layout.phtml
drwxrwxr-x 1 vagrant vagrant 102 2012-10-16 17:04 home
drwxrwxr-x 1 vagrant vagrant 170 2012-10-16 17:04 error

It seems that NFS on vagrant doesn’t really do well with new file structures being created from external programs. I hypothesize that some meta information is not being passed by the creating IDE.

An obvious fix is to reload vagrant.

But I don’t want to re-run all my chef and shell scripts. Also it seems silly to restart linux just to take care of this.

Well it turns out that it’s much easier to fix this. Just remount the filesystem. Here’s what you do (assuming your vagrant FS is mounted in /vagrant):

sudo mount -o remount /vagrant

Can’t connect to local MySQL server through socket

If you’re using vagrant for your web development (with MySQL) you may be forwarding 3306 from guest to host so that you could work with mysql easier.

I do this, and found that if I use the standard “mysql -uUSER -pPASS” syntax, I get the following error

ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)

Easy enough to fix. Just make sure to add –protocol=TCP to your request. So that your command ends up looking like this

mysql -uUSER -pPASS --protocol=TCP --port=PORT

Duplicity s3 error

I was using duplicity on Ubuntu recently and got this error

root@oneiric64:~# duplicity --encrypt-key 11111 --sign-key 2222 /usr/local/db/ s3://bucket/devdb
Traceback (most recent call last):
 File "/usr/bin/duplicity", line 1359, in <module>
 with_tempdir(main)
 File "/usr/bin/duplicity", line 1342, in with_tempdir
 fn()
 File "/usr/bin/duplicity", line 1202, in main
 action = commandline.ProcessCommandLine(sys.argv[1:])
 File "/usr/lib/python2.7/dist-packages/duplicity/commandline.py", line 950, in ProcessCommandLine
 backup, local_pathname = set_backend(args[0], args[1])
 File "/usr/lib/python2.7/dist-packages/duplicity/commandline.py", line 843, in set_backend
 globals.backend = backend.get_backend(bend)
 File "/usr/lib/python2.7/dist-packages/duplicity/backend.py", line 156, in get_backend
 return _backends[pu.scheme](pu)
 File "/usr/lib/python2.7/dist-packages/duplicity/backends/botobackend.py", line 46, in __init__
 from boto.s3.key import Key
ImportError: No module named boto.s3.key

It turns out, that the duplicity package on Ubuntu doesn’t grab all its dependencies. All you have to do is install the python-boto package:

sudo apt-get install python-boto

On macosx, choose:

sudo pip install boto

Generate some white noise on your MacOSX

Do you ever work in a coffee shop or some other public place?

Do you get annoying loud people who just won’t let you work and insist on yapping?

Is listening to music too distracting?

If you said yes to all those questions, then white noise is for you. I recently ran into just this problem with some very annoying man in a suit screaming something to the effect of “yes! no! buy! sell! you’re fired!” or something like that.

I recalled that in linux you could actually pipe /dev/urandom into /dev/audio or /dev/dsp to generate white noise. This is exactly what I needed. Except MacOSX doesn’t hav a /dev/audio endpoint, plus the sound of pure randomness is a bit too harsh — I know I’m a wimp.

So I started searching and found this article. It uses sox to manipulate speaker output. The steps are very easy, first install sox.

brew install sox

Then, generate white noise.

play -t sl - synth $len  pinknoise band -n 1200 200 tremolo 20 .1  < /dev/zero

Then CTRL-C when you’re done.

Neat.

pcre.h fatal error

I was trying to install the pecl oauth library on Ubuntu recently and ran into an annoying problem.

sudo pecl install oauth

Gave me a stupid error: “/usr/include/php5/ext/pcre/php_pcre.h:29:18: fatal error: pcre.h: No such file or directory”

Upon further investigation, I realized that Ubuntu has a package specifically for this: libpcre3-dev. Though pecl doesn’t say anything aside from the cryptic pcre.h. So all I had to do is install that and run pecl install again.

sudo apt-get install libpcre3-dev

Install latest sphinx on ubuntu

I spun up my 11.10 box and found out that latest sphinxsearch package in apt is 0.9+. Ew. Who wants that? The latest sphinx is 2.0.4 with all sorts of goodies.

Setting up 2.0.4 is really easy. Just grab the deb for 11.10 from sphinx site (I got sphinxsearch_2.0.4-oneric_amd64.deb), make sure you install mysql-server, and then just

sudo dpkg -i sphinxsearch_2.0.4-oneric_amd64.deb

dpkg will unpack but refuse to install if you don’t have libpq5 install. Not sure why. So to get that fixed just

sudo apt-get install libpq5

.local subdomain on MacOS X

I’ve been testing an application that lives at myapp.com. It’s been setup to run as a myapp.local on dev boxes.

Naturally, the first thing you do is put it in your /etc/hosts and that should supersede any dns lookups and you should be good to go, right?

No. That’s not what I found. I found that the .local domain was consistently slow (on the order of 5 seconds before issuing web request). Domains in my /etc/hosts file, setup as .dev or .myawesomesite worked fine. But .local was taking the consistent 5s of (what seemed like) dns resolution before hitting the web server. But my colleagues weren’t seeing the issue, so what the what?

It turns out that MacOS makes some use of zero configuration networking via the Bonjour protocol. And .local has special meaning in this system. So every time I was making any query to myapp.local, it was first querying bonjour for the resource.

Unfortunately, I have yet to find a workaround for this. So for now, I’ve changed my myapp.local to myapp.dev and everything works fine.

Oh and my colleagues weren’t seeing this problem because they were on Ubuntu :)

Virtualbox, MacOSX Lion, and kernel panics

Been meaning to write about this for a while. Have you ever had a kernel panic on a Mac? Not pleasant. Grey screen of death. Have to reboot. This is especially annoying when it keeps happening again and again.

I started noticing this phenomenon when using Virtualbox with Vagrant. It seemed to start happening more and more after I upgraded Virtualbox to 4.1+ and got really really bad when I went up to 4.1.12.

It became truly noticeable with Vagrant b/c when you build up a new virtual machine, you tend to do a lot of restarts. And my computer would reliably kernel panic every time I would do a vagrant reload.

I noticed quite a few complaints around the internet about the kernel panics on Lion. And found two main discussions about this specific issue.

Indeed, it seems like the issue is definitely with Apple. VirtualBox just happens to exacerbate it. And in case you’re wondering about whether 10.7.4 fixed the issue, the answer is: no, no it didn’t.

So what’s worked? The only thing that’s worked for me was downgrading my VirtualBox. I had to go all the way down to 4.0.16 before the kernel panics stopped completely. I know it’s terrible going down to 4.0+, but at least I have a stable machine again.

In the meantime, I’m hoping Apple gets around to fixing this. It’s a pretty annoying issue, and according to posts on the internet, it’s a bug in the networking driver.

Come on Apple. You can do this!

dnsmasq and dns server order

Following up on my earlier post about using dnsmasq with the android emulator, I have to add an addendum that’s probably worth its own post.

Since we want dnsmasq to pick up the hosts file from the host machine and then to default to other (internet) nameservers, we need to make sure that the host IP (127.0.0.1) is first in Mac’s dns server order. Until you do that, the local dns server will not be used.

This is what my server ordered looked like before. This set up didn’t work and the host was not picking up the /etc/hosts entries.

This is what I had to change the host order to.