2019-11-25 06:52:36 +00:00
|
|
|
import shutil
|
|
|
|
import subprocess as sp
|
|
|
|
import os
|
|
|
|
import stat
|
|
|
|
|
|
|
|
from abc import ABC
|
2019-12-03 10:40:41 +00:00
|
|
|
from . import logger
|
2019-11-25 06:52:36 +00:00
|
|
|
from os.path import join as join_path
|
2020-01-13 04:57:41 +00:00
|
|
|
import uncloud.common.shared as shared
|
2019-12-22 07:26:48 +00:00
|
|
|
|
2019-11-25 06:52:36 +00:00
|
|
|
|
|
|
|
class ImageStorageHandler(ABC):
|
2019-12-30 09:35:07 +00:00
|
|
|
handler_name = "base"
|
2019-12-23 07:58:04 +00:00
|
|
|
|
2019-11-25 06:52:36 +00:00
|
|
|
def __init__(self, image_base, vm_base):
|
|
|
|
self.image_base = image_base
|
|
|
|
self.vm_base = vm_base
|
|
|
|
|
|
|
|
def import_image(self, image_src, image_dest, protect=False):
|
|
|
|
"""Put an image at the destination
|
2019-12-28 10:39:11 +00:00
|
|
|
:param image_src: An Image file
|
|
|
|
:param image_dest: A path where :param src: is to be put.
|
2019-11-25 06:52:36 +00:00
|
|
|
:param protect: If protect is true then the dest is protect (readonly etc)
|
|
|
|
The obj must exist on filesystem.
|
|
|
|
"""
|
|
|
|
|
|
|
|
raise NotImplementedError()
|
|
|
|
|
|
|
|
def make_vm_image(self, image_path, path):
|
|
|
|
"""Copy image from src to dest
|
|
|
|
|
2019-12-28 10:39:11 +00:00
|
|
|
:param image_path: A path
|
|
|
|
:param path: A path
|
2019-11-25 06:52:36 +00:00
|
|
|
|
|
|
|
src and destination must be on same storage system i.e both on file system or both on CEPH etc.
|
|
|
|
"""
|
|
|
|
raise NotImplementedError()
|
|
|
|
|
|
|
|
def resize_vm_image(self, path, size):
|
|
|
|
"""Resize image located at :param path:
|
|
|
|
:param path: The file which is to be resized
|
|
|
|
:param size: Size must be in Megabytes
|
|
|
|
"""
|
|
|
|
raise NotImplementedError()
|
|
|
|
|
|
|
|
def delete_vm_image(self, path):
|
|
|
|
raise NotImplementedError()
|
|
|
|
|
2019-12-23 07:58:04 +00:00
|
|
|
def execute_command(self, command, report=True, error_origin=None):
|
|
|
|
if not error_origin:
|
|
|
|
error_origin = self.handler_name
|
|
|
|
|
2019-11-25 06:52:36 +00:00
|
|
|
command = list(map(str, command))
|
|
|
|
try:
|
2019-12-23 07:58:04 +00:00
|
|
|
sp.check_output(command, stderr=sp.PIPE)
|
|
|
|
except sp.CalledProcessError as e:
|
2019-12-30 09:35:07 +00:00
|
|
|
_stderr = e.stderr.decode("utf-8").strip()
|
2019-11-25 06:52:36 +00:00
|
|
|
if report:
|
2019-12-30 09:35:07 +00:00
|
|
|
logger.exception("%s:- %s", error_origin, _stderr)
|
2019-11-25 06:52:36 +00:00
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
|
|
|
def vm_path_string(self, path):
|
|
|
|
raise NotImplementedError()
|
|
|
|
|
|
|
|
def qemu_path_string(self, path):
|
|
|
|
raise NotImplementedError()
|
|
|
|
|
|
|
|
def is_vm_image_exists(self, path):
|
|
|
|
raise NotImplementedError()
|
|
|
|
|
|
|
|
|
|
|
|
class FileSystemBasedImageStorageHandler(ImageStorageHandler):
|
2019-12-30 09:35:07 +00:00
|
|
|
handler_name = "Filesystem"
|
2019-12-23 07:58:04 +00:00
|
|
|
|
2019-11-25 06:52:36 +00:00
|
|
|
def import_image(self, src, dest, protect=False):
|
|
|
|
dest = join_path(self.image_base, dest)
|
|
|
|
try:
|
|
|
|
shutil.copy(src, dest)
|
|
|
|
if protect:
|
2019-12-30 09:35:07 +00:00
|
|
|
os.chmod(
|
|
|
|
dest, stat.S_IRUSR | stat.S_IRGRP | stat.S_IROTH
|
|
|
|
)
|
2019-11-25 06:52:36 +00:00
|
|
|
except Exception as e:
|
|
|
|
logger.exception(e)
|
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
|
|
|
def make_vm_image(self, src, dest):
|
|
|
|
src = join_path(self.image_base, src)
|
|
|
|
dest = join_path(self.vm_base, dest)
|
|
|
|
try:
|
2019-12-26 09:30:15 +00:00
|
|
|
shutil.copyfile(src, dest)
|
2019-11-25 06:52:36 +00:00
|
|
|
except Exception as e:
|
|
|
|
logger.exception(e)
|
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
|
|
|
def resize_vm_image(self, path, size):
|
|
|
|
path = join_path(self.vm_base, path)
|
2019-12-30 09:35:07 +00:00
|
|
|
command = [
|
|
|
|
"qemu-img",
|
|
|
|
"resize",
|
|
|
|
"-f",
|
|
|
|
"raw",
|
|
|
|
path,
|
|
|
|
"{}M".format(size),
|
|
|
|
]
|
2019-11-25 06:52:36 +00:00
|
|
|
if self.execute_command(command):
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
self.delete_vm_image(path)
|
|
|
|
return False
|
|
|
|
|
|
|
|
def delete_vm_image(self, path):
|
|
|
|
path = join_path(self.vm_base, path)
|
|
|
|
try:
|
|
|
|
os.remove(path)
|
|
|
|
except Exception as e:
|
|
|
|
logger.exception(e)
|
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
|
|
|
def vm_path_string(self, path):
|
|
|
|
return join_path(self.vm_base, path)
|
|
|
|
|
|
|
|
def qemu_path_string(self, path):
|
|
|
|
return self.vm_path_string(path)
|
|
|
|
|
|
|
|
def is_vm_image_exists(self, path):
|
|
|
|
path = join_path(self.vm_base, path)
|
|
|
|
command = ["ls", path]
|
|
|
|
return self.execute_command(command, report=False)
|
|
|
|
|
|
|
|
|
|
|
|
class CEPHBasedImageStorageHandler(ImageStorageHandler):
|
2019-12-30 09:35:07 +00:00
|
|
|
handler_name = "Ceph"
|
2019-12-23 07:58:04 +00:00
|
|
|
|
2019-11-25 06:52:36 +00:00
|
|
|
def import_image(self, src, dest, protect=False):
|
|
|
|
dest = join_path(self.image_base, dest)
|
2019-12-23 07:58:04 +00:00
|
|
|
import_command = ["rbd", "import", src, dest]
|
|
|
|
commands = [import_command]
|
2019-11-25 06:52:36 +00:00
|
|
|
if protect:
|
2019-12-30 09:35:07 +00:00
|
|
|
snap_create_command = [
|
|
|
|
"rbd",
|
|
|
|
"snap",
|
|
|
|
"create",
|
|
|
|
"{}@protected".format(dest),
|
|
|
|
]
|
|
|
|
snap_protect_command = [
|
|
|
|
"rbd",
|
|
|
|
"snap",
|
|
|
|
"protect",
|
|
|
|
"{}@protected".format(dest),
|
|
|
|
]
|
2019-12-23 07:58:04 +00:00
|
|
|
commands.append(snap_create_command)
|
|
|
|
commands.append(snap_protect_command)
|
2019-11-25 06:52:36 +00:00
|
|
|
|
2019-12-23 07:58:04 +00:00
|
|
|
result = True
|
|
|
|
for command in commands:
|
|
|
|
result = result and self.execute_command(command)
|
2019-11-25 06:52:36 +00:00
|
|
|
|
2019-12-23 07:58:04 +00:00
|
|
|
return result
|
2019-11-25 06:52:36 +00:00
|
|
|
|
|
|
|
def make_vm_image(self, src, dest):
|
|
|
|
src = join_path(self.image_base, src)
|
|
|
|
dest = join_path(self.vm_base, dest)
|
|
|
|
|
|
|
|
command = ["rbd", "clone", "{}@protected".format(src), dest]
|
|
|
|
return self.execute_command(command)
|
|
|
|
|
|
|
|
def resize_vm_image(self, path, size):
|
|
|
|
path = join_path(self.vm_base, path)
|
|
|
|
command = ["rbd", "resize", path, "--size", size]
|
|
|
|
return self.execute_command(command)
|
|
|
|
|
|
|
|
def delete_vm_image(self, path):
|
|
|
|
path = join_path(self.vm_base, path)
|
|
|
|
command = ["rbd", "rm", path]
|
|
|
|
return self.execute_command(command)
|
|
|
|
|
|
|
|
def vm_path_string(self, path):
|
|
|
|
return join_path(self.vm_base, path)
|
|
|
|
|
|
|
|
def qemu_path_string(self, path):
|
|
|
|
return "rbd:{}".format(self.vm_path_string(path))
|
|
|
|
|
|
|
|
def is_vm_image_exists(self, path):
|
|
|
|
path = join_path(self.vm_base, path)
|
|
|
|
command = ["rbd", "info", path]
|
|
|
|
return self.execute_command(command, report=False)
|
2019-12-22 07:26:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
def get_storage_handler():
|
2020-01-13 04:57:41 +00:00
|
|
|
__storage_backend = shared.shared.settings["storage"]["storage_backend"]
|
2019-12-30 09:35:07 +00:00
|
|
|
if __storage_backend == "filesystem":
|
2019-12-22 07:26:48 +00:00
|
|
|
return FileSystemBasedImageStorageHandler(
|
2020-01-13 04:57:41 +00:00
|
|
|
vm_base=shared.shared.settings["storage"]["vm_dir"],
|
|
|
|
image_base=shared.shared.settings["storage"]["image_dir"],
|
2019-12-22 07:26:48 +00:00
|
|
|
)
|
2019-12-30 09:35:07 +00:00
|
|
|
elif __storage_backend == "ceph":
|
2019-12-22 07:26:48 +00:00
|
|
|
return CEPHBasedImageStorageHandler(
|
2020-01-13 04:57:41 +00:00
|
|
|
vm_base=shared.shared.settings["storage"]["ceph_vm_pool"],
|
|
|
|
image_base=shared.shared.settings["storage"]["ceph_image_pool"],
|
2019-12-22 07:26:48 +00:00
|
|
|
)
|
|
|
|
else:
|
2020-01-13 04:57:41 +00:00
|
|
|
raise Exception("Unknown Image Storage Handler")
|