from copy import deepcopy
from http import HTTPStatus
import logging
-from os import mkdir
+from os import makedirs
+from os import path
import queue
-from shutil import rmtree
import threading
import time
import traceback
+from typing import Dict
from unittest.mock import Mock
from importlib_metadata import entry_points
from osm_common.dbbase import DbException
from osm_ng_ro.vim_admin import LockRenew
-from osm_ro_plugin import sdnconn, vimconn
+from osm_ro_plugin import sdnconn
+from osm_ro_plugin import vimconn
from osm_ro_plugin.sdn_dummy import SdnDummyConnector
from osm_ro_plugin.vim_dummy import VimDummyConnector
import yaml
return "BUILD", ro_vim_item_update
except (vimconn.VimConnException, NsWorkerException) as e:
+ self.logger.debug(traceback.format_exc())
self.logger.error(
"task={} {} new-vm: {}".format(task_id, ro_task["target_id"], e)
)
vim_info_info = yaml.safe_load(vim_info["vim_info"])
if vim_info_info.get("name"):
vim_info["name"] = vim_info_info["name"]
- except Exception:
- pass
+ except Exception as vim_info_error:
+ self.logger.exception(
+ f"{vim_info_error} occured while getting the vim_info from yaml"
+ )
except vimconn.VimConnException as e:
# Mark all tasks at VIM_ERROR status
self.logger.error(
try:
flavor_data = task["find_params"]["flavor_data"]
vim_flavor_id = target_vim.get_flavor_id_from_data(flavor_data)
- except vimconn.VimConnNotFoundException:
- pass
+ except vimconn.VimConnNotFoundException as flavor_not_found_msg:
+ self.logger.warning(
+ f"VimConnNotFoundException occured: {flavor_not_found_msg}"
+ )
if not vim_flavor_id and task.get("params"):
# CREATE
return "FAILED", ro_vim_item_update
+class VimInteractionUpdateVdu(VimInteractionBase):
+ def exec(self, ro_task, task_index, task_depends):
+ task = ro_task["tasks"][task_index]
+ task_id = task["task_id"]
+ db_task_update = {"retries": 0}
+ created = False
+ created_items = {}
+ target_vim = self.my_vims[ro_task["target_id"]]
+
+ try:
+ if task.get("params"):
+ vim_vm_id = task["params"].get("vim_vm_id")
+ action = task["params"].get("action")
+ context = {action: action}
+ target_vim.action_vminstance(vim_vm_id, context)
+ # created = True
+ ro_vim_item_update = {
+ "vim_id": vim_vm_id,
+ "vim_status": "DONE",
+ "created": created,
+ "created_items": created_items,
+ "vim_details": None,
+ "vim_message": None,
+ }
+ self.logger.debug(
+ "task={} {} vm-migration done".format(task_id, ro_task["target_id"])
+ )
+ return "DONE", ro_vim_item_update, db_task_update
+ except (vimconn.VimConnException, NsWorkerException) as e:
+ self.logger.error(
+ "task={} vim={} VM Migration:"
+ " {}".format(task_id, ro_task["target_id"], e)
+ )
+ ro_vim_item_update = {
+ "vim_status": "VIM_ERROR",
+ "created": created,
+ "vim_message": str(e),
+ }
+
+ return "FAILED", ro_vim_item_update, db_task_update
+
+
class VimInteractionSdnNet(VimInteractionBase):
@staticmethod
def _match_pci(port_pci, mapping):
return self.new(ro_task, task_create_index, None)
def new(self, ro_task, task_index, task_depends):
-
task = ro_task["tasks"][task_index]
task_id = task["task_id"]
target_vim = self.my_vims[ro_task["target_id"]]
try:
# CREATE
params = task["params"]
- vlds_to_connect = params["vlds"]
- associated_vim = params["target_vim"]
+ vlds_to_connect = params.get("vlds", [])
+ associated_vim = params.get("target_vim")
# external additional ports
additional_ports = params.get("sdn-ports") or ()
- _, _, vim_account_id = associated_vim.partition(":")
+ _, _, vim_account_id = (
+ (None, None, None)
+ if associated_vim is None
+ else associated_vim.partition(":")
+ )
if associated_vim:
# get associated VIM
return "FAILED", ro_vim_item_update, db_task_update
-class NsWorker(threading.Thread):
- REFRESH_BUILD = 5 # 5 seconds
- REFRESH_ACTIVE = 60 # 1 minute
- REFRESH_ERROR = 600
- REFRESH_IMAGE = 3600 * 10
- REFRESH_DELETE = 3600 * 10
- QUEUE_SIZE = 100
- terminate = False
+class VimInteractionResize(VimInteractionBase):
+ def exec(self, ro_task, task_index, task_depends):
+ task = ro_task["tasks"][task_index]
+ task_id = task["task_id"]
+ db_task_update = {"retries": 0}
+ created = False
+ target_flavor_uuid = None
+ created_items = {}
+ refreshed_vim_info = {}
+ target_vim = self.my_vims[ro_task["target_id"]]
+
+ try:
+ if task.get("params"):
+ vim_vm_id = task["params"].get("vim_vm_id")
+ flavor_dict = task["params"].get("flavor_dict")
+ self.logger.info("flavor_dict %s", flavor_dict)
+
+ try:
+ target_flavor_uuid = target_vim.get_flavor_id_from_data(flavor_dict)
+ except Exception as e:
+ self.logger.info("Cannot find any flavor matching %s.", str(e))
+ try:
+ target_flavor_uuid = target_vim.new_flavor(flavor_dict)
+ except Exception as e:
+ self.logger.error("Error creating flavor at VIM %s.", str(e))
+
+ if target_flavor_uuid is not None:
+ resized_status = target_vim.resize_instance(
+ vim_vm_id, target_flavor_uuid
+ )
+
+ if resized_status:
+ # Refresh VM to get new vim_info
+ vm_to_refresh_list = [vim_vm_id]
+ vim_dict = target_vim.refresh_vms_status(vm_to_refresh_list)
+ refreshed_vim_info = vim_dict[vim_vm_id]
+
+ ro_vim_item_update = {
+ "vim_id": vim_vm_id,
+ "vim_status": "DONE",
+ "created": created,
+ "created_items": created_items,
+ "vim_details": None,
+ "vim_message": None,
+ }
+
+ if refreshed_vim_info and refreshed_vim_info.get("status") not in (
+ "ERROR",
+ "VIM_ERROR",
+ ):
+ ro_vim_item_update["vim_details"] = refreshed_vim_info["vim_info"]
+
+ self.logger.debug(
+ "task={} {} resize done".format(task_id, ro_task["target_id"])
+ )
+ return "DONE", ro_vim_item_update, db_task_update
+ except (vimconn.VimConnException, NsWorkerException) as e:
+ self.logger.error(
+ "task={} vim={} Resize:" " {}".format(task_id, ro_task["target_id"], e)
+ )
+ ro_vim_item_update = {
+ "vim_status": "VIM_ERROR",
+ "created": created,
+ "vim_message": str(e),
+ }
+
+ return "FAILED", ro_vim_item_update, db_task_update
+
+
+class ConfigValidate:
+ def __init__(self, config: Dict):
+ self.conf = config
+
+ @property
+ def active(self):
+ # default 1 min, allowed >= 60 or -1, -1 disables periodic checks
+ if (
+ self.conf["period"]["refresh_active"] >= 60
+ or self.conf["period"]["refresh_active"] == -1
+ ):
+ return self.conf["period"]["refresh_active"]
+
+ return 60
+
+ @property
+ def build(self):
+ return self.conf["period"]["refresh_build"]
+ @property
+ def image(self):
+ return self.conf["period"]["refresh_image"]
+
+ @property
+ def error(self):
+ return self.conf["period"]["refresh_error"]
+
+ @property
+ def queue_size(self):
+ return self.conf["period"]["queue_size"]
+
+
+class NsWorker(threading.Thread):
def __init__(self, worker_index, config, plugins, db):
"""
-
:param worker_index: thread index
:param config: general configuration of RO, among others the process_id with the docker id where it runs
:param plugins: global shared dict with the loaded plugins
self.plugin_name = "unknown"
self.logger = logging.getLogger("ro.worker{}".format(worker_index))
self.worker_index = worker_index
- self.task_queue = queue.Queue(self.QUEUE_SIZE)
+ # refresh periods for created items
+ self.refresh_config = ConfigValidate(config)
+ self.task_queue = queue.Queue(self.refresh_config.queue_size)
# targetvim: vimplugin class
self.my_vims = {}
# targetvim: vim information from database
"sdn_net": VimInteractionSdnNet(
self.db, self.my_vims, self.db_vims, self.logger
),
+ "update": VimInteractionUpdateVdu(
+ self.db, self.my_vims, self.db_vims, self.logger
+ ),
"affinity-or-anti-affinity-group": VimInteractionAffinityGroup(
self.db, self.my_vims, self.db_vims, self.logger
),
"migrate": VimInteractionMigration(
self.db, self.my_vims, self.db_vims, self.logger
),
+ "verticalscale": VimInteractionResize(
+ self.db, self.my_vims, self.db_vims, self.logger
+ ),
}
self.time_last_task_processed = None
# lists of tasks to delete because nsrs or vnfrs has been deleted from db
self.task_lock.release()
return False
- def _process_vim_config(self, target_id, db_vim):
+ def _process_vim_config(self, target_id: str, db_vim: dict) -> None:
"""
Process vim config, creating vim configuration files as ca_cert
:param target_id: vim/sdn/wim + id
return
file_name = ""
+ work_dir = "/app/osm_ro/certs"
try:
if db_vim["config"].get("ca_cert_content"):
- file_name = "{}:{}".format(target_id, self.worker_index)
+ file_name = f"{work_dir}/{target_id}:{self.worker_index}"
- try:
- mkdir(file_name)
- except FileExistsError:
- pass
+ if not path.isdir(file_name):
+ makedirs(file_name)
file_name = file_name + "/ca_cert"
self.vim_targets.remove(target_id)
self.logger.info("Unloaded {}".format(target_id))
- rmtree("{}:{}".format(target_id, self.worker_index))
- except FileNotFoundError:
- pass # this is raised by rmtree if folder does not exist
except Exception as e:
self.logger.error("Cannot unload {}: {}".format(target_id, e))
)
plugin_name = ""
vim = None
+ step = "Getting {}={} from db".format(target, _id)
try:
- step = "Getting {}={} from db".format(target, _id)
# TODO process for wim, sdnc, ...
vim = self.db.get_one(target_database, {"_id": _id})
persistent_info={},
)
else: # sdn
- plugin_name = "rosdn_" + vim["type"]
+ plugin_name = "rosdn_" + (vim.get("type") or vim.get("wim_type"))
step = "Loading plugin '{}'".format(plugin_name)
vim_module_conn = self._load_plugin(plugin_name, "sdn")
step = "Loading {}'".format(target_id)
wim = deepcopy(vim)
wim_config = wim.pop("config", {}) or {}
wim["uuid"] = wim["_id"]
- wim["wim_url"] = wim["url"]
+ if "url" in wim and "wim_url" not in wim:
+ wim["wim_url"] = wim["url"]
+ elif "url" not in wim and "wim_url" in wim:
+ wim["url"] = wim["wim_url"]
if wim.get("dpid"):
wim_config["dpid"] = wim.pop("dpid")
"tasks.status": ["SCHEDULED", "BUILD", "DONE", "FAILED"],
"locked_at.lt": now - self.task_locked_time,
"to_check_at.lt": self.time_last_task_processed,
+ "to_check_at.gt": -1,
},
update_dict={"locked_by": self.my_id, "locked_at": now},
fail_on_empty=False,
return None
- def _get_db_all_tasks(self):
- """
- Read all content of table ro_tasks to log it
- :return: None
- """
- try:
- # Checking the content of the BD:
-
- # read and return
- ro_task = self.db.get_list("ro_tasks")
- for rt in ro_task:
- self._log_ro_task(rt, None, None, "TASK_WF", "GET_ALL_TASKS")
- return ro_task
-
- except DbException as e:
- self.logger.error("Database exception at _get_db_all_tasks: {}".format(e))
- except Exception as e:
- self.logger.critical(
- "Unexpected exception at _get_db_all_tasks: {}".format(e), exc_info=True
- )
-
- return None
-
- def _log_ro_task(self, ro_task, db_ro_task_update, db_ro_task_delete, mark, event):
- """
- Generate a log with the following format:
-
- Mark;Event;ro_task_id;locked_at;modified_at;created_at;to_check_at;locked_by;
- target_id;vim_info.refresh_at;vim_info;no_of_tasks;task_status;action_id;
- task_array_index;task_id;task_action;task_item;task_args
-
- Example:
-
- TASK_WF;GET_TASK;888f1864-749a-4fc2-bc1a-97c0fffd6a6f:2;1642158724.8210013;
- 1642158640.7986135;1642158640.7986135;1642158640.7986135;b134c9494e75:0a
- ;vim:b7ff9e24-8868-4d68-8a57-a59dc11d0327;None;{'created': False,
- 'created_items': None, 'vim_id': None, 'vim_name': None, 'vim_status': None,
- 'vim_details': None, 'vim_message': None, 'refresh_at': None};1;SCHEDULED;
- 888f1864-749a-4fc2-bc1a-97c0fffd6a6f;0;888f1864-749a-4fc2-bc1a-97c0fffd6a6f:2;
- CREATE;image;{'filter_dict': {'name': 'ubuntu-os-cloud:image-family:ubuntu-1804-lts'}}
- """
- try:
- line = []
- i = 0
- if ro_task is not None and isinstance(ro_task, dict):
- for t in ro_task["tasks"]:
- line.clear()
- line.append(mark)
- line.append(event)
- line.append(ro_task.get("_id", ""))
- line.append(str(ro_task.get("locked_at", "")))
- line.append(str(ro_task.get("modified_at", "")))
- line.append(str(ro_task.get("created_at", "")))
- line.append(str(ro_task.get("to_check_at", "")))
- line.append(str(ro_task.get("locked_by", "")))
- line.append(str(ro_task.get("target_id", "")))
- line.append(str(ro_task.get("vim_info", {}).get("refresh_at", "")))
- line.append(str(ro_task.get("vim_info", "")))
- line.append(str(ro_task.get("tasks", "")))
- if isinstance(t, dict):
- line.append(str(t.get("status", "")))
- line.append(str(t.get("action_id", "")))
- line.append(str(i))
- line.append(str(t.get("task_id", "")))
- line.append(str(t.get("action", "")))
- line.append(str(t.get("item", "")))
- line.append(str(t.get("find_params", "")))
- line.append(str(t.get("params", "")))
- else:
- line.extend([""] * 2)
- line.append(str(i))
- line.extend([""] * 5)
-
- i += 1
- self.logger.debug(";".join(line))
- elif db_ro_task_update is not None and isinstance(db_ro_task_update, dict):
- i = 0
- while True:
- st = "tasks.{}.status".format(i)
- if st not in db_ro_task_update:
- break
- line.clear()
- line.append(mark)
- line.append(event)
- line.append(db_ro_task_update.get("_id", ""))
- line.append(str(db_ro_task_update.get("locked_at", "")))
- line.append(str(db_ro_task_update.get("modified_at", "")))
- line.append("")
- line.append(str(db_ro_task_update.get("to_check_at", "")))
- line.append(str(db_ro_task_update.get("locked_by", "")))
- line.append("")
- line.append(str(db_ro_task_update.get("vim_info.refresh_at", "")))
- line.append("")
- line.append(str(db_ro_task_update.get("vim_info", "")))
- line.append(str(str(db_ro_task_update).count(".status")))
- line.append(db_ro_task_update.get(st, ""))
- line.append("")
- line.append(str(i))
- line.extend([""] * 3)
- i += 1
- self.logger.debug(";".join(line))
-
- elif db_ro_task_delete is not None and isinstance(db_ro_task_delete, dict):
- line.clear()
- line.append(mark)
- line.append(event)
- line.append(db_ro_task_delete.get("_id", ""))
- line.append("")
- line.append(db_ro_task_delete.get("modified_at", ""))
- line.extend([""] * 13)
- self.logger.debug(";".join(line))
-
- else:
- line.clear()
- line.append(mark)
- line.append(event)
- line.extend([""] * 16)
- self.logger.debug(";".join(line))
-
- except Exception as e:
- self.logger.error("Error logging ro_task: {}".format(e))
-
def _delete_task(self, ro_task, task_index, task_depends, db_update):
"""
Determine if this task need to be done or superseded
"created_items", False
)
- self.logger.warning("Needed delete: {}".format(needed_delete))
+ self.logger.debug("Needed delete: {}".format(needed_delete))
if my_task["status"] == "FAILED":
return None, None # TODO need to be retry??
needed_delete = False
if needed_delete:
- self.logger.warning(
+ self.logger.debug(
"Deleting ro_task={} task_index={}".format(ro_task, task_index)
)
return self.item2class[my_task["item"]].delete(ro_task, task_index)
fail_on_empty=False,
)
- self.logger.warning("ro_task_dependency={}".format(ro_task_dependency))
+ self.logger.debug("ro_task_dependency={}".format(ro_task_dependency))
if ro_task_dependency:
for task_index, task in enumerate(ro_task_dependency["tasks"]):
if task["task_id"] == task_id:
return ro_task_dependency, task_index
raise NsWorkerException("Cannot get depending task {}".format(task_id))
+ def update_vm_refresh(self):
+ """Enables the VM status updates if self.refresh_config.active parameter
+ is not -1 and than updates the DB accordingly
+
+ """
+ try:
+ self.logger.debug("Checking if VM status update config")
+ next_refresh = time.time()
+ if self.refresh_config.active == -1:
+ next_refresh = -1
+ else:
+ next_refresh += self.refresh_config.active
+
+ if next_refresh != -1:
+ db_ro_task_update = {}
+ now = time.time()
+ next_check_at = now + (24 * 60 * 60)
+ next_check_at = min(next_check_at, next_refresh)
+ db_ro_task_update["vim_info.refresh_at"] = next_refresh
+ db_ro_task_update["to_check_at"] = next_check_at
+
+ self.logger.debug(
+ "Finding tasks which to be updated to enable VM status updates"
+ )
+ refresh_tasks = self.db.get_list(
+ "ro_tasks",
+ q_filter={
+ "tasks.status": "DONE",
+ "to_check_at.lt": 0,
+ },
+ )
+ self.logger.debug("Updating tasks to change the to_check_at status")
+ for task in refresh_tasks:
+ q_filter = {
+ "_id": task["_id"],
+ }
+ self.db.set_one(
+ "ro_tasks",
+ q_filter=q_filter,
+ update_dict=db_ro_task_update,
+ fail_on_empty=True,
+ )
+
+ except Exception as e:
+ self.logger.error(f"Error updating tasks to enable VM status updates: {e}")
+
def _process_pending_tasks(self, ro_task):
ro_task_id = ro_task["_id"]
now = time.time()
next_refresh = time.time()
if task["item"] in ("image", "flavor"):
- next_refresh += self.REFRESH_IMAGE
+ next_refresh += self.refresh_config.image
elif new_status == "BUILD":
- next_refresh += self.REFRESH_BUILD
+ next_refresh += self.refresh_config.build
elif new_status == "DONE":
- next_refresh += self.REFRESH_ACTIVE
+ if self.refresh_config.active == -1:
+ next_refresh = -1
+ else:
+ next_refresh += self.refresh_config.active
else:
- next_refresh += self.REFRESH_ERROR
+ next_refresh += self.refresh_config.error
next_check_at = min(next_check_at, next_refresh)
db_ro_task_update["vim_info.refresh_at"] = next_refresh
if self.logger.getEffectiveLevel() == logging.DEBUG:
self._log_ro_task(ro_task, None, None, "TASK_WF", "GET_TASK")
"""
+ # Check if vim status refresh is enabled again
+ self.update_vm_refresh()
# 0: get task_status_create
lock_object = None
task_status_create = None
dependency_task = dependency_ro_task["tasks"][
dependency_task_index
]
- self.logger.warning(
+ self.logger.debug(
"dependency_ro_task={} dependency_task_index={}".format(
dependency_ro_task, dependency_task_index
)
)
if task["action"] == "DELETE":
- (new_status, db_vim_info_update,) = self._delete_task(
+ (
+ new_status,
+ db_vim_info_update,
+ ) = self._delete_task(
ro_task, task_index, task_depends, db_ro_task_update
)
new_status = (
# self._create_task(ro_task, task_index, task_depends, db_ro_task_update)
_update_refresh(new_status)
else:
- if (
- ro_task["vim_info"]["refresh_at"]
- and now > ro_task["vim_info"]["refresh_at"]
- ):
- new_status, db_vim_info_update = self.item2class[
+ refresh_at = ro_task["vim_info"]["refresh_at"]
+ if refresh_at and refresh_at != -1 and now > refresh_at:
+ (
+ new_status,
+ db_vim_info_update,
+ ) = self.item2class[
task["item"]
].refresh(ro_task)
_update_refresh(new_status)
"""
ro_task = self._get_db_task()
if ro_task:
- self.logger.warning("Task to process: {}".format(ro_task))
+ self.logger.debug("Task to process: {}".format(ro_task))
time.sleep(1)
self._process_pending_tasks(ro_task)
busy = True