#!/usr/bin/python # Copyright (C) 2012-2015 Codethink Limited # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; version 2 of the License. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License along # with this program. If not, see . '''A Morph deployment write extension for compressed raw disk images that are compatible with google compute engine.''' import cliapp import os import sys import time import tempfile import shutil import morphlib.writeexts from subprocess import call class GoogleComputeEngineWriteExtension(morphlib.writeexts.WriteExtension): def process_args(self, args): if len(args) != 2: raise cliapp.AppException('Wrong number of command line args') DISK_NAME = 'disk.raw' temp_root, location = args try: temp_dir = tempfile.mkdtemp() disk_location = os.path.join(temp_dir, DISK_NAME) with self.created_disk_image(disk_location): self.format_btrfs(disk_location) self.create_system(temp_root, disk_location) self.status(msg='Compressing disk image') call(["tar", '-czf', location, '-C', temp_dir, DISK_NAME]) self.status(msg='Compressed disk image has been created at %s' % location) except Exception: self.status(msg='Failure to deploy system to %s' % location) raise finally: try: shutil.rmtree(temp_dir) except OSError as exc: if exc.errno != errno.ENOENT: raise GoogleComputeEngineWriteExtension().run()