def settings_ita(workspace_id): """IT-Automation 設定 Args: workspace_id (int): workspace id Returns: Response: HTTP Respose """ app_name = "ワークスペース情報:" exec_stat = "IT-Automation初期設定" error_detail = "" try: globals.logger.debug('#' * 50) globals.logger.debug('CALL {}'.format( inspect.currentframe().f_code.co_name)) globals.logger.debug('#' * 50) ita_db_name = "ita_db" ita_db_user = "******" ita_db_password = "******" # パラメータ情報(JSON形式) payload = request.json.copy() # ワークスペースアクセス情報取得 access_info = api_access_info.get_access_info(workspace_id) # namespaceの取得 namespace = common.get_namespace_name(workspace_id) # *-*-*-* podが立ち上がるのを待つ *-*-*-* start_time = time.time() while True: globals.logger.debug("waiting for ita pod up...") # PodがRunningになったら終了 if is_ita_pod_running(namespace): break # timeout current_time = time.time() if (current_time - start_time) > WAIT_SEC_ITA_POD_UP: globals.logger.debug( "ITA pod start Time out S:{} sec:{}".format( start_time, (current_time - start_time))) error_detail = "IT-Automation 初期設定でタイムアウトしました。再度、実行してください。" raise common.UserException(error_detail) time.sleep(1) # *-*-*-* podが立ち上がるのを待つ *-*-*-* start_time = time.time() while True: globals.logger.debug("waiting for ita mariaDB up...") # PodがRunningになったら終了 if is_ita_mysql_running(namespace, ita_db_user, ita_db_password): break # timeout current_time = time.time() if (current_time - start_time) > WAIT_SEC_ITA_POD_UP: globals.logger.debug( "ITA mariaDB start Time out S:{} sec:{}".format( start_time, (current_time - start_time))) error_detail = "IT-Automation 初期設定でタイムアウトしました。再度、実行してください。" raise common.UserException(error_detail) time.sleep(1) # *-*-*-* パスワード更新済み判定とする *-*-*-* # command = "mysql -u %s -p%s %s < /app/epoch/tmp/ita_table_update.sql" % (ita_db_user, ita_db_password, ita_db_name) command = "mysql -u %s -p%s %s -e'UPDATE A_ACCOUNT_LIST SET PW_LAST_UPDATE_TIME = \"2999-12-31 23:59:58\" WHERE USER_ID = 1;'" % ( ita_db_user, ita_db_password, ita_db_name) stdout_ita = subprocess.check_output([ "kubectl", "exec", "-i", "-n", namespace, "deployment/it-automation", "--", "bash", "-c", command ], stderr=subprocess.STDOUT) # *-*-*-* 認証情報準備 *-*-*-* host = "{}.{}.svc:{}".format(EPOCH_ITA_HOST, namespace, EPOCH_ITA_PORT) user_id = access_info["ITA_USER"] user_init_pass = "******" user_pass = access_info["ITA_PASSWORD"] # パスワード暗号化 init_auth = base64.b64encode((user_id + ':' + user_init_pass).encode()) auth = base64.b64encode((user_id + ':' + user_pass).encode()) # すでに1度でもインポート済みの場合は、処理しない ret_is_import = is_already_imported(host, auth, init_auth) if ret_is_import == 200: globals.logger.debug("ITA initialize Imported.(success)") # 正常終了 ret_status = 200 # 戻り値をそのまま返却 return jsonify({"result": ret_status}), ret_status # 一度もインポートしていないときに処理する if ret_is_import == 0: # *-*-*-* インポート実行(初期パスワードで実行) *-*-*-* task_id = import_process(host, init_auth) # *-*-*-* インポート結果確認 *-*-*-* globals.logger.debug('---- monitoring import dialog ----') # POST送信する # ヘッダ情報 header = { 'host': host, 'Content-Type': 'application/json', 'Authorization': init_auth, 'X-Command': 'FILTER', } # 実行パラメータ設定 data = {"2": {"LIST": [task_id]}} # json文字列に変換("utf-8"形式に自動エンコードされる) json_data = json.dumps(data) start_time = time.time() while True: globals.logger.debug("monitoring...") time.sleep(3) # timeout current_time = time.time() if (current_time - start_time) > WAIT_SEC_ITA_IMPORT: globals.logger.debug("ITA menu import Time out") error_detail = "IT-Automation 初期設定でタイムアウトしました。再度、実行してください。" raise common.UserException(error_detail) # リクエスト送信 dialog_response = requests.post( 'http://' + host + '/default/menu/07_rest_api_ver1.php?no=2100000213', headers=header, data=json_data) if dialog_response.status_code != 200: globals.logger.error("no=2100000213 error:{}".format( dialog_response.status_code)) globals.logger.error(dialog_response.text) error_detail = "IT-Automation 初期設定が失敗しました。再度、実行してください。" # タイミングによって500応答が返されるため継続 # raise common.UserException(error_detail) continue # ファイルがあるメニューのため、response.textをデバッグ出力すると酷い目にあう # print(dialog_response.text) dialog_resp_data = json.loads(dialog_response.text) if dialog_resp_data["status"] != "SUCCEED" or dialog_resp_data[ "resultdata"]["CONTENTS"]["RECORD_LENGTH"] != 1: globals.logger.error("no=2100000213 status:{}".format( dialog_resp_data["status"])) globals.logger.error(dialog_response.text) error_detail = "IT-Automation 初期設定が失敗しました。再度、実行してください。" raise common.UserException(error_detail) record = dialog_resp_data["resultdata"]["CONTENTS"]["BODY"][1] globals.logger.debug(json.dumps(record)) if record[3] == u"完了(異常)": error_detail = "IT-Automation ITAのメニューインポートに失敗しました。" raise common.UserException(error_detail) if record[3] == u"完了": break error_detail = "IT-Automation 初期設定のパスワード初期化に失敗しました。" # *-*-*-* パスワード変更 *-*-*-* command = "mysql -u {} -p{} {} -e'UPDATE A_ACCOUNT_LIST".format(ita_db_user, ita_db_password, ita_db_name) + \ " SET PASSWORD = \"{}\"".format(hashlib.md5((user_pass).encode()).hexdigest()) + \ " WHERE USER_ID = 1;'" stdout_ita = subprocess.check_output([ "kubectl", "exec", "-i", "-n", namespace, "deployment/it-automation", "--", "bash", "-c", command ], stderr=subprocess.STDOUT) error_detail = "IT-Automation 初期設定のユーザー情報生成に失敗しました。" # *-*-*-* EPOCHユーザー更新 *-*-*-* command = "mysql -u {} -p{} {} -e'UPDATE A_ACCOUNT_LIST".format(ita_db_user, ita_db_password, ita_db_name) + \ " SET USERNAME = \"{}\"".format(access_info["ITA_EPOCH_USER"]) + \ " , PASSWORD = \"{}\"".format(hashlib.md5((access_info["ITA_EPOCH_PASSWORD"]).encode()).hexdigest()) + \ " , PW_LAST_UPDATE_TIME = \"2999-12-31 23:59:58\"" + \ " WHERE USER_ID = 2;'" stdout_ita = subprocess.check_output([ "kubectl", "exec", "-i", "-n", namespace, "deployment/it-automation", "--", "bash", "-c", command ], stderr=subprocess.STDOUT) # 正常終了 ret_status = 200 # 戻り値をそのまま返却 return jsonify({"result": ret_status}), ret_status except common.UserException as e: return common.server_error_to_message(e, app_name + exec_stat, error_detail) except Exception as e: return common.server_error_to_message(e, app_name + exec_stat, error_detail)
def settings_manifest_templates(workspace_id): """manifest templates setting Returns: Response: HTTP Respose """ app_name = "ワークスペース情報:" exec_stat = "manifestテンプレートファイル登録" error_detail = "" try: globals.logger.debug('#' * 50) globals.logger.debug('CALL {}'.format( inspect.currentframe().f_code.co_name)) globals.logger.debug('#' * 50) # パラメータ情報(JSON形式) prameter save payload = request.json.copy() # ワークスペースアクセス情報取得 access_info = api_access_info.get_access_info(workspace_id) # namespaceの取得 namespace = common.get_namespace_name(workspace_id) ita_restapi_endpoint = "http://{}.{}.svc:{}/default/menu/07_rest_api_ver1.php".format( EPOCH_ITA_HOST, namespace, EPOCH_ITA_PORT) ita_user = access_info['ITA_USER'] ita_pass = access_info['ITA_PASSWORD'] # HTTPヘッダの生成 filter_headers = { 'host': EPOCH_ITA_HOST + ':' + EPOCH_ITA_PORT, 'Content-Type': 'application/json', 'Authorization': base64.b64encode( (ita_user + ':' + ita_pass).encode()), 'X-Command': 'FILTER', } edit_headers = { 'host': EPOCH_ITA_HOST + ':' + EPOCH_ITA_PORT, 'Content-Type': 'application/json', 'Authorization': base64.b64encode( (ita_user + ':' + ita_pass).encode()), 'X-Command': 'EDIT', } # # マニフェストテンプレートの取得 # content = { "1": { "NORMAL": "0" }, "3": { "NORMAL": "TPF_epoch_template_yaml" }, } manitpl_resp = requests.post(ita_restapi_endpoint + '?no=' + ita_menu_manifest_template, headers=filter_headers, data=json.dumps(content)) manitpl_json = json.loads(manitpl_resp.text) globals.logger.debug('---- Current Manifest Templates ----') # logger.debug(manitpl_resp.text) # globals.logger.debug(manitpl_json) req_data = payload['manifests'] mani_req_len = len(req_data) mani_ita_len = manitpl_json['resultdata']['CONTENTS']['RECORD_LENGTH'] max_loop_cnt = max(mani_req_len, mani_ita_len) globals.logger.debug("max_loop_cnt: " + str(max_loop_cnt)) ita_data = manitpl_json['resultdata']['CONTENTS']["BODY"] ita_data.pop(0) # globals.logger.debug(ita_data) edit_data = {"UPLOAD_FILE": []} tpl_cnt = 0 for i in range(max_loop_cnt): # ITAを廃止 if i > mani_req_len - 1: tmp_data = [] for j, item in enumerate(ita_data[i]): if j == 0: tmp_data.append('廃止') else: tmp_data.append(item) edit_data[str(i)] = tmp_data # ITAに新規登録する elif i > mani_ita_len - 1: tpl_cnt += 1 tmp_data = {} tmp_data['0'] = "登録" tmp_data['3'] = "TPF_epoch_template_yaml" + str(tpl_cnt) tmp_data['4'] = req_data[i]["file_name"] tmp_data['5'] = "VAR_image:\n"\ "VAR_image_tag:\n"\ "VAR_param01:\n"\ "VAR_param02:\n"\ "VAR_param03:\n"\ "VAR_param04:\n"\ "VAR_param05:\n"\ "VAR_param06:\n"\ "VAR_param07:\n"\ "VAR_param08:\n"\ "VAR_param09:\n"\ "VAR_param10:\n"\ "VAR_param11:\n"\ "VAR_param12:\n"\ "VAR_param13:\n"\ "VAR_param14:\n"\ "VAR_param15:\n"\ "VAR_param16:\n"\ "VAR_param17:\n"\ "VAR_param18:\n"\ "VAR_param19:\n"\ "VAR_param20:" edit_data[str(i)] = tmp_data edit_data["UPLOAD_FILE"].append({ "4": base64.b64encode( req_data[i]["file_text"].encode()).decode() }) # ITAを更新する else: tpl_cnt += 1 tmp_data = ita_data[i] tmp_data[0] = "更新" tmp_data[3] = "TPF_epoch_template_yaml" + str(tpl_cnt) tmp_data[4] = req_data[i]["file_name"] tmp_data[5] = "VAR_image:\n"\ "VAR_image_tag:\n"\ "VAR_param01:\n"\ "VAR_param02:\n"\ "VAR_param03:\n"\ "VAR_param04:\n"\ "VAR_param05:\n"\ "VAR_param06:\n"\ "VAR_param07:\n"\ "VAR_param08:\n"\ "VAR_param09:\n"\ "VAR_param10:\n"\ "VAR_param11:\n"\ "VAR_param12:\n"\ "VAR_param13:\n"\ "VAR_param14:\n"\ "VAR_param15:\n"\ "VAR_param16:\n"\ "VAR_param17:\n"\ "VAR_param18:\n"\ "VAR_param19:\n"\ "VAR_param20:" edit_data[str(i)] = tmp_data edit_data["UPLOAD_FILE"].append({ "4": base64.b64encode( req_data[i]["file_text"].encode()).decode() }) # globals.logger.debug(edit_data) # ITAへREST実行 manutemplate_edit_resp = requests.post(ita_restapi_endpoint + '?no=' + ita_menu_manifest_template, headers=edit_headers, data=json.dumps(edit_data)) # manitemplate_json = json.loads(manutemplate_edit_resp.text) # globals.logger.debug(manitemplate_json) # 正常終了 ret_status = 200 # 戻り値をそのまま返却 return jsonify({"result": ret_status}), ret_status except common.UserException as e: return common.server_error_to_message(e, app_name + exec_stat, error_detail) except Exception as e: return common.server_error_to_message(e, app_name + exec_stat, error_detail)
def create_ita(workspace_id): """IT-Automation Pod Create Args: workspace_id (int): workspace id Returns: Response: HTTP Respose """ app_name = "ワークスペース情報:" exec_stat = "IT-Automation環境構築" error_detail = "" try: globals.logger.debug('#' * 50) globals.logger.debug('CALL {}'.format( inspect.currentframe().f_code.co_name)) globals.logger.debug('#' * 50) # namespace定義 name = common.get_namespace_name(workspace_id) globals.logger.debug("ita-pod create start") # templateの展開 with tempfile.TemporaryDirectory() as tempdir: file_name = 'ita_install.yaml' yaml_param = { "HTTP_PROXY": os.environ.get("EPOCH_HTTP_PROXY"), "HTTPS_PROXY": os.environ.get("EPOCH_HTTPS_PROXY"), "NO_PROXY": os.environ.get("EPOCH_HOSTNAME"), } yaml_text = render_template(file_name, param=yaml_param) # yaml一時ファイル生成 path_yamlfile = '{}/{}'.format(tempdir, file_name) with open(path_yamlfile, mode='w') as fp: fp.write(yaml_text) # kubectl実行 try: result_kubectl = subprocess.check_output( ['kubectl', 'apply', '-f', path_yamlfile, '-n', name], stderr=subprocess.STDOUT) globals.logger.debug( 'COMMAAND SUCCEED: kubectl apply -f {}\n{}'.format( path_yamlfile, result_kubectl.decode('utf-8'))) except subprocess.CalledProcessError as e: globals.logger.error('COMMAND ERROR RETURN:{}\n{}'.format( e.returncode, e.output.decode('utf-8'))) exec_detail = "IT-AutomationのPodが生成できません。環境を確認してください。" raise common.UserException(exec_detail) except Exception: raise # 対象となるdeploymentを定義 # deployments = [ "deployment/ita-worker" ] # envs = [ # "HTTP_PROXY=" + os.environ['EPOCH_HTTP_PROXY'], # "HTTPS_PROXY=" + os.environ['EPOCH_HTTPS_PROXY'], # "http_proxy=" + os.environ['EPOCH_HTTP_PROXY'], # "https_proxy=" + os.environ['EPOCH_HTTPS_PROXY'] # ] # exec_detail = "環境変数[PROXY]を確認してください" # for deployment_name in deployments: # for env_name in envs: # # 環境変数の設定 # try: # result_kubectl = subprocess.check_output(["kubectl","set","env",deployment_name,"-n",name,env_name],stderr=subprocess.STDOUT) # globals.logger.debug('COMMAAND SUCCEED: kubectl set env {}\n{}'.format(deployment_name, result_kubectl.decode('utf-8'))) # except subprocess.CalledProcessError as e: # globals.logger.error('COMMAND ERROR RETURN:{}\n{}'.format(e.returncode, e.output.decode('utf-8'))) # exec_detail = "{}の環境変数[PROXY]の設定ができません。環境を確認してください。".format(deployment_name) # raise common.UserException(exec_detail) # except Exception: # raise exec_detail = "" # 正常終了 ret_status = 200 # 戻り値をそのまま返却 return jsonify({"result": ret_status}), ret_status except common.UserException as e: return common.server_error_to_message(e, app_name + exec_stat, error_detail) except Exception as e: return common.server_error_to_message(e, app_name + exec_stat, error_detail)
def settings_git_environment(workspace_id): """git environment setting Args: workspace_id (int): workspace ID Returns: Response: HTTP Respose """ app_name = "ワークスペース情報:" exec_stat = "IT-Automation git情報設定" error_detail = "" try: globals.logger.debug('#' * 50) globals.logger.debug('CALL {}'.format( inspect.currentframe().f_code.co_name)) globals.logger.debug('#' * 50) # パラメータ情報(JSON形式) prameter save payload = request.json.copy() # ワークスペースアクセス情報取得 access_info = api_access_info.get_access_info(workspace_id) # namespaceの取得 namespace = common.get_namespace_name(workspace_id) ita_restapi_endpoint = "http://{}.{}.svc:{}/default/menu/07_rest_api_ver1.php".format( EPOCH_ITA_HOST, namespace, EPOCH_ITA_PORT) ita_user = access_info['ITA_USER'] ita_pass = access_info['ITA_PASSWORD'] # HTTPヘッダの生成 filter_headers = { 'host': EPOCH_ITA_HOST + ':' + EPOCH_ITA_PORT, 'Content-Type': 'application/json', 'Authorization': base64.b64encode( (ita_user + ':' + ita_pass).encode()), 'X-Command': 'FILTER', } edit_headers = { 'host': EPOCH_ITA_HOST + ':' + EPOCH_ITA_PORT, 'Content-Type': 'application/json', 'Authorization': base64.b64encode( (ita_user + ':' + ita_pass).encode()), 'X-Command': 'EDIT', } # # オペレーションの取得 # opelist_resp = requests.post(ita_restapi_endpoint + '?no=' + ite_menu_operation, headers=filter_headers) opelist_json = json.loads(opelist_resp.text) globals.logger.debug('---- Operation ----') #logger.debug(opelist_resp.text.encode().decode('unicode-escape')) # globals.logger.debug(opelist_resp.text) # 項目位置の取得 column_indexes_opelist = column_indexes( column_names_opelist, opelist_json['resultdata']['CONTENTS']['BODY'][0]) globals.logger.debug('---- Operation Index ----') # globals.logger.debug(column_indexes_opelist) # # オペレーションの追加処理 # opelist_edit = [] for idx_req, row_req in enumerate( payload['cd_config']['environments']): if search_opration(opelist_json['resultdata']['CONTENTS']['BODY'], column_indexes_opelist, row_req['git_repositry']['url']) == -1: # オペレーションになければ、追加データを設定 opelist_edit.append({ str(column_indexes_common['method']): param_value_method_entry, str(column_indexes_opelist['operation_name']): param_value_operation_name_prefix + row_req['git_repositry']['url'], str(column_indexes_opelist['operation_date']): param_value_operation_date, str(column_indexes_opelist['remarks']): row_req['git_repositry']['url'], }) if len(opelist_edit) > 0: # # オペレーションの追加がある場合 # ope_add_resp = requests.post(ita_restapi_endpoint + '?no=' + ite_menu_operation, headers=edit_headers, data=json.dumps(opelist_edit)) globals.logger.debug('---- ope_add_resp ----') #logger.debug(ope_add_resp.text.encode().decode('unicode-escape')) globals.logger.debug(ope_add_resp.text) # 追加後再取得(オペレーションIDが決定する) opelist_resp = requests.post(ita_restapi_endpoint + '?no=' + ite_menu_operation, headers=filter_headers) opelist_json = json.loads(opelist_resp.text) globals.logger.debug('---- Operation ----') #logger.debug(opelist_resp.text.encode().decode('unicode-escape')) # # Git環境情報の取得 # gitlist_resp = requests.post(ita_restapi_endpoint + '?no=' + ita_menu_gitenv_param, headers=filter_headers) gitlist_json = json.loads(gitlist_resp.text) globals.logger.debug('---- Git Environments ----') #logger.debug(gitlist_resp.text.encode().decode('unicode-escape')) #logger.debug(gitlist_resp.text) # 項目位置の取得 column_indexes_gitlist = column_indexes( column_names_gitlist, gitlist_json['resultdata']['CONTENTS']['BODY'][0]) globals.logger.debug('---- Git Environments Index ----') # logger.debug(column_indexes_gitlist) # Responseデータの初期化 response = {"items": []} # Git環境情報の追加・更新 gitlist_edit = [] for idx_req, row_req in enumerate( payload['cd_config']['environments']): idx_git = search_gitlist( gitlist_json['resultdata']['CONTENTS']['BODY'], column_indexes_gitlist, row_req['git_repositry']['url']) if idx_git == -1: # リストになければ、追加データを設定 # 追加対象のURLのオペレーション idx_ope = search_opration( opelist_json['resultdata']['CONTENTS']['BODY'], column_indexes_opelist, row_req['git_repositry']['url']) # 追加処理データの設定 gitlist_edit.append({ str(column_indexes_common['method']): param_value_method_entry, str(column_indexes_gitlist['host']): param_value_host, str(column_indexes_gitlist['operation_id']): opelist_json['resultdata']['CONTENTS']['BODY'][idx_ope][ column_indexes_opelist['operation_id']], str(column_indexes_gitlist['operation']): format_opration_info( opelist_json['resultdata']['CONTENTS']['BODY'] [idx_ope], column_indexes_opelist), str(column_indexes_gitlist['git_url']): row_req['git_repositry']['url'], str(column_indexes_gitlist['git_user']): payload['cd_config']['environments_common'] ['git_repositry']['user'], str(column_indexes_gitlist['git_password']): payload['cd_config']['environments_common'] ['git_repositry']['token'], }) # レスポンスデータの設定 response["items"].append({ 'operation_id': opelist_json['resultdata']['CONTENTS']['BODY'][idx_ope][ column_indexes_opelist['operation_id']], 'git_url': row_req['git_repositry']['url'], 'git_user': payload['cd_config']['environments_common'] ['git_repositry']['user'], 'git_password': payload['cd_config']['environments_common'] ['git_repositry']['token'], }) else: # リストにあれば、更新データを設定 gitlist_edit.append({ str(column_indexes_common['method']): param_value_method_update, str(column_indexes_common['record_no']): gitlist_json['resultdata']['CONTENTS']['BODY'][idx_git][ column_indexes_common['record_no']], str(column_indexes_gitlist['host']): gitlist_json['resultdata']['CONTENTS']['BODY'][idx_git][ column_indexes_gitlist['host']], str(column_indexes_gitlist['operation']): gitlist_json['resultdata']['CONTENTS']['BODY'][idx_git][ column_indexes_gitlist['operation']], str(column_indexes_gitlist['git_url']): row_req['git_repositry']['url'], str(column_indexes_gitlist['git_user']): payload['cd_config']['environments_common'] ['git_repositry']['user'], str(column_indexes_gitlist['git_password']): payload['cd_config']['environments_common'] ['git_repositry']['token'], str(column_indexes_gitlist['lastupdate']): gitlist_json['resultdata']['CONTENTS']['BODY'][idx_git][ column_indexes_gitlist['lastupdate']], }) # レスポンスデータの設定 response["items"].append({ 'operation_id': gitlist_json['resultdata']['CONTENTS']['BODY'][idx_git][ column_indexes_gitlist['operation_id']], 'git_url': row_req['git_repositry']['url'], 'git_user': payload['cd_config']['environments_common'] ['git_repositry']['user'], 'git_password': payload['cd_config']['environments_common'] ['git_repositry']['token'], }) globals.logger.debug('---- Git Environments Post ----') #logger.debug(json.dumps(gitlist_edit).encode().decode('unicode-escape')) # logger.debug(json.dumps(gitlist_edit)) gitlist_edit_resp = requests.post(ita_restapi_endpoint + '?no=' + ita_menu_gitenv_param, headers=edit_headers, data=json.dumps(gitlist_edit)) globals.logger.debug('---- Git Environments Post Response ----') #logger.debug(gitlist_edit_resp.text.encode().decode('unicode-escape')) # globals.logger.debug(gitlist_edit_resp.text) # 正常終了 ret_status = 200 # 戻り値をそのまま返却 return jsonify({ "result": ret_status, "rows": response["items"] }), ret_status except common.UserException as e: return common.server_error_to_message(e, app_name + exec_stat, error_detail) except Exception as e: return common.server_error_to_message(e, app_name + exec_stat, error_detail)
def settings_manifest_parameter(workspace_id): """manifest parameter setting Returns: Response: HTTP Respose """ app_name = "ワークスペース情報:" exec_stat = "実行環境取得" error_detail = "" try: globals.logger.debug('#' * 50) globals.logger.debug('CALL {}'.format( inspect.currentframe().f_code.co_name)) globals.logger.debug('#' * 50) # パラメータ情報(JSON形式) prameter save payload = request.json.copy() # ワークスペースアクセス情報取得 access_info = api_access_info.get_access_info(workspace_id) # namespaceの取得 namespace = common.get_namespace_name(workspace_id) ita_restapi_endpoint = "http://{}.{}.svc:{}/default/menu/07_rest_api_ver1.php".format( EPOCH_ITA_HOST, namespace, EPOCH_ITA_PORT) ita_user = access_info['ITA_USER'] ita_pass = access_info['ITA_PASSWORD'] # HTTPヘッダの生成 filter_headers = { 'host': EPOCH_ITA_HOST + ':' + EPOCH_ITA_PORT, 'Content-Type': 'application/json', 'Authorization': base64.b64encode( (ita_user + ':' + ita_pass).encode()), 'X-Command': 'FILTER', } edit_headers = { 'host': EPOCH_ITA_HOST + ':' + EPOCH_ITA_PORT, 'Content-Type': 'application/json', 'Authorization': base64.b64encode( (ita_user + ':' + ita_pass).encode()), 'X-Command': 'EDIT', } # globals.logger.debug(payload) # # オペレーションの取得 # opelist_resp = requests.post(ita_restapi_endpoint + '?no=' + ite_menu_operation, headers=filter_headers) opelist_json = json.loads(opelist_resp.text) globals.logger.debug('---- Operation ----') # logger.debug(opelist_resp.text) # logger.debug(opelist_json) # 項目位置の取得 column_indexes_opelist = column_indexes( column_names_opelist, opelist_json['resultdata']['CONTENTS']['BODY'][0]) globals.logger.debug('---- Operation Index ----') # logger.debug(column_indexes_opelist) # # マニフェスト環境パラメータの取得 # content = {"1": {"NORMAL": "0"}} maniparam_resp = requests.post(ita_restapi_endpoint + '?no=' + ita_menu_manifest_param, headers=filter_headers, data=json.dumps(content)) maniparam_json = json.loads(maniparam_resp.text) globals.logger.debug('---- Current Manifest Parameters ----') # logger.debug(maniparam_resp.text) globals.logger.debug(maniparam_json) # 項目位置の取得 column_indexes_maniparam = column_indexes( column_names_manifest_param, maniparam_json['resultdata']['CONTENTS']['BODY'][0]) globals.logger.debug('---- Manifest Parameters Index ----') # logger.debug(column_indexes_maniparam) # Responseデータの初期化 response = { "result": "200", } globals.logger.debug("opelist:{}".format( opelist_json['resultdata']['CONTENTS']['BODY'])) # マニフェスト環境パラメータのデータ成型 maniparam_edit = [] for environment in payload['ci_config']['environments']: idx_ope = -1 # cd_configの同一環境情報からgit_urlを取得する Get git_url from the same environment information in cd_config for cd_environment in payload['cd_config']['environments']: if environment['environment_id'] == cd_environment[ 'environment_id']: globals.logger.debug("git_url:{}".format( cd_environment['git_repositry']['url'])) idx_ope = search_opration( opelist_json['resultdata']['CONTENTS']['BODY'], column_indexes_opelist, cd_environment['git_repositry']['url']) # ITAからオペレーション(=環境)が取得できなければ異常 if idx_ope == -1: error_detail = "CD環境が設定されていません。" raise common.UserException(error_detail) req_maniparam_operation_id = opelist_json['resultdata'][ 'CONTENTS']['BODY'][idx_ope][ column_indexes_common['record_no']] for idx_manifile, row_manifile in enumerate( environment['manifests']): image = None image_tag = None param01 = None param02 = None param03 = None param04 = None param05 = None param06 = None param07 = None param08 = None param09 = None param10 = None param11 = None param12 = None param13 = None param14 = None param15 = None param16 = None param17 = None param18 = None param19 = None param20 = None # parameters成型 for key, value in row_manifile['parameters'].items(): if key == 'image': image = value elif key == 'image_tag': image_tag = value elif key == 'param01': param01 = value elif key == 'param02': param02 = value elif key == 'param03': param03 = value elif key == 'param04': param04 = value elif key == 'param05': param05 = value elif key == 'param06': param06 = value elif key == 'param07': param07 = value elif key == 'param08': param08 = value elif key == 'param09': param09 = value elif key == 'param10': param10 = value elif key == 'param11': param11 = value elif key == 'param12': param12 = value elif key == 'param13': param13 = value elif key == 'param14': param14 = value elif key == 'param15': param15 = value elif key == 'param16': param16 = value elif key == 'param17': param17 = value elif key == 'param18': param18 = value elif key == 'param19': param19 = value elif key == 'param20': param20 = value # 既存データ確認 maniparam_id = -1 for idx_maniparam, row_maniparam in enumerate( maniparam_json['resultdata']['CONTENTS']['BODY']): current_maniparam_operation_id = row_maniparam[ column_indexes_maniparam['operation_id']] current_maniparam_include_index = row_maniparam[ column_indexes_maniparam['indexes']] if current_maniparam_operation_id == req_maniparam_operation_id and \ current_maniparam_include_index == str(idx_manifile + 1): maniparam_id = row_maniparam[ column_indexes_common['record_no']] break if maniparam_id == -1: # 追加処理データの設定 maniparam_edit.append({ str(column_indexes_common['method']): param_value_method_entry, str(column_indexes_maniparam['host']): param_value_host, str(column_indexes_maniparam['operation']): format_opration_info( opelist_json['resultdata']['CONTENTS']['BODY'] [idx_ope], column_indexes_opelist), str(column_indexes_maniparam['indexes']): idx_manifile + 1, str(column_indexes_maniparam['image']): image, str(column_indexes_maniparam['image_tag']): image_tag, str(column_indexes_maniparam['param01']): param01, str(column_indexes_maniparam['param02']): param02, str(column_indexes_maniparam['param03']): param03, str(column_indexes_maniparam['param04']): param04, str(column_indexes_maniparam['param05']): param05, str(column_indexes_maniparam['param06']): param06, str(column_indexes_maniparam['param07']): param07, str(column_indexes_maniparam['param08']): param08, str(column_indexes_maniparam['param09']): param09, str(column_indexes_maniparam['param10']): param10, str(column_indexes_maniparam['param11']): param11, str(column_indexes_maniparam['param12']): param12, str(column_indexes_maniparam['param13']): param13, str(column_indexes_maniparam['param14']): param14, str(column_indexes_maniparam['param15']): param15, str(column_indexes_maniparam['param16']): param16, str(column_indexes_maniparam['param17']): param17, str(column_indexes_maniparam['param18']): param18, str(column_indexes_maniparam['param19']): param19, str(column_indexes_maniparam['param20']): param20, str(column_indexes_maniparam['template_name']): '"{{ TPF_epoch_template_yaml' + str(idx_manifile + 1) + ' }}"', }) globals.logger.debug( '---- Manifest Parameters Item(Add) ----') globals.logger.debug(maniparam_edit[len(maniparam_edit) - 1]) else: # 更新処理 maniparam_edit.append({ str(column_indexes_common['method']): param_value_method_update, str(column_indexes_common['record_no']): maniparam_id, str(column_indexes_maniparam['host']): maniparam_json['resultdata']['CONTENTS']['BODY'] [idx_maniparam][column_indexes_maniparam['host']], str(column_indexes_maniparam['operation']): maniparam_json['resultdata']['CONTENTS']['BODY'] [idx_maniparam][column_indexes_maniparam['operation']], str(column_indexes_maniparam['indexes']): maniparam_json['resultdata']['CONTENTS']['BODY'] [idx_maniparam][column_indexes_maniparam['indexes']], str(column_indexes_maniparam['image']): image, str(column_indexes_maniparam['image_tag']): image_tag, str(column_indexes_maniparam['param01']): param01, str(column_indexes_maniparam['param02']): param02, str(column_indexes_maniparam['param03']): param03, str(column_indexes_maniparam['param04']): param04, str(column_indexes_maniparam['param05']): param05, str(column_indexes_maniparam['param06']): param06, str(column_indexes_maniparam['param07']): param07, str(column_indexes_maniparam['param08']): param08, str(column_indexes_maniparam['param09']): param09, str(column_indexes_maniparam['param10']): param10, str(column_indexes_maniparam['param11']): param11, str(column_indexes_maniparam['param12']): param12, str(column_indexes_maniparam['param13']): param13, str(column_indexes_maniparam['param14']): param14, str(column_indexes_maniparam['param15']): param15, str(column_indexes_maniparam['param16']): param16, str(column_indexes_maniparam['param17']): param17, str(column_indexes_maniparam['param18']): param18, str(column_indexes_maniparam['param19']): param19, str(column_indexes_maniparam['param20']): param20, str(column_indexes_maniparam['template_name']): maniparam_json['resultdata']['CONTENTS']['BODY'] [idx_maniparam][ column_indexes_maniparam['template_name']], str(column_indexes_maniparam['lastupdate']): maniparam_json['resultdata']['CONTENTS']['BODY'] [idx_maniparam][ column_indexes_maniparam['lastupdate']], }) globals.logger.debug( '---- Manifest Parameters Item(Update) ----') globals.logger.debug(maniparam_edit[len(maniparam_edit) - 1]) globals.logger.debug('---- Deleting Manifest Parameters Setting ----') # 既存データをすべて廃止する for idx_maniparam, row_maniparam in enumerate( maniparam_json['resultdata']['CONTENTS']['BODY']): # 1行目無視する if idx_maniparam == 0: continue flgExists = False for idx_edit, row_edit in enumerate(maniparam_edit): # 該当するrecord_noがあれば、チェックする if str(column_indexes_common['record_no']) in row_edit: if row_edit[str( column_indexes_common['record_no'] )] == row_maniparam[column_indexes_common['record_no']]: flgExists = True break # 該当するレコードがない場合は、廃止として追加する if not flgExists: # 削除用のデータ設定 maniparam_edit.append({ str(column_indexes_common['method']): param_value_method_delete, str(column_indexes_common['record_no']): row_maniparam[column_indexes_common['record_no']], str(column_indexes_maniparam['lastupdate']): row_maniparam[column_indexes_maniparam['lastupdate']], }) globals.logger.debug('---- Updating Manifest Parameters ----') # globals.logger.debug(json.dumps(maniparam_edit)) manuparam_edit_resp = requests.post(ita_restapi_endpoint + '?no=' + ita_menu_manifest_param, headers=edit_headers, data=json.dumps(maniparam_edit)) maniparam_json = json.loads(manuparam_edit_resp.text) globals.logger.debug('---- Manifest Parameters Post Response ----') # logger.debug(manuparam_edit_resp.text) # globals.logger.debug(maniparam_json) if maniparam_json["status"] != "SUCCEED" or maniparam_json[ "resultdata"]["LIST"]["NORMAL"]["error"]["ct"] != 0: raise common.UserException( manuparam_edit_resp.text.encode().decode('unicode-escape')) # 正常終了 ret_status = 200 # 戻り値をそのまま返却 return jsonify({"result": ret_status}), ret_status except common.UserException as e: return common.server_error_to_message(e, app_name + exec_stat, error_detail) except Exception as e: return common.server_error_to_message(e, app_name + exec_stat, error_detail)
def get_cd_operations(workspace_id): """get cd-operations list Returns: Response: HTTP Respose """ app_name = "ワークスペース情報:" exec_stat = "Operation情報取得" error_detail = "" try: globals.logger.debug('#' * 50) globals.logger.debug('CALL {}'.format( inspect.currentframe().f_code.co_name)) globals.logger.debug('#' * 50) # ワークスペースアクセス情報取得 access_info = api_access_info.get_access_info(workspace_id) # namespaceの取得 namespace = common.get_namespace_name(workspace_id) ita_restapi_endpoint = "http://{}.{}.svc:{}/default/menu/07_rest_api_ver1.php".format( EPOCH_ITA_HOST, namespace, EPOCH_ITA_PORT) ita_user = access_info['ITA_USER'] ita_pass = access_info['ITA_PASSWORD'] # HTTPヘッダの生成 filter_headers = { 'host': EPOCH_ITA_HOST + ':' + EPOCH_ITA_PORT, 'Content-Type': 'application/json', 'Authorization': base64.b64encode( (ita_user + ':' + ita_pass).encode()), 'X-Command': 'FILTER', } # # オペレーションの取得 # opelist_resp = requests.post(ita_restapi_endpoint + '?no=' + ite_menu_operation, headers=filter_headers) globals.logger.debug('---- Operation ----') globals.logger.debug(opelist_resp.text) if common.is_json_format(opelist_resp.text): opelist_json = json.loads(opelist_resp.text) else: error_detail = "Operation情報取得失敗" raise common.UserException(error_detail) rows = opelist_json # 正常終了 ret_status = 200 # 戻り値をそのまま返却 return jsonify({"result": ret_status, "rows": rows}), ret_status except common.UserException as e: return common.server_error_to_message(e, app_name + exec_stat, error_detail) except Exception as e: return common.server_error_to_message(e, app_name + exec_stat, error_detail)
def cd_result_logs_get(workspace_id, conductor_id): """CD実行結果ログ取得 cd result logs get Args: workspace_id (int): workspace id conductor_id (str): conductor id Returns: Response: HTTP Respose """ try: globals.logger.debug('#' * 50) globals.logger.debug( 'CALL {} workspace_id[{}] conductor_id[{}]'.format( inspect.currentframe().f_code.co_name, workspace_id, conductor_id)) globals.logger.debug('#' * 50) # ワークスペースアクセス情報取得 get workspace access info. access_info = api_access_info.get_access_info(workspace_id) # namespaceの取得 get namespace namespace = common.get_namespace_name(workspace_id) ita_restapi_endpoint = "http://{}.{}.svc:{}/default/menu/07_rest_api_ver1.php".format( EPOCH_ITA_HOST, namespace, EPOCH_ITA_PORT) ita_user = access_info['ITA_USER'] ita_pass = access_info['ITA_PASSWORD'] # POST送信する post sender # HTTPヘッダの生成 HTTP header generation filter_headers = { 'host': EPOCH_ITA_HOST + ':' + EPOCH_ITA_PORT, 'Content-Type': 'application/json', 'Authorization': base64.b64encode( (ita_user + ':' + ita_pass).encode()), 'X-Command': 'DOWNLOAD', } # 実行パラメータ設定 parameter setting data = {"CONDUCTOR_INSTANCE_NO": [str(int(conductor_id) + 1)]} # json文字列に変換 json convert json_data = json.dumps(data) # リクエスト送信 request post exec_response = requests.post(ita_restapi_endpoint + '?no=' + ite_menu_conductor_download, headers=filter_headers, data=json_data) if exec_response.status_code != 200: globals.logger.error(exec_response.text) error_detail = multi_lang.get_text( "EP034-0007", "実行結果ログ取得の呼び出しに失敗しました status:{0}".format( exec_response.status_code), exec_response.status_code) raise common.UserException(error_detail) # globals.logger.debug("-------------------------") # globals.logger.debug("response:") # globals.logger.debug(exec_response) # globals.logger.debug("response_text:") # globals.logger.debug(exec_response.text) # globals.logger.debug("-------------------------") # 戻り値用の器設定 Device setting for return value rows = { "manifest_embedding": {}, "manifest_commit_push": {}, } # 実行してすぐは読込ができない状況があるのでチェックする # Check because there are situations where it cannot be read immediately after execution. if not common.is_json_format(exec_response.text): # 実行途中の正常終了として返す Returns as normal termination during execution globals.logger.debug("not start!") ret_status = 404 # 実行前なので404を返却 Return 404 because it is before execution return jsonify({"result": ret_status, "rows": rows}), ret_status resp_data = json.loads(exec_response.text) # globals.logger.debug(f"resp_data:{resp_data}") if resp_data["status"] != "SUCCEED": globals.logger.error("no={} status:{}".format( ite_menu_conductor_exec, resp_data["status"])) error_detail = multi_lang.get_text( "EP034-0008", "実行結果ログ取得の呼び出しに失敗しました ita-status:{0} resultdata:{1}".format( eresp_data["status"], eresp_data["resultdata"]), eresp_data["status"], eresp_data["resultdata"]) raise common.UserException(error_detail) body_cnt = 0 for body in resp_data["resultdata"]["CONTENTS"]["BODY"]: # download_input_key = body["INPUT_DATA"] # base64_input_log_zip = resp_data["resultdata"]["CONTENTS"]["DOWNLOAD"][download_key] download_result_key = body["RESULT_DATA"] base64_result_log_zip = resp_data["resultdata"]["CONTENTS"][ "DOWNLOAD_FILE"][body_cnt][download_result_key] # globals.logger.debug(f"base64_result_log_zip:{base64_result_log_zip}") binary_result_log_zip = base64.b64decode( base64_result_log_zip.encode()) with tempfile.TemporaryDirectory() as tempdir: # zipファイル生成 Zip file generation path_zip_file = '{}/{}'.format(tempdir, download_result_key) # globals.logger.debug(f"path_zip_file:{path_zip_file}") with open(path_zip_file, mode='bw') as fp: fp.write(binary_result_log_zip) # zipファイルに含まれているファイル名の取得 # Get the file name contained in the zip file with zipfile.ZipFile(path_zip_file) as log_zip: # zipファイルに含まれているファイルのリストを返す # Returns a list of files contained in the zip file for f in log_zip.namelist(): # ファイル名の末尾が"/"じゃない内容を解凍 # Unzip the contents that do not end with "/" in the file name if f[-1:] != "/": # globals.logger.debug(f"f:{f}") # zipファイルに含まれているファイルを取り出す # Extract the files contained in the zip file log_zip.extract(f, tempdir) # 解凍した各フォルダの内容を再度解凍 # Unzip the contents of each unzipped folder again files = glob.glob(tempdir + "/**/*.zip") for file in files: # globals.logger.debug(f"file:{file}") with zipfile.ZipFile(file) as log_zip: for f in log_zip.namelist(): if f == "exec.log": # 実行ログ Execution log # globals.logger.debug(f"f:{f}") with log_zip.open(f) as f_log: exec_logs = f_log.read() # globals.logger.debug(f"logs:{exec_logs}") elif f == "error.log": # エラーログ error log # globals.logger.debug(f"f:{f}") with log_zip.open(f) as f_log: error_logs = f_log.read() # globals.logger.debug(f"logs:{error_logs}") sub_dir_name = os.path.basename(os.path.dirname(file)) # globals.logger.debug(f"sub_dir_name:{sub_dir_name}") if sub_dir_name == "0000000001": # フォルダの1番目は、Manifest埋め込みのログ # The first folder is the Manifest embedded log rows["manifest_embedding"] = { "execute_logs": exec_logs.decode('utf-8'), "error_logs": error_logs.decode('utf-8'), } elif sub_dir_name == "0000000002": # フォルダの2番目は、Manifest Commit&Pushのログ # The second folder is the Manifest Commit & Push log rows["manifest_commit_push"] = { "execute_logs": exec_logs.decode('utf-8'), "error_logs": error_logs.decode('utf-8'), } body_cnt += 1 # globals.logger.debug(f"rows:{rows}") # 正常終了 normal end ret_status = 200 # 戻り値をそのまま返却 return jsonify({"result": ret_status, "rows": rows}), ret_status except common.UserException as e: return common.server_error(e) except Exception as e: return common.server_error(e)
def cd_result_get(workspace_id, conductor_id): """CD実行結果取得 cd result get Args: workspace_id (int): workspace id conductor_id (str): conductor id Returns: Response: HTTP Respose """ try: globals.logger.debug('#' * 50) globals.logger.debug( 'CALL {} workspace_id[{}] conductor_id[{}]'.format( inspect.currentframe().f_code.co_name, workspace_id, conductor_id)) globals.logger.debug('#' * 50) # ワークスペースアクセス情報取得 get workspace access info. access_info = api_access_info.get_access_info(workspace_id) # namespaceの取得 get namespace namespace = common.get_namespace_name(workspace_id) ita_restapi_endpoint = "http://{}.{}.svc:{}/default/menu/07_rest_api_ver1.php".format( EPOCH_ITA_HOST, namespace, EPOCH_ITA_PORT) ita_user = access_info['ITA_USER'] ita_pass = access_info['ITA_PASSWORD'] # POST送信する post sender # HTTPヘッダの生成 HTTP header generation filter_headers = { 'host': EPOCH_ITA_HOST + ':' + EPOCH_ITA_PORT, 'Content-Type': 'application/json', 'Authorization': base64.b64encode( (ita_user + ':' + ita_pass).encode()), 'X-Command': 'INFO', } # 実行パラメータ設定 parameter setting data = {"CONDUCTOR_INSTANCE_ID": conductor_id} # json文字列に変換 json convert json_data = json.dumps(data) # リクエスト送信 request post exec_response = requests.post(ita_restapi_endpoint + '?no=' + ite_menu_conductor_result, headers=filter_headers, data=json_data) if exec_response.status_code != 200: globals.logger.error(exec_response.text) error_detail = multi_lang.get_text( "EP034-0005", "実行結果取得の呼び出しに失敗しました status:{0}".format( exec_response.status_code), exec_response.status_code) raise common.UserException(error_detail) # globals.logger.debug("-------------------------") # globals.logger.debug("response:") # globals.logger.debug(exec_response.text) # globals.logger.debug("-------------------------") resp_data = json.loads(exec_response.text) if resp_data["status"] != "SUCCEED": globals.logger.error("no={} status:{}".format( ite_menu_conductor_exec, resp_data["status"])) error_detail = multi_lang.get_text( "EP034-0006", "実行結果取得の呼び出しに失敗しました ita-status:{0} resultdata:{1}".format( eresp_data["status"], eresp_data["resultdata"]), eresp_data["status"], eresp_data["resultdata"]) raise common.UserException(error_detail) rows = resp_data # 正常終了 normal end ret_status = 200 # 戻り値をそのまま返却 return to it-automation results return jsonify({"result": ret_status, "rows": rows}), ret_status except common.UserException as e: return common.server_error(e) except Exception as e: return common.server_error(e)
def cd_execute_cancel(workspace_id, conductor_id): """CD実行取り消し cd execute cancel Args: workspace_id (int): workspace id conductor_id (str): conductor id Returns: Response: HTTP Respose """ try: globals.logger.debug('#' * 50) globals.logger.debug('CALL {}'.format( inspect.currentframe().f_code.co_name)) globals.logger.debug('#' * 50) # ワークスペースアクセス情報取得 get workspace access info. access_info = api_access_info.get_access_info(workspace_id) # namespaceの取得 get namespace namespace = common.get_namespace_name(workspace_id) ita_restapi_endpoint = "http://{}.{}.svc:{}/default/menu/07_rest_api_ver1.php".format( EPOCH_ITA_HOST, namespace, EPOCH_ITA_PORT) ita_user = access_info['ITA_USER'] ita_pass = access_info['ITA_PASSWORD'] # HTTPヘッダの生成 HTTP header generation filter_headers = { 'host': EPOCH_ITA_HOST + ':' + EPOCH_ITA_PORT, 'Content-Type': 'application/json', 'Authorization': base64.b64encode( (ita_user + ':' + ita_pass).encode()), 'X-Command': 'CANCEL', } # 実行パラメータ設定 Execution parameter setting data = {"CONDUCTOR_INSTANCE_ID": conductor_id} # json文字列に変換("utf-8"形式に自動エンコードされる) Convert to json string (automatically encoded in "utf-8" format) json_data = json.dumps(data) # リクエスト送信 response = requests.post(ita_restapi_endpoint + '?no=' + ite_menu_conductor_cancel, headers=filter_headers, data=json_data) globals.logger.debug(response.text) resp_data = json.loads(response.text) # Even if it fails, "status" is returned as "SUCCEED", so the error is judged by "RESULTCODE" (success: 0, failure: 2). # 失敗時も"status"は"SUCCEED"で返ってくるため、"RESULTCODE"(成功:0, 失敗:2)でエラーを判断 if resp_data["resultdata"]["RESULTCODE"] == "002": globals.logger.error("no={} status:{}".format( ite_menu_conductor_cancel, resp_data["status"])) error_detail = multi_lang.get_text( "EP034-0004", "CD予約取り消しの呼び出しに失敗しました ita-status:{0} resultdata:{1}".format( resp_data["status"], resp_data["resultdata"]), resp_data["status"], resp_data["resultdata"]) raise common.UserException(error_detail) # 正常終了 Successful completion ret_status = 200 # 戻り値をそのまま返却 Return the return value as it is return jsonify({"result": ret_status}), ret_status except common.UserException as e: return common.server_error(e) except Exception as e: return common.server_error(e)
def cd_execute(workspace_id): """cd execute Returns: Response: HTTP Respose """ app_name = "ワークスペース情報:" exec_stat = "CD実行" error_detail = "" try: globals.logger.debug('#' * 50) globals.logger.debug('CALL {}'.format( inspect.currentframe().f_code.co_name)) globals.logger.debug('#' * 50) # パラメータ情報(JSON形式) prameter save payload = request.json.copy() # ワークスペースアクセス情報取得 get workspace access info. access_info = api_access_info.get_access_info(workspace_id) # namespaceの取得 get namespace namespace = common.get_namespace_name(workspace_id) ita_restapi_endpoint = "http://{}.{}.svc:{}/default/menu/07_rest_api_ver1.php".format( EPOCH_ITA_HOST, namespace, EPOCH_ITA_PORT) ita_user = access_info['ITA_USER'] ita_pass = access_info['ITA_PASSWORD'] operation_id = payload["operation_id"] conductor_class_no = payload["conductor_class_no"] preserve_datetime = payload["preserve_datetime"] # POST送信する # HTTPヘッダの生成 filter_headers = { 'host': EPOCH_ITA_HOST + ':' + EPOCH_ITA_PORT, 'Content-Type': 'application/json', 'Authorization': base64.b64encode( (ita_user + ':' + ita_pass).encode()), 'X-Command': 'EXECUTE', } # 実行パラメータ設定 data = { "CONDUCTOR_CLASS_NO": conductor_class_no, "OPERATION_ID": operation_id, "PRESERVE_DATETIME": preserve_datetime, } # json文字列に変換("utf-8"形式に自動エンコードされる) json_data = json.dumps(data) # リクエスト送信 exec_response = requests.post(ita_restapi_endpoint + '?no=' + ite_menu_conductor_exec, headers=filter_headers, data=json_data) if exec_response.status_code != 200: globals.logger.error(exec_response.text) error_detail = multi_lang.get_text( "EP034-0001", "CD実行の呼び出しに失敗しました status:{0}".format( exec_response.status_code), exec_response.status_code) raise common.UserException(error_detail) globals.logger.debug("-------------------------") globals.logger.debug("response:") globals.logger.debug(exec_response.text) globals.logger.debug("-------------------------") resp_data = json.loads(exec_response.text) if resp_data["status"] != "SUCCEED": globals.logger.error("no={} status:{}".format( ite_menu_conductor_exec, resp_data["status"])) error_detail = multi_lang.get_text( "EP034-0002", "CD実行の呼び出しに失敗しました ita-status:{0} resultdata:{1}".format( resp_data["status"], resp_data["resultdata"]), resp_data["status"], resp_data["resultdata"]) raise common.UserException(error_detail) if resp_data["status"] != "SUCCEED": globals.logger.error("no={} status:{}".format( ite_menu_conductor_exec, resp_data["status"])) error_detail = multi_lang.get_text( "EP034-0002", "CD実行の呼び出しに失敗しました ita-status:{0} resultdata:{1}".format( resp_data["status"], resp_data["resultdata"]), resp_data["status"], resp_data["resultdata"]) raise common.UserException(error_detail) # 戻り値が"000"以外の場合は、エラーを返す # If the return value is other than "000", an error is returned. if resp_data["resultdata"]["RESULTCODE"] != "000": globals.logger.error("RESULTCODE error: resultdata:{}".format( resp_data["resultdata"])) error_detail = resp_data["resultdata"]["RESULTINFO"] raise common.UserException(error_detail) # 作業IDを退避 cd_result_id = resp_data["resultdata"]["CONDUCTOR_INSTANCE_ID"] # 正常終了 ret_status = 200 # 戻り値をそのまま返却 return jsonify({ "result": ret_status, "cd_result_id": cd_result_id }), ret_status except common.UserException as e: return common.server_error_to_message(e, app_name + exec_stat, error_detail) except Exception as e: return common.server_error_to_message(e, app_name + exec_stat, error_detail)