Example #1
0
def vm_reaper():
    """ Iterates through each task in the db which has not yet been cleaned and runs the reaper

    This function iterates through each task. If the task is either failed or passed, ie, the
    task has completed, then the VM is cleaned up and then the docker container. If both of
    these operations occur, then the cleanup is set to True.
    """
    tasks = tapi.task().get(cleanup=False)['objects']
    for task in tasks:
        if task['result'] in ["failed", "passed", "invalid"]:
            vm_cleanup = False
            docker_cleanup = False

            if task['provider'] == "Sprout" and task['vm_name'] == "Sprout":
                vm_cleanup = True
            else:
                if task['provider'] and task['vm_name']:
                    logger.info('Cleaning up {} on {}'.format(
                        task['vm_name'], task['provider']))
                    if task['vm_name'] == "None":
                        vm_cleanup = True
                    else:
                        appliance = Appliance(task['provider'],
                                              task['vm_name'])
                        try:
                            if appliance.does_vm_exist():
                                logger.info("Destroying {}".format(
                                    appliance.vm_name))
                                appliance.destroy()
                            vm_cleanup = True
                        except Exception:
                            logger.info('Exception occured cleaning up')

            containers = dockerbot.dc.containers(all=True)
            for container in containers:
                if task['tid'] in container['Names'][0]:
                    logger.info('Cleaning up docker container {}'.format(
                        container['Id']))
                    dockerbot.dc.remove_container(container['Id'], force=True)
                    docker_cleanup = True
                    break
            else:
                docker_cleanup = True

            if docker_cleanup and vm_cleanup:
                tapi.task(task['tid']).put({'cleanup': True})
Example #2
0
def vm_reaper():
    """ Iterates through each task in the db which has not yet been cleaned and runs the reaper

    This function iterates through each task. If the task is either failed or passed, ie, the
    task has completed, then the VM is cleaned up and then the docker container. If both of
    these operations occur, then the cleanup is set to True.
    """
    tasks = tapi.task().get(cleanup=False)['objects']
    for task in tasks:
        if task['result'] in ["failed", "passed", "invalid"]:
            vm_cleanup = False
            docker_cleanup = False

            if task['provider'] == "Sprout" and task['vm_name'] == "Sprout":
                vm_cleanup = True
            else:
                if task['provider'] and task['vm_name']:
                    logger.info('Cleaning up {} on {}'.format(task['vm_name'], task['provider']))
                    if task['vm_name'] == "None":
                        vm_cleanup = True
                    else:
                        appliance = Appliance(task['provider'], task['vm_name'])
                        try:
                            if appliance.does_vm_exist():
                                logger.info("Destroying {}".format(appliance.vm_name))
                                appliance.destroy()
                            vm_cleanup = True
                        except Exception:
                            logger.info('Exception occured cleaning up')

            containers = dockerbot.dc.containers(all=True)
            for container in containers:
                if task['tid'] in container['Names'][0]:
                    logger.info('Cleaning up docker container {}'.format(container['Id']))
                    dockerbot.dc.remove_container(container['Id'], force=True)
                    docker_cleanup = True
                    break
            else:
                docker_cleanup = True

            if docker_cleanup and vm_cleanup:
                tapi.task(task['tid']).put({'cleanup': True})
Example #3
0
def get_appliance(provider):
    '''Fixture to provision appliance to the provider being tested if necessary'''
    global appliance_list, main_provider
    appliance_vm_prefix = "long-test_ssa-appl_"

    if provider.key not in appliance_list:
        try:
            # see if the current appliance is on the needed provider
            ip_addr = urlparse(store.base_url).hostname
            appl_name = provider.mgmt.get_vm_name_from_ip(ip_addr)
            logger.info(
                "re-using already provisioned appliance on {}...".format(
                    provider.key))
            main_provider = provider.key
            appliance = Appliance(provider.key, appl_name)
            appliance.configure_fleecing()
            appliance_list[provider.key] = appliance
        except Exception as e:
            logger.exception(e)
            # provision appliance and configure
            ver_to_prov = str(version.current_version())
            logger.info("provisioning {} appliance on {}...".format(
                ver_to_prov, provider.key))
            appliance = None
            try:
                appliance = provision_appliance(
                    vm_name_prefix=appliance_vm_prefix,
                    version=ver_to_prov,
                    provider_name=provider.key)
                logger.info("appliance IP address: " + str(appliance.address))
                appliance.configure(setup_fleece=True)
            except Exception as e:
                logger.exception(e)
                if appliance is not None:
                    appliance.destroy()
                raise CFMEException(
                    'Appliance encountered error during initial setup: {}'.
                    format(str(e)))
            appliance_list[provider.key] = appliance
    return appliance_list[provider.key]
Example #4
0
def get_appliance(provider_crud):
    '''Fixture to provision appliance to the provider being tested if necessary'''
    global appliance_list, main_provider
    appliance_vm_prefix = "test_vm_analysis"

    if provider_crud.key not in appliance_list:
        try:
            # see if the current appliance is on the needed provider
            ip_addr = re.findall(r'[0-9]+(?:\.[0-9]+){3}', conf.env['base_url'])[0]
            appl_name = provider_crud.get_mgmt_system().get_vm_name_from_ip(ip_addr)
            logger.info("re-using already provisioned appliance on {}...".format(provider_crud.key))
            main_provider = provider_crud.key
            appliance = Appliance(provider_crud.key, appl_name)
            appliance.configure_fleecing()
            appliance_list[provider_crud.key] = appliance
        except Exception as e:
            logger.error("Exception: %s" % str(e))
            # provision appliance and configure
            ver_to_prov = str(version.current_version())
            logger.info("provisioning {} appliance on {}...".format(ver_to_prov, provider_crud.key))
            appliance = None
            try:
                appliance = provision_appliance(
                    vm_name_prefix=appliance_vm_prefix,
                    version=ver_to_prov,
                    provider_name=provider_crud.key)
                logger.info("appliance IP address: " + str(appliance.address))
                appliance.configure(setup_fleece=True)
            except Exception as e:
                logger.error("Exception: %s" % str(e))
                if appliance is not None:
                    appliance.destroy()
                raise CFMEException(
                    'Appliance encountered error during initial setup: {}'.format(str(e)))
            appliance_list[provider_crud.key] = appliance
    return appliance_list[provider_crud.key]
def get_appliance(provider):
    '''Fixture to provision appliance to the provider being tested if necessary'''
    global appliance_list, main_provider
    appliance_vm_prefix = "long-test_ssa-appl_"

    if provider.key not in appliance_list:
        try:
            # see if the current appliance is on the needed provider
            ip_addr = urlparse(store.base_url).hostname
            appl_name = provider.mgmt.get_vm_name_from_ip(ip_addr)
            logger.info("re-using already provisioned appliance on {}...".format(provider.key))
            main_provider = provider.key
            appliance = Appliance(provider.key, appl_name)
            appliance.configure_fleecing()
            appliance_list[provider.key] = appliance
        except Exception as e:
            logger.exception(e)
            # provision appliance and configure
            ver_to_prov = str(version.current_version())
            logger.info("provisioning {} appliance on {}...".format(ver_to_prov, provider.key))
            appliance = None
            try:
                appliance = provision_appliance(
                    vm_name_prefix=appliance_vm_prefix,
                    version=ver_to_prov,
                    provider_name=provider.key)
                logger.info("appliance IP address: " + str(appliance.address))
                appliance.configure(setup_fleece=True)
            except Exception as e:
                logger.exception(e)
                if appliance is not None:
                    appliance.destroy()
                raise CFMEException(
                    'Appliance encountered error during initial setup: {}'.format(str(e)))
            appliance_list[provider.key] = appliance
    return appliance_list[provider.key]