diff options
-rwxr-xr-x | bin/virtualization | 75 |
1 files changed, 54 insertions, 21 deletions
diff --git a/bin/virtualization b/bin/virtualization index acb3c79..db70849 100755 --- a/bin/virtualization +++ b/bin/virtualization @@ -30,6 +30,7 @@ import os import re import logging import lsb_release +import platform import requests from requests.packages.urllib3.exceptions import NewConnectionError import shlex @@ -50,6 +51,7 @@ import tarfile import time import urllib.request from urllib.parse import urlparse +from uuid import uuid4 DEFAULT_TIMEOUT = 500 @@ -551,7 +553,9 @@ class LXDTest(object): self.rootfs_tarball = None self.template_tarball = None self.name = 'testbed' - self.image_alias = 'ubuntu' + self.image_alias = uuid4().hex + self.default_remote = "ubuntu:" + self.os_version = platform.linux_distribution()[1] def run_command(self, cmd): task = RunCommand(cmd) @@ -561,8 +565,15 @@ class LXDTest(object): logging.error(' STDOUT: {}'.format(task.stdout)) logging.error(' STDERR: {}'.format(task.stderr)) return False - - return True + else: + logging.debug('Command {}:'.format(task.cmd)) + if task.stdout != '': + logging.debug(' STDOUT: {}'.format(task.stdout)) + elif task.stderr != '': + logging.debug(' STDERR: {}'.format(task.stderr)) + else: + logging.debug(' Command returned no output') + return True def setup(self): # Initialize LXD @@ -570,12 +581,12 @@ class LXDTest(object): logging.debug("Attempting to initialize LXD") # TODO: Need a method to see if LXD is already initialized if not self.run_command('lxd init --auto'): - logging.error('Error encounterd while initializing LXD') + logging.debug('Error encounterd while initializing LXD') result = False # Retrieve and insert LXD images - logging.debug("Downloading template.") if self.template_url is not None: + logging.debug("Downloading template.") targetfile = urlparse(self.template_url).path.split('/')[-1] filename = os.path.join('/tmp', targetfile) if not os.path.isfile(filename): @@ -592,8 +603,8 @@ class LXDTest(object): "Skipping Download.".format(filename)) self.template_tarball = filename - logging.debug("Downloading rootfs.") if self.rootfs_url is not None: + logging.debug("Downloading rootfs.") targetfile = urlparse(self.rootfs_url).path.split('/')[-1] filename = os.path.join('/tmp', targetfile) if not os.path.isfile(filename): @@ -610,15 +621,24 @@ class LXDTest(object): self.rootfs_tarball = filename # Insert images - # TODO: check to see if ubuntu already exists - logging.debug("Importing images into LXD") - cmd = 'lxc image import {} rootfs {} --alias {}'.format( - self.template_tarball, self.rootfs_tarball, - self.image_alias) - if not self.run_command(cmd): - logging.error('Error encountered while attempting to ' - 'import images into LXD') - result = False + if result is True: + logging.debug("Importing images into LXD") + cmd = 'lxc image import {} rootfs {} --alias {}'.format( + self.template_tarball, self.rootfs_tarball, + self.image_alias) + if not self.run_command(cmd): + logging.error('Error encountered while attempting to ' + 'import images into LXD') + result = False + else: + logging.debug("No local image available, attempting to " + "import from default remote.") + cmd = 'lxc image copy {}{} local: --alias {}'.format( + self.default_remote, self.os_version, self.image_alias) + if not self.run_command(cmd): + loggging.error('Error encountered while attempting to ' + 'import images from default remote.') + result = False return result @@ -626,16 +646,21 @@ class LXDTest(object): """ Downloads LXD files for same release as host machine """ + # TODO: Clean this up to use a non-internet simplestream on MAAS server logging.debug("Attempting download of {} from {}".format(filename, url)) try: resp = urllib.request.urlretrieve(url, filename) except (IOError, - Error, + OSError, urllib.error.HTTPError, urllib.error.URLError) as exception: logging.error("Failed download of image from %s: %s", - image_url, exception) + url, exception) + return False + except ValueError as verr: + logging.error("Invalid URL %s" % url) + logging.error("%s" % verr) return False if not os.path.isfile(filename): @@ -665,10 +690,18 @@ class LXDTest(object): if not self.run_command('lxc launch {} {}'.format(self.image_alias, self.name)): return False + + logging.debug("Container listing:") + cmd = ("lxc list") + if not self.run_command(cmd): + return False + + logging.debug("Testing container") cmd = ("lxc exec {} dd if=/dev/urandom of=testdata.txt " "bs=1024 count=1000".format(self.name)) if not self.run_command(cmd): return False + return True @@ -764,12 +797,12 @@ def main(): # silence normal output from requests module logging.getLogger("requests").setLevel(logging.WARNING) - # to check if not len(sys.argv) > 1 - if len(vars(args)) == 0: + # Verify args + try: + args.func(args) + except AttributeError: parser.print_help() return False - args.func(args) - if __name__ == "__main__": main() |