Skip to content

Instantly share code, notes, and snippets.

@allenap
Created June 5, 2013 16:20
Show Gist options
  • Select an option

  • Save allenap/5715193 to your computer and use it in GitHub Desktop.

Select an option

Save allenap/5715193 to your computer and use it in GitHub Desktop.
#!/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 (
absolute_import,
print_function,
unicode_literals,
)
__metaclass__ = type
import argparse
from io import BytesIO
from sys import stdin
from apiclient.maas_client import (
MAASClient,
MAASDispatcher,
MAASOAuth,
)
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())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment