June 5th, 2013, 16:25 UTC


Workaround for uploading files to MAAS

It turns out that it’s not possible to use maas-cli to upload files to MAAS. It’s not something that most people need to do because tools like Juju use MAAS’s API directly to upload files.

However, the following workaround can be used like so:

upload.py http://example.com/MAAS/api/1.0/ my:api:key my_filename < my_file
#!/usr/bin/env python2.7
# Copyright 2013 Canonical Ltd.  This software is licensed under the
# GNU Affero General Public License version 3 (see the file LICENSE).

"""Upload files to MAAS.

Pipe or redirect the file to upload to stdin.

This is a workaround while file uploads don't work via maas-cli; see
https://bugs.launchpad.net/maas/+bug/1187826 for details.

from __future__ import (

__metaclass__ = type

import argparse
from io import BytesIO
from sys import stdin

from apiclient.maas_client import (

argument_parser = argparse.ArgumentParser(description=__doc__)
argument_parser.add_argument("url", type=unicode)
argument_parser.add_argument("creds", type=unicode)
argument_parser.add_argument("filename", type=unicode)

def upload(url, creds, filename, data):
    params = {"filename": filename, "file": BytesIO(data)}
    auth = MAASOAuth(*creds.split(":"))
    dispatcher = MAASDispatcher()
    client = MAASClient(auth, dispatcher, url)
    client.post("files/", "add", **params)

if __name__ == "__main__":
    args = argument_parser.parse_args()
    upload(args.url, args.creds, args.filename, stdin.read())