diff options
author | k.kedron <k.kedron@partner.samsung.com> | 2020-06-05 14:51:01 +0200 |
---|---|---|
committer | Krystian Kedron <k.kedron@partner.samsung.com> | 2020-06-24 09:21:00 +0000 |
commit | 16fe29ac226610f79c8da1f691437ec7fe6e79c4 (patch) | |
tree | 5b73d8f1724c561609c05fd2802ad80b7acfb4c0 /catalog-be/src/main/resources/scripts/sdcBePy/common/normative | |
parent | fca8a0b1af32083b8ea025135b120091aec9714f (diff) |
Improvement sdc-BE-init python scripts
- Implemented retries when request fail
- Moved configuration variables to the Properties file
- Extended sdcBeProxy
- Implemented script to run import/update (should fix deployment glitch)
- Updated the import_Normatives recipes to use new script
Issue-ID: SDC-2784
Signed-off-by: Krystian Kedron <k.kedron@partner.samsung.com>
Change-Id: I83fab898783ad8d3b3d532af43d75bc54d033c33
Diffstat (limited to 'catalog-be/src/main/resources/scripts/sdcBePy/common/normative')
3 files changed, 77 insertions, 30 deletions
diff --git a/catalog-be/src/main/resources/scripts/sdcBePy/common/normative/main.py b/catalog-be/src/main/resources/scripts/sdcBePy/common/normative/main.py index a30d46d4d5..ad8338cee4 100644 --- a/catalog-be/src/main/resources/scripts/sdcBePy/common/normative/main.py +++ b/catalog-be/src/main/resources/scripts/sdcBePy/common/normative/main.py @@ -1,16 +1,56 @@ +import time +from datetime import datetime + +from sdcBePy.common.bColors import BColors +from sdcBePy.common.logger import print_and_exit from sdcBePy.common.normative.toscaElements import process_and_create_normative_element from sdcBePy.common.normative.toscaTypes import process_and_create_normative_types +from sdcBePy.common.errors import ResourceCreationError +from sdcBePy import properties + +colors = BColors() def process_element_list(normative_elements_list, sdc_be_proxy): + attempt = 0 for normative_element in normative_elements_list: - process_and_create_normative_element(normative_element, - sdc_be_proxy=sdc_be_proxy) + while True: + attempt += 1 + try: + process_and_create_normative_element(normative_element, + sdc_be_proxy=sdc_be_proxy) + break + except ResourceCreationError as e: + _check_and_retry(attempt, e.error_code, e.message) + except Exception as e: + _check_and_retry(attempt, 1, str(e)) def process_type_list(normative_type_list, sdc_be_proxy, update_version): + attempt = 0 for normative_type in normative_type_list: - process_and_create_normative_types(normative_type, - sdc_be_proxy=sdc_be_proxy, - update_version=update_version) + while True: + attempt += 1 + try: + process_and_create_normative_types(normative_type, + sdc_be_proxy=sdc_be_proxy, + update_version=update_version) + break + except ResourceCreationError as e: + _check_and_retry(attempt, e.error_code, e.message) + normative_type.normative_types_list = _reduce(normative_type.normative_types_list, e.resource_name) + except Exception as e: + _check_and_retry(attempt, 1, str(e)) + + +def _check_and_retry(attempt, code, message): + if attempt == properties.retry_attempts + 1: + print_and_exit(code, message) + + print(colors.FAIL + '[WARRING]: ' + datetime.now().strftime('%Y/%m/%d %H:%M:%S') + + ' ' + message + ", try again: #" + str(attempt) + colors.END_C) + time.sleep(properties.retry_time) + +def _reduce(_list, element): + return _list[_list.index(element)::] diff --git a/catalog-be/src/main/resources/scripts/sdcBePy/common/normative/toscaElements.py b/catalog-be/src/main/resources/scripts/sdcBePy/common/normative/toscaElements.py index 1d4e734351..5cdca0a095 100644 --- a/catalog-be/src/main/resources/scripts/sdcBePy/common/normative/toscaElements.py +++ b/catalog-be/src/main/resources/scripts/sdcBePy/common/normative/toscaElements.py @@ -3,8 +3,9 @@ import zipfile import pycurl -from sdcBePy.common.logger import debug, log, print_name_and_return_code, error_and_exit +from sdcBePy.common.logger import debug, log, print_name_and_return_code, print_and_exit from sdcBePy.common.sdcBeProxy import SdcBeProxy +from sdcBePy.common.errors import ResourceCreationError def process_and_create_normative_element(normative_element, @@ -49,9 +50,11 @@ def _send_request(sdc_be_proxy, file_dir, url_suffix, element_name, http_res = sdc_be_proxy.post_file(url_suffix, multi_part_form_data) if http_res is not None: debug("http response =", http_res) - debug("response buffer", str(sdc_be_proxy.con.buffer.getvalue(), "UTF-8")) + + response = sdc_be_proxy.get_response_from_buffer() + debug("response buffer", response) # c.close() - return element_name, http_res, sdc_be_proxy.con.buffer.getvalue() + return element_name, http_res, response except Exception as inst: print("ERROR=" + str(inst)) @@ -99,9 +102,9 @@ def print_and_check_result(result, exit_on_success): if result is not None: print_name_and_return_code(result[0], result[1]) if result[1] is None or result[1] not in [200, 201, 409]: - error_and_exit(1, "Failed to create the normatives elements!!") + raise ResourceCreationError("Failed to create the normatives elements!!", 1) else: if exit_on_success is True: - error_and_exit(0, "All normatives elements created successfully!!") + print_and_exit(0, "All normatives elements created successfully!!") else: - error_and_exit(1, "Results is None!!") + raise ResourceCreationError("Results is None!", 1) diff --git a/catalog-be/src/main/resources/scripts/sdcBePy/common/normative/toscaTypes.py b/catalog-be/src/main/resources/scripts/sdcBePy/common/normative/toscaTypes.py index 48b20ccb1e..5d64f448d3 100644 --- a/catalog-be/src/main/resources/scripts/sdcBePy/common/normative/toscaTypes.py +++ b/catalog-be/src/main/resources/scripts/sdcBePy/common/normative/toscaTypes.py @@ -3,7 +3,8 @@ import zipfile import pycurl -from sdcBePy.common.logger import print_name_and_return_code, error_and_exit, log, debug +from sdcBePy.common.errors import ResourceCreationError +from sdcBePy.common.logger import print_name_and_return_code, print_and_exit, log, debug from sdcBePy.common.sdcBeProxy import SdcBeProxy @@ -32,25 +33,27 @@ def print_and_check_results(results, update_version, exit_on_success=False): print("----------------------------------------") check_results_and_exit(results, update_version, exit_on_success) else: - error_and_exit(1, "results is none -> error occurred!!") + raise ResourceCreationError("Results is none -> error occurred!!", 1) def check_results_and_exit(results, update_version, exit_on_success): if not _results_ok(results, _get_response_code(update_version)): - error_and_exit(1, "Failed to create the normatives types !!") + raise ResourceCreationError("Failed to create the normatives types !!", 1) else: if exit_on_success: - error_and_exit(0, "All normatives types created successfully!!") + print_and_exit(0, "All normatives types created successfully!!") def _create_normatives_type(file_dir, sdc_be_proxy, types, update_version): results = [] response_codes = _get_response_code(update_version) - for normativeType in types: - result = _send_request(sdc_be_proxy, file_dir, normativeType, update_version) + for normative_type in types: + result = _send_request(sdc_be_proxy, file_dir, normative_type, update_version) results.append(result) if result[1] is None or result[1] not in response_codes: - print("Failed creating normative type " + normativeType + ". " + str(result[1])) + raise ResourceCreationError("Failed creating normative type " + normative_type + ". " + str(result[1]), + 1, + normative_type) return results @@ -67,12 +70,13 @@ def _send_request(sdc_be_proxy, file_dir, element_name, update_version): send = _create_send_body(file_dir, element_name) debug(send) - httpRes = sdc_be_proxy.post_file(url, send) - if httpRes is not None: - debug("http response=", httpRes) - debug(sdc_be_proxy.con.buffer.getvalue()) + http_res = sdc_be_proxy.post_file(url, send) + if http_res is not None: + debug("http response=", http_res) - return element_name, httpRes, sdc_be_proxy.con.buffer.getvalue() + response = sdc_be_proxy.get_response_from_buffer() + debug(response) + return element_name, http_res, response except Exception as inst: print("ERROR=" + str(inst)) @@ -90,15 +94,15 @@ def _create_send_body(file_dir, element_name): debug(path) current_json_file = file_dir + element_name + "/" + element_name + ".json" - jsonFile = open(current_json_file) + json_file = open(current_json_file) debug("before load json") - json_data = json.load(jsonFile, strict=False) + json_data = json.load(json_file, strict=False) debug(json_data) - jsonAsStr = json.dumps(json_data) + json_as_str = json.dumps(json_data) - return [('resourceMetadata', jsonAsStr), ('resourceZip', (pycurl.FORM_FILE, path))] + return [('resourceMetadata', json_as_str), ('resourceZip', (pycurl.FORM_FILE, path))] def _results_ok(results, response_codes): @@ -110,11 +114,11 @@ def _results_ok(results, response_codes): def _get_response_code(update_version): - responseCodes = [200, 201] + response_codes = [200, 201] if update_version is False: - responseCodes.append(409) + response_codes.append(409) - return responseCodes + return response_codes def _boolean_to_string(boolean_value): |