Exemple #1
0
def wait_for_ssh():
    def check_ssh():
        if not is_ssh_available():
            raise VMException("SSH is unreachable.")

    util.retry(
        check_ssh, retries=30, wait_time=3,
        message="SSH is not yet available. Retrying..."
    )
Exemple #2
0
def ensure_is_ready():
    yurt_util.retry(util.initialize_lxd,
                    retries=3,
                    wait_time=3,
                    message="LXD init failed. Retrying...")

    # wait for LXD to be available
    yurt_util.retry(util.get_pylxd_client,
                    retries=3,
                    wait_time=3,
                    message="LXD is not yet available. Retrying...")
    util.check_network_config()
    util.check_profile_config()
Exemple #3
0
def configure_lxd():
    if not is_initialized():
        logging.info("Initializing LXD...")
        initialize_lxd()

    def check_config():
        if not is_remote_configured():
            logging.info("Setting up remote...")
            configure_remote()
        if not is_network_configured():
            logging.info("Configuring network...")
            configure_network()
        if not is_profile_configured():
            logging.info("Configuring profiles...")
            configure_profile()

    retry(check_config, retries=10, wait_time=6)
Exemple #4
0
def stop(force=False):
    vm_state = state()

    def confirm_shutdown():
        if state() == State.Running:
            raise VMException("VM is still running.")

    if vm_state != State.Running:
        logging.info("Yurt is not running.")
    else:
        try:
            if force:
                logging.info("Forcing shutdown...")
            else:
                logging.info("Attempting to shut down gracefully...")

            vbox.stop_vm(_VM_NAME, force=force)
            util.retry(confirm_shutdown, retries=6, wait_time=10)
        except VBoxException as e:
            logging.error(e.message)
            raise VMException("Shut down failed")
Exemple #5
0
def follow_operation(operation_uri: str, unpack_metadata=None):
    """
    Params:
        operation_uri:      URI of the operation to follow.
        unpack_metadata:    Function to unpack the operation's metadata. Return a line of text to summarize
                            the current progress of the operation.
                            If not given, progress will not be shown.
    """
    import time
    from yurt.util import retry

    operations = get_pylxd_client().operations

    # Allow time for operation to be created.
    try:
        retry(
            lambda: operations.get(operation_uri),  # pylint: disable=no-member
            retries=10,
            wait_time=0.5
        )
        operation = operations.get(operation_uri)  # pylint: disable=no-member
    except pylxd.exceptions.NotFound:
        raise LXCException(
            f"Timed out while waiting for operation to be created.")

    logging.info(operation.description)
    while True:
        try:
            operation = operations.get(  # pylint: disable=no-member
                operation_uri
            )
            if unpack_metadata:
                print(f"\r{unpack_metadata(operation.metadata)}", end="")
            time.sleep(0.5)
        except pylxd.exceptions.NotFound:
            print("\nDone")
            break
        except KeyboardInterrupt:
            break
Exemple #6
0
    def setUpClass(cls):
        cls.discard_vm = os.environ.get(
            "YURT_TEST_DISCARD_VM_POLICY") == "discard"

        if cls.discard_vm:
            if vm.state() == vm.State.Running:
                vm.stop(force=True)
            if vm.state() == vm.State.Stopped:
                vm.destroy()

        if vm.state() == vm.State.NotInitialized:
            vm.init()
            logging.info("Waiting for VM registration...")
            time.sleep(3)

        if vm.state() == vm.State.Stopped:
            vm.start()
            lxc.configure_lxd()

        def check_if_running():
            if vm.state() != vm.State.Running:
                raise YurtException("VM Not running")

        util.retry(check_if_running)