Enbo Wang | 6ab8b62 | 2019-04-23 13:42:21 +0000 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | |
| 3 | import sys |
| 4 | import argparse |
| 5 | import json |
| 6 | import os |
| 7 | import shutil |
| 8 | import random |
| 9 | import time |
| 10 | import tempfile |
| 11 | import zipfile |
| 12 | |
| 13 | import conf |
| 14 | import ems_util |
| 15 | |
| 16 | |
| 17 | def do_install_sw(sw_to_be_installed, ne_info): |
| 18 | """ |
| 19 | return err, reason, installed_ne_sw |
| 20 | """ |
| 21 | |
| 22 | sw_install_dir_in_ne = conf.PNF_SIMULATORS_DIR + '/' + ne_info['omIP'] + conf.PNF_SW_INSTALL_DIR |
| 23 | |
| 24 | if sw_to_be_installed.startswith('/'): |
| 25 | file_location = sw_to_be_installed |
| 26 | else: |
| 27 | sw_download_dir_in_ne = ne_info.get("downloadedSwLocation", "") |
| 28 | file_location = sw_download_dir_in_ne + '/' + sw_to_be_installed |
| 29 | |
| 30 | if not os.access(file_location, os.R_OK): |
| 31 | return True, "Missing to be installed SW file %s" % file_location, None |
| 32 | |
| 33 | try: |
| 34 | if not os.path.isdir(sw_install_dir_in_ne): |
| 35 | os.makedirs(sw_install_dir_in_ne) |
| 36 | except OSError as e: |
| 37 | return True, str(e), None |
| 38 | |
| 39 | temp_dir = tempfile.mkdtemp(dir=sw_install_dir_in_ne) |
| 40 | if file_location.endswith(".zip"): |
| 41 | with zipfile.ZipFile(file_location) as sw_zip: |
| 42 | sw_zip.extractall(temp_dir) |
| 43 | else: |
| 44 | return True, "Only support zip file", None |
| 45 | |
| 46 | manifest_location = temp_dir + '/' + conf.MANIFEST_FILE |
| 47 | if os.access(manifest_location, os.R_OK): |
| 48 | with open(manifest_location) as f_manifest: |
| 49 | manifest = json.load(f_manifest) |
| 50 | else: |
| 51 | shutil.rmtree(temp_dir, ignore_errors=True) |
| 52 | return True, "Missing manifest file in %s" % file_location, None |
| 53 | |
| 54 | try: |
| 55 | target_sw_name = manifest["name"] |
| 56 | target_sw_version = manifest["version"] |
| 57 | except KeyError as e: |
| 58 | shutil.rmtree(temp_dir, ignore_errors=True) |
| 59 | return True, "Missing key %s in %s of %s" % (str(e), conf.MANIFEST_FILE, file_location), None |
| 60 | |
| 61 | if "targetSwVersion" in ne_info and ne_info["targetSwVersion"] != target_sw_version: |
| 62 | shutil.rmtree(temp_dir, ignore_errors=True) |
| 63 | return True, "Conflicted targetVersion for %s" % file_location, None |
| 64 | |
| 65 | ne_info["targetSwVersion"] = target_sw_version |
| 66 | ems_util.update_ne_info(ne_info) |
| 67 | |
| 68 | target_sw_parent_dir = sw_install_dir_in_ne + '/' + target_sw_version |
| 69 | try: |
| 70 | if not os.path.isdir(target_sw_parent_dir): |
| 71 | os.makedirs(target_sw_parent_dir) |
| 72 | except OSError as e: |
| 73 | shutil.rmtree(temp_dir, ignore_errors=True) |
| 74 | return True, str(e), None |
| 75 | |
| 76 | target_sw_dir = target_sw_parent_dir + '/' + target_sw_name |
| 77 | if os.path.isdir(target_sw_dir): |
| 78 | shutil.rmtree(target_sw_dir, ignore_errors=True) |
| 79 | |
| 80 | try: |
| 81 | shutil.move(temp_dir, target_sw_dir) |
| 82 | except shutil.Error as e: |
| 83 | shutil.rmtree(temp_dir, ignore_errors=True) |
| 84 | return True, str(e), None |
| 85 | |
| 86 | installed_ne_sw = target_sw_name + '-' + target_sw_version |
| 87 | |
| 88 | installed_sw_db = target_sw_parent_dir + '/' + conf.INSTALLED_SW |
| 89 | if os.path.isfile(installed_sw_db): |
| 90 | with open(installed_sw_db) as f_installed_sw: |
| 91 | installed_sw_table = json.load(f_installed_sw) |
| 92 | if not installed_sw_table: |
| 93 | installed_sw_table = {} |
| 94 | else: |
| 95 | installed_sw_table = {} |
| 96 | |
| 97 | target_sw_info = { |
| 98 | "name": target_sw_name, |
| 99 | "version": target_sw_version, |
| 100 | "installedLocation": target_sw_dir |
| 101 | } |
| 102 | installed_sw_table[installed_ne_sw] = target_sw_info |
| 103 | |
| 104 | with open(installed_sw_db, 'w') as f_installed_sw: |
| 105 | json.dump(installed_sw_table, f_installed_sw, indent=2) |
| 106 | |
| 107 | ne_info["installedSw"] = installed_sw_table |
| 108 | |
| 109 | return False, None, installed_ne_sw |
| 110 | |
| 111 | |
| 112 | def generate_notification(install_process_id, install_status, installed_ne_sw_info, failed_sw_info): |
| 113 | notification = { |
| 114 | "objectClass": "EMSClass", |
| 115 | "objectInstance": "EMSInstance", |
| 116 | "notificationId": random.randint(1, conf.MAX_INT), |
| 117 | "eventTime": time.asctime(), |
| 118 | "systemDN": "emssimulator", |
| 119 | "notificationType": "notifyInstallNESwStatusChanged", |
| 120 | "installProcessId": install_process_id, |
| 121 | "installOperationStatus": install_status |
| 122 | } |
| 123 | |
| 124 | if installed_ne_sw_info: |
| 125 | notification["installedNESwInfo"] = installed_ne_sw_info |
| 126 | |
| 127 | if failed_sw_info: |
| 128 | notification["failedSwInfo"] = failed_sw_info |
| 129 | |
| 130 | return notification |
| 131 | |
| 132 | |
| 133 | def install_ne_sw(sw_to_be_installed, ne_id): |
| 134 | ne_info = ems_util.get_ne_info_from_db_by_id(ne_id) |
| 135 | |
| 136 | install_process_id = random.randint(1, conf.MAX_INT) |
| 137 | result = conf.REQ_SUCCESS |
| 138 | ret_value = { |
| 139 | "installProcessId": install_process_id, |
| 140 | "result": result |
| 141 | } |
| 142 | |
| 143 | if not ne_info: |
| 144 | ret_value["result"] = conf.REQ_FAILURE |
| 145 | ret_value["reason"] = "Can not find NE %s" % ne_id |
| 146 | return ret_value |
| 147 | |
| 148 | ne_info["status"] = conf.STATUS_INSTALLING |
| 149 | ems_util.update_ne_info(ne_info) |
| 150 | |
| 151 | installed_ne_sw_info = [] |
| 152 | failed_sw_info = [] |
| 153 | |
| 154 | err, reason, installed_ne_sw = do_install_sw(sw_to_be_installed, ne_info) |
| 155 | |
| 156 | if not err: |
| 157 | installed_ne_sw_info.append(installed_ne_sw) |
| 158 | else: |
| 159 | result = conf.REQ_FAILURE |
| 160 | failed_sw_entry = { |
| 161 | "failedSw": installed_ne_sw, |
| 162 | "failureReason": reason |
| 163 | } |
| 164 | failed_sw_info.append(failed_sw_entry) |
| 165 | |
| 166 | num_installed_ne_sw = len(installed_ne_sw_info) |
| 167 | |
| 168 | if num_installed_ne_sw == 1: |
| 169 | install_status = "NE_SWINSTALLATION_SUCCESSFUL" |
| 170 | elif num_installed_ne_sw == 0: |
| 171 | install_status = "NE_SWINSTALLATION_FAILED" |
| 172 | else: |
| 173 | install_status = "NE_SWINSTALLATION_PARTIALLY_SUCCESSFUL" |
| 174 | |
| 175 | notification = generate_notification(install_process_id, install_status, installed_ne_sw_info, failed_sw_info) |
| 176 | ems_util.send_notification(notification, install_process_id) |
| 177 | |
| 178 | if result == conf.REQ_SUCCESS: |
| 179 | ems_util.update_ne_info(ne_info) |
| 180 | else: |
| 181 | ret_value["result"] = result |
| 182 | ret_value["reason"] = json.dumps(failed_sw_info) |
| 183 | |
| 184 | # for automated software management, there is no listOfStepNumbersAndDurations |
| 185 | return ret_value |
| 186 | |
| 187 | |
| 188 | def main(): |
| 189 | parser = argparse.ArgumentParser() |
| 190 | |
| 191 | parser.add_argument("--swToBeInstalled", help="The NE software to be installed", required=True) |
| 192 | parser.add_argument("--neIdentifier", help="The NE where the software can be installed", required=True) |
| 193 | |
| 194 | args = parser.parse_args() |
| 195 | |
| 196 | ret_value = install_ne_sw(args.swToBeInstalled, args.neIdentifier) |
| 197 | print json.dumps(ret_value) |
| 198 | |
| 199 | if ret_value["result"] == conf.REQ_SUCCESS: |
| 200 | sys.exit(conf.RET_CODE_SUCCESS) |
| 201 | else: |
| 202 | sys.exit(conf.RET_CODE_FAILURE) |
| 203 | |
| 204 | |
| 205 | if __name__ == '__main__': |
| 206 | main() |