2019-12-28 10:39:11 +00:00
|
|
|
import os
|
|
|
|
import subprocess as sp
|
|
|
|
import logging
|
|
|
|
import socket
|
|
|
|
import json
|
|
|
|
import tempfile
|
|
|
|
import time
|
|
|
|
|
|
|
|
from contextlib import suppress
|
|
|
|
from multiprocessing import Process
|
|
|
|
from os.path import join as join_path
|
|
|
|
from os.path import isdir
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
class VMQMPHandles:
|
|
|
|
def __init__(self, path):
|
|
|
|
self.path = path
|
|
|
|
self.sock = socket.socket(socket.AF_UNIX)
|
|
|
|
self.file = self.sock.makefile()
|
|
|
|
|
|
|
|
def __enter__(self):
|
|
|
|
self.sock.connect(self.path)
|
|
|
|
|
|
|
|
# eat qmp greetings
|
|
|
|
self.file.readline()
|
|
|
|
|
|
|
|
# init qmp
|
|
|
|
self.sock.sendall(b'{ "execute": "qmp_capabilities" }')
|
|
|
|
self.file.readline()
|
|
|
|
|
|
|
|
return self.sock, self.file
|
|
|
|
|
|
|
|
def __exit__(self, exc_type, exc_val, exc_tb):
|
|
|
|
self.file.close()
|
|
|
|
self.sock.close()
|
|
|
|
|
|
|
|
if exc_type:
|
2019-12-30 09:35:07 +00:00
|
|
|
logger.error(
|
|
|
|
"Couldn't get handle for VM.", exc_type, exc_val, exc_tb
|
|
|
|
)
|
2019-12-28 10:39:11 +00:00
|
|
|
raise exc_type("Couldn't get handle for VM.") from exc_type
|
|
|
|
|
|
|
|
|
|
|
|
class TransferVM(Process):
|
2019-12-29 18:48:04 +00:00
|
|
|
def __init__(self, src_uuid, dest_sock_path, host, socket_dir):
|
2019-12-28 10:39:11 +00:00
|
|
|
self.src_uuid = src_uuid
|
|
|
|
self.host = host
|
|
|
|
self.src_sock_path = os.path.join(socket_dir, self.src_uuid)
|
2019-12-29 18:48:04 +00:00
|
|
|
self.dest_sock_path = dest_sock_path
|
2019-12-28 10:39:11 +00:00
|
|
|
|
|
|
|
super().__init__()
|
|
|
|
|
|
|
|
def run(self):
|
|
|
|
with suppress(FileNotFoundError):
|
|
|
|
os.remove(self.src_sock_path)
|
|
|
|
|
2019-12-30 09:35:07 +00:00
|
|
|
command = [
|
|
|
|
"ssh",
|
|
|
|
"-nNT",
|
|
|
|
"-L",
|
|
|
|
"{}:{}".format(self.src_sock_path, self.dest_sock_path),
|
|
|
|
"root@{}".format(self.host),
|
|
|
|
]
|
2019-12-28 10:39:11 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
p = sp.Popen(command)
|
|
|
|
except Exception as e:
|
2019-12-30 09:35:07 +00:00
|
|
|
logger.error(
|
|
|
|
"Couldn' forward unix socks over ssh.", exc_info=e
|
|
|
|
)
|
2019-12-28 10:39:11 +00:00
|
|
|
else:
|
|
|
|
time.sleep(2)
|
|
|
|
vmm = VMM()
|
2019-12-30 16:14:08 +00:00
|
|
|
logger.debug("Executing: ssh forwarding command: %s", command)
|
2019-12-30 09:35:07 +00:00
|
|
|
vmm.execute_command(
|
|
|
|
self.src_uuid,
|
|
|
|
command="migrate",
|
|
|
|
arguments={"uri": "unix:{}".format(self.src_sock_path)},
|
|
|
|
)
|
2019-12-28 10:39:11 +00:00
|
|
|
|
|
|
|
while p.poll() is None:
|
2019-12-30 16:14:08 +00:00
|
|
|
success, output = vmm.execute_command(self.src_uuid, command="query-migrate")
|
2019-12-28 10:39:11 +00:00
|
|
|
if success:
|
2019-12-30 09:35:07 +00:00
|
|
|
status = output["return"]["status"]
|
2019-12-30 16:14:08 +00:00
|
|
|
logger.info('Migration Status: {}'.format(status))
|
|
|
|
if status == "completed":
|
|
|
|
vmm.stop(self.src_uuid)
|
|
|
|
return
|
|
|
|
elif status in ['failed', 'cancelled']:
|
2019-12-28 10:39:11 +00:00
|
|
|
return
|
|
|
|
else:
|
2019-12-30 16:14:08 +00:00
|
|
|
logger.error("Couldn't be able to query VM {} that was in migration".format(self.src_uuid))
|
2019-12-28 10:39:11 +00:00
|
|
|
return
|
2019-12-30 16:14:08 +00:00
|
|
|
|
|
|
|
time.sleep(2)
|
2019-12-28 10:39:11 +00:00
|
|
|
|
|
|
|
|
|
|
|
class VMM:
|
|
|
|
# Virtual Machine Manager
|
2019-12-30 09:35:07 +00:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
qemu_path="/usr/bin/qemu-system-x86_64",
|
2019-12-31 10:30:02 +00:00
|
|
|
vmm_backend=os.path.expanduser("~/uncloud/vmm/"),
|
2019-12-30 09:35:07 +00:00
|
|
|
):
|
2019-12-28 10:39:11 +00:00
|
|
|
self.qemu_path = qemu_path
|
|
|
|
self.vmm_backend = vmm_backend
|
2019-12-30 09:35:07 +00:00
|
|
|
self.socket_dir = os.path.join(self.vmm_backend, "sock")
|
2019-12-28 10:39:11 +00:00
|
|
|
|
2019-12-29 18:14:39 +00:00
|
|
|
if not os.path.isdir(self.vmm_backend):
|
2019-12-30 09:35:07 +00:00
|
|
|
logger.info(
|
|
|
|
"{} does not exists. Creating it...".format(
|
|
|
|
self.vmm_backend
|
|
|
|
)
|
|
|
|
)
|
2019-12-29 18:14:39 +00:00
|
|
|
os.makedirs(self.vmm_backend, exist_ok=True)
|
|
|
|
|
|
|
|
if not os.path.isdir(self.socket_dir):
|
2019-12-30 09:35:07 +00:00
|
|
|
logger.info(
|
|
|
|
"{} does not exists. Creating it...".format(
|
|
|
|
self.socket_dir
|
|
|
|
)
|
|
|
|
)
|
2019-12-29 18:14:39 +00:00
|
|
|
os.makedirs(self.socket_dir, exist_ok=True)
|
|
|
|
|
2019-12-28 10:39:11 +00:00
|
|
|
def is_running(self, uuid):
|
|
|
|
sock_path = os.path.join(self.vmm_backend, uuid)
|
|
|
|
try:
|
|
|
|
sock = socket.socket(socket.AF_UNIX)
|
|
|
|
sock.connect(sock_path)
|
|
|
|
recv = sock.recv(4096)
|
|
|
|
except Exception as err:
|
|
|
|
# unix sock doesn't exists or it is closed
|
2019-12-30 09:35:07 +00:00
|
|
|
logger.debug(
|
|
|
|
"VM {} sock either don' exists or it is closed. It mean VM is stopped.".format(
|
|
|
|
uuid
|
|
|
|
),
|
|
|
|
exc_info=err,
|
|
|
|
)
|
2019-12-28 10:39:11 +00:00
|
|
|
else:
|
|
|
|
# if we receive greetings from qmp it mean VM is running
|
|
|
|
if len(recv) > 0:
|
|
|
|
return True
|
|
|
|
|
|
|
|
with suppress(FileNotFoundError):
|
|
|
|
os.remove(sock_path)
|
|
|
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
def start(self, *args, uuid, migration=False):
|
|
|
|
# start --> sucess?
|
|
|
|
migration_args = ()
|
|
|
|
if migration:
|
2019-12-30 09:35:07 +00:00
|
|
|
migration_args = (
|
|
|
|
"-incoming",
|
|
|
|
"unix:{}".format(os.path.join(self.socket_dir, uuid)),
|
|
|
|
)
|
2019-12-28 10:39:11 +00:00
|
|
|
|
|
|
|
if self.is_running(uuid):
|
2019-12-30 09:35:07 +00:00
|
|
|
logger.warning("Cannot start VM. It is already running.")
|
2019-12-28 10:39:11 +00:00
|
|
|
else:
|
2019-12-30 09:35:07 +00:00
|
|
|
qmp_arg = (
|
|
|
|
"-qmp",
|
|
|
|
"unix:{},server,nowait".format(
|
|
|
|
join_path(self.vmm_backend, uuid)
|
|
|
|
),
|
|
|
|
)
|
|
|
|
vnc_arg = (
|
|
|
|
"-vnc",
|
|
|
|
"unix:{}".format(tempfile.NamedTemporaryFile().name),
|
|
|
|
)
|
|
|
|
|
|
|
|
command = [
|
|
|
|
"sudo",
|
|
|
|
"-p",
|
|
|
|
"Enter password to start VM {}: ".format(uuid),
|
|
|
|
self.qemu_path,
|
|
|
|
*args,
|
|
|
|
*qmp_arg,
|
|
|
|
*migration_args,
|
|
|
|
*vnc_arg,
|
|
|
|
"-daemonize",
|
|
|
|
]
|
2019-12-28 10:39:11 +00:00
|
|
|
try:
|
|
|
|
sp.check_output(command, stderr=sp.PIPE)
|
|
|
|
except sp.CalledProcessError as err:
|
2019-12-30 09:35:07 +00:00
|
|
|
logger.exception(
|
|
|
|
"Error occurred while starting VM.\nDetail %s",
|
|
|
|
err.stderr.decode("utf-8"),
|
|
|
|
)
|
2019-12-28 10:39:11 +00:00
|
|
|
else:
|
2019-12-29 18:14:39 +00:00
|
|
|
with suppress(sp.CalledProcessError):
|
2019-12-30 09:35:07 +00:00
|
|
|
sp.check_output(
|
|
|
|
[
|
|
|
|
"sudo",
|
|
|
|
"-p",
|
|
|
|
"Enter password to correct permission for uncloud-vmm's directory",
|
|
|
|
"chmod",
|
|
|
|
"-R",
|
|
|
|
"o=rwx,g=rwx",
|
|
|
|
self.vmm_backend,
|
|
|
|
]
|
|
|
|
)
|
2019-12-29 18:14:39 +00:00
|
|
|
|
|
|
|
# TODO: Find some good way to check whether the virtual machine is up and
|
|
|
|
# running without relying on non-guarenteed ways.
|
|
|
|
for _ in range(10):
|
|
|
|
time.sleep(2)
|
|
|
|
status = self.get_status(uuid)
|
2019-12-30 09:35:07 +00:00
|
|
|
if status in ["running", "inmigrate"]:
|
2019-12-29 18:14:39 +00:00
|
|
|
return status
|
2019-12-30 09:35:07 +00:00
|
|
|
logger.warning(
|
|
|
|
"Timeout on VM's status. Shutting down VM %s", uuid
|
|
|
|
)
|
2019-12-29 18:14:39 +00:00
|
|
|
self.stop(uuid)
|
|
|
|
# TODO: What should we do more. VM can still continue to run in background.
|
|
|
|
# If we have pid of vm we can kill it using OS.
|
2019-12-28 10:39:11 +00:00
|
|
|
|
|
|
|
def execute_command(self, uuid, command, **kwargs):
|
|
|
|
# execute_command -> sucess?, output
|
|
|
|
try:
|
2019-12-30 09:35:07 +00:00
|
|
|
with VMQMPHandles(os.path.join(self.vmm_backend, uuid)) as (
|
|
|
|
sock_handle,
|
|
|
|
file_handle,
|
|
|
|
):
|
|
|
|
command_to_execute = {"execute": command, **kwargs}
|
|
|
|
sock_handle.sendall(
|
|
|
|
json.dumps(command_to_execute).encode("utf-8")
|
|
|
|
)
|
2019-12-28 10:39:11 +00:00
|
|
|
output = file_handle.readline()
|
2019-12-29 18:14:39 +00:00
|
|
|
except Exception:
|
2019-12-30 09:35:07 +00:00
|
|
|
logger.exception(
|
|
|
|
"Error occurred while executing command and getting valid output from qmp"
|
|
|
|
)
|
2019-12-28 10:39:11 +00:00
|
|
|
else:
|
|
|
|
try:
|
|
|
|
output = json.loads(output)
|
2019-12-29 18:14:39 +00:00
|
|
|
except Exception:
|
2019-12-30 09:35:07 +00:00
|
|
|
logger.exception(
|
|
|
|
"QMP Output isn't valid JSON. %s", output
|
|
|
|
)
|
2019-12-28 10:39:11 +00:00
|
|
|
else:
|
2019-12-30 09:35:07 +00:00
|
|
|
return "return" in output, output
|
2019-12-28 10:39:11 +00:00
|
|
|
return False, None
|
|
|
|
|
|
|
|
def stop(self, uuid):
|
2019-12-30 09:35:07 +00:00
|
|
|
success, output = self.execute_command(
|
|
|
|
command="quit", uuid=uuid
|
|
|
|
)
|
2019-12-28 10:39:11 +00:00
|
|
|
return success
|
|
|
|
|
|
|
|
def get_status(self, uuid):
|
2019-12-30 09:35:07 +00:00
|
|
|
success, output = self.execute_command(
|
|
|
|
command="query-status", uuid=uuid
|
|
|
|
)
|
2019-12-28 10:39:11 +00:00
|
|
|
if success:
|
2019-12-30 09:35:07 +00:00
|
|
|
return output["return"]["status"]
|
2019-12-28 10:39:11 +00:00
|
|
|
else:
|
2019-12-29 18:14:39 +00:00
|
|
|
# TODO: Think about this for a little more
|
2019-12-30 09:35:07 +00:00
|
|
|
return "STOPPED"
|
2019-12-28 10:39:11 +00:00
|
|
|
|
|
|
|
def discover(self):
|
|
|
|
vms = [
|
2019-12-30 09:35:07 +00:00
|
|
|
uuid
|
|
|
|
for uuid in os.listdir(self.vmm_backend)
|
2019-12-28 10:39:11 +00:00
|
|
|
if not isdir(join_path(self.vmm_backend, uuid))
|
|
|
|
]
|
|
|
|
return vms
|
|
|
|
|
|
|
|
def get_vnc(self, uuid):
|
2019-12-30 09:35:07 +00:00
|
|
|
success, output = self.execute_command(
|
|
|
|
uuid, command="query-vnc"
|
|
|
|
)
|
2019-12-28 10:39:11 +00:00
|
|
|
if success:
|
2019-12-30 09:35:07 +00:00
|
|
|
return output["return"]["service"]
|
2019-12-28 10:39:11 +00:00
|
|
|
return None
|
|
|
|
|
2019-12-29 18:48:04 +00:00
|
|
|
def transfer(self, src_uuid, destination_sock_path, host):
|
2019-12-30 09:35:07 +00:00
|
|
|
p = TransferVM(
|
|
|
|
src_uuid,
|
|
|
|
destination_sock_path,
|
|
|
|
socket_dir=self.socket_dir,
|
|
|
|
host=host,
|
|
|
|
)
|
2019-12-28 10:39:11 +00:00
|
|
|
p.start()
|
2019-12-29 18:48:04 +00:00
|
|
|
|
|
|
|
# TODO: the following method should clean things that went wrong
|
|
|
|
# e.g If VM migration fails or didn't start for long time
|
|
|
|
# i.e 15 minutes we should stop the waiting VM.
|
|
|
|
def maintenace(self):
|
2019-12-30 09:35:07 +00:00
|
|
|
pass
|