def logs_insert(workspace_id=None, username=None, log_kind=None): """ログ出力 Log output Args: workspace_id (int): workspace id username (str): username log_kind (str): log_kind Returns: response: HTTP Respose """ try: globals.logger.debug( 'CALL {}:from[{}] workspace_id[{}] member[{}] log_kind[{}]'.format( inspect.currentframe().f_code.co_name, request.method, workspace_id, username, log_kind)) with dbconnector() as db, dbcursor(db) as cursor: # Requestからcontentsを設定 Set contents from Request contents = request.json.copy() # logs insert実行 log_id = da_logs.insert_logs(cursor, workspace_id, username, log_kind, contents) globals.logger.debug('insert log_id:{}'.format(log_id)) return jsonify({"result": "200"}), 200 except Exception as e: return common.serverError( e, "{} error".format(inspect.currentframe().f_code.co_name))
def get_workspace(workspace_id): """ワークスペース詳細 Args: workspace_id (int): ワークスペースID Returns: response: HTTP Respose """ globals.logger.debug('CALL get_workspace:{}'.format(workspace_id)) try: with dbconnector() as db, dbcursor(db) as cursor: # workspace情報データ取得 fetch_rows = da_workspace.select_workspace_id(cursor, workspace_id) if len(fetch_rows) > 0: # Response用のjsonに変換 response_rows = convert_workspace_response(fetch_rows) return jsonify({ "result": "200", "rows": response_rows, "time": str(datetime.now(globals.TZ)) }), 200 else: # 0件のときは404応答 return jsonify({"result": "404"}), 404 except Exception as e: return common.serverError(e)
def post_listener(workspace_id): """TEKTON Listener 転送処理 Args: workspace_id (int): ワークスペースID Returns: response: 応答結果 """ try: globals.logger.debug( "post_listener call: worksapce_id:{}".format(workspace_id)) # TEKTONのイベントリスナーの転送先 listener_url = "http://el-event-listener.{}.svc:8080/".format( tekton_pipeline_namespace(workspace_id)) # TEKTONのイベントリスナーへ転送 response = requests.post(listener_url, headers=request.headers, data=request.data) # TEKTONのリスナーの応答をそのまま返す return Response(response.text, headers=dict(response.raw.headers), status=response.status_code) except requests.exceptions.RequestException as e: return Response("", status=404) except Exception as e: return common.serverError(e)
def get_tekton_taskrun_logs(workspace_id, taskrun_name): """TEKTON taskログ取得 Args: workspace_id (int): ワークスペースID taskrun_name (string): taskrun名 Returns: Response: HTTP Respose """ globals.logger.debug('CALL get_tekton_taskrun:{},{}'.format( workspace_id, taskrun_name)) try: result_kubectl = subprocess.check_output([ 'kubectl', 'tkn', 'taskrun', 'logs', taskrun_name, '-n', tekton_pipeline_namespace(workspace_id), ], stderr=subprocess.STDOUT) globals.logger.debug('COMMAAND SUCCEED: kubectl tkn taskrun logs') # 正常応答 return jsonify({ "result": "200", "log": result_kubectl.decode('utf-8') }), 200 except Exception as e: return common.serverError(e)
def workspace_status_delete(workspace_id, id): """ワークスペース状態情報削除 worksapce status info. delete Args: workspace_id (int): ワークスペースID id (int): ワークスペース状態情報ID Returns: response: HTTP Respose """ globals.logger.debug( "CALL workspace_status_delete:workspace_id:{}".format(workspace_id)) try: with dbconnector() as db, dbcursor(db) as cursor: # ワークスペース状態情報 delete実行 worksapce status info. delete upd_cnt = da_workspace_status.delete_workspace_status( cursor, workspace_id) globals.logger.debug("workspace_status:ret:{}".format(upd_cnt)) if upd_cnt == 0: # データがないときは404応答 db.rollback() return jsonify({"result": "404"}), 404 return jsonify({"result": "200"}), 200 except Exception as e: return common.serverError(e)
def call_workspace_logs(workspace_id, log_kind): """workspace情報毎のログ出力処理 Args: workspace_id (int): workspace id log_kind (str): log_kind Returns: response: HTTP Respose """ try: globals.logger.debug('=' * 50) globals.logger.debug( 'CALL {}:from[{}] workspace_id[{}] log_kind[{}]'.format( inspect.currentframe().f_code.co_name, request.method, workspace_id, log_kind)) globals.logger.debug('=' * 50) # log出力 log output return logs_insert(workspace_id=workspace_id, log_kind=log_kind) except Exception as e: return common.serverError( e, "{} error".format(inspect.currentframe().f_code.co_name))
def manifest_file_get(workspace_id, file_id): """マニフェストテンプレートファイル取得 Args: workspace_id (int): ワークスペースID file_id (int): ファイルID Returns: response: HTTP Respose """ globals.logger.debug( "CALL manifest_file_get: [ workspace_id: {}, manifest_id: {} ]".format( workspace_id, file_id)) try: with dbconnector() as db, dbcursor(db) as cursor: # manifest情報の取得 fetch_rows = da_manifest.select_manifest_id( cursor, workspace_id, file_id) if len(fetch_rows) == 0: # データがないときは404応答 db.rollback() return jsonify({"result": "404"}), 404 # Response用のjsonに変換 response_rows = fetch_rows return jsonify({"result": "200", "rows": response_rows}) except Exception as e: return common.serverError(e)
def get_tekton_task(workspace_id): """CI結果情報取得 Returns: response: HTTP Respose """ try: globals.logger.debug('CALL {}:from[{}] workspace_id[{}]'.format( inspect.currentframe().f_code.co_name, request.method, workspace_id)) with dbconnector() as db, dbcursor(db) as cursor: # CI result information select execution - CI結果情報 select実行 fetch_rows = da_ci_result.select_tekton_pipeline_task( cursor, workspace_id) # Successful completion - 正常終了 res_status = 200 return jsonify({"result": res_status, "rows": fetch_rows}), res_status except Exception as e: return common.serverError( e, "tekton_pipeline_task db registration error")
def manifest_file_delete(workspace_id, manifest_id): """マニフェストテンプレートファイル削除 Args: workspace_id (int): ワークスペースID file_id (int): ファイルID Returns: response: HTTP Respose """ globals.logger.debug( "CALL manifest_file_delete:workspace_id:{}, manifest_id:{}".format( workspace_id, manifest_id)) try: with dbconnector() as db, dbcursor(db) as cursor: # manifests情報 delete実行 upd_cnt = da_manifest.delete_manifest(cursor, workspace_id, manifest_id) globals.logger.debug("delete_manifest:ret:{}".format(upd_cnt)) if upd_cnt == 0: # データがないときは404応答 db.rollback() return jsonify({"result": "404"}), 404 return jsonify({"result": "200"}) except Exception as e: return common.serverError(e)
def call_workspace_member_logs(workspace_id, username, log_kind): """workspace情報のメンバーごとのログ出力呼び出し Log output call for each member of workspace information Args: workspace_id (int): workspace id username (str): username log_kind (str): log_kind Returns: response: HTTP Respose """ try: globals.logger.debug('=' * 50) globals.logger.debug( 'CALL {}:from[{}] workspace_id[{}] member[{}] log_kind[{}]'.format( inspect.currentframe().f_code.co_name, request.method, workspace_id, username, log_kind)) globals.logger.debug('=' * 50) # log出力 log output return logs_insert(workspace_id=workspace_id, username=username, log_kind=log_kind) except Exception as e: return common.serverError( e, "{} error".format(inspect.currentframe().f_code.co_name))
def get_workspace_before(workspace_id): """更新前のワークスペース履歴 - Workspace history before update Args: workspace_id (int): workspace id Returns: response: HTTP Respose """ globals.logger.debug('CALL get_workspace_before:{}'.format(workspace_id)) try: with dbconnector() as db, dbcursor(db) as cursor: # get workspace history - workspace履歴取得 fetch_rows = da_workspace.select_history(cursor, workspace_id) if len(fetch_rows) > 0: # Convert to json for Response - Response用のjsonに変換 response_rows = convert_workspace_response(fetch_rows) return jsonify({ "result": "200", "rows": response_rows, "time": str(datetime.now(globals.TZ)) }), 200 else: # 404 response when 0 - 0件のときは404応答 return jsonify({"result": "404"}), 404 except Exception as e: return common.serverError(e)
def get_organization(organization_id): """オーガナイゼーション詳細 Args: organization_id (int): organization ID Returns: response: HTTP Respose """ globals.logger.debug('CALL get_organization:{}'.format(organization_id)) try: with dbconnector() as db, dbcursor(db) as cursor: # organization情報データ取得 fetch_rows = da_organization.select_organization_id( cursor, organization_id) if len(fetch_rows) > 0: # Response用のjsonに変換 response_rows = fetch_rows return jsonify({ "result": "200", "rows": response_rows, "time": str(datetime.now(globals.TZ)) }), 200 else: # 0件のときは404応答 return jsonify({"result": "404"}), 404 except Exception as e: return common.serverError(e, "organization db get row error")
def patch_tekton_task(workspace_id, task_id): """CI結果情報更新 Args: workspace_id (int): ワークスペース ID task_id (int): task ID Returns: response: HTTP Respose """ globals.logger.debug('CALL patch_tekton_task:{}_{}'.format( workspace_id, task_id)) try: # Requestからinfo項目を生成する info = request.json with dbconnector() as db, dbcursor(db) as cursor: # CI結果情報更新 upd_cnt = da_ci_result.update_tekton_pipeline_task( cursor, info, task_id) if upd_cnt == 0: # データがないときは404応答 db.rollback() return jsonify({"result": "404"}), 404 # 正常終了 return jsonify({"result": "200"}), 200 except Exception as e: return common.serverError(e, "tekton_pipeline_task db update error")
def workspace_status_get(workspace_id): """ワークスペース状態情報取得 worksapce status info. get Args: workspace_id (int): ワークスペースID Returns: response: HTTP Respose """ globals.logger.debug("CALL workspace_status_get:{}".format(workspace_id)) try: # 登録内容は基本的に、引数のJsonの値を使用する(追加項目があればここで記載) with dbconnector() as db, dbcursor(db) as cursor: # ワークスペース状態情報 select実行 worksapce status info. select fetch_rows = da_workspace_status.select_workspace_status( cursor, workspace_id) if len(fetch_rows) == 0: # データがないときは404応答 db.rollback() return jsonify({"result": "404"}), 404 # Response用のjsonに変換[変換が必要な場合は関数化する] response_row = json.loads(fetch_rows[0]["info"]) return jsonify(response_row), 200 except Exception as e: return common.serverError(e)
def list_workspace(): """ワークスペース一覧取得 Returns: response: HTTP Respose """ globals.logger.debug('CALL list_workspace') try: # Requestからorganization_id項目を抽出する organization_id = request.json['common']['organization_id'] with dbconnector() as db, dbcursor(db) as cursor: # select実行 fetch_rows = da_workspace.select_workspace(cursor, organization_id) # Response用のjsonに変換 response_rows = convert_workspace_response(fetch_rows) return jsonify({ "result": "200", "rows": response_rows, "time": str(datetime.now(globals.TZ)) }), 200 except Exception as e: return common.serverError(e)
def workspace_status_update(workspace_id): """ワークスペース状態情報更新 worksapce status info. update Args: workspace_id (int): ワークスペースID Returns: response: HTTP Respose """ globals.logger.debug( "CALL workspace_status_update:{}".format(workspace_id)) try: # 更新対象のJson値をパラメータとして受け取る Receive the Json value to be updated as a parameter info_upadate_colums = request.json with dbconnector() as db, dbcursor(db) as cursor: # ワークスペース状態情報 update実行 worksapce status info. update upd_cnt = da_workspace_status.update_workspace_status( cursor, workspace_id, info_upadate_colums) globals.logger.debug('update workspace_id:{}'.format(workspace_id)) if upd_cnt == 0: # データがないときは404応答 db.rollback() return jsonify({"result": "404"}), 404 return jsonify({"result": "200"}), 200 except Exception as e: return common.serverError(e)
def workspace_status_registration(workspace_id): """ワークスペース状態状態登録 workspace status registration Args: workspace_id (int): ワークスペースID worksapce id Returns: response: HTTP Respose """ globals.logger.debug( "CALL workspace_access_registration:{}".format(workspace_id)) try: # 登録内容は基本的に、引数のJsonの値を使用する(追加項目があればここで記載) info = request.json with dbconnector() as db, dbcursor(db) as cursor: # ワークスペース状態情報 insert実行 worksapce status info. insert da_workspace_status.insert_workspace_status( cursor, workspace_id, info) globals.logger.debug('insert workspace_id:{}'.format(workspace_id)) return jsonify({"result": "200"}), 200 except Exception as e: return common.serverError(e)
def workspace_access_update(workspace_id): """ワークスペースアクセス情報更新 Args: workspace_id (int): ワークスペースID Returns: response: HTTP Respose """ globals.logger.debug( "CALL workspace_access_update:{}".format(workspace_id)) try: # 登録内容は基本的に、引数のJsonの値を使用する(追加項目があればここで記載) info = request.json with dbconnector() as db, dbcursor(db) as cursor: # ワークスペースアクセス情報 update実行 upd_cnt = da_workspace_access.update_workspace_access( cursor, workspace_id, info) globals.logger.debug('update workspace_id:{}'.format(workspace_id)) if upd_cnt == 0: # データがないときは404応答 db.rollback() return jsonify({"result": "404"}), 404 return jsonify({"result": "200"}), 200 except Exception as e: return common.serverError(e)
def update_manifestParameter(workspace_id): """ワークスペース変更 Args: workspace_id (int): ワークスペースID Returns: response: HTTP Respose """ globals.logger.debug( "CALL update_manifestParameter:{}".format(workspace_id)) try: request_json = request.json.copy() update_at = request_json["update_at"] update_at = parser.parse(update_at) # Requestからspecification項目を生成する specification = request_json with dbconnector() as db, dbcursor(db) as cursor: # workspace情報取得 workspaceInfo = da_workspace.select_workspace_id( cursor, workspace_id) # workspace情報のmanifestParameter部を差し替え db_specification = json.loads(workspaceInfo[0]["specification"]) # 登録する配列用のindex i = 0 for db_env in db_specification["ci_config"]["environments"]: for in_env in specification["ci_config"]["environments"]: if db_env["environment_id"] == in_env["environment_id"]: db_specification["ci_config"]["environments"][i][ "manifests"] = in_env["manifests"] break i += 1 # manifestパラメータの項目説明も更新に含める db_specification["parameter-info"] = specification[ "parameter-info"] # workspace情報 update実行 upd_cnt = da_workspace.update_workspace(cursor, db_specification, workspace_id, update_at) if upd_cnt == 0: # データがないときは404応答 db.rollback() return jsonify({"result": "404"}), 404 # Response用のjsonに変換 response_rows = workspaceInfo return jsonify({"result": "200", "rows": response_rows}) except Exception as e: return common.serverError(e)
def put_workspace(workspace_id): """ワークスペース更新 Args: workspace_id (int): ワークスペースID Returns: response: HTTP Respose """ globals.logger.debug("CALL put_workspace:{}".format(workspace_id)) app_name = "ワークスペース情報更新:" exec_stat = "初期化" exec_detail = "" try: request_json = request.json.copy() update_at = request_json["update_at"] update_at = parser.parse(update_at) globals.logger.debug(f"update_at:{update_at}") # Requestからspecification項目を生成する specification = convert_workspace_specification(request_json) exec_stat = "DB接続" with dbconnector() as db, dbcursor(db) as cursor: # workspace情報 update実行 exec_stat = "更新実行" upd_cnt = da_workspace.update_workspace(cursor, specification, workspace_id, update_at) if upd_cnt == 0: # データがないときは404応答 db.rollback() return jsonify({ "result": "404", "errorStatement": app_name + exec_stat, "errorDetail": "更新するデータがありません" }), 404 # workspace履歴追加 exec_stat = "登録実行(履歴)" da_workspace.insert_history(cursor, workspace_id) # workspace情報の再取得 exec_stat = "登録データ確認" fetch_rows = da_workspace.select_workspace_id(cursor, workspace_id) # Response用のjsonに変換 response_rows = fetch_rows return jsonify({"result": "200", "rows": response_rows}) except Exception as e: return common.serverError(e, app_name + exec_stat, exec_detail)
def create_workspace(): """ワークスペース生成 Returns: response: HTTP Respose """ globals.logger.debug('CALL create_workspace') app_name = "ワークスペース情報登録:" exec_stat = "初期化" exec_detail = "" globals.logger.debug(request.json) try: # Requestからorganization_id項目を抽出する organization_id = request.json['common']['organization_id'] # Requestからspecification項目を生成する specification = convert_workspace_specification(request.json) exec_stat = "DB接続" with dbconnector() as db, dbcursor(db) as cursor: # workspace情報 insert実行(戻り値:追加したワークスペースID) exec_stat = "登録実行" workspace_id = da_workspace.insert_workspace( cursor, organization_id, specification) globals.logger.debug('insert workspaced_id:{}'.format( str(workspace_id))) # workspace履歴追加 exec_stat = "登録実行(履歴)" da_workspace.insert_history(cursor, workspace_id) # workspace情報 データ再取得 exec_stat = "登録データ確認" fetch_rows = da_workspace.select_workspace_id(cursor, workspace_id) # Response用のjsonに変換 response_rows = fetch_rows globals.logger.info('CREATED workspace:{} , organization:{}'.format( str(workspace_id), str(organization_id))) return jsonify({"result": "200", "rows": response_rows}) except Exception as e: return common.serverError(e, app_name + exec_stat, exec_detail)
def call_logs(): """ログ出力処理 Returns: response: HTTP Respose """ try: globals.logger.debug('=' * 50) globals.logger.debug('CALL {}:from[{}]'.format( inspect.currentframe().f_code.co_name, request.method)) globals.logger.debug('=' * 50) # log出力 log output return logs_insert() except Exception as e: return common.serverError( e, "{} error".format(inspect.currentframe().f_code.co_name))
def post_tekton_task(workspace_id): """tekton_pipeline_task生成 Args: workspace_id (int): ワークスペースID Returns: Response: HTTP Respose """ globals.logger.debug('CALL post_tekton_task:{}'.format(workspace_id)) try: # image tagの生成 image_tag = '{}.{}'.format( re.sub(r'.*/', '', request.json['git_branch']), datetime.now(globals.TZ).strftime("%Y%m%d-%H%M%S")) with dbconnector() as db, dbcursor(db) as cursor: info = request.json info['workspace_id'] = workspace_id info['status'] = TASK_STATUS_RUNNING info['container_registry_image_tag'] = image_tag # tekton_pipeline_task情報の登録(戻り値:task_id) task_id = da_tekton.insert_tekton_pipeline_task(cursor, info) # こちらはDBCloseに纏めてコミットされることを考慮しているため単一コミットはしない return jsonify({ "result": "200", "param": { "task_id": task_id, "container_registry_image_tag": image_tag } }), 200 except Exception as e: return common.serverError(e)
def update_workspace(workspace_id): """ワークスペース変更 Args: workspace_id (int): ワークスペースID Returns: response: HTTP Respose """ try: if request.method == 'PUT': # ワークスペース更新 put workspace return put_workspace(workspace_id) elif request.method == 'PATCH': # ワークスペース更新パッチ patch update workspace return patch_workspace(workspace_id) else: # Error raise Exception("method not support!") except Exception as e: return common.serverError(e)
def patch_tekton_task(workspace_id, task_id): """tekton_pipeline_task更新 Args: workspace_id (int): ワークスペースID task_id (int): タスクID Returns: Response: HTTP Respose """ globals.logger.debug('CALL patch_tekton_task:{},{}'.format( workspace_id, task_id)) try: # # SQL生成 # sqlstmt = ( 'UPDATE tekton_pipeline_task SET ' # + ','.join(map(lambda x: '{} = %({})s'.format(x,x), request.json.keys())) # + ' WHERE task_id = %(task_id)s' ) # # 更新パラメータ # update_param = request.json.copy() # update_param['task_id'] = task_id # 更新実行 with dbconnector() as db, dbcursor(db) as cursor: upd_cnt = da_tekton.update_tekton_pipeline_task( cursor, request.json, task_id) if upd_cnt == 0: # データがないときは404応答 db.rollback() return jsonify({"result": "404"}), 404 # 正常応答 return jsonify({"result": "200"}), 200 except Exception as e: return common.serverError(e)
def post_tekton_task(workspace_id): """CI結果情報登録 Returns: response: HTTP Respose """ globals.logger.debug('CALL post_tekton_task:{}'.format(workspace_id)) try: # image tagの生成 image_tag = '{}.{}'.format( re.sub(r'.*/', '', request.json['git_branch']), datetime.now(globals.TZ).strftime("%Y%m%d-%H%M%S")) with dbconnector() as db, dbcursor(db) as cursor: # Requestからinfo項目を生成する info = request.json info['workspace_id'] = workspace_id info['status'] = TASK_STATUS_RUNNING info['container_registry_image_tag'] = image_tag # CI結果情報 insert実行(戻り値:追加したtask_id) task_id = da_ci_result.insert_tekton_pipeline_task(cursor, info) globals.logger.debug('insert task_id:{}'.format(str(task_id))) return jsonify({ "result": "200", "param": { "task_id": task_id, "container_registry_image_tag": image_tag } }), 200 except Exception as e: return common.serverError( e, "tekton_pipeline_task db registration error")
def delete_tekton_pipeline(workspace_id): """TEKTON pipeline削除 Args: workspace_id (int): ワークスペースID Returns: Response: HTTP Respose """ globals.logger.debug('CALL delete_tekton_pipeline:{}'.format(workspace_id)) try: # pipelineを削除 delete_workspace_pipeline(workspace_id, YAML_KIND_PIPELINE) # tektonの実行情報を削除(削除しないと止まってしまうため) delete_tekton_pipelinerun(workspace_id) # namespaceを削除 delete_workspace_pipeline(workspace_id, YAML_KIND_NAMESPACE) return jsonify({"result": "200"}), 200 except Exception as e: return common.serverError(e)
def manifest_file_registration(workspace_id): """マニフェストテンプレートファイル登録 Args: workspace_id (int): ワークスペースID Returns: response: HTTP Respose """ globals.logger.debug( "CALL manifest_file_registration:{}".format(workspace_id)) try: # 登録内容は基本的に、引数のJsonの値を使用する(追加項目があればここで記載) specification = request.json response_rows = [] with dbconnector() as db, dbcursor(db) as cursor: for spec in specification['manifests']: # manifest情報 insert実行 manifest_id = da_manifest.insert_manifest( cursor, workspace_id, spec) globals.logger.debug('insert manifest_id:{}'.format( str(manifest_id))) # manifest情報の再取得 fetch_rows = da_manifest.select_manifest_id( cursor, workspace_id, manifest_id) # 戻り値に内容を追加 response_rows.append(fetch_rows[0]) return jsonify({"result": "200", "rows": response_rows}) except Exception as e: return common.serverError(e)
def manifest_file_update(workspace_id, file_id): """マニフェストテンプレートファイル更新 Args: workspace_id (int): ワークスペースID file_id (int): ファイルID Returns: response: HTTP Respose """ globals.logger.debug("CALL manifest_file_update:{}".format(file_id)) try: # 登録内容は基本的に、引数のJsonの値を使用する(追加項目があればここで記載) specification = request.json with dbconnector() as db, dbcursor(db) as cursor: # workspace情報 update実行 upd_cnt = da_manifest.update_manifest(cursor, workspace_id, specification, file_id) if upd_cnt == 0: # データがないときは404応答 db.rollback() return jsonify({"result": "404"}), 404 # manifest情報の再取得 fetch_rows = da_manifest.select_manifest_id( cursor, workspace_id, file_id) # Response用のjsonに変換 response_rows = fetch_rows return jsonify({"result": "200", "rows": response_rows}) except Exception as e: return common.serverError(e)
def list_organization(): """オーガナイゼーション一覧取得 Returns: response: HTTP Respose """ globals.logger.debug('CALL list_organization') try: with dbconnector() as db, dbcursor(db) as cursor: # select実行 fetch_rows = da_organization.select_organization(cursor) # Response用のjsonに変換 response_rows = fetch_rows return jsonify({ "result": "200", "rows": response_rows, "time": str(datetime.now(globals.TZ)) }), 200 except Exception as e: return common.serverError(e, "organization db get list error")