def delete_template(self, template_id): self.log.debug("delete template [%d]" % template_id) try: template = self.db.get_object(Template, template_id) if template is None: return ok("already removed") # user can only delete the template which created by himself except super admin if g.user.id != template.creator_id and not self.user_manager.is_super_admin( g.user): return forbidden() if len( self.db.find_all_objects_by(Experiment, template_id=template_id)) > 0: return forbidden("template already in use") # remove template cache and storage self.cache.invalidate(self.__get_template_cache_key(template_id)) self.storage.delete(template.url) # remove record in DB self.db.delete_all_objects_by(HackathonTemplateRel, template_id=template.id) self.db.delete_object(template) return ok("delete template success") except Exception as ex: self.log.error(ex) return internal_server_error("delete template failed")
def getConnectInfo(self): connection_name = request.args.get("name") self.log.debug("Guacamole connecion request, connection name: %s" % connection_name) expr = Experiment.objects(virtual_environments__name=connection_name ).no_dereference().first() if not expr: return not_found("not_found") if expr.user.id != g.user.id: return forbidden("forbidden") remote_paras = expr.virtual_environments.get( name=connection_name).remote_paras # TODO Support DYNAMIC host/port in case of they cannot be determined on provision phase if K8S_UNIT.REMOTE_PARAMETER_HOST_NAME not in remote_paras: # TTT available_public_ips = self.util.safe_get_config( "ukylin.k8s.ips", ["119.3.202.71", "49.4.90.39"]) random_ip = available_public_ips[random.randint( 0, len(available_public_ips) - 1)] remote_paras[K8S_UNIT.REMOTE_PARAMETER_HOST_NAME] = random_ip self.log.debug("get guacamole config by id: %s, paras: %r" % (connection_name, remote_paras)) return remote_paras
def score_team(self, judge, ctx): team = self.__get_team_by_id(ctx.team_id) if not team: return not_found("team not found") if not self.admin_manager.is_hackathon_admin(team.hackathon.id, judge.id): return forbidden() score = filter(lambda x: x.judge.id == judge.id, team.scores) assert len(score) < 2 if score: score = score[0] score.score = ctx.score score.reason = ctx.get("reason") score.update_time = self.util.get_now() else: score = TeamScore( score=ctx.score, judge=judge, reason=ctx.get("reason")) team.scores.append(score) team.save() return self.__response_get_score(judge, team.scores)
def score_team(self, judge, ctx): team = self.__get_team_by_id(ctx.team_id) if not team: return not_found("team not found") if not self.admin_manager.is_hackathon_admin(team.hackathon.id, judge.id): return forbidden() score = [x for x in team.scores if x.judge.id == judge.id] assert len(score) < 2 if score: score = score[0] score.score = ctx.score score.reason = ctx.get("reason") score.update_time = self.util.get_now() else: score = TeamScore(score=ctx.score, judge=judge, reason=ctx.get("reason")) team.scores.append(score) team.save() return self.__response_get_score(judge, team.scores)
def authenticate_and_call(*args, **kwargs): if not user_manager.validate_token(): return unauthorized("login required") if not hack_manager.validate_hackathon_name(): return bad_request("hackathon name invalid") if not admin_manager.validate_admin_privilege_http(): return forbidden("access denied") return func(*args, **kwargs)
def getConnectInfo(self): connection_name = request.args.get("name") expr = Experiment.objects(virtual_environments__name=connection_name).no_dereference().first() if not expr: return not_found("not_found") if expr.user.id != g.user.id: return forbidden("forbidden") ve = expr.virtual_environments.get(name=connection_name) self.log.debug("get guacamole config by id: %s, paras: %r" % (connection_name, ve.remote_paras)) return ve.remote_paras
def delete_template(self, template_id): self.log.debug("delete template [%s]" % template_id) try: template = self.get_template_info_by_id(template_id) if template is None: return ok("already removed") # user can only delete the template which created by himself except super admin if g.user.id != template.creator.id and not g.user.is_super: return forbidden() if Experiment.objects(template=template).count() > 0: return forbidden("template already in use") # remove record in DB # the Hackathon used this template will imply the mongoengine's PULL reverse_delete_rule self.log.debug("delete template {}".format(template.name)) template.delete() return ok("delete template success") except Exception as ex: self.log.error(ex) return internal_server_error("delete template failed")
def delete_template_from_team(self, template_id): """Delete template from current user's team Team should exist and current login user must be the leader """ team = self.__get_valid_team_by_user(g.user.id, g.hackathon.id) if not team: return precondition_failed("you don't join any team so you cannot add teamplate") if team.leader.id != g.user.id: return forbidden("team leader required") else: return self.hackathon_template_manager.delete_template_from_hackathon(template_id)
def getConnectInfo(self): connection_name = request.args.get("name") guacamole_config = self.db.find_first_object_by(VirtualEnvironment, name=connection_name, status=VEStatus.RUNNING, remote_provider=VERemoteProvider.Guacamole) if guacamole_config is None: return not_found("not_found") if guacamole_config.experiment.user_id != g.user.id: return forbidden("forbidden") self.log.debug("get guacamole config by id: %s, paras: %s" % (connection_name, guacamole_config.remote_paras)) return json.loads(guacamole_config.remote_paras)
def update_hackathon_organizer(self, hackathon, body): organizer = self.db.get_object(HackathonOrganizer, body["id"]) if not organizer: return not_found() if organizer.hackathon_id != hackathon.id: return forbidden() organizer.name = body.get("name", organizer.name) organizer.description = body.get("description", organizer.description) organizer.homepage = body.get("homepage", organizer.homepage) organizer.logo = body.get("logo", organizer.logo) organizer.update_time = self.util.get_now() self.db.commit() return organizer.dic()
def delete_template(self, template_id): self.log.debug("delete template [%d]" % template_id) try: template = self.db.get_object(Template, template_id) if template is None: return ok("already removed") # user can only delete the template which created by himself except super admin if g.user.id != template.creator_id and not self.user_manager.is_super_admin(g.user): return forbidden() if len(self.db.find_all_objects_by(Experiment, template_id=template_id)) > 0: return forbidden("template already in use") # remove template cache and storage self.cache.invalidate(self.__get_template_cache_key(template_id)) self.storage.delete(template.url) # remove record in DB self.db.delete_all_objects_by(HackathonTemplateRel, template_id=template.id) self.db.delete_object(template) return ok("delete template success") except Exception as ex: self.log.error(ex) return internal_server_error("delete template failed")
def delete_template(self, template_id): self.log.debug("delete template [%s]" % template_id) try: template = self.get_template_info_by_id(template_id) if template is None: return ok("already removed") # user can only delete the template which created by himself except super admin if g.user.id != template.creator.id and not g.user.is_super: return forbidden() if Experiment.objects(template=template).count() > 0: return forbidden("template already in use") # remove template cache and storage self.cache.invalidate(self.__get_template_cache_key(template_id)) self.storage.delete(template.url) # remove record in DB # the Hackathon used this template will imply the mongoengine's PULL reverse_delete_rule template.delete() return ok("delete template success") except Exception as ex: self.log.error(ex) return internal_server_error("delete template failed")
def getConnectInfo(self): connection_name = request.args.get("name") guacamole_config = self.db.find_first_object_by( VirtualEnvironment, name=connection_name, status=VEStatus.RUNNING, remote_provider=VERemoteProvider.Guacamole) if guacamole_config is None: return not_found("not_found") if guacamole_config.experiment.user_id != g.user.id: return forbidden("forbidden") self.log.debug("get guacamole config by id: %s, paras: %s" % (connection_name, guacamole_config.remote_paras)) return json.loads(guacamole_config.remote_paras)
def add_template_for_team(self, args): """Add template to team of the current user by template name template_id must be included in args. Current login user must have a team and HE must be its leader """ if "template_id" not in args: return bad_request("template id invalid") team = self.__get_valid_team_by_user(g.user.id, g.hackathon.id) if not team: return precondition_failed("you don't join any team so you cannot add teamplate") if team.leader.id != g.user.id: return forbidden("team leader required") else: return self.hackathon_template_manager.add_template_to_hackathon(args["template_id"])
def update_hackathon_organizer(self, hackathon, body): organizer = self.db.get_object(HackathonOrganizer, body["id"]) if not organizer: return not_found() if organizer.hackathon_id != hackathon.id: return forbidden() organizer.name = body.get("name", organizer.name) organizer.organization_type = body.get("organization_type", organizer.organization_type) organizer.description = body.get("description", organizer.description) organizer.homepage = body.get("homepage", organizer.homepage) organizer.logo = body.get("logo", organizer.logo) organizer.update_time = self.util.get_now() self.db.commit() return organizer.dic()
def score_team(self, judge, ctx): team = self.__get_team_by_id(ctx.team_id) if not team: return not_found("team not found") if not self.admin_manager.is_hackathon_admin(team.hackathon_id, judge.id): return forbidden() score = self.db.find_first_object_by(TeamScore, team_id=team.id, judge_id=judge.id) if score: score.score = ctx.score score.reason = ctx.get("reason") score.update_time = self.util.get_now() self.db.commit() else: score = TeamScore(score=ctx.score, team_id=team.id, judge_id=judge.id, reason=ctx.get("reason")) self.db.add_object(score) return self.get_score(judge, team.id)
def getConnectInfo(self): """ Get guacamole connection information from DB table virtual_environment according to request arg field of "name" :return: value of column remote_paras in DB table virtual_environment if no record is found in DB, return not_found method if record exists but user_id of experiment does not equal global user id, return forbidden method :rtype: json form """ connection_name = request.args.get("name") guacamole_config = self.db.find_first_object_by(VirtualEnvironment, name=connection_name, status=VEStatus.RUNNING, remote_provider=VERemoteProvider.Guacamole) if guacamole_config is None: return not_found("not_found") if guacamole_config.experiment.user_id != g.user.id: return forbidden("forbidden") self.log.debug("get guacamole config by id: %s, paras: %s" % (connection_name, guacamole_config.remote_paras)) return json.loads(guacamole_config.remote_paras)
def update_hackathon_award(self, hackathon, body): award = self.db.get_object(Award, body.id) if not award: return not_found("award not found") if award.hackathon.name != hackathon.name: return forbidden() level = award.level if body.get("level"): level = int(body.level) if level > 10: level = 10 award.name = body.get("name", award.name) award.level = body.get("level", level) award.quota = body.get("quota", award.quota) award.award_url = body.get("award_url", award.award_url) award.description = body.get("description", award.description) award.update_time = self.util.get_now() self.db.commit() return award.dic()
def getConnectInfo(self): connection_name = request.args.get("name") self.log.debug("Guacamole connecion request, connection name: %s" % connection_name) expr = Experiment.objects(virtual_environments__name=connection_name).no_dereference().first() if not expr: return not_found("not_found") if expr.user.id != g.user.id: return forbidden("forbidden") remote_paras = expr.virtual_environments.get(name=connection_name).remote_paras # TODO Support DYNAMIC host/port in case of they cannot be determined on provision phase if K8S_UNIT.REMOTE_PARAMETER_HOST_NAME not in remote_paras: # TTT available_public_ips = self.util.safe_get_config("ukylin.k8s.ips", ["119.3.202.71", "49.4.90.39" ]) random_ip = available_public_ips[random.randint(0, len(available_public_ips) - 1)] remote_paras[K8S_UNIT.REMOTE_PARAMETER_HOST_NAME] = random_ip self.log.debug("get guacamole config by id: %s, paras: %r" % (connection_name, remote_paras)) return remote_paras