Exemplo n.º 1
0
def ensure_vnet_lxc_environment(config):
    """
    Checks and creates the LXC environment
    param: dict config: The config created by get_config()
    :raises RuntimeError: If unsupported OS, or missing packages
    """
    # Check if there are any LXC machines in the config
    if "lxc" not in [
            settings.MACHINE_TYPE_PROVIDER_MAPPING[machine["type"]]
            for machine in config["machines"].values()
    ]:
        logger.debug(
            "Skipping LXC environment creation, no LXC machines in config")
        return

    # Check if we are on a supported OS
    if not check_for_supported_os(config, "lxc"):
        logger.critical(
            "Unable to create LXC environment on your machine, OS not supported"
        )
        raise RuntimeError("OS not supported for provider LXC")

    # Check if all required packages have been installed
    if not check_for_installed_packages(config, "lxc"):
        logger.critical(
            "Not all required host packages seem to be installed, please fix this before proceeding"
        )
        raise RuntimeError("Missing host packages")

    # Check if the storage pool exists
    if not check_if_lxc_storage_pool_exists(settings.LXC_STORAGE_POOL_NAME):
        logger.info("VNet LXC storage pool does not exist, creating it")
        create_lxc_storage_pool(name=settings.LXC_STORAGE_POOL_NAME,
                                driver=settings.LXC_STORAGE_POOL_DRIVER)
    else:
        logger.debug("VNet LXC storage pool {} found".format(
            settings.LXC_STORAGE_POOL_NAME))

    # Check if the profile exists
    if not check_if_lxc_profile_exists(settings.LXC_VNET_PROFILE):
        logger.info("VNet LXC profile does not exist, creating it")
        create_vnet_lxc_profile(settings.LXC_VNET_PROFILE)
    else:
        logger.debug("VNet profile {} found".format(settings.LXC_VNET_PROFILE))

    # Check if the base image exists
    if not check_if_lxc_image_exists(settings.LXC_BASE_IMAGE_ALIAS,
                                     by_alias=True):
        logger.info("Base image does not exist, creating it")
        create_lxc_base_image_container(config)
        change_lxc_machine_status(settings.LXC_BASE_IMAGE_MACHINE_NAME,
                                  status="start")
        configure_lxc_base_machine(config)
        create_lxc_image_from_container(settings.LXC_BASE_IMAGE_MACHINE_NAME,
                                        alias=settings.LXC_BASE_IMAGE_ALIAS)
        destroy_lxc_machine(settings.LXC_BASE_IMAGE_MACHINE_NAME, wait=False)
    else:
        logger.debug("Base image {} found".format(
            settings.LXC_BASE_IMAGE_ALIAS))
Exemplo n.º 2
0
 def test_destroy_lxc_machine_does_nothing_if_machine_does_not_exist(self):
     self.client.containers.get.side_effect = NotFound(response="blaap")
     destroy_lxc_machine("banaan")
     self.assertFalse(self.machine.delete.called)
     self.assertFalse(self.machine.stop.called)
Exemplo n.º 3
0
 def test_destroy_lxc_machine_calls_machine_stop_when_status_is_running(
         self):
     self.machine.status = "Running"
     destroy_lxc_machine("banaan")
     self.machine.stop.assert_called_once_with(wait=True)
Exemplo n.º 4
0
 def test_destroy_lxc_machine_does_not_call_stop_by_default(self):
     destroy_lxc_machine("banaan")
     self.assertFalse(self.machine.stop.called)
Exemplo n.º 5
0
 def test_destroy_lxc_machine_calls_container_delete_method(self):
     destroy_lxc_machine("banaan")
     self.machine.delete.assert_called_once_with(wait=False)
Exemplo n.º 6
0
 def test_destroy_lxc_machine_calls_lxd_client(self):
     destroy_lxc_machine("banaan")
     self.lxd_client.assert_called_once_with()