Transactions in MAAS

... and how Django gets it so wrong

In MAAS, to ensure that a function is run within its own database transaction, decorate it with @transactional:

from maasserver.utils.orm import transactional

def do_something_databasey():

If a transaction is already in progress when do_something_databasey is called, it will instead be called within a savepoint.

That’s it.

Now for the why.

[Read More]
MAAS  Python 

Introduction to blocking and non-blocking code in MAAS

MAAS has a couple of function decorators that are designed to help blocking code work with non-blocking code: synchronous and asynchronous.

The blocking, or synchronous, code in MAAS is primarily though not solely the realm of Django, which handles all web API calls and some web views. The biggest responsibility of Django these days is the ORM and database migrations.

Django doesn’t do non-blocking, or asynchronous, at all. For that MAAS uses Twisted. Twisted has many useful pieces that can be used in other projects, but most of the time the Twisted reactor is what you want.

[Read More]
MAAS  Python 

South, South 2, and Django Migrations

A couple of months ago we on the MAAS team found ourselves in a bit of a pickle: we needed to be able to support a product targeted at both Django <1.7 and Django ≥1.7 with database migrations. This is a problem because South is replaced by Django’s own migration support in 1.7, and there are differences.

[Read More]
Django  MAAS 

Fewer sudo password prompts

The first thing I do on a new Ubuntu installation is to stop sudo from asking me for a password in every terminal I open:

echo 'Defaults !tty_tickets' | sudo tee /etc/sudoers.d/no_tty_tickets

This is especially useful if you use screen, tmux, or byobu.

Preparing for Python 3 in MAAS

Something we’ve done in MAAS — which is Python 2 only so far — is to put:

from __future__ import (

__metaclass__ = type

str = None

at the top of every source file. We knew that we would port MAAS to Python 3 at some point, and we hoped that doing this would help that effort. We’ll find out if that’s the case soon enough: one of our goals for this cycle is to port MAAS to Python 3.

[Read More]

Even Google can't write shell scripts

Google’s Shell Style Guide — via Hacker News — doesn’t mention using set -e or set -u, and neither does Gentoo’s guide, which seriously undermines them both. They both have sensible advice, but those two settings are the two most important things to include when scripting with Bash. Bash doesn’t even become a scripting language until those are set; without, scripts are just interactive transcripts without the interactive part, i.e. a human to stop execution when there are errors or unexpected behaviour.

[Read More]