def delete_manifest_template(workspace_id, file_id): """manifest テンプレート削除 manifest template delete Args: workspace_id (int): workspace ID file_id (int): file ID 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) # ヘッダ情報 headers = { 'Content-Type': 'application/json', } # DELETE送信(作成) resourceProtocol = os.environ['EPOCH_RS_WORKSPACE_PROTOCOL'] resourceHost = os.environ['EPOCH_RS_WORKSPACE_HOST'] resourcePort = os.environ['EPOCH_RS_WORKSPACE_PORT'] apiurl = "{}://{}:{}/workspace/{}/manifests/{}".format( resourceProtocol, resourceHost, resourcePort, workspace_id, file_id) response = requests.delete(apiurl, headers=headers) if response.status_code == 200 and common.is_json_format( response.text): # 200(正常)かつ、レスポンスデータがJSON形式の場合は、後続の処理を実行 pass elif response.status_code == 404: # manifestデータが見つからない(404)場合 error_detail = 'manifest template data not found' raise common.UserException(error_detail) elif response.status_code != 200: # 200(正常), 404(not found) 以外の応答の場合 error_detail = 'CALL responseAPI Error' raise common.UserException(error_detail) # ITA呼び出し globals.logger.debug("CALL ita_registration Start") response = ita_registration(workspace_id) globals.logger.debug("CALL ita_registration End response:") globals.logger.debug(response) # 正常終了 normal return code ret_status = 200 return jsonify({"result": ret_status, "rows": response}), 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_manifest_template_list(workspace_id): """manifest テンプレート登録 manifest template registration Args: workspace_id (int): workspace ID 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) resourceProtocol = os.environ['EPOCH_RS_WORKSPACE_PROTOCOL'] resourceHost = os.environ['EPOCH_RS_WORKSPACE_HOST'] resourcePort = os.environ['EPOCH_RS_WORKSPACE_PORT'] apiurl = "{}://{}:{}/workspace/{}/manifests".format( resourceProtocol, resourceHost, resourcePort, workspace_id) # ヘッダ情報 headers = { 'Content-Type': 'application/json', } # GET送信(作成) response = requests.get(apiurl, headers=headers) globals.logger.debug("get_manifests return --------------------") globals.logger.debug(json.loads(response.text)) globals.logger.debug("--------------------") if response.status_code == 200 and common.is_json_format( response.text): # 200(正常)かつ、レスポンスデータがJSON形式の場合は、後続の処理を実行 pass elif response.status_code == 404: error_detail = 'manifest template data not found' raise common.UserException(error_detail) elif response.status_code != 200: # 200(正常), 404(not found) 以外の応答の場合 error_detail = 'CALL responseAPI Error' raise common.UserException(error_detail) ret_manifests = json.loads(response.text) rows = ret_manifests["rows"] # 正常終了 normal return code 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 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 merge_workspace_members(workspace_id): """ワークスペース該当メンバー登録 workspace member registration Request: json { "rows": [ { "user_id": "", "roles": [ { "kind": "", } ] }, ], "role_update_at": "", } Returns: Response: HTTP Respose """ app_name = multi_lang.get_text("EP020-0003", "ワークスペース情報:") exec_stat = multi_lang.get_text("EP020-0005", "メンバー登録") error_detail = "" return_code = 500 try: globals.logger.debug('#' * 50) globals.logger.debug('CALL {}'.format(inspect.currentframe().f_code.co_name)) globals.logger.debug('#' * 50) # 引数はJSON形式 Arguments are in JSON format req_json = request.json.copy() # ヘッダ情報 header info. post_headers = { 'Content-Type': 'application/json', } # workspace get api_url = "{}://{}:{}/workspace/{}".format(os.environ['EPOCH_RS_WORKSPACE_PROTOCOL'], os.environ['EPOCH_RS_WORKSPACE_HOST'], os.environ['EPOCH_RS_WORKSPACE_PORT'], workspace_id) response = requests.get(api_url, headers=post_headers) if response.status_code == 200 and common.is_json_format(response.text): # 取得したワークスペース情報を退避 Save the acquired workspace information ret = json.loads(response.text) ws_row = ret["rows"][0] elif response.status_code == 404: return_code = 400 error_detail = multi_lang.get_text("EP000-0022", "更新対象(workspace)がありません", "workspace") # 情報取得できない場合はエラー Error if information cannot be obtained raise common.UserException("{} not found workspace !".format(inspect.currentframe().f_code.co_name)) else: if response.status_code == 500 and common.is_json_format(response.text): # 戻り値がJsonの場合は、値を取得 If the return value is Json, get the value ret = json.loads(response.text) # 詳細エラーがある場合は詳細を設定 Set details if there are detailed errors if ret["errorDetail"] is not None: error_detail = ret["errorDetail"] raise common.UserException("{} Error get workspace db status:{}".format(inspect.currentframe().f_code.co_name, response.status_code)) globals.logger.debug("Exclusive check") globals.logger.debug("db[{}] update[{}]".format(str(ws_row["role_update_at"]), req_json["role_update_at"])) # 排他チェック:ロール更新日時が一致しているかチェックする # Exclusive check: Check if the role update datetime match if str(ws_row["role_update_at"]) != req_json["role_update_at"]: return_code = 400 error_detail = multi_lang.get_text("EP000-0023", "対象の情報(workspace)が他で更新されたため、更新できません\n画面更新後、再度情報を入力・選択して実行してください", "workspace") raise common.UserException("{} update exclusive check error".format(inspect.currentframe().f_code.co_name)) ret_status = response.status_code # ログインユーザーの情報取得 Get login user information user_id = common.get_current_user(request.headers) api_url = "{}://{}:{}/{}/user/{}".format(os.environ['EPOCH_EPAI_API_PROTOCOL'], os.environ['EPOCH_EPAI_API_HOST'], os.environ['EPOCH_EPAI_API_PORT'], os.environ["EPOCH_EPAI_REALM_NAME"], user_id ) # # get users - ユーザー取得 # response = requests.get(api_url) if response.status_code != 200 and response.status_code != 404: error_detail = multi_lang.get_text("EP020-0008", "ユーザー情報の取得に失敗しました") raise common.UserException("{} Error user get status:{}".format(inspect.currentframe().f_code.co_name, response.status_code)) users = json.loads(response.text) # globals.logger.debug(f"users:{users}") # 取得したユーザーのロールを取得 # Get the role of the acquired user api_url = "{}://{}:{}/{}/user/{}/roles/epoch-system".format(os.environ['EPOCH_EPAI_API_PROTOCOL'], os.environ['EPOCH_EPAI_API_HOST'], os.environ['EPOCH_EPAI_API_PORT'], os.environ["EPOCH_EPAI_REALM_NAME"], user_id ) # # get user role - ユーザーロール情報取得 # response = requests.get(api_url) if response.status_code != 200: error_detail = multi_lang.get_text("EP020-0009", "ユーザーロール情報の取得に失敗しました") globals.logger.debug(error_detail) raise common.UserException("{} Error user role get status:{}".format(inspect.currentframe().f_code.co_name, response.status_code)) ret_roles = json.loads(response.text) # globals.logger.debug(f"roles:{ret_roles}") bool_owener_role = False # オーナーロール設定可能権限かどうかチェックする # Check if the owner role can be set for get_role in ret_roles["rows"]: # globals.logger.debug('role:{}'.format(get_role["name"])) # ロールがあればチェックOK # Check OK if there is a roll if get_role["name"] == const.ROLE_WS_ROLE_OWNER_ROLE_SETTING[0].format(workspace_id): bool_owener_role = True break # 権限「オーナー変更」保有しているユーザーを抽出する # Extract users who have the authority "change owner" owener_users = api_service_common.get_workspace_members_by_role(workspace_id, const.ROLE_WS_ROLE_OWNER_ROLE_SETTING[0].format(workspace_id)) # globals.logger.debug(f"owener_users{owener_users}") owener_users_id_array = [] # チェック用に配列化 # Arranged for checking for user in owener_users: owener_users_id_array.append(user["user_id"]) # オーナーロール変更以外がオーナーロールありに変更したかチェック # Check if other than the owner role change has changed to have an owner role if not bool_owener_role: bool_auth_error = False for row in req_json["rows"]: globals.logger.debug(f"req_row:{row}") bool_owner_update = False for role in row["roles"]: # オーナー更新ありのフラグをONにする # Turn on the flag with owner update globals.logger.debug("owener_check({}):({})".format(common.get_role_name(role["kind"]), const.ROLE_WS_OWNER[0])) if common.get_role_name(role["kind"]) == const.ROLE_WS_OWNER[0]: bool_owner_update = True # オーナーロールの変更がある場合、権限がないのでエラーとする # If there is a change in the owner role, it will be an error because you do not have the authority. if common.get_role_name(role["kind"]) == const.ROLE_WS_OWNER[0] and \ row["user_id"] not in owener_users_id_array: bool_auth_error = True break # オーナーありのユーザーからオーナー権限を削除した際は権限がないのでエラーとする # If you delete the owner authority from a user who has an owner, you will get an error because you do not have the authority. if not bool_owner_update and row["user_id"] in owener_users_id_array: bool_auth_error = True break # 権限エラーの場合は、処理を抜ける # In case of permission error, exit the process if bool_auth_error: break # 権限エラー # Permission error if bool_auth_error: # ログイン者が唯一のオーナーの時は退去できない # Can't move out when the login person is the only owner error_detail = multi_lang.get_text("EP020-0024", "オーナー権限を変更することはできません") raise common.AuthException(error_detail) else: # オーナーロール変更ありのユーザーが、オーナーロール変更した際、オーナーロールのユーザーが残るかどうか # Check if a user with an owner role change has a user change with an owner role change owner_add_cnt = 0 owner_del_cnt = 0 for row in req_json["rows"]: bool_owner_update = False for role in row["roles"]: # オーナー更新ありのフラグをONにする # Turn on the flag with owner update if common.get_role_name(role["kind"]) == const.ROLE_WS_OWNER[0]: bool_owner_update = True break # オーナーありのユーザーからオーナー権限を削除した件数をカウント # Count the number of cases where the owner authority is deleted from the owner-owned user if not bool_owner_update and row["user_id"] in owener_users_id_array: owner_del_cnt += 1 # 新規オーナーかチェックする # Check if you are a new owner elif bool_owner_update and row["user_id"] not in owener_users_id_array: owner_add_cnt += 1 # オーナーが0人となる場合はエラーとする # If the number of owners is 0, an error will occur. if (len(owener_users) - owner_del_cnt) == 0 and owner_add_cnt == 0: # ログイン者が唯一のオーナーの時は退去できない # Can't move out when the login person is the only owner error_detail = multi_lang.get_text("EP020-0025", "最低1人以上のオーナーは必要です") raise common.AuthException(error_detail) for row in req_json["rows"]: # 登録前にすべてのroleを削除する # Delete all roles before registration roles = const.ALL_ROLES # # ロールの制御のurl - role control url # api_url = "{}://{}:{}/{}/user/{}/roles/epoch-system".format(os.environ['EPOCH_EPAI_API_PROTOCOL'], os.environ['EPOCH_EPAI_API_HOST'], os.environ['EPOCH_EPAI_API_PORT'], os.environ["EPOCH_EPAI_REALM_NAME"], row["user_id"], ) del_roles = [] for role in roles: del_role = { "name": role.format(workspace_id), } del_roles.append(del_role) post_data = { "roles" : del_roles } # # delete workspace role - ワークスペース ロールの削除 # response = requests.delete(api_url, headers=post_headers, data=json.dumps(post_data)) if response.status_code != 200: error_detail = multi_lang.get_text("EP020-0006", "ロールの削除に失敗しました") raise common.UserException("{} Error user role delete status:{}".format(inspect.currentframe().f_code.co_name, response.status_code)) # 登録するroleの情報を編集 # Edit the information of the role to be registered add_roles = [] for role in row["roles"]: add_role = { "name": common.get_role_name(role["kind"]).format(workspace_id), "enabled": True, } add_roles.append(add_role) post_data = { "roles" : add_roles } # # append workspace role - ワークスペース ロールの付与 # response = requests.post(api_url, headers=post_headers, data=json.dumps(post_data)) if response.status_code != 200: error_detail = multi_lang.get_text("EP020-0007", "ロールの登録に失敗しました") globals.logger.debug(error_detail) raise common.UserException("{} Error user role add status:{}".format(inspect.currentframe().f_code.co_name, response.status_code)) # # logs output - ログ出力 # post_data = { "action" : "role update", "role_update_user_id" : row["user_id"], "role_update_user_roles" : add_roles, } api_url = "{}://{}:{}/workspace/{}/member/{}/logs/{}".format(os.environ['EPOCH_RS_LOGS_PROTOCOL'], os.environ['EPOCH_RS_LOGS_HOST'], os.environ['EPOCH_RS_LOGS_PORT'], workspace_id, users["info"]["username"], const.LOG_KIND_UPDATE ) response = requests.post(api_url, headers=post_headers, data=json.dumps(post_data)) if response.status_code != 200: error_detail = multi_lang.get_text("EP020-0023", "ログ出力に失敗しました") globals.logger.debug(error_detail) raise common.UserException("{} Error log output status:{}".format(inspect.currentframe().f_code.co_name, response.status_code)) # # workspace info. Roll update date update - ワークスペース情報 ロール更新日の更新 # post_data = { "role_update_at" : "", } globals.logger.debug("role datetime update Start") api_url = "{}://{}:{}/workspace/{}".format(os.environ['EPOCH_RS_WORKSPACE_PROTOCOL'], os.environ['EPOCH_RS_WORKSPACE_HOST'], os.environ['EPOCH_RS_WORKSPACE_PORT'], workspace_id) response = requests.patch(api_url, headers=post_headers, data=json.dumps(post_data)) if response.status_code != 200: error_detail = multi_lang.get_text("EP000-0024", "対象の情報({})を更新できませんでした", "workspace") globals.logger.debug(error_detail) raise common.UserException("{} Error workspace-db update status:{}".format(inspect.currentframe().f_code.co_name, response.status_code)) globals.logger.debug("role datetime update Succeed!") return jsonify({"result": "200"}), 200 except common.AuthException as e: return jsonify({"result": 400, "errorDetail": error_detail}), 400 except common.UserException as e: return common.user_error_to_message(e, app_name + exec_stat, error_detail, return_code) 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)