Beispiel #1
0
def start():
    from datetime import datetime

    vm_state = state()

    if vm_state == State.NotInitialized:
        raise VMException("VM has not yet been initialized.")

    if vm_state == State.Running:
        logging.info("VM is already running.")
        return

    try:
        logging.info("Booting up...")

        console_file_name = os.path.join(config.vm_install_dir, "console.log")
        vbox.attach_serial_console(_VM_NAME, console_file_name)

        vbox.start_vm(_VM_NAME)
        util.sleep_for(10, show_spinner=True)
        logging.info("Waiting for the machine to be ready...")
        util.sleep_for(10, show_spinner=True)

        current_port = config.get_config(config.Key.ssh_port)
        host_ssh_port = vbox.setup_ssh_port_forwarding(_VM_NAME, current_port)
        config.set_config(config.Key.ssh_port, host_ssh_port)

        vbox.setup_lxd_port_forwarding(_VM_NAME)

    except (VBoxException, ConfigReadException) as e:
        logging.error(e.message)
        raise VMException("Start up failed")
Beispiel #2
0
def initialize_lxd():
    if is_initialized():
        return

    try:
        with open(os.path.join(config.provision_dir, "lxd-init.yaml"), "r") as f:
            init = f.read()
    except OSError as e:
        raise LXCException(f"Error reading lxd-init.yaml {e}")

    try:
        logging.info("Updating package information...")
        vm.run_cmd("sudo apt update", show_spinner=True)
        vm.run_cmd("sudo usermod yurt -a -G lxd")

        logging.info("Initializing LXD...")
        vm.run_cmd(
            "sudo lxd init --preseed",
            stdin=init,
            show_spinner=True
        )
        _setup_yurt_socat()

        logging.info("Done.")
        config.set_config(config.Key.is_lxd_initialized, True)
    except VMException as e:
        logging.error(e)
        logging.error("Restart the VM to try again: 'yurt vm restart'")
        raise LXCException("Failed to initialize LXD.")
Beispiel #3
0
def setup_port_forwarding():
    ssh_port = _get_unused_port()
    vm_name_ = vm_name()

    vbox.setup_port_forwarding(vm_name_, "ssh", ssh_port, 22)
    config.set_config(config.Key.ssh_port, ssh_port)

    lxd_port = _get_unused_port()
    vbox.setup_port_forwarding(vm_name_, "lxd", lxd_port, 80)
    config.set_config(config.Key.lxd_port, lxd_port)
Beispiel #4
0
def init():
    global _VM_NAME
    from uuid import uuid4

    if state() is not State.NotInitialized:
        logging.info("The VM has already been initialized.")
        logging.info(
            "If you need to start over, destroy the existing environment first with `yurt vm destroy`"
        )
        return

    download_image()

    vm_name = "{0}-{1}".format(config.app_name, uuid4())

    try:
        logging.info("Importing appliance...")
        vbox.import_vm(vm_name, config.image, config.vm_install_dir,
                       config.vm_memory)

        config.set_config(config.Key.vm_name, vm_name)
        _VM_NAME = vm_name

        input("""Installing VirtualBox network interface.
Accept VirtualBox's prompt to allow networking with the host.
Press enter to continue...""")
        _attach_config_disk()
        _attach_storage_pool_disk()
        _setup_network()

    except (
            ConfigWriteException,
            VBoxException,
            VMException,
    ) as e:
        logging.error(e.message)
        logging.info("Cleaning up...")
        destroy()
        delete_instance_files()
        raise VMException("Initialization failed.")
    except KeyboardInterrupt:
        logging.info("\nUser interrupted initialization. Cleaning up.")
        destroy()
        delete_instance_files()
        raise VMException("Initialization failed")
Beispiel #5
0
def _setup_network():
    try:
        interface_name = vbox.create_hostonly_interface()
        interface_info = vbox.get_interface_info(interface_name)
        ip_address = interface_info["IPAddress"]
        network_mask = interface_info["NetworkMask"]
        config.set_config(config.Key.interface, interface_name)
        config.set_config(config.Key.interface_ip_address, ip_address)
        config.set_config(config.Key.interface_netmask, network_mask)

        vbox.modify_vm(
            _VM_NAME,
            {
                "nic1": "nat",
                "nictype1": "virtio",
                "natnet1": "10.0.2.0/24",
                "natdnshostresolver1": "on",
                "nic2": "hostonly",
                "nictype2": "virtio",
                "hostonlyadapter2": interface_name,
                "nicpromisc2": "allow-all",
            },
        )

    except VBoxException as e:
        logging.error(e.message)
        raise VMException("Network initialization failed")
Beispiel #6
0
def initialize_lxd():
    lxd_init = """config:
  core.https_address: '[::]:8443'
  core.trust_password: yurtsecret
networks: []
storage_pools:
- config:
    source: /dev/sdc
  description: ""
  name: yurtpool
  driver: zfs
profiles:
- config: {}
  description: ""
  devices:
    root:
      path: /
      pool: yurtpool
      type: disk
  name: default
cluster: null
    """

    try:
        logging.info("Installing LXD. This might take a few minutes...")
        run_in_vm("sudo snap install lxd", show_spinner=True)
        logging.info("LXD installed. Configuring...")

        run_in_vm("sudo lxd.migrate -yes", show_spinner=True)
        run_in_vm(
            "sudo lxd init --preseed",
            stdin=lxd_init,
            show_spinner=True
        )
        logging.info("Done.")
        config.set_config(config.Key.is_lxd_initialized, True)
    except RemoteCommandException as e:
        logging.error(e)
        logging.info("Restart the VM to try again: 'yurt shutdown; yurt boot'")
        raise LXCException("Failed to initialize LXD.")