Thursday, May 23, 2013

Deploying a development machine with juju

I've just been reading Stavros' blog post about provisioning and deploying virtual machines using ansible: An example of provisioning and deployment with Ansible.
It's worth a read, I've recently come round to the same way of thinking - keeping my development machine as clean as possible and using virtual box machines to install dependencies on, but there's another side to it. Sometimes I want a machine without having to divide my laptop's precious resources, using the cloud that type of thing is easy. But using ubuntu's juju it's even easier. is ubuntu's answer to service orchestration. There's a good video of it in use at OSCON 2012. It's great at the large stuff but it's also great at small stuff - which is where this post comes in. I find it helpful to just think of juju as a way of installing charms on a cloud machine, and charms are just scripts. That way it sounds less scary.

I've got some very simple charms for you to get started here. The one I'm going to cover in this post is called devenv. The purpose of devenv is to setup a simple development environment in the cloud. Sometimes I need to make use of mysql, mongo or postgres, so it covers those as well

How to deploy devenv

After you've followed the intructions on to get setup with your cloud provider it's as simple as running the script in my charm_collection repo. Here's what's going on under the covers:
juju deploy --repository=charms --constraints "mem=8G" local:precise/devenv

With juju deploy we're expecting to see our charm in the charms/precise/devenv folder below the current directory. Juju then zips the charm up and deploys it on a machine of at least 8GB memory, which we specified with out mem=8G constraint.
Deploying our charm means running a couple of hooks. install and start. Hooks are just scripts which get run on the deployed machine.
Install looks like this:
apt-get -y install vim tmux git bzr mercurial
wget -O /home/ubuntu/.tmux.conf
wget -O /home/ubuntu/.vimrc
echo "set editing-mode vi" > /home/ubuntu/.inputrc

There's nothing much going on here, make sure some packages are installed, copy some of my configuration files from github to the right places and set vi editing-mode in bash. I could, and probably will add to this by getting my install hook to clone the right repositories, install a few more languages and maybe copy around some of my ssh keys, but this is a good start.
Because all I want to do is install some applications and not run any services the start hook just echos that I'm now up and running.
echo "Running"

The interesting part is the relationships with databases, sometimes I need to be able to use a database for my development work. This can be done by making use of juju's relationships. For convenience I've got 3 hooks that point to the same python file:
mongo-relation-changed ->
mysql-relation-changed ->
pq-relation-changed ->
for this relationship all I want to do is write a config file to my home directory, so that I can make use of the db. The database charms set a number of key/value pair using the relation-set command. all I need to do is call the relation-get command when I want to get a particular value. Here's how it's done for mongo and mysql:
def mysql_relation_changed():
    host = os.popen('relation-get host').read().strip()
    user = os.popen('relation-get user').read().strip()
    database = os.popen('relation-get database').read().strip()
    password = os.popen('relation-get password').read().strip()
    slave = os.popen('relation-get slave').read().strip()
    with open('/home/ubuntu/mysql.conf', 'w') as mfile:
        mfile.write('host=%s\n' % host)
        mfile.write('user=%s\n' % user)
        mfile.write('database=%s\n' % database)
        mfile.write('password=%s\n' % password)
        mfile.write('slave=%s\n' % slave)

    print "Done!"

def mongo_relation_changed():
    host = os.popen('relation-get hostname').read().strip()
    port = os.popen('relation-get port').read().strip()
    with open('/home/ubuntu/mongo.conf', 'w') as mfile:
        mfile.write('host=%s\n' % host)
        mfile.write('port=%s\n' % port)

    print "Done!"
To start create a relationship you just need to tell juju to make one:
juju add-relation devenv mongodb

This will add a relation between my devenv machine and a machine running mongo (which I would have deployed previously) From here, I have a cloud machine up and running with all the tools I need, and a mongo db ready to use.


  1. Thanks for this :-). Looks pretty simple.

    You may want to define a helper function to get rid of much of the boilerplate:

    def relation_get(var):
    return os.popen('relation-get ' + var).read().strip()

  2. agritourism center in TuscanyTuscany farmhouses; Selection of the best accommodations in Tuscany such as farmhouses (farm holidays), holiday apartments, villas, hotels and bed and breakfasts in Tuscany area.