def activate(sw_version_to_be_activated, ne_id):
    ne_info = ems_util.get_ne_info_from_db_by_id(ne_id)

    activate_process_id = random.randint(1, conf.MAX_INT)
    result = conf.REQ_SUCCESS
    ret_value = {"activateProcessId": activate_process_id, "result": result}

    if not ne_info:
        ret_value["result"] = conf.REQ_FAILURE
        ret_value["reason"] = "Can not find NE %s" % ne_id

        logger.error(ret_value["reason"])
        return ret_value

    err, reason = do_activate(sw_version_to_be_activated, ne_info)

    if not err:
        ne_info["status"] = conf.STATUS_ACTIVATED
        ems_util.update_ne_info(ne_info)

        logger.info("Activate SW success")
        activate_status = "NE_SWACTIVATION_SUCCESSFUL"
    else:
        ret_value["result"] = conf.REQ_FAILURE
        ret_value["reason"] = reason

        logger.error("Activate SW failure, reason: %s" % ret_value["reason"])
        activate_status = "NE_SWACTIVATION_FAILED"

    notification = generate_notification(activate_process_id, activate_status,
                                         sw_version_to_be_activated, reason)
    ems_util.send_notification(notification, activate_process_id)

    # for automated software management, there is no listOfStepNumbersAndDurations
    return notification, ret_value
def install(sw_to_be_installed, ne_id):
    ne_info = ems_util.get_ne_info_from_db_by_id(ne_id)

    install_process_id = random.randint(1, conf.MAX_INT)
    result = conf.REQ_SUCCESS
    ret_value = {"installProcessId": install_process_id, "result": result}

    if not ne_info:
        ret_value["result"] = conf.REQ_FAILURE
        ret_value["reason"] = "Can not find NE %s" % ne_id

        logger.error(ret_value["reason"])
        return ret_value

    ne_info["status"] = conf.STATUS_INSTALLING
    ems_util.update_ne_info(ne_info)

    installed_ne_sw_info = []
    failed_sw_info = []

    err, reason, installed_ne_sw = do_install(sw_to_be_installed, ne_info)

    if not err:
        installed_ne_sw_info.append(installed_ne_sw)
    else:
        result = conf.REQ_FAILURE
        failed_sw_entry = {
            "failedSw": installed_ne_sw,
            "failureReason": reason
        }

        logger.error("Failed installed SW: %s" % str(failed_sw_entry))
        failed_sw_info.append(failed_sw_entry)

    num_installed_ne_sw = len(installed_ne_sw_info)

    if num_installed_ne_sw == 1:
        install_status = "NE_SWINSTALLATION_SUCCESSFUL"
    elif num_installed_ne_sw == 0:
        install_status = "NE_SWINSTALLATION_FAILED"
    else:
        install_status = "NE_SWINSTALLATION_PARTIALLY_SUCCESSFUL"
    logger.info("Install SW status: %s" % install_status)

    notification = generate_notification(install_process_id, install_status,
                                         installed_ne_sw_info, failed_sw_info)
    ems_util.send_notification(notification, install_process_id)

    if result == conf.REQ_SUCCESS:
        ems_util.update_ne_info(ne_info)

        logger.info("Install SW success")
    else:
        ret_value["result"] = result
        ret_value["reason"] = json.dumps(failed_sw_info)

        logger.info("Install SW failure, reason: %s" % ret_value["reason"])

    # for automated software management, there is no listOfStepNumbersAndDurations
    return notification, ret_value
def post_check(pnf_name,
               old_sw_version,
               target_sw_version,
               rule_name,
               additional_data_file=None):
    logger.info(
        "PostCheck for oldSwVersion: %s, targetSwVersion: %s, ruleName: %s, additionalDataFile: %s"
        % (old_sw_version, target_sw_version, rule_name, additional_data_file))

    ne_info = ems_util.get_ne_info_from_db_by_id(pnf_name)

    if not ne_info:
        ret_value = {
            "result": conf.RESULT_FAILURE,
            "reason": "Can not find NE %s" % pnf_name
        }

        logger.error(ret_value["reason"])
        return ret_value

    old_sw_version_in_db = ne_info.get("oldSwVersion", "")
    current_sw_version_in_db = ne_info.get("currentSwVersion", "")

    if old_sw_version != old_sw_version_in_db:
        ret_value = {
            "result":
            conf.RESULT_FAILURE,
            "reason":
            "Old SW version %s in PNF is not matched with oldSwVersion %s" %
            (old_sw_version_in_db, old_sw_version)
        }

        logger.error(ret_value["reason"])
        return ret_value

    if target_sw_version != current_sw_version_in_db:
        ret_value = {
            "result":
            conf.RESULT_FAILURE,
            "reason":
            "Current SW version %s in PNF is not matched with targetSwVersion %s"
            % (current_sw_version_in_db, target_sw_version)
        }

        logger.error(ret_value["reason"])
        return ret_value

    ne_info["checkStatus"] = conf.STATUS_POSTCHECKED
    ems_util.update_ne_info(ne_info)
    logger.info("PostCheck SW success, check status: %s" %
                ne_info["checkStatus"])

    ret_value = {"result": conf.RESULT_SUCCESS}

    return ret_value
def do_activate(sw_version_to_be_activated, ne_info):
    """
    return err, reason
    """

    logger.info("swVersionToBeActivated: %s" % sw_version_to_be_activated)

    installed_sw = ne_info.get("installedSw", {})
    if sw_version_to_be_activated in installed_sw:
        target_sw_version = installed_sw[sw_version_to_be_activated]["version"]
    else:
        target_sw_version = sw_version_to_be_activated

    sw_install_dir_in_ne = ems_util.get_install_dir(ne_info['omIP'])
    logger.info("SW has been installed at %s" % sw_install_dir_in_ne)

    if "targetSwVersion" in ne_info:
        if ne_info["targetSwVersion"] != target_sw_version:
            msg = "Conflicted targetVersion with to be activated %s" % target_sw_version
            logger.error(msg)
            return True, msg
        del ne_info["targetSwVersion"]

    old_sw_version = ne_info.get("oldSwVersion", "")

    if target_sw_version != ne_info["currentSwVersion"]:
        ne_info["oldSwVersion"] = ne_info["currentSwVersion"]
        ne_info["currentSwVersion"] = target_sw_version
        ne_info["status"] = conf.STATUS_ACTIVATING
        ems_util.update_ne_info(ne_info)

        if target_sw_version != old_sw_version:
            old_sw_dir = os.path.join(sw_install_dir_in_ne, old_sw_version)
            if old_sw_version and os.path.isdir(old_sw_dir):
                shutil.rmtree(old_sw_dir, ignore_errors=True)

    old_cwd = os.getcwd()
    os.chdir(sw_install_dir_in_ne)
    if os.path.islink(conf.CURRENT_VERSION_DIR):
        os.remove(conf.CURRENT_VERSION_DIR)
    os.symlink(target_sw_version, conf.CURRENT_VERSION_DIR)
    os.chdir(old_cwd)

    if "downloadedSwLocation" in ne_info:
        if os.path.isdir(ne_info["downloadedSwLocation"]):
            shutil.rmtree(ne_info["downloadedSwLocation"], ignore_errors=True)
        del ne_info["downloadedSwLocation"]

    return False, None
Exemple #5
0
def download(sw_to_be_downloaded, ne_id):
    ne_info = ems_util.get_ne_info_from_db_by_id(ne_id)

    download_process_id = random.randint(1, conf.MAX_INT)
    result = conf.REQ_SUCCESS
    ret_value = {
        "downloadProcessId": download_process_id,
        "result": result
    }

    if not ne_info:
        ret_value["result"] = conf.REQ_FAILURE
        ret_value["reason"] = "Can not find NE %s" % ne_id

        logger.error(ret_value["reason"])
        return ret_value

    ne_info["status"] = conf.STATUS_DOWNLOADING
    ems_util.update_ne_info(ne_info)

    num_sw_to_be_downloaded = len(sw_to_be_downloaded)

    downloaded_ne_sw_info = []
    failed_sw_info = []

    sw_download_parent_dir = ems_util.get_download_dir(ne_info['omIP'])
    logger.info("SW will be downloaded to %s" % sw_download_parent_dir)

    sw_download_dir = ne_info.get("downloadedSwLocation", "")
    try:
        if not os.path.isdir(sw_download_parent_dir):
            os.makedirs(sw_download_parent_dir)

        if sw_download_dir and not os.path.isdir(sw_download_dir):
            os.makedirs(sw_download_dir)
    except OSError as e:
        ret_value["result"] = conf.REQ_FAILURE
        ret_value["reason"] = str(e)

        logger.error(ret_value["reason"])
        return ret_value

    if not sw_download_dir:
        sw_download_dir = tempfile.mkdtemp(dir=sw_download_parent_dir)

    for sw_info in sw_to_be_downloaded:
        err, reason, file_location = do_download(sw_info, sw_download_dir)
        if not err:
            logger.info("Downloaded SW file location: %s" % file_location)
            downloaded_ne_sw_info.append(file_location)
        else:
            result = conf.REQ_FAILURE
            failed_sw_entry = {
                "failedSw": file_location,
                "failureReason": reason
            }

            logger.error("Failed downloaded SW: %s" % str(failed_sw_entry))
            failed_sw_info.append(failed_sw_entry)

    num_downloaded_ne_sw = len(downloaded_ne_sw_info)

    if num_downloaded_ne_sw == num_sw_to_be_downloaded:
        download_status = "NE_SWDOWNLOAD_SUCCESSFUL"
    elif num_downloaded_ne_sw == 0:
        download_status = "NE_SWDOWNLOAD_FAILED"
    else:
        download_status = "NE_SWDOWNLOAD_PARTIALLY_SUCCESSFUL"
    logger.info("Download SW status: %s" % download_status)

    notification = generate_notification(download_process_id, download_status, downloaded_ne_sw_info, failed_sw_info)
    ems_util.send_notification(notification, download_process_id)

    if result == conf.REQ_SUCCESS:
        ne_info["downloadedSwLocation"] = sw_download_dir
        ems_util.update_ne_info(ne_info)

        logger.info("Download SW success")
    else:
        shutil.rmtree(sw_download_dir, ignore_errors=True)

        ret_value["result"] = result
        ret_value["reason"] = json.dumps(failed_sw_info)

        logger.info("Download SW failure, reason: %s" % ret_value["reason"])

    # for automated software management, there is no listOfStepNumbersAndDurations
    return notification, ret_value
def do_install(sw_to_be_installed, ne_info):
    """
    return err, reason, installed_ne_sw
    """

    logger.info("swToBeInstalled: %s" % sw_to_be_installed)

    sw_install_dir_in_ne = ems_util.get_install_dir(ne_info['omIP'])

    if sw_to_be_installed.startswith('/'):
        file_location = sw_to_be_installed
    else:
        sw_download_dir_in_ne = ne_info.get("downloadedSwLocation", "")
        file_location = os.path.join(sw_download_dir_in_ne, sw_to_be_installed)

    if not os.access(file_location, os.R_OK):
        msg = "Missing to be installed SW file %s" % file_location
        logger.error(msg)
        return True, msg, None

    try:
        if not os.path.isdir(sw_install_dir_in_ne):
            os.makedirs(sw_install_dir_in_ne)
    except OSError as e:
        msg = str(e)
        logger.error(msg)
        return True, msg, None

    temp_dir = tempfile.mkdtemp(dir=sw_install_dir_in_ne)
    if file_location.endswith(".zip"):
        with zipfile.ZipFile(file_location) as sw_zip:
            sw_zip.extractall(temp_dir)
    else:
        msg = "Only support zip file"
        logger.error(msg)
        return True, msg, None

    manifest_location = os.path.join(temp_dir, conf.MANIFEST_FILE)
    if os.access(manifest_location, os.R_OK):
        with open(manifest_location) as f_manifest:
            manifest = json.load(f_manifest)
    else:
        shutil.rmtree(temp_dir, ignore_errors=True)
        msg = "Missing manifest file in %s" % file_location
        logger.error(msg)
        return True, msg, None

    try:
        target_sw_name = manifest["name"]
        target_sw_version = manifest["version"]
    except KeyError as e:
        shutil.rmtree(temp_dir, ignore_errors=True)
        msg = "Missing key %s in %s of %s" % (str(e), conf.MANIFEST_FILE,
                                              file_location)
        logger.error(msg)
        return True, msg, None

    if "targetSwVersion" in ne_info and ne_info[
            "targetSwVersion"] != target_sw_version:
        shutil.rmtree(temp_dir, ignore_errors=True)
        msg = "Conflicted targetVersion for %s" % file_location
        logger.error(msg)
        return True, msg, None

    ne_info["targetSwVersion"] = target_sw_version
    ems_util.update_ne_info(ne_info)

    target_sw_parent_dir = os.path.join(sw_install_dir_in_ne,
                                        target_sw_version)
    try:
        if not os.path.isdir(target_sw_parent_dir):
            os.makedirs(target_sw_parent_dir)
    except OSError as e:
        shutil.rmtree(temp_dir, ignore_errors=True)
        msg = str(e)
        logger.error(msg)
        return True, msg, None

    target_sw_dir = os.path.join(target_sw_parent_dir, target_sw_name)
    if os.path.isdir(target_sw_dir):
        shutil.rmtree(target_sw_dir, ignore_errors=True)

    try:
        shutil.move(temp_dir, target_sw_dir)
    except shutil.Error as e:
        shutil.rmtree(temp_dir, ignore_errors=True)
        msg = str(e)
        logger.error(msg)
        return True, msg, None
    logger.info("Install SW to %s" % target_sw_dir)

    installed_ne_sw = target_sw_name + '-' + target_sw_version
    logger.info("Installed SW: %s" % installed_ne_sw)

    installed_sw_db = os.path.join(target_sw_parent_dir,
                                   conf.INSTALLED_SW_FILE)
    if os.path.isfile(installed_sw_db):
        with open(installed_sw_db) as f_installed_sw:
            installed_sw_table = json.load(f_installed_sw)
        if not installed_sw_table:
            installed_sw_table = {}
    else:
        installed_sw_table = {}

    target_sw_info = {
        "name": target_sw_name,
        "version": target_sw_version,
        "installedLocation": target_sw_dir
    }
    installed_sw_table[installed_ne_sw] = target_sw_info

    with open(installed_sw_db, 'w') as f_installed_sw:
        json.dump(installed_sw_table, f_installed_sw, indent=2)

    ne_info["installedSw"] = installed_sw_table

    return False, None, installed_ne_sw
Exemple #7
0
def fallback(ne_info_list):
    logger.info("NE info list: %s" % ne_info_list)

    ne_list = []
    num_failure = 0

    for ne_info in ne_info_list:
        if ne_info.get("status") == conf.STATUS_DOWNLOADING:
            ne_info["status"] = conf.STATUS_ACTIVATED
            ems_util.update_ne_info(ne_info)

            ne_entry = {
                "nEIdentification": ne_info["nEIdentification"],
                "swFallbackStatus": "fallbackSuccessful"
            }
            ne_list.append(ne_entry)
            continue

        sw_install_dir_in_ne = ems_util.get_install_dir(ne_info['omIP'])

        if ne_info.get("status") == conf.STATUS_INSTALLING:
            old_sw_version = ne_info.get("currentSwVersion", "")
            current_sw_version = ne_info.get("targetSwVersion", "")
        else:
            old_sw_version = ne_info.get("oldSwVersion", "")
            current_sw_version = ne_info.get("currentSwVersion", "")

        old_sw_dir = os.path.join(sw_install_dir_in_ne, old_sw_version)

        if not old_sw_version or not os.path.isdir(old_sw_dir):
            ne_entry = {
                "nEIdentification": ne_info["nEIdentification"],
                "swFallbackStatus": "fallbackUnsuccessful"
            }
            logger.error("oldSwVersion (%s) or oldSwDirectory (%s) is none" %
                         (old_sw_version, old_sw_dir))
            ne_list.append(ne_entry)

            num_failure += 1
            continue

        current_sw_dir = os.path.join(sw_install_dir_in_ne, current_sw_version)

        if current_sw_version and os.path.isdir(
                current_sw_dir) and current_sw_dir != old_sw_dir:
            shutil.rmtree(current_sw_dir, ignore_errors=True)

        old_cwd = os.getcwd()
        os.chdir(sw_install_dir_in_ne)
        if os.path.islink(conf.CURRENT_VERSION_DIR):
            os.remove(conf.CURRENT_VERSION_DIR)
        os.symlink(old_sw_version, conf.CURRENT_VERSION_DIR)
        os.chdir(old_cwd)

        installed_sw_db = os.path.join(old_sw_dir, conf.INSTALLED_SW_FILE)
        if os.path.isfile(installed_sw_db):
            with open(installed_sw_db) as f_installed_sw:
                installed_sw_table = json.load(f_installed_sw)
            if not installed_sw_table:
                installed_sw_table = {}
        else:
            installed_sw_table = {}

        ne_info["installedSw"] = installed_sw_table
        if "oldSwVersion" in ne_info:
            ne_info["currentSwVersion"] = ne_info["oldSwVersion"]
            del ne_info["oldSwVersion"]

        if "targetSwVersion" in ne_info:
            del ne_info["targetSwVersion"]

        if "downloadedSwLocation" in ne_info:
            if os.path.isdir(ne_info["downloadedSwLocation"]):
                shutil.rmtree(ne_info["downloadedSwLocation"],
                              ignore_errors=True)
            del ne_info["downloadedSwLocation"]

        ne_info["status"] = conf.STATUS_ACTIVATED
        ems_util.update_ne_info(ne_info)

        ne_entry = {
            "nEIdentification": ne_info["nEIdentification"],
            "swFallbackStatus": "fallbackSuccessful"
        }
        ne_list.append(ne_entry)

    if num_failure == 0:
        result = conf.RESULT_SUCCESS
    elif num_failure == len(ne_info_list):
        result = conf.RESULT_FAILURE
    else:
        result = conf.RESULT_PARTLY
    logger.info("Fallback SW result: %s" % result)

    ret_value = {"nEList": ne_list, "result": result}

    return ret_value