def updatedu(self, du, full_sync_flag="false", directory_to_import_from=None): """Start Tool Update """ dudata = du.get("du_data") builds = dudata.get('build') deployment_field = dudata.get('deployment_field') localDu = self.deploymentunitDB.GetDeploymentUnitByName(dudata["name"]) du_id = str(localDu.get("_id")) try: if dudata.get("operation").lower() == "update": DuHelperService.add_update_du(dudata, du_id, self.logo_path, directory_to_import_from, self.full_logo_path, False) # HANLDE BUILD if builds is not None and len(builds) > 0: builds_handled = [ ] # WE need to deactivate all other builds builds_not_to_process = [] for build in builds: if build.get("to_process", "true").lower() == "true": if build.get("to_process"): build.pop("to_process") if build.get("to_process_reason"): build.pop("to_process_reason") BuildHelperService.add_update_build( build, du_id, join( directory_to_import_from, os.path.join("artifacts", dudata["repository_to_use"]))) builds_handled.append(build["build_number"]) else: builds_not_to_process.append(build["build_number"]) # SUPPOSE THE ACCOUNT SENDS 2 BUILDS THAT ARE ACTIVE THEY WILL BE HANDLED # BUT ALL OTHER BUILDS SHOULD BE MADE INACTIVE IN LOCAL for build in self.buildsDB.get_all_builds(du_id): if build["build_number"] not in builds_handled and build[ "build_number"] not in builds_not_to_process: build_id = build.get("_id") build["_id"] = {} build["_id"]["oid"] = str(build_id) build["status"] = "0" self.buildsDB.update_build(build) # HANLDE DEPLOYMENT FIELDS if deployment_field is not None \ and len(deployment_field) > 0: HelperServices.add_update_deployment_fields( deployment_field['fields'], du_id) return { "result": "success", "message": dudata["name"] + " was updated" } except Exception as e_value: # catch *all* exceptions traceback.print_exc() return {"result": "failed", "message": str(e_value)}
def generate_new_state(details): # details has "deployment_field":{"hi":"hellow"} which is handled below state_id = None deployment_fields_id = None dep_fields = None try: state={"type":"dustate","build_id":details.get("build_id"),\ "name":details.get("name"),"parent_entity_id":details.get("parent_entity_id")\ ,"approval_status":"Created"}# DATA VERIFIED IN add_update_state if details.get("deployment_field") and len( details.get("deployment_field").get("fields", [])) > 0: dep_fields = details.get("deployment_field") else: dep_fields = deploymentFieldsDB.GetDeploymentFields( details.get("parent_entity_id")) state_id = add_update_state(state, None) if dep_fields: deployment_fields_id = HelperServices.add_update_deployment_fields( dep_fields.get("fields"), state_id) return state_id except Exception as e: # catch *all* exceptions traceback.print_exc() if state_id is not None: delete_state(state_id) if deployment_fields_id is not None: deploymentFieldsDB.DeleteDeploymentFields(deployment_fields_id) raise e
def add_update_state(self, state, full_sync_flag="false", directory_to_import_from=None): """Start State Addition/Update""" deployment_fields_data = None try: state_data = state.get("state_data") StateHelperService.convert_parent_names_to_ids(state_data, True) # IF DU STATE if state_data.get("build_id"): build = self.buildsDB.get_build_by_number( state_data.get("parent_entity_id"), state_data.get("build_id"), False) if build: state_data["build_id"] = str(build.get("_id")) else: raise Exception ("Build with number: "+str(state_data.get("build_id"))\ +" and parent_entity_id: "+str(state_data.get("parent_entity_id")) +" was not found in DB") if state_data.get("deployment_field"): deployment_fields_data = state_data.get("deployment_field") # IF DU PACKAGE STATE if state_data.get("states"): StateHelperService.convert_parent_to_states(state_data) existing_state=self.statedb.get_state_by_parent_entity_id_name(state_data.get("name"),\ state_data.get("parent_entity_id"), False) if existing_state: StateHelperService.add_update_state( state_data, str(existing_state.get("_id"))) state_id = str(existing_state.get("_id")) else: state_id = str( StateHelperService.add_update_state(state_data, None)) if deployment_fields_data: HelperServices.add_update_deployment_fields( deployment_fields_data.get("fields"), state_id) return { "result": "success", "message": state_data["name"] + " was handled" } except Exception as e_value: # catch *all* exceptions traceback.print_exc() return {"result": "failed", "message": str(e_value)}
def update_state(): deployment_fields_data = None keys_allowed_to_update = ["deployment_field", "approval_status", "_id"] data = request.json validate(data, 'UpdateStateData', relative_path + '/swgger/StateAPI/updateState.yml') state = request.get_json() for key in state.keys(): if key not in keys_allowed_to_update: state.pop(key) if (state.get("deployment_field") is not None): deployment_fields_data = state.get("deployment_field") StateHelperService.add_update_state(state, state.get("_id").get("oid")) if (deployment_fields_data is not None): HelperServices.add_update_deployment_fields( deployment_fields_data.get("fields"), str(state.get("_id").get("oid"))) return jsonify( json.loads( dumps({ "result": "success", "message": "State was updated successfully" }))), 200
def adddu(self, du, full_sync_flag="false", directory_to_import_from=None): """Start Tool Addition""" # MAINTAINING ARRAY TO MEMORISE INSERTED IDS inserted_du_list = [] inserted_build_list = [] inserted_deployment_fields_list = [] dudata = du.get("du_data") deployment_field = dudata.get("deployment_field") builds = dudata.get("build") DuHelperService.check_if_du_exists(dudata) try: du_inserted = DuHelperService.add_update_du( dudata, None, self.logo_path, directory_to_import_from, self.full_logo_path, False) inserted_du_list.append(du_inserted) if deployment_field is not None and len(deployment_field) > 0: inserted_deployment_fields_list.append( HelperServices.add_update_deployment_fields( deployment_field["fields"], du_inserted)) # preparing Build data if builds is not None and len(builds) > 0: for build in builds: if build.get("to_process", "true").lower() == "true": inserted_build_list.append( BuildHelperService.add_update_build( build, du_inserted, join( directory_to_import_from, os.path.join( "artifacts", dudata["repository_to_use"])))) return { "result": "success", "message": dudata["name"] + " was inserted" } except Exception as e_value: # catch *all* exceptions traceback.print_exc() for rec in inserted_deployment_fields_list: self.deploymentFieldsDB.DeleteDeploymentFields(rec) for rec in inserted_du_list: self.deploymentunitDB.DeleteDeploymentUnit(rec) for rec in inserted_build_list: self.buildsDB.delete_build(rec) return {"result": "failed", "message": str(e_value)}
def add_state(): try: state_id = None deployment_fields_data = None deployment_fields_id = None states_to_rollback = [] parent_entity_id_list = [] data = request.json validate(data, 'State', relative_path + '/swgger/StateAPI/addStates.yml') state_data = request.get_json() StateHelperService.check_state_mandate_fields(state_data) if state_data.get("deployment_field") and len( state_data.get("deployment_field").get("fields", [])) > 0: deployment_fields_data = state_data.get("deployment_field") else: deployment_fields_data = deploymentFieldsdb.GetDeploymentFields( state_data.get("parent_entity_id")) for idx, item in enumerate(state_data.get("states", [])): if isinstance(item, dict): build_details = buildDB.get_build_by_id( item.get("build_id"), False) if build_details: state_data["states"][idx]=StateHelperService.generate_new_state({"build_id":str(build_details.get("_id")),\ "name":item.get("name"),"parent_entity_id":build_details.get("parent_entity_id"),"deployment_field":item.get("deployment_field")}) parent_entity_id_list.append( build_details.get("parent_entity_id")) states_to_rollback.append(state_data["states"][idx]) else: raise Exception("Invalid build_id:" + str(item.get("build_id"))) elif statedb.get_state_by_id(item): parent_entity_id_list.append( statedb.get_state_by_id(item).get("parent_entity_id")) else: raise Exception("Invalid build_id/state_id: " + item) duplicates = [ x for x in parent_entity_id_list if parent_entity_id_list.count(x) > 1 ] if len(duplicates) > 0: raise Exception( "More than one state request was found for parent_entity_id:" + ",".join(list(set(duplicates)))) state_id = StateHelperService.add_update_state(state_data, None) if deployment_fields_data: deployment_fields_id = HelperServices.add_update_deployment_fields( deployment_fields_data.get("fields"), str(state_id)) return jsonify( json.loads( dumps({ "result": "success", "message": "State created successfully", "data": { "_id": state_id } }))), 200 except Exception as e: # catch *all* exceptions for new_state_id in states_to_rollback: StateHelperService.delete_state(new_state_id) if state_id is not None: StateHelperService.delete_state(state_id) if deployment_fields_id is not None: deploymentFieldsdb.DeleteDeploymentFields(deployment_fields_id) raise e
def updatetool(self, tool, full_sync_flag="false", directory_to_import_from=None): """Start Tool Update """ tooldata = tool.get("tool_data") localTool = self.toolDB.get_tool_by_name(tooldata["name"]) ToolHelperService.check_if_tool_data_is_valid(tooldata, localTool) tool_id = str(localTool.get("_id")) try: if tooldata.get("operation").lower() == "update": ToolHelperService.add_update_tool(tooldata, tool_id, self.logo_path, directory_to_import_from, self.full_logo_path) versions = tooldata.get("versions") for record in versions: if record.get("operation") not in [ "delete", "update", "insert" ]: continue VersionsData = record localVersion = self.versionsDB.get_version_by_tool_id_name_and_number( tool_id, VersionsData["version_name"], VersionsData["version_number"]) if localVersion: version_id = str(localVersion["_id"]) if record.get("operation").lower( ) == "delete" and full_sync_flag == "true": SyncHelperService.delete_version_and_related_builds( version_id) else: # HANDLE VERSION # WE SEE THAT THE VERSION HAS TO BE BE UPDATED OR INSERTED # IF ITS A EXISTING VERSION WE WILL ALREADY HAVE VERSION_ID if record.get("operation").lower() == "update": ToolHelperService.add_update_version( VersionsData, tool_id, version_id, False) # IF ITS A NEW VERSION if record.get("operation").lower() == "insert": version_id = ToolHelperService.add_update_version( VersionsData, tool_id, None, False) # HANLDE BUILD if VersionsData.get('build') is not None and len( VersionsData.get('build')) > 0: builds_handled = [ ] # WE need to deactivate all other builds for build in VersionsData.get('build'): BuildHelperService.add_update_build( build, version_id, join( directory_to_import_from, os.path.join( "artifacts", VersionsData["repository_to_use"]))) builds_handled.append(build["build_number"]) # SUPPOSE THE ACCOUNT SENDS 2 BUILDS THAT ARE ACTIVE THEY WILL BE HANDLED # BUT ALL OTHER BUILDS SHOULD BE MADE INACTIVE IN LOCAL for build in self.buildsDB.get_all_builds(version_id): if build["build_number"] not in builds_handled: build_id = build.get("_id") build["_id"] = {} build["_id"]["oid"] = str(build_id) build["status"] = "0" self.buildsDB.update_build(build) # HANLDE DOCUMENT if VersionsData.get('document') is not None \ and len(VersionsData.get('document')) > 0: HelperServices.add_update_documents( VersionsData['document']['documents'], version_id) # HANLDE DEPLOYMENT FIELDS if VersionsData.get('deployment_field') is not None \ and len(VersionsData.get('deployment_field')) > 0: HelperServices.add_update_deployment_fields( VersionsData['deployment_field']['fields'], version_id) # HANLDE MEDIA FILES if VersionsData.get('media_file') is not None \ and len(VersionsData.get('media_file')) > 0: HelperServices.add_update_media_files( VersionsData['media_file']['media_files'], version_id, directory_to_import_from, self.full_media_files_path, self.media_files_path) return { "result": "success", "message": tooldata["name"] + " was updated" } except Exception as e_value: # catch *all* exceptions traceback.print_exc() return {"result": "failed", "message": str(e_value)}
def addtool(self, tool, full_sync_flag="false", directory_to_import_from=None): """Start Tool Addition""" # MAINTAINING ARRAY TO MEMORISE INSERTED IDS inserted_tools_list = [] inserted_build_list = [] inserted_versions_list = [] inserted_deployment_fields_list = [] inserted_media_files_list = [] inserted_documents_list = [] tooldata = tool.get("tool_data") ToolHelperService.check_if_tool_exists(tooldata) try: tooldata = tool.get("tool_data") tool_inserted = ToolHelperService.add_update_tool( tool.get("tool_data"), None, self.logo_path, directory_to_import_from, self.full_logo_path) inserted_tools_list.append(tool_inserted) versions = tooldata.get("versions") if versions is None: raise Exception("versions is missing from tool_data") for VersionsData in versions: Versionresult = ToolHelperService.add_update_version( VersionsData, tool_inserted, None, False) inserted_versions_list.append(Versionresult) # preparing version data # preparing DeploymentFields data if VersionsData.get("deployment_field") is not None and len( VersionsData.get("deployment_field")) > 0: inserted_deployment_fields_list.append( HelperServices.add_update_deployment_fields( VersionsData.get("deployment_field")["fields"], Versionresult)) if VersionsData.get('media_file') is not None \ and len(VersionsData.get('media_file')) > 0: inserted_media_files_list.append( HelperServices.add_update_media_files( VersionsData.get('media_file')['media_files'], Versionresult, directory_to_import_from, self.full_media_files_path, self.media_files_path)) # preparing Document data if VersionsData.get("document") is not None and len( VersionsData.get("document")) > 0: inserted_documents_list.append( HelperServices.add_update_documents( VersionsData.get("document")["documents"], Versionresult)) # preparing Build data if VersionsData.get("build") is not None and len( VersionsData.get("build")): for build in VersionsData.get("build"): inserted_build_list.append( BuildHelperService.add_update_build( build, Versionresult, join( directory_to_import_from, os.path.join( "artifacts", VersionsData["repository_to_use"])))) return { "result": "success", "message": tooldata["name"] + " was inserted" } except Exception as e_value: # catch *all* exceptions traceback.print_exc() for rec in inserted_deployment_fields_list: self.deploymentFieldsDB.DeleteDeploymentFields(rec) for rec in inserted_media_files_list: self.mediaFilesDB.delete_media_file(rec) for rec in inserted_documents_list: self.documentsDB.DeleteDocuments(rec) for rec in inserted_versions_list: self.versionsDB.delete_version(rec) for rec in inserted_tools_list: self.toolDB.delete_tool(rec) for rec in inserted_build_list: self.buildsDB.delete_build(rec) return {"result": "failed", "message": str(e_value)}
def add_update_du(deploymentUnitData, deploymentUnitId=None, logo_path=None, directory_to_import_from=None, full_logo_path=None,handle_dependencies=True): """Add update DeploymentUnit data""" # Mandatory Keys keys_to_validate=["name"] for key in keys_to_validate: if not deploymentUnitData.get(key): raise Exception ("mandatory key: "+ key+" is missing in DU details") buildData = None deploymentFeildData = None result = None transaction_type = None if not deploymentUnitId: transaction_type = "new" # preparing deploymentUnit data if deploymentUnitData.get("build") is not None: buildData = deploymentUnitData.get("build") if deploymentUnitData.get("deployment_field") is not None: deploymentFeildData = deploymentUnitData.get("deployment_field")[ "fields"] #VALIDATION if deploymentUnitId: if not deploymentunitdb.GetDeploymentUnitById(deploymentUnitId): raise ValueError("No DU with this _id is found in database") else: #TRY TO SEARCH WITH NAME existing_du=deploymentunitdb.GetDeploymentUnitByName(deploymentUnitData.get("name")) if existing_du: deploymentUnitId=str(existing_du["_id"]) #VALIDATIONS FOR NEW DU if not deploymentUnitId: keys_to_validate=["type"] for key in keys_to_validate: if not deploymentUnitData.get(key): raise Exception ("mandatory key: "+ key+" is missing in du details") add_missing_attributes_for_du(deploymentUnitData) if deploymentUnitData.get("name"): HelperServices.validate_name(deploymentUnitData.get("name"),"deploymentunit name") if deploymentUnitData.get("flexible_attributes"): FlexibleAttributesHelper.validate_entity_value("DeploymentUnit", deploymentUnitData.get("flexible_attributes")) deploymentUnitData = verify_du_and_du_set_data(deploymentUnitData) deploymentUnitData = HelperServices.add_update_logo( deploymentUnitData, logo_path, full_logo_path, directory_to_import_from) #TRIM NOT REQUIRED DATA deploymentUnitData = trim_du_duset_data(deploymentUnitData) if deploymentUnitData.get("repository_to_use"): if not RepositoryHelperService.check_if_repo_exists_by_name(deploymentUnitData.get("repository_to_use")): raise Exception(deploymentUnitData.get("repository_to_use") + ": No such repository exists") # ADD UPDATE DATA if deploymentUnitId: deploymentUnitData["_id"] = {"oid": deploymentUnitId} result = deploymentunitdb.UpdateDeploymentUnit(deploymentUnitData) else: deploymentUnitData["status"] = "1" result = deploymentunitdb.AddDeploymentUnit(deploymentUnitData) deploymentUnitId = result if result is None: raise Exception( "Unable to create/update deploymentUnit " + deploymentUnitData["name"]) else: # RELOAD TEAM PERMISSIONS teamService.generate_details() if handle_dependencies: dep_fields_result = None try: if buildData is not None: for build in buildData: build["parent_entity_type"] = "du" BuildHelperService.add_update_build(build, deploymentUnitId, None) # preparing deployment_field data # if duDatabackup.get("deployment_field") is not None: if deploymentFeildData is not None: dep_fields_result = HelperServices.add_update_deployment_fields( deploymentFeildData, deploymentUnitId) #SET DU AS UPDATED if either of dep fields or build is updated if transaction_type <> "new" and str(result) == "0" \ and str(dep_fields_result) == "1": result = "1" except Exception as e: if transaction_type == "new": deploymentunitdb.DeleteDeploymentUnit(deploymentUnitId) buildsDB.delete_build_by_parent_entitity_id(deploymentUnitId) deploymentFieldsDB.delete_dep_field_by_parent_entity_id(deploymentUnitId) return e return str(result)