Wednesday, April 25, 2012

Deploying with Capistrano in /mnt on EC2 machines

I use Ubuntu on my EC2 instances. These are based on standard AMIs issued by Ubuntu at http://cloud.ubuntu.com/ami. These instances all have ephemeral storage mounted at /mnt by default, with a size of 145GB for small instances.

Since /mnt is backed by ephemeral storage, this directory is an ideal location for deploying Rails applications, since you will not typically want to include your source controlled application code in any backup of your instances. Besides, /mnt has a large size, compared to a few GBs for the root partition.

My rails apps are deployed using Capistrano at /mnt/deployment/ on the target EC2 instances.

I run my deployment as a non-root user. Since /mnt is mounted as root by EC2, this can be a problem, especially since I do not like to use sudo in deployment scripts unless absolutely necessary. Note that even if you chown /mnt to be owned by something non-root, it will revert to root (and lose all data, being ephemeral) whenever the instance is stopped and started again.

The solution I have found is to add an upstart script, which I named /etc/init/mounted-ephemeral.conf, following the conventions of other such scripts.

$ sudo cat /etc/init/mounted-ephemeral.conf 
start on mounted MOUNTPOINT=/mnt

task

console output

exec chown user.eng /mnt

This script ensures that the ownership of /mnt is always set to the user I want, whenever the machine starts up.

No comments: