def run(self, complect_revision_id): """ Run deploy process :param complect_revision_id: :return: """ # Check complect_revision_id exists if complect_revision_id is None: raise ApiError(code="COMPLECT_REVISION_ID", message="Complect revision id can't be null") # Query complect revision information, we need Complect deploy_target complect_revision_response = post_request( url=PROCESSOR_HOST, headers=HEADERS, request_data=get_info_request( method="complect_revision.fetch", id=complect_revision_id, )) complect_revision_result = complect_revision_response["result"] if complect_revision_result.get("errors", {}).get("code") \ is not None: raise ApiError( code=complect_revision_result["errors"]["code"], message=complect_revision_result["errors"]["message"], ) # Query complect by complect_id complect_id = complect_revision_result["response"]["complect_id"] with self.create_session() as session: complect_model = session.query(Complect).get(complect_id) deploy_target = complect_model.deploy_target response = post_request( url=PROCESSOR_HOST, headers=HEADERS, request_data=get_create_request( method="task.create", script="scripts.deploy.run", type=self.TASK_TYPE, extra={"complect_revision_id": complect_revision_id}, args={ "complect_revision_id": complect_revision_id, "target": deploy_target, })) result = response["result"] if not result["status"]: raise ApiError(code="UNHANDLED", message=result["errors"]["message"]) return result["response"]
def post_request(url, headers, request_data): """ Выполнение POST запроса """ try: res = requests.post(url, json=request_data, headers=headers) except requests.exceptions.HTTPError as errh: raise ApiError(code="PROCESSOR_HTTP_ERROR", message=errh) except requests.exceptions.ConnectionError as errc: raise ApiError(code="PROCESSOR_CONNECTING_ERROR", message=errc) except requests.exceptions.Timeout as errt: raise ApiError(code="PROCESSOR_TIMEOUT_ERROR", message=errt) except requests.exceptions.RequestException as err: raise ApiError(code="PROCESSOR_REQUEST_ERROR", message=err) return res.json()
def fetch(self, id): with self.create_session() as session: suite_model = self.suite.get(id, session=session) if not suite_model: raise ApiError( code="NOT_EXISTS", message="Can't find suite with id=%r" % id ) return suite_model.to_dict()
def remove(self, user_id, profile_ids): try: ids = [] for profile_id in profile_ids: ids.append((user_id, profile_id)) return self.access_profile_user.remove(ids) except VersionNoObject as e: raise ApiError(code="NOT_EXISTS", message=e.args[0]) from e
def _store(self, title=None, state=None, profile_id=None, id=None, meta=None, is_enabled=None, action=None, session=None): if action == "update": suite_model = self.suite.get(id, session=session) if not suite_model: raise ApiError( code="NOT_EXISTS", message="Can't find suite with id=%r" % id ) else: if not profile_id: raise ApiError( code="MISSING_PROFILE_ID", message="profile_id must be given for creating suite" ) suite_model = Suite( profile_id=profile_id, created=datetime.now(), version=0, ) if title is not None: suite_model.title = title if state is not None: suite_model.state = state if meta is not None: suite_model.meta = meta if is_enabled is not None: suite_model.is_enabled = is_enabled suite_model.version += 1 session.add(suite_model) session.flush() return suite_model.to_dict()
def create(self, complect_id, try_create_revision=False): """ Создание задачи """ if complect_id is None: raise ApiError( code="INVALID_COMPLECT_ID", message="Complect id can't be null" ) with self.create_session() as session: tiny_complect = session.query(Complect).options( load_only("compiler_target") ).get(complect_id) if tiny_complect is None: raise ApiError( code="NOT_EXISTS", message="Can't find complect with id=%r" % complect_id ) response = post_request( url=PROCESSOR_HOST, headers=HEADERS, request_data=get_create_request( method="task.create", script="scripts.processor.run", type="compiler", extra={"complect_id": complect_id}, args={ "complect_id": complect_id, "target": tiny_complect.compiler_target, "try_create_revision": try_create_revision, } ) ) result = response["result"] if result["status"]: return result["response"] raise ApiError(code="UNHANDLED", message=result["errors"]["message"])
def _get_complect_info(self, complect_id): compilers = self._get_compilers() with self.create_session() as session: complect = session.query(Complect).get(complect_id) if complect is None: raise ApiError( code="NOT_EXISTS", message="Complect with id=%r not exists!" % complect_id, ) compiler_info = compilers.get(complect.compiler_target) if compiler_info is None: raise ApiError( code="TARGET_NOT_EXISTS", message="Compiler with id=%r not exists!" % complect.compiler_target, ) return { "debug_target_url": compiler_info["infengine_url"], }
def info(task_id): """ Получение информации о задаче """ result = post_request( url=PROCESSOR_HOST, headers=HEADERS, request_data=get_info_request(method="task.info", task_id=task_id) ) result = result["result"] if result["status"]: return result["response"] raise ApiError(code="NOT_EXISTS", message=result["errors"]["message"])
def remove(self, account_id, complect_ids) -> bool: """ Удаление :param account_id: идентификатор аккаунта :param complect_ids: список идентификаторов профиля """ try: ids = [] for complect_id in complect_ids: ids.append((account_id, complect_id)) return self.access_complect_account.remove(ids) except VersionNoObject as e: raise ApiError(code="NOT_EXISTS", message=e.args[0]) from e
def fetch(self, user_id, profile_ids): with self.create_session() as session: result = [] for profile_id in profile_ids: permission_model = self.access_profile_user.get( (user_id, profile_id), session=session) if not permission_model: raise ApiError( code="NOT_EXISTS", message="Can't find permission with user_id={} " "and profile_id={}".format(user_id, profile_id)) result.append(permission_model.to_dict()) return result
def fetch(self, account_id, complect_id) -> dict: """ Получение записи :param account_id: идентификатор аккаунта :param complect_id: идентификатор комплекта """ with self.create_session() as session: access_complect_account = self.access_complect_account.get( (account_id, complect_id), session=session) if not access_complect_account: raise ApiError( code="NOT_EXISTS", message="Can't find access complect account with " "account_id={} and complect_id={}".format( account_id, complect_id)) return access_complect_account.to_dict()
def fetch(self, account_id, profile_id): """ Получение записи :param account_id: идентификатор аккаунта :param profile_id: идентификатор профиля """ with self.create_session() as session: access_profile_account_model = self.access_account.get( (account_id, profile_id), session=session) if not access_profile_account_model: raise ApiError( code="NOT_EXISTS", message="Can't find access profile account with " "account_id={} and profile_id={}".format( account_id, profile_id)) return access_profile_account_model.to_dict()
def _get_compilers(self): response = post_request(url=PROCESSOR_HOST, headers=HEADERS, request_data=get_info_request( method="cluster.list_compilers", )) try: compilers_list = response["result"]["response"]["items"] except KeyError: logger.exception("Error in gettings compilers") raise ApiError( code="ERROR", message="Error in gettings compilers", ) compilers = {v["code"]: v for v in compilers_list} return compilers
def remove(self, id): try: with self.create_session() as session: query_result = session.query(Template).filter( Template.suite_id.in_( [id] if not isinstance(id, list) else id ) ) query_result.delete(synchronize_session=False) session.commit() return self.suite.remove(id=id, session=session) except VersionNoObject as e: raise ApiError(code="NOT_EXISTS", message=e.args[0]) from e
def list_(cls, extra=None, offset=None, limit=None, order=None): """ Get deploy task's list """ result = post_request(url=PROCESSOR_HOST, headers=HEADERS, request_data=get_info_request(method="task.list", type=cls.TASK_TYPE, extra=extra, offset=offset, limit=limit, order=order)) result = result["result"] if result["status"]: return result["response"] raise ApiError(code="NOT_EXISTS", message=result["errors"]["message"])
def list_of_complect_revisions(self, complect_id=None, offset=None, limit=None, order=None): result = self._get_complect_revision_list( complect_id=complect_id, offset=offset, limit=limit, order=order, ) result = result["result"] if result["status"]: return result["response"] raise ApiError(code="NOT_EXISTS", message=result["errors"]["message"])
def list_(extra=None, offset=None, limit=None, order=None): """ Получение списка задач по типу """ result = post_request( url=PROCESSOR_HOST, headers=HEADERS, request_data=get_info_request( method="task.list", type="compiler", extra=extra, offset=offset, limit=limit, order=order ) ) result = result["result"] if result["status"]: return result["response"] raise ApiError(code="NOT_EXISTS", message=result["errors"]["message"])
def _fetch(self, pk=None, *, session, query_result=False, **kwargs): if not ((pk is not None) ^ bool(kwargs)): raise RuntimeError("Either pk or kwargs must be given!") if pk is not None: if isinstance(self.primary_key, tuple): self._validate_id(pk) kwargs = dict(zip(self.primary_key, pk)) else: kwargs = {self.primary_key: pk} it = session.query(self.entity).filter_by(**kwargs) try: it_first = it.first() except sa.exc.DataError: raise ApiError(message="Invalid object id in request", code="INVALID_PARAMS") return (it_first, it) if query_result else it_first
def _store(self, user_id=None, profile_ids=None, action=None, session=None): for item in profile_ids: if action == "update": permission_model = self.access_profile_user.get( (user_id, item["profile_id"]), session=session) if not permission_model: raise ApiError( code="NOT_EXISTS", message="Can't find permission with user_id={} " "and profile_id={}".format(user_id, item["profile_id"])) permission_model.permissions = self._form_permissions( copy.deepcopy(permission_model.permissions), item["permissions"]) else: permission_model = self.access_profile_user.get( (user_id, item["profile_id"]), session=session) if permission_model: permission_model.permissions = item["permissions"] else: permission_model = AccessProfileUser( user_id=user_id, profile_id=item["profile_id"], permissions=item["permissions"]) session.add(permission_model) return { "user_id": user_id, "profile_ids": profile_ids, }
def __call__(self, *args, **kwargs): call = { "jsonrpc": "2.0", "method": "%s.%s" % (self.component_name, self.method), "params": _make_params(args, kwargs), "id": 1, } data = json.dumps(call).encode("utf-8") try: resp = urllib.request.urlopen(self.url, data=data) except urllib.error.HTTPError as e: raise ProtocolError( "HTTP Error %s.%s %s: %s" % (self.component_name, self.method, str(e), e.read())) from e resp_data = json.load(resp) if not resp_data["result"]["status"]: errors = resp_data["result"]["errors"] raise ApiError(code=errors["code"], message=errors["message"]) return resp_data["result"].get("response", {})
def import_file(profile_id, file_name, data): """ Импортирование шаблонов из файла """ result = post_request( url=PROCESSOR_HOST, headers=None, request_data=get_create_request( method="task.create", script="scripts.suite.import_run", type=TYPE, args={ "profile_id": profile_id, "file_name": file_name, "data": data } ) ) result = result["result"] if result["status"]: return result["response"] raise ApiError(code="UNHANDLED", message=result["errors"]["message"])
def export(self, ids): """ Экспортирование шаблонов """ suites = {} template_rpc = TemplateRpc( tracer=self.tracer, create_session=self.create_session ) for suite_id in ids: suites[suite_id] = {} suite = self.fetch(suite_id) suites[suite_id]["name"] = suite["title"] or suite_id suites[suite_id]["templates"] = [] templates = template_rpc.list(suite_id=suite_id) for template in templates["items"]: suites[suite_id]["templates"].append(template["content"]) result = post_request( url=PROCESSOR_HOST, headers=None, request_data=get_create_request( method="task.create", script="scripts.suite.export_run", type=TYPE, args={"suites": suites} ) ) result = result["result"] if result["status"]: return result["response"] raise ApiError(code="UNHANDLED", message=result["errors"]["message"])