def _scan_for_services(services_dir, pj):
    from son_editor.models.descriptor import Service
    session = db_session()
    try:
        for service_file in os.listdir(services_dir):
            if service_file.endswith(".yml"):
                service = Service()
                file_path = os.path.join(services_dir, service_file)
                load_ns_vnf_from_disk(file_path, service)
                db_service = session.query(Service). \
                    filter(Service.project == pj). \
                    filter(Service.name == service.name). \
                    filter(Service.vendor == service.vendor). \
                    filter(Service.version == service.version). \
                    first()
                if not db_service:
                    logger.info("Found service in project {}: {}".format(pj.name, service.uid))
                    service.project = pj
                    session.add(service)
                    session.commit()
                else:
                    session.rollback()
                if get_file_path("nsd", service) != file_path:
                    shutil.move(file_path, get_file_path("nsd", service))  # rename to expected name format
            elif os.path.isdir(os.path.join(services_dir, service_file)):
                _scan_for_services(os.path.join(services_dir, service_file), pj)
    except:
        session.rollback()
def _scan_for_services(services_dir, pj):
    from son_editor.models.descriptor import Service
    session = db_session()
    try:
        for service_file in os.listdir(services_dir):
            if service_file.endswith(".yml"):
                service = Service()
                file_path = os.path.join(services_dir, service_file)
                load_ns_vnf_from_disk(file_path, service)
                db_service = session.query(Service). \
                    filter(Service.project == pj). \
                    filter(Service.name == service.name). \
                    filter(Service.vendor == service.vendor). \
                    filter(Service.version == service.version). \
                    first()
                if not db_service:
                    logger.info("Found service in project {}: {}".format(
                        pj.name, service.uid))
                    service.project = pj
                    session.add(service)
                    session.commit()
                else:
                    session.rollback()
                if get_file_path("nsd", service) != file_path:
                    shutil.move(file_path, get_file_path(
                        "nsd", service))  # rename to expected name format
            elif os.path.isdir(os.path.join(services_dir, service_file)):
                _scan_for_services(os.path.join(services_dir, service_file),
                                   pj)
    except:
        session.rollback()
Exemplo n.º 3
0
def _scan_for_functions(function_dir, pj):
    """
    Scan project for functions
    
    Scans the given directory for functions and adds them to the project.
    
    :param function_dir: The functions directory in the project
    :param pj: The project model from the database
    """
    session = db_session()
    from son_editor.models.descriptor import Function
    try:
        for function_folder in os.listdir(function_dir):
            folder_path = os.path.join(function_dir, function_folder)
            if os.path.isdir(folder_path):
                yaml_files = [
                    file for file in os.listdir(folder_path)
                    if file.endswith(".yml")
                ]
                if len(yaml_files) == 1:
                    function = Function()
                    file_path = os.path.join(folder_path, yaml_files[0])
                    load_ns_vnf_from_disk(file_path, function)
                    db_function = session.query(Function). \
                        filter(Function.project == pj). \
                        filter(Function.name == function.name). \
                        filter(Function.vendor == function.vendor). \
                        filter(Function.version == function.version). \
                        first()
                    if not db_function:
                        logger.info("Found function in project {}: {}".format(
                            pj.name, function.uid))
                        function.project = pj
                        session.add(function)
                        session.commit()
                    else:
                        function = db_function
                    # rename folder if necessary
                    target_folder = os.path.normpath(
                        get_file_path("vnf", function).replace(
                            get_file_name(function), ''))
                    if os.path.normpath(folder_path) != target_folder:
                        shutil.move(folder_path, target_folder)
                        file_path = file_path.replace(folder_path,
                                                      target_folder)
                    # rename file if necessary
                    if not os.path.exists(get_file_path("vnf", function)):
                        shutil.move(file_path, get_file_path("vnf", function))
                else:
                    logger.info(
                        "Multiple or no yaml files in folder {}. Ignoring".
                        format(folder_path))

    except:
        logger.exception("Could not load function descriptor:")
    finally:
        session.rollback()
Exemplo n.º 4
0
def _scan_for_functions(function_dir, pj):
    """
    Scan project for functions
    
    Scans the given directory for functions and adds them to the project.
    
    :param function_dir: The functions directory in the project
    :param pj: The project model from the database
    """
    session = db_session()
    from son_editor.models.descriptor import Function
    try:
        for function_folder in os.listdir(function_dir):
            folder_path = os.path.join(function_dir, function_folder)
            if os.path.isdir(folder_path):
                yaml_files = [file for file in os.listdir(folder_path) if file.endswith(".yml")]
                if len(yaml_files) == 1:
                    function = Function()
                    file_path = os.path.join(folder_path, yaml_files[0])
                    load_ns_vnf_from_disk(file_path, function)
                    db_function = session.query(Function). \
                        filter(Function.project == pj). \
                        filter(Function.name == function.name). \
                        filter(Function.vendor == function.vendor). \
                        filter(Function.version == function.version). \
                        first()
                    if not db_function:
                        logger.info("Found function in project {}: {}".format(pj.name, function.uid))
                        function.project = pj
                        session.add(function)
                        session.commit()
                    else:
                        function = db_function
                    # rename folder if necessary
                    target_folder = os.path.normpath(
                        get_file_path("vnf", function).replace(get_file_name(function), ''))
                    if os.path.normpath(folder_path) != target_folder:
                        shutil.move(folder_path, target_folder)
                        file_path = file_path.replace(folder_path, target_folder)
                    # rename file if necessary
                    if not os.path.exists(get_file_path("vnf", function)):
                        shutil.move(file_path, get_file_path("vnf", function))
                else:
                    logger.info("Multiple or no yaml files in folder {}. Ignoring".format(folder_path))

    except:
        logger.exception("Could not load function descriptor:")
    finally:
        session.rollback()
def _scan_catalogue(cat_path, model, ws):
    from pathlib import Path
    from son_editor.models.private_descriptor import PrivateDescriptor
    session = db_session()
    try:
        for vendor in cat_path.iterdir():
            if not vendor.is_dir():
                continue
            for name in vendor.iterdir():
                if not name.is_dir():
                    continue
                for version in name.iterdir():
                    path = Path(str(version) + "/descriptor.yml")
                    if path.exists() and path.is_file():
                        logger.info("Found private ns/vnf: {}".format(path))
                        model = load_ns_vnf_from_disk(str(path), model)
                        model.workspace = ws
                        if not session.query(PrivateDescriptor).filter(
                                PrivateDescriptor.uid == model.uid).first():
                            session.add(model)
                            session.commit()
                        else:
                            session.rollback()
    except:
        session.rollback()
Exemplo n.º 6
0
def _scan_catalogue(cat_path, model, ws):
    """
    Scan a private catalogue dir
    
    Scans the given catalogue path for new descriptors 
    and writes it into the descriptor model
    
    :param cat_path: The catalogue path containing either nss or vnfs 
    :param model: The descriptor model
    :param ws: The workspace to attach the descriptors to
    """
    from pathlib import Path
    from son_editor.models.private_descriptor import PrivateDescriptor
    session = db_session()
    try:
        for vendor in cat_path.iterdir():
            if not vendor.is_dir():
                continue
            for name in vendor.iterdir():
                if not name.is_dir():
                    continue
                for version in name.iterdir():
                    path = Path(str(version) + "/descriptor.yml")
                    if path.exists() and path.is_file():
                        logger.info("Found private ns/vnf: {}".format(path))
                        model = load_ns_vnf_from_disk(str(path), model)
                        model.workspace = ws
                        if not session.query(PrivateDescriptor).filter(PrivateDescriptor.uid == model.uid).first():
                            session.add(model)
                            session.commit()
                        else:
                            session.rollback()
    except:
        session.rollback()
Exemplo n.º 7
0
def _scan_catalogue(cat_path, model, ws):
    """
    Scan a private catalogue dir
    
    Scans the given catalogue path for new descriptors 
    and writes it into the descriptor model
    
    :param cat_path: The catalogue path containing either nss or vnfs 
    :param model: The descriptor model
    :param ws: The workspace to attach the descriptors to
    """
    from pathlib import Path
    from son_editor.models.private_descriptor import PrivateDescriptor
    session = db_session()
    try:
        for vendor in cat_path.iterdir():
            if not vendor.is_dir():
                continue
            for name in vendor.iterdir():
                if not name.is_dir():
                    continue
                for version in name.iterdir():
                    path = Path(str(version) + "/descriptor.yml")
                    if path.exists() and path.is_file():
                        logger.info("Found private ns/vnf: {}".format(path))
                        model = load_ns_vnf_from_disk(str(path), model)
                        model.workspace = ws
                        if not session.query(PrivateDescriptor).filter(
                                PrivateDescriptor.uid == model.uid).first():
                            session.add(model)
                            session.commit()
                        else:
                            session.rollback()
    except:
        session.rollback()
def _scan_catalogue(cat_path, model, ws):
    from pathlib import Path
    from son_editor.models.private_descriptor import PrivateDescriptor
    session = db_session()
    try:
        for vendor in cat_path.iterdir():
            if not vendor.is_dir():
                continue
            for name in vendor.iterdir():
                if not name.is_dir():
                    continue
                for version in name.iterdir():
                    path = Path(str(version) + "/descriptor.yml")
                    if path.exists() and path.is_file():
                        logger.info("Found private ns/vnf: {}".format(path))
                        model = load_ns_vnf_from_disk(str(path), model)
                        model.workspace = ws
                        if not session.query(PrivateDescriptor).filter(PrivateDescriptor.uid == model.uid).first():
                            session.add(model)
                            session.commit()
                        else:
                            session.rollback()
    except:
        session.rollback()