Esempio 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))
Esempio n. 2
0
def create_lxc_image_from_container(container, alias=None, description=None):
    """
    Create a LXC image from a container
    :param str container: The container to create the image from
    :param str alias: Creates an alias for the image
    :param str description: A description for the image alias
    """
    # Stop it first
    change_lxc_machine_status(container, status="stop")

    # Create the image
    logger.info("Creating image from LXC container {}".format(container))
    client = get_lxd_client()
    container = client.containers.get(container)
    img = container.publish(wait=True)
    logger.info("Image {} created successfully".format(img.fingerprint))

    # Create the alias if requested
    if alias:
        logger.info("Adding alias {} to newly created image".format(alias))
        img.add_alias(alias, description)
Esempio n. 3
0
 def test_change_lxc_machine_status_calls_wait_for_lxc_machine_status_with_running(
         self):
     change_lxc_machine_status("banaan", status="start")
     self.wait_for_lxc_machine_status.assert_called_once_with(
         self.machine, "Running")
Esempio n. 4
0
 def test_change_lxc_machine_status_catches_timeout_error(self):
     self.wait_for_lxc_machine_status.side_effect = TimeoutError()
     change_lxc_machine_status("banaan")
Esempio n. 5
0
 def test_change_lxc_machine_status_does_not_call_stop_when_start_passed(
         self):
     change_lxc_machine_status("banaan", status="start")
     self.assertFalse(self.machine.stop.called)
Esempio n. 6
0
 def test_change_lxc_machine_status_calls_wait_for_lxc_machine_status_with_stopped(
         self):
     change_lxc_machine_status("banaan", status="stop")
     self.wait_for_lxc_machine_status.assert_called_once_with(
         self.machine, "Stopped")
Esempio n. 7
0
 def test_change_lxc_machine_status_deals_with_not_found_error(self):
     self.client.containers.get.side_effect = NotFound(response="blaap")
     change_lxc_machine_status("banaan")
     self.assertFalse(self.machine.stop.called)
     self.assertFalse(self.machine.start.called)
     self.assertFalse(self.wait_for_lxc_machine_status.called)
Esempio n. 8
0
 def test_change_lxc_machine_status_call_start_when_passed(self):
     change_lxc_machine_status("banaan", status="start")
     self.machine.start.assert_called_once_with(wait=True)
Esempio n. 9
0
 def test_change_lxc_machine_status_does_not_call_start_by_default(self):
     change_lxc_machine_status("banaan")
     self.assertFalse(self.machine.start.called)
Esempio n. 10
0
 def test_change_lxc_machine_status_calls_machine_stop_by_default(self):
     change_lxc_machine_status("banaan")
     self.machine.stop.assert_called_once_with()
Esempio n. 11
0
 def test_change_lxc_machine_status_calls_containers_get(self):
     change_lxc_machine_status("banaan")
     self.client.containers.get.assert_called_once_with("banaan")
Esempio n. 12
0
 def test_change_lxc_machine_status_calls_sleep(self):
     change_lxc_machine_status("banaan")
     self.sleep.assert_called_once_with(1)
Esempio n. 13
0
 def test_change_lxc_machine_status_calls_lxd_client(self):
     change_lxc_machine_status("banaan")
     self.lxd_client.assert_called_once_with()