def commit_data(cls, data):
        new_obj = None

        if isinstance(data, list) or isinstance(data, tuple):
            cls.session.add_all(data)
        else:
            if isinstance(data, dict):
                new_obj = cls(**data)
            else:
                new_obj = data

        try:
            cls.session.add(new_obj)
            cls.session.commit()
            result = {
                'success': True,
                'result': data,
                'id': new_obj.id if new_obj else None
            }
        except:
            cls.session.rollback()
            result = {'success': False, 'result': traceback_format_exc()}

        # cls.session.close()
        return result
Example #2
0
def fake_interruptible(func, args=None, kwargs=None, timeout=DEFAULT_TIMEOUT):
    """
    Fake, non-interruptible, using no threads and no signals
    implementation used for debugging. This ignores the timeout and just
    the function as-is.
    """

    try:
        return NO_ERROR, func(*(args or ()), **(kwargs or {}))
    except Exception:
        return ERROR_MSG + traceback_format_exc(), NO_VALUE
Example #3
0
 def runner():
     try:
         _res = func(*(args or ()), **(kwargs or {}))
         results.put((
             NO_ERROR,
             _res,
         ))
     except Exception:
         results.put((
             ERROR_MSG + traceback_format_exc(),
             NO_VALUE,
         ))
Example #4
0
    def update_all_radios_tracks(cls):
        results = {}
        radios = cls.all()
        for radio in radios:

            try:
                res = cls.update_radio_tracks(radio_name=radio.name)
                results[radio.name] = res

            except:
                results['dd'] = {'success': False, 'result': traceback_format_exc()}

        return results
Example #5
0
def authenticate_event(content):
    sid = request.sid
    try:
        if 'token' not in content:
            raise Exception('Json web token introuvable')
        authentication_events.authentication(content["token"], sid)
        logger.info("[%s] [%s] [%s] [%s] [%s] [%s]", "SOCKET", request.host,
                    "authenticate", type(content), content, "OK")
    except Exception as e:
        logger.warning("[%s] [%s] [%s] [%s] [%s] [%s]\n%s", "SOCKET",
                       request.host, "authenticate", type(content), content,
                       "ERROR", traceback_format_exc())
        emit('error', str(e), room=sid, namepace='/')
Example #6
0
 def runner():
     """
     Run the func and send results back in a queue as a tuple of
     (`error`, `value`)
     """
     try:
         _res = func(*(args or ()), **(kwargs or {}))
         results.put((
             NO_ERROR,
             _res,
         ))
     except Exception:
         results.put((
             ERROR_MSG + traceback_format_exc(),
             NO_VALUE,
         ))
Example #7
0
    def interruptible(func, args=None, kwargs=None, timeout=DEFAULT_TIMEOUT):
        """
        Windows, threads-based interruptible runner. It can work also on
        POSIX, but is not reliable and works only if everything is pickable.
        """
        # We run `func` in a thread and block on a queue until timeout
        results = Queue()

        def runner():
            """
            Run the func and send results back in a queue as a tuple of
            (`error`, `value`)
            """
            try:
                _res = func(*(args or ()), **(kwargs or {}))
                results.put((
                    NO_ERROR,
                    _res,
                ))
            except Exception:
                results.put((
                    ERROR_MSG + traceback_format_exc(),
                    NO_VALUE,
                ))

        tid = start_new_thread(runner, ())

        try:
            # wait for the queue results up to timeout
            err_res = results.get(timeout=timeout)

            if not err_res:
                return ERROR_MSG, NO_VALUE

            return err_res

        except (Queue_Empty, MpTimeoutError):
            return TIMEOUT_MSG % locals(), NO_VALUE

        except Exception:
            return ERROR_MSG + traceback_format_exc(), NO_VALUE

        finally:
            try:
                async_raise(tid, Exception)
            except (SystemExit, ValueError):
                pass
Example #8
0
def message_send_event(content):
    sid = request.sid
    socket = sockets.find_socket(sid)
    try:
        if socket is None or socket.authenticated is False:
            raise Exception('Socket user introuvable')
        b, s = check_json(content, 'conversation_id')
        if not b:
            raise Exception('Param conversation_id introuvable')
        message_send(content["conversation_id"], content, socket)
        logger.info("[%s] [%s] [%s] [%s] [%s] [%s]", "SOCKET", request.host,
                    "message", type(content), content, "OK")
    except Exception as e:
        logger.warning("[%s] [%s] [%s] [%s] [%s] [%s]\n%s", "SOCKET",
                       request.host, "message", type(content), content,
                       "ERROR", traceback_format_exc())
        emit('error', str(e), room=sid, namepace='/')
    db.session.close()
Example #9
0
def leave_circle_event(content):
    sid = request.sid
    try:
        socket = sockets.find_socket(sid)
        if socket is None or socket.authenticated is False:
            raise Exception('Socket user introuvable')
        b, s = check_json(content, 'circle_id')
        if not b:
            raise Exception('Parameter circle_id missing')
        room_events.leave_circle(content["circle_id"], socket)
        logger.info("[%s] [%s] [%s] [%s] [%s] [%s]", "SOCKET", request.host,
                    "leave_circle", type(content), content, "OK")
    except Exception as e:
        logger.warning("[%s] [%s] [%s] [%s] [%s] [%s]\n%s", "SOCKET",
                       request.host, "leave_circle", type(content), content,
                       "ERROR", traceback_format_exc())
        emit('error', str(e), room=sid, namepace='/')
    db.session.close()
Example #10
0
    def interruptible(func, args=None, kwargs=None, timeout=DEFAULT_TIMEOUT):
        """
        POSIX, signals-based interruptible runner.
        """
        def handler(signum, frame):
            raise TimeoutError

        try:
            create_signal(SIGALRM, handler)
            setitimer(ITIMER_REAL, timeout)
            return NO_ERROR, func(*(args or ()), **(kwargs or {}))

        except TimeoutError:
            return TIMEOUT_MSG % locals(), NO_VALUE

        except Exception:
            return ERROR_MSG + traceback_format_exc(), NO_VALUE

        finally:
            setitimer(ITIMER_REAL, 0)
    def update_row(cls, data, create_new=False):

        # if call from class
        if isinstance(cls, api.DeclarativeMeta):
            if cls.unique_search_field not in data.keys():
                return {
                    'success': False,
                    'result': 'Missing filter_id argument'
                }
            obj = cls.query(cls).filter(
                getattr(cls, cls.unique_search_field) == data[
                    cls.unique_search_field])

        # if call from object
        else:
            attr = getattr(cls, cls.unique_search_field)
            obj = cls.query(cls).filter(
                attr == (getattr(cls, cls.unique_search_field)))

        if obj.count() == 1:
            try:
                obj.update(data)
                cls.session.commit()
                result = {'success': True, 'result': data}
            except:
                cls.session.rollback()
                result = {'success': False, 'result': traceback_format_exc()}

        elif obj.count() == 0 and create_new:
            return cls.commit_data(data)

        else:
            result = {
                'success': False,
                'result':
                f'Instead of 1 object - {obj.count()} object was found'
            }

        # cls.session.close()
        return result
Example #12
0
    def _retension_thread_procedure(self):
        log('FileStorage: Retension thread started')
        previous_check_time = 0
        while True:
            try:
                now = time_time()
                if now - previous_check_time > 10 * 60:  # every 10 minutes
                    log('FileStorage: Check for outdated files')
                    previous_check_time = time_time()
                    self._check_retention()

                # Wait for 60 seconds with a possibility
                # to be interrupted through stop() call:
                with self._condition_stop:
                    if not self._stopping:
                        self._condition_stop.wait(60)
                    if self._stopping:
                        log('Retension thread found stop signal')
                        break
            except Exception:
                logging_error(traceback_format_exc())
                time_sleep(60)  # prevent from flooding
Example #13
0
#########################################
# OPEN AUDIO DEVICE
#
#########################################

try:
    sd = sounddevice.OutputStream(device=AUDIO_DEVICE_ID,
                                  blocksize=512,
                                  samplerate=44100,
                                  channels=2,
                                  dtype='int16',
                                  callback=AudioCallback)
    sd.start()
    print("Opened audio device #{}".format(AUDIO_DEVICE_ID))
except Exception:
    print("\n[ERROR] {}".format(traceback_format_exc()))
    print("Invalid audio device #{}".format(AUDIO_DEVICE_ID))
    exit(1)

#########################################
# KEYBOARD THREAD (NEEDS ROOT)
#
#########################################
if USE_KEYBOARD:
    from keyboard import read_key as keyboard_read_key

    def Keyboard():
        global preset
        try:
            while True:
                if keyboard_read_key() == '-':
Example #14
0
    def deploy(self, session, indata, version, nsr_id, *args, **kwargs):
        print("ns.deploy session={} indata={} version={} nsr_id={}".format(
            session, indata, version, nsr_id))
        validate_input(indata, deploy_schema)
        action_id = indata.get("action_id", str(uuid4()))
        task_index = 0
        # get current deployment
        db_nsr = None
        # db_nslcmop = None
        db_nsr_update = {}  # update operation on nsrs
        db_vnfrs_update = {}
        # db_nslcmop_update = {}        # update operation on nslcmops
        db_vnfrs = {}  # vnf's info indexed by _id
        vdu2cloud_init = {}
        step = ''
        logging_text = "Task deploy nsr_id={} action_id={} ".format(
            nsr_id, action_id)
        self.logger.debug(logging_text + "Enter")
        try:
            step = "Getting ns and vnfr record from db"
            # db_nslcmop = self.db.get_one("nslcmops", {"_id": nslcmop_id})
            db_nsr = self.db.get_one("nsrs", {"_id": nsr_id})
            db_ro_tasks = []
            db_new_tasks = []
            # read from db: vnf's of this ns
            step = "Getting vnfrs from db"
            db_vnfrs_list = self.db.get_list("vnfrs", {"nsr-id-ref": nsr_id})
            if not db_vnfrs_list:
                raise NsException("Cannot obtain associated VNF for ns")
            for vnfr in db_vnfrs_list:
                db_vnfrs[vnfr["_id"]] = vnfr
                db_vnfrs_update[vnfr["_id"]] = {}
            now = time()
            db_ro_nsr = self.db.get_one("ro_nsrs", {"_id": nsr_id},
                                        fail_on_empty=False)
            if not db_ro_nsr:
                db_ro_nsr = self._create_db_ro_nsrs(nsr_id, now)
            ro_nsr_public_key = db_ro_nsr["public_key"]

            # check that action_id is not in the list of actions. Suffixed with :index
            if action_id in db_ro_nsr["actions"]:
                index = 1
                while True:
                    new_action_id = "{}:{}".format(action_id, index)
                    if new_action_id not in db_ro_nsr["actions"]:
                        action_id = new_action_id
                        self.logger.debug(logging_text +
                                          "Changing action_id in use to {}".
                                          format(action_id))
                        break
                    index += 1

            def _create_task(item,
                             action,
                             target_record,
                             target_record_id,
                             extra_dict=None):
                nonlocal task_index
                nonlocal action_id
                nonlocal nsr_id

                task = {
                    "action_id": action_id,
                    "nsr_id": nsr_id,
                    "task_id": "{}:{}".format(action_id, task_index),
                    "status": "SCHEDULED",
                    "action": action,
                    "item": item,
                    "target_record": target_record,
                    "target_record_id": target_record_id,
                }
                if extra_dict:
                    task.update(extra_dict)  # params, find_params, depends_on
                task_index += 1
                return task

            def _create_ro_task(vim_account_id,
                                item,
                                action,
                                target_record,
                                target_record_id,
                                extra_dict=None):
                nonlocal action_id
                nonlocal task_index
                nonlocal now

                _id = action_id + ":" + str(task_index)
                db_ro_task = {
                    "_id":
                    _id,
                    "locked_by":
                    None,
                    "locked_at":
                    0.0,
                    "target_id":
                    "vim:" + vim_account_id,
                    "vim_info": {
                        "created": False,
                        "created_items": None,
                        "vim_id": None,
                        "vim_name": None,
                        "vim_status": None,
                        "vim_details": None,
                        "refresh_at": None,
                    },
                    "modified_at":
                    now,
                    "created_at":
                    now,
                    "to_check_at":
                    now,
                    "tasks": [
                        _create_task(item, action, target_record,
                                     target_record_id, extra_dict)
                    ],
                }
                return db_ro_task

            def _process_image_params(target_image, vim_info):
                find_params = {}
                if target_image.get("image"):
                    find_params["filter_dict"] = {
                        "name": target_image.get("image")
                    }
                if target_image.get("vim_image_id"):
                    find_params["filter_dict"] = {
                        "id": target_image.get("vim_image_id")
                    }
                if target_image.get("image_checksum"):
                    find_params["filter_dict"] = {
                        "checksum": target_image.get("image_checksum")
                    }
                return {"find_params": find_params}

            def _process_flavor_params(target_flavor, vim_info):
                def _get_resource_allocation_params(quota_descriptor):
                    """
                    read the quota_descriptor from vnfd and fetch the resource allocation properties from the
                     descriptor object
                    :param quota_descriptor: cpu/mem/vif/disk-io quota descriptor
                    :return: quota params for limit, reserve, shares from the descriptor object
                    """
                    quota = {}
                    if quota_descriptor.get("limit"):
                        quota["limit"] = int(quota_descriptor["limit"])
                    if quota_descriptor.get("reserve"):
                        quota["reserve"] = int(quota_descriptor["reserve"])
                    if quota_descriptor.get("shares"):
                        quota["shares"] = int(quota_descriptor["shares"])
                    return quota

                flavor_data = {
                    "disk": int(target_flavor["storage-gb"]),
                    # "ram": max(int(target_flavor["memory-mb"]) // 1024, 1),
                    # ^ TODO manage at vim_connectors MB instead of GB
                    "ram": int(target_flavor["memory-mb"]),
                    "vcpus": target_flavor["vcpu-count"],
                }
                if target_flavor.get("guest-epa"):
                    extended = {}
                    numa = {}
                    epa_vcpu_set = False
                    if target_flavor["guest-epa"].get("numa-node-policy"):
                        numa_node_policy = target_flavor["guest-epa"].get(
                            "numa-node-policy")
                        if numa_node_policy.get("node"):
                            numa_node = numa_node_policy["node"][0]
                            if numa_node.get("num-cores"):
                                numa["cores"] = numa_node["num-cores"]
                                epa_vcpu_set = True
                            if numa_node.get("paired-threads"):
                                if numa_node["paired-threads"].get(
                                        "num-paired-threads"):
                                    numa["paired-threads"] = int(
                                        numa_node["paired-threads"]
                                        ["num-paired-threads"])
                                    epa_vcpu_set = True
                                if len(numa_node["paired-threads"].get(
                                        "paired-thread-ids")):
                                    numa["paired-threads-id"] = []
                                    for pair in numa_node["paired-threads"][
                                            "paired-thread-ids"]:
                                        numa["paired-threads-id"].append(
                                            (str(pair["thread-a"]),
                                             str(pair["thread-b"])))
                            if numa_node.get("num-threads"):
                                numa["threads"] = int(numa_node["num-threads"])
                                epa_vcpu_set = True
                            if numa_node.get("memory-mb"):
                                numa["memory"] = max(
                                    int(numa_node["memory-mb"] / 1024), 1)
                    if target_flavor["guest-epa"].get("mempage-size"):
                        extended["mempage-size"] = target_flavor[
                            "guest-epa"].get("mempage-size")
                    if target_flavor["guest-epa"].get(
                            "cpu-pinning-policy") and not epa_vcpu_set:
                        if target_flavor["guest-epa"][
                                "cpu-pinning-policy"] == "DEDICATED":
                            if target_flavor["guest-epa"].get("cpu-thread-pinning-policy") and \
                                    target_flavor["guest-epa"]["cpu-thread-pinning-policy"] != "PREFER":
                                numa["cores"] = max(flavor_data["vcpus"], 1)
                            else:
                                numa["threads"] = max(flavor_data["vcpus"], 1)
                            epa_vcpu_set = True
                    if target_flavor["guest-epa"].get(
                            "cpu-quota") and not epa_vcpu_set:
                        cpuquota = _get_resource_allocation_params(
                            target_flavor["guest-epa"].get("cpu-quota"))
                        if cpuquota:
                            extended["cpu-quota"] = cpuquota
                    if target_flavor["guest-epa"].get("mem-quota"):
                        vduquota = _get_resource_allocation_params(
                            target_flavor["guest-epa"].get("mem-quota"))
                        if vduquota:
                            extended["mem-quota"] = vduquota
                    if target_flavor["guest-epa"].get("disk-io-quota"):
                        diskioquota = _get_resource_allocation_params(
                            target_flavor["guest-epa"].get("disk-io-quota"))
                        if diskioquota:
                            extended["disk-io-quota"] = diskioquota
                    if target_flavor["guest-epa"].get("vif-quota"):
                        vifquota = _get_resource_allocation_params(
                            target_flavor["guest-epa"].get("vif-quota"))
                        if vifquota:
                            extended["vif-quota"] = vifquota
                if numa:
                    extended["numas"] = [numa]
                if extended:
                    flavor_data["extended"] = extended

                extra_dict = {"find_params": {"flavor_data": flavor_data}}
                flavor_data_name = flavor_data.copy()
                flavor_data_name["name"] = target_flavor["name"]
                extra_dict["params"] = {"flavor_data": flavor_data_name}
                return extra_dict

            def _process_net_params(target_vld, vim_info):
                nonlocal indata
                extra_dict = {}
                if vim_info.get("vim_network_name"):
                    extra_dict["find_params"] = {
                        "filter_dict": {
                            "name": vim_info.get("vim_network_name")
                        }
                    }
                elif vim_info.get("vim_network_id"):
                    extra_dict["find_params"] = {
                        "filter_dict": {
                            "id": vim_info.get("vim_network_id")
                        }
                    }
                elif target_vld.get("mgmt-network"):
                    extra_dict["find_params"] = {
                        "mgmt": True,
                        "name": target_vld["id"]
                    }
                else:
                    # create
                    extra_dict["params"] = {
                        "net_name":
                        "{}-{}".format(
                            indata["name"][:16],
                            target_vld.get("name", target_vld["id"])[:16]),
                        "ip_profile":
                        vim_info.get('ip_profile'),
                        "provider_network_profile":
                        vim_info.get('provider_network'),
                    }
                    if not target_vld.get("underlay"):
                        extra_dict["params"]["net_type"] = "bridge"
                    else:
                        extra_dict["params"][
                            "net_type"] = "ptp" if target_vld.get(
                                "type") == "ELINE" else "data"
                return extra_dict

            def _process_vdu_params(target_vdu, vim_info):
                nonlocal vnfr_id
                nonlocal nsr_id
                nonlocal indata
                nonlocal vnfr
                nonlocal vdu2cloud_init
                vnf_preffix = "vnfrs:{}".format(vnfr_id)
                ns_preffix = "nsrs:{}".format(nsr_id)
                image_text = ns_preffix + ":image." + target_vdu["ns-image-id"]
                flavor_text = ns_preffix + ":flavor." + target_vdu[
                    "ns-flavor-id"]
                extra_dict = {"depends_on": [image_text, flavor_text]}
                net_list = []
                for iface_index, interface in enumerate(
                        target_vdu["interfaces"]):
                    if interface.get("ns-vld-id"):
                        net_text = ns_preffix + ":vld." + interface["ns-vld-id"]
                    else:
                        net_text = vnf_preffix + ":vld." + interface[
                            "vnf-vld-id"]
                    extra_dict["depends_on"].append(net_text)
                    net_item = {
                        "name": interface["name"],
                        "net_id": "TASK-" + net_text,
                        "vpci": interface.get("vpci"),
                        "type": "virtual",
                        # TODO mac_address: used for  SR-IOV ifaces #TODO for other types
                        # TODO floating_ip: True/False (or it can be None)
                    }
                    if interface.get("type") in ("SR-IOV", "PCI-PASSTHROUGH"):
                        net_item["use"] = "data"
                        net_item["model"] = interface["type"]
                        net_item["type"] = interface["type"]
                    elif interface.get("type") == "OM-MGMT" or interface.get("mgmt-interface") or \
                            interface.get("mgmt-vnf"):
                        net_item["use"] = "mgmt"
                    else:  # if interface.get("type") in ("VIRTIO", "E1000", "PARAVIRT"):
                        net_item["use"] = "bridge"
                        net_item["model"] = interface.get("type")
                    net_list.append(net_item)
                    if interface.get("mgmt-vnf"):
                        extra_dict["mgmt_vnf_interface"] = iface_index
                    elif interface.get("mgmt-interface"):
                        extra_dict["mgmt_vdu_interface"] = iface_index

                # cloud config
                cloud_config = {}
                if target_vdu.get("cloud-init"):
                    if target_vdu["cloud-init"] not in vdu2cloud_init:
                        vdu2cloud_init[
                            target_vdu["cloud-init"]] = self._get_cloud_init(
                                target_vdu["cloud-init"])
                    cloud_content_ = vdu2cloud_init[target_vdu["cloud-init"]]
                    cloud_config["user-data"] = self._parse_jinja2(
                        cloud_content_, target_vdu.get("additionalParams"),
                        target_vdu["cloud-init"])
                if target_vdu.get("boot-data-drive"):
                    cloud_config["boot-data-drive"] = target_vdu.get(
                        "boot-data-drive")
                ssh_keys = []
                if target_vdu.get("ssh-keys"):
                    ssh_keys += target_vdu.get("ssh-keys")
                if target_vdu.get("ssh-access-required"):
                    ssh_keys.append(ro_nsr_public_key)
                if ssh_keys:
                    cloud_config["key-pairs"] = ssh_keys

                extra_dict["params"] = {
                    "name":
                    "{}-{}-{}-{}".format(indata["name"][:16],
                                         vnfr["member-vnf-index-ref"][:16],
                                         target_vdu["vdu-name"][:32],
                                         target_vdu.get("count-index") or 0),
                    "description":
                    target_vdu["vdu-name"],
                    "start":
                    True,
                    "image_id":
                    "TASK-" + image_text,
                    "flavor_id":
                    "TASK-" + flavor_text,
                    "net_list":
                    net_list,
                    "cloud_config":
                    cloud_config or None,
                    "disk_list":
                    None,  # TODO
                    "availability_zone_index":
                    None,  # TODO
                    "availability_zone_list":
                    None,  # TODO
                }
                return extra_dict

            def _process_items(target_list, existing_list, db_record,
                               db_update, db_path, item, process_params):
                nonlocal db_ro_tasks
                nonlocal db_new_tasks
                nonlocal task_index

                # ensure all the target_list elements has an "id". If not assign the index
                for target_index, tl in enumerate(target_list):
                    if tl and not tl.get("id"):
                        tl["id"] = str(target_index)

                # step 1 networks to be deleted/updated
                for vld_index, existing_vld in enumerate(existing_list):
                    target_vld = next((vld for vld in target_list
                                       if vld["id"] == existing_vld["id"]),
                                      None)
                    for existing_vim_index, existing_vim_info in enumerate(
                            existing_vld.get("vim_info", ())):
                        if not existing_vim_info:
                            continue
                        if target_vld:
                            target_viminfo = next(
                                (target_viminfo
                                 for target_viminfo in target_vld.get(
                                     "vim_info", ())
                                 if existing_vim_info["vim_account_id"] ==
                                 target_viminfo["vim_account_id"]), None)
                        else:
                            target_viminfo = None
                        if not target_viminfo:
                            # must be deleted
                            self._assign_vim(
                                existing_vim_info["vim_account_id"])
                            db_new_tasks.append(
                                _create_task(
                                    item,
                                    "DELETE",
                                    target_record="{}.{}.vim_info.{}".format(
                                        db_record, vld_index,
                                        existing_vim_index),
                                    target_record_id="{}.{}".format(
                                        db_record, existing_vld["id"])))
                            # TODO delete
                    # TODO check one by one the vims to be created/deleted

                # step 2 networks to be created
                for target_vld in target_list:
                    vld_index = -1
                    for vld_index, existing_vld in enumerate(existing_list):
                        if existing_vld["id"] == target_vld["id"]:
                            break
                    else:
                        vld_index += 1
                        db_update[db_path +
                                  ".{}".format(vld_index)] = target_vld
                        existing_list.append(target_vld)
                        existing_vld = None

                    for vim_index, vim_info in enumerate(
                            target_vld["vim_info"]):
                        existing_viminfo = None
                        if existing_vld:
                            existing_viminfo = next(
                                (existing_viminfo
                                 for existing_viminfo in existing_vld.get(
                                     "vim_info", ())
                                 if vim_info["vim_account_id"] ==
                                 existing_viminfo["vim_account_id"]), None)
                        # TODO check if different. Delete and create???
                        # TODO delete if not exist
                        if existing_viminfo:
                            continue

                        extra_dict = process_params(target_vld, vim_info)

                        self._assign_vim(vim_info["vim_account_id"])
                        db_ro_tasks.append(
                            _create_ro_task(
                                vim_info["vim_account_id"],
                                item,
                                "CREATE",
                                target_record="{}.{}.vim_info.{}".format(
                                    db_record, vld_index, vim_index),
                                target_record_id="{}.{}".format(
                                    db_record, target_vld["id"]),
                                extra_dict=extra_dict))

                        db_update[db_path +
                                  ".{}".format(vld_index)] = target_vld

            def _process_action(indata):
                nonlocal db_ro_tasks
                nonlocal db_new_tasks
                nonlocal task_index
                nonlocal db_vnfrs
                nonlocal db_ro_nsr

                if indata["action"] == "inject_ssh_key":
                    key = indata.get("key")
                    user = indata.get("user")
                    password = indata.get("password")
                    for vnf in indata.get("vnf", ()):
                        if vnf.get("_id") not in db_vnfrs:
                            raise NsException("Invalid vnf={}".format(
                                vnf["_id"]))
                        db_vnfr = db_vnfrs[vnf["_id"]]
                        for target_vdu in vnf.get("vdur", ()):
                            vdu_index, vdur = next(
                                (i_v for i_v in enumerate(db_vnfr["vdur"])
                                 if i_v[1]["id"] == target_vdu["id"]),
                                (None, None))
                            if not vdur:
                                raise NsException(
                                    "Invalid vdu vnf={}.{}".format(
                                        vnf["_id"], target_vdu["id"]))
                            vim_info = vdur["vim_info"][0]
                            self._assign_vim(vim_info["vim_account_id"])
                            target_record = "vnfrs:{}:vdur.{}.ssh_keys".format(
                                vnf["_id"], vdu_index)
                            extra_dict = {
                                "depends_on": [
                                    "vnfrs:{}:vdur.{}".format(
                                        vnf["_id"], vdur["id"])
                                ],
                                "params": {
                                    "ip_address":
                                    vdur.gt("ip_address"),
                                    "user":
                                    user,
                                    "key":
                                    key,
                                    "password":
                                    password,
                                    "private_key":
                                    db_ro_nsr["private_key"],
                                    "salt":
                                    db_ro_nsr["_id"],
                                    "schema_version":
                                    db_ro_nsr["_admin"]["schema_version"]
                                }
                            }
                            db_ro_tasks.append(
                                _create_ro_task(vim_info["vim_account_id"],
                                                "vdu",
                                                "EXEC",
                                                target_record=target_record,
                                                target_record_id=None,
                                                extra_dict=extra_dict))

            with self.write_lock:
                if indata.get("action"):
                    _process_action(indata)
                else:
                    # compute network differences
                    # NS.vld
                    step = "process NS VLDs"
                    _process_items(target_list=indata["ns"]["vld"] or [],
                                   existing_list=db_nsr.get("vld") or [],
                                   db_record="nsrs:{}:vld".format(nsr_id),
                                   db_update=db_nsr_update,
                                   db_path="vld",
                                   item="net",
                                   process_params=_process_net_params)

                    step = "process NS images"
                    _process_items(target_list=indata["image"] or [],
                                   existing_list=db_nsr.get("image") or [],
                                   db_record="nsrs:{}:image".format(nsr_id),
                                   db_update=db_nsr_update,
                                   db_path="image",
                                   item="image",
                                   process_params=_process_image_params)

                    step = "process NS flavors"
                    _process_items(target_list=indata["flavor"] or [],
                                   existing_list=db_nsr.get("flavor") or [],
                                   db_record="nsrs:{}:flavor".format(nsr_id),
                                   db_update=db_nsr_update,
                                   db_path="flavor",
                                   item="flavor",
                                   process_params=_process_flavor_params)

                    # VNF.vld
                    for vnfr_id, vnfr in db_vnfrs.items():
                        # vnfr_id need to be set as global variable for among others nested method _process_vdu_params
                        step = "process VNF={} VLDs".format(vnfr_id)
                        target_vnf = next((vnf
                                           for vnf in indata.get("vnf", ())
                                           if vnf["_id"] == vnfr_id), None)
                        target_list = target_vnf.get(
                            "vld") if target_vnf else None
                        _process_items(
                            target_list=target_list or [],
                            existing_list=vnfr.get("vld") or [],
                            db_record="vnfrs:{}:vld".format(vnfr_id),
                            db_update=db_vnfrs_update[vnfr["_id"]],
                            db_path="vld",
                            item="net",
                            process_params=_process_net_params)

                        target_list = target_vnf.get(
                            "vdur") if target_vnf else None
                        step = "process VNF={} VDUs".format(vnfr_id)
                        _process_items(
                            target_list=target_list or [],
                            existing_list=vnfr.get("vdur") or [],
                            db_record="vnfrs:{}:vdur".format(vnfr_id),
                            db_update=db_vnfrs_update[vnfr["_id"]],
                            db_path="vdur",
                            item="vdu",
                            process_params=_process_vdu_params)

                step = "Updating database, Creating ro_tasks"
                if db_ro_tasks:
                    self.db.create_list("ro_tasks", db_ro_tasks)
                step = "Updating database, Appending tasks to ro_tasks"
                for task in db_new_tasks:
                    if not self.db.set_one("ro_tasks",
                                           q_filter={
                                               "tasks.target_record":
                                               task["target_record"]
                                           },
                                           update_dict={
                                               "to_check_at": now,
                                               "modified_at": now
                                           },
                                           push={"tasks": task},
                                           fail_on_empty=False):
                        self.logger.error(
                            logging_text +
                            "Cannot find task for target_record={}".format(
                                task["target_record"]))
                    # TODO something else appart from logging?
                step = "Updating database, nsrs"
                if db_nsr_update:
                    self.db.set_one("nsrs", {"_id": nsr_id}, db_nsr_update)
                for vnfr_id, db_vnfr_update in db_vnfrs_update.items():
                    if db_vnfr_update:
                        step = "Updating database, vnfrs={}".format(vnfr_id)
                        self.db.set_one("vnfrs", {"_id": vnfr_id},
                                        db_vnfr_update)

            self.logger.debug(logging_text + "Exit")
            return {
                "status": "ok",
                "nsr_id": nsr_id,
                "action_id": action_id
            }, action_id, True

        except Exception as e:
            if isinstance(e, (DbException, NsException)):
                self.logger.error(
                    logging_text +
                    "Exit Exception while '{}': {}".format(step, e))
            else:
                e = traceback_format_exc()
                self.logger.critical(
                    logging_text +
                    "Exit Exception while '{}': {}".format(step, e),
                    exc_info=True)
            raise NsException(e)
Example #15
0
    def complete_task(self, worker, prio=None):
        query = worker[0]
        args = worker[1]
        callback = worker[2]
        data_pack = worker[3]
        get_info = worker[4]
        query_type = worker[5]
        try:
            if get_info:
                get_info['time'] = timestamp() - get_info['time']

            if args:
                self.cursor.execute(query, args)
            else:
                self.cursor.execute(query)

            if query_type == 0:
                if get_info:
                    if callback:
                        if data_pack:
                            callback(data_pack, get_info)
                        else:
                            callback(get_info)
                else:
                    if callback:
                        if data_pack:
                            callback(data_pack)
                        else:
                            callback()
            if query_type == 1:
                data = self.cursor.fetchone()
                if get_info:
                    if callback:
                        if data_pack:
                            callback(data, data_pack, get_info)
                        else:
                            callback(data, get_info)
                else:
                    if callback:
                        if data_pack:
                            callback(data, data_pack)
                        else:
                            callback(data)

            if query_type == 2:
                data = self.cursor.fetchall()
                if get_info:
                    if callback:
                        if data_pack:
                            callback(data, data_pack, get_info)
                        else:
                            callback(data, get_info)
                else:
                    if callback:
                        if data_pack:
                            callback(data, data_pack)
                        else:
                            callback(data)
            if prio:
                self._p_queue.task_done()
            else:
                self._r_queue.task_done()

        except Exception as SQL_ERROR:

            # Possible errors
            class_error, actual_error, traceback = exc_info()

            format_error = '-' * 64 + '\nExceptions probable cause (SQL Query: {0})\n{1}\nActual Error:\n{2}'.format(
                query, class_error, SQL_ERROR)

            if self.error_handling == 'print':

                print(format_error)

                print('-' * 64)

            else:

                echo_console(format_error)

                echo_console('-' * 64)

            logging_error(traceback_format_exc())
Example #16
0
async def on_message(message):
    command = 'bracket' # Pre-setting the variable for use in the try/except block below (L78)
    # If the bot is the user, do not respond
    if message.author == client.user:
        return

    # If the bot is mentioned in a message, respond with a message informing it of being a bot
    if client.user.mentioned_in(message):
        # If @everyone or @here is used, ignore
        if "@everyone" in message.content or "@here" in message.content:
            return
        # Choose from a random response, then follow with a Bot message
        responses = ["Ok", "Thanks", "Sounds good to me", "Buff Rashid", "Buff Rashid", "Beep Boop", "Yes", "No", "Good to know", "Glad to hear it", "I'll keep that in mind", "The answer lies in the heart of battle", "Go home and be a family man"]
        await message.channel.send("{0} \n**I am a Bot that plays Rashid. Mentions cause my little Rashid brain to short circuit. Did you have ~~an eagle spi~~ a command?**".format(random_choice(responses)))
        return

    try:
        # Check if the channel is in the DB
        # Add it if it isn't
        if not settings_exist(message.guild.id, message.channel.id):
            raise Exception("Lizard-BOT failed to create DB entry for: " + message.guild.name + ". Guild ID: " + message.guild.id)

        # Get prefix for the guild
        prefix = read_db('guild', 'prefix-lizard', message.guild.id)

        # Check if the attempted_cmd is !prefix-lizard and has too many args
        if (message.content.split(' ')[0] == "!prefix-lizard" or message.content.split(' ')[0] == "!prefliz") and len(message.content.split()) > 1:
            await message.channel.send("Too many arguments. Check help-lizard for more info")
            return
        # Hardcode prefix command to be accessible via !
        elif message.content.split(' ')[0] == "!prefix-lizard" or message.content.split(' ')[0] == "!prefliz":
            response = await client.interface.call_command('prefix-lizard', 0, 0, 0, guild=message.guild.id)
            if response:
                await message.channel.send(response)
            return
        # If other commands don't start with the correct prefix, do nothing
        elif not message.content.startswith(prefix):
            return
        # Check if the attempted_cmd takes arguments
        elif message.content.split(' ')[0][1:].lower() in client.no_arg_cmds and len(message.content.split()) > 1:
            await message.channel.send("Too many arguments. Check help-lizard for more info")
            return

        # Rotate through commands to see if the message matches
        for command in client.commands:
            command = command.lower() # Lower the command for easier matching
            msg = message.content # The message
            attempted_cmd = msg.split(' ')[0][1:].lower() # Get the attempted command from the beginning of the string

            if attempted_cmd in ['challonge', 'chal', 'edit'] and len(msg.split(' ')) > 1:
                attempted_cmd += ' ' + msg.split(' ')[1].lower()

            # Check if the message begins with a command
            if attempted_cmd and attempted_cmd == command:
                user = message.author # The author
                kwargs = {'guild':message.guild.id}

                # Remove the command from the start
                msg = msg[len(command)+1:].strip()

                # Check command to see if we need keyword args
                if command in ['challonge checkin', 'chal checkin']:
                    kwargs['guild_members'] = message.guild.members
                elif command in ['edit botrole', 'edit role']:
                    kwargs['guild_default_role'] = message.guild.default_role
                    kwargs['role_mentions'] = message.role_mentions
                elif command in ['edit bracket', 'edit pingtest', 'edit status', 'edit seeding', 'edit stream', 'edit tos']:
                    kwargs['channel_mentions'] = message.channel_mentions
                    if command in ['edit tos']:
                        kwargs['mentions'] = message.mentions
                elif command in ['draw']:
                    kwargs['full_msg'] = message
                    kwargs['client'] = client

                # Await the interface calling the command
                response = await client.interface.call_command(command, msg, user, message.channel, **kwargs)
                # If there is a response, send it
                if response:
                    await message.channel.send(response)
                break
    except Exception:
        string_info = str(sys_exc_info()[1]) # Error message
        function_name = string_info.split(':')[0] # The command the error message came from

        # Expected error
        # Return friendly user message
        # Additional checks needed for challonge and edit commands that have multiple subcommands
        if client.interface._func_mapping[command].__name__ in function_name.strip("*").lower() or ('challonge' in client.interface._func_mapping[command].__name__ and 'challonge' in function_name.strip("*").lower()) or ('edit' in client.interface._func_mapping[command].__name__ and 'edit' in function_name.strip("*").lower()):
            await message.channel.send(function_name.replace('_', '-') + ': ' + ':'.join(string_info.split(':')[1:]))
        elif command == 'dev':
            # Return user message
            await message.channel.send(escape_markdown(traceback_format_exc()))
            # Print error to console
            traceback_print_exc()
        else:
            # Print error to console
            traceback_print_exc()
            # If we get this far and something breaks
            # Something is very wrong. Send user generic error message
            await message.channel.send("I is broken.\nBuff Rashid and submit an issue via <https://github.com/lizardman301/Lizard-bot-rsf/issues>\nOr just tell Lizardman301. That's what I do.")