本文整理汇总了Python中hackathon.hmongo.models.Experiment类的典型用法代码示例。如果您正苦于以下问题:Python Experiment类的具体用法?Python Experiment怎么用?Python Experiment使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Experiment类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: pre_allocate_expr
def pre_allocate_expr(self, context):
# TODO: too complex, not check
hackathon_id = context.hackathon_id
self.log.debug("executing pre_allocate_expr for hackathon %s " % hackathon_id)
hackathon = Hackathon.objects(id=hackathon_id).first()
hackathon_templates = hackathon.templates
for template in hackathon_templates:
try:
template = template
pre_num = int(hackathon.config.get(HACKATHON_CONFIG.PRE_ALLOCATE_NUMBER, 1))
query = Q(status=EStatus.STARTING) | Q(status=EStatus.RUNNING)
curr_num = Experiment.objects(user=None, hackathon=hackathon, template=template).filter(query).count()
self.log.debug("pre_alloc_exprs: pre_num is %d, curr_num is %d, remain_num is %d " %
(pre_num, curr_num, pre_num - curr_num))
# TODO Should support VE_PROVIDER.K8S only in future after k8s Template is supported
# if template.provider == VE_PROVIDER.K8S:
if curr_num < pre_num:
start_num = Experiment.objects(user=None, template=template, status=EStatus.STARTING).count()
allowed_currency = int(hackathon.config.get(HACKATHON_CONFIG.PRE_ALLOCATE_CONCURRENT, 1))
if start_num >= allowed_currency:
self.log.debug(
"there are already %d Experiments starting, will check later ... " % allowed_currency)
return
else:
remain_num = min(allowed_currency, pre_num) - start_num
self.log.debug(
"no starting template: %s , remain num is %d ... " % (template.name, remain_num))
self.start_pre_alloc_exprs(None, template.name, hackathon.name, remain_num)
break
except Exception as e:
self.log.error(e)
self.log.error("check default experiment failed")
示例2: get_registration_detail
def get_registration_detail(self, user, hackathon, registration=None):
detail = {
"hackathon": hackathon.dic(),
"user": self.user_manager.user_display_info(user)}
if not registration:
registration = registration or self.get_registration_by_user_and_hackathon(user.id, hackathon.id)
if not registration:
return detail
# "asset" is alreay in registration
detail["registration"] = registration.dic()
# experiment if any
try:
exp = Experiment.objects(
user=user.id,
hackathon=hackathon.id,
status__in=[EStatus.STARTING, EStatus.RUNNING]).first()
if exp:
detail["experiment"] = exp.dic()
except Exception as e:
self.log.error(e)
return detail
示例3: __on_create_success
def __on_create_success(self, context):
self.log.debug("experiment started %s successfully. Setting remote parameters." % context.experiment_id)
# set experiment status
# update the status of virtual environment
expr = Experiment.objects(id=context.experiment_id).first()
virtual_env = expr.virtual_environments[0]
# guacamole parameters
k8s_dict = virtual_env.k8s_resource
# TODO need to choose right port/protocol based on template
vnc_port = k8s_dict['ports']
if len(vnc_port):
gc = {
K8S_UNIT.REMOTE_PARAMETER_NAME: virtual_env.name,
K8S_UNIT.REMOTE_PARAMETER_DISPLAY_NAME: vnc_port[0][K8S_UNIT.PORTS_NAME],
# TODO need to query K8S list all supported IPs and pick one randomly either here or connecting phase
# K8S_UNIT.REMOTE_PARAMETER_HOST_NAME: "49.4.90.39",
K8S_UNIT.REMOTE_PARAMETER_PROTOCOL: "vnc",
K8S_UNIT.REMOTE_PARAMETER_PORT: vnc_port[0][K8S_UNIT.PORTS_PUBLIC_PORT],
# K8S_UNIT.REMOTE_PARAMETER_USER_NAME: "",
# K8S_UNIT.REMOTE_PARAMETER_PASSWORD: "",
}
self.log.debug("expriment %s remote parameters: %s" % (expr.id, str(gc)))
virtual_env.remote_paras = gc
virtual_env.status = VEStatus.RUNNING
expr.status = EStatus.RUNNING
expr.save()
示例4: schedule_create_k8s_service
def schedule_create_k8s_service(self, context):
template_unit = context.template_content.units[0]
experiment = Experiment.objects.get(id=context.experiment_id)
virtual_env = experiment.virtual_environments[0]
k8s_dict = virtual_env.k8s_resource
adapter = self.__get_adapter_from_ctx(K8SServiceAdapter, context)
template_unit.set_ports(k8s_dict['ports'])
labels = {
"template_name": context.template_name,
"hackathon_id": context.hackathon_id,
"experiment_id": context.experiment_id,
}
try:
deploy_name, port = adapter.create_k8s_environment(virtual_env.name, template_unit, labels=labels)
expr = Experiment.objects(id=context.experiment_id).first()
virtual_env = expr.virtual_environments[0]
k8s_dict = virtual_env.k8s_resource
vnc_port = k8s_dict['ports']
vnc_port[0][K8S_UNIT.PORTS_PUBLIC_PORT] = port
expr.save()
# check deployment's status
if self.__wait_for_k8s_status(adapter, virtual_env.name, K8S_DEPLOYMENT_STATUS.AVAILABLE):
self.log.debug("k8s deployment succeeds: %s" % str(context))
self.__on_create_success(context)
return True
else:
self.log.error("k8s deployment fails: %s" % str(context))
self.__on_message("k8s_service_create_failed", context)
except Exception as e:
self.__on_message("k8s_service_create_failed", context)
return False
示例5: _on_virtual_environment_unexpected_error
def _on_virtual_environment_unexpected_error(self, context):
self.log.warn("experiment unexpected error: " + context.experiment_id)
expr = Experiment.objects(id=context.experiment_id).no_dereference() \
.only("status", "virtual_environments").first()
if "virtual_environment_name" in context:
expr.virtual_environments.get(name=context.virtual_environment_name).status = VEStatus.UNEXPECTED_ERROR
expr.save()
示例6: __create_useful_k8s_dict
def __create_useful_k8s_dict(hackathon, experiment, template_unit):
# FIXME K8s dict need a db model, not a dict
_experiments = Experiment.objects(hackathon=hackathon).all()
_virtual_envs = []
for e in _experiments:
_virtual_envs += list(e.virtual_environments)
# TODO Need to check the rules about K8s resource name
_names = [v.name for v in _virtual_envs]
count = 0
name = None
while count < 100:
count += 1
name = "{}-{}-{}".format(template_unit.name, experiment.id, count)
if name not in _names:
break
if count >= 100:
raise RuntimeError("Can't get useful env name.")
# Ensure that the external ports do not conflict
ports = copy.deepcopy(template_unit.get_ports())
return {
"name": "{}".format(name).lower(),
"ports": ports,
}
示例7: __setup_virtual_machine_done
def __setup_virtual_machine_done(self, sctx):
try:
self.log.debug("azure virtual environment %d vm setup done" % sctx.current_job_index)
ctx = sctx.job_ctxs[sctx.current_job_index]
# update the status of virtual environment
expr = Experiment.objects(id=sctx.experiment_id).first()
ve = expr.virtual_environments[sctx.current_job_index]
adapter = self.__get_adapter_from_sctx(sctx, VirtualMachineAdapter)
ve.status = VEStatus.RUNNING
expr.save()
self._on_virtual_environment_success(Context(
experiment_id=expr.id))
azure_resource = AzureVirtualMachine(name=ctx.virtual_machine_name,
label=ctx.virtual_machine_label,
dns="%s.chinacloudapp.cn" % ctx.cloud_service_name,
end_points=[])
# todo record AzureDeployment, AzureCloudService and so on in db for roll back
vm_role = adapter.get_virtual_machine_role(ctx.cloud_service_name,
ctx.deployment_name,
ctx.virtual_machine_name)
if (not vm_role) or (not vm_role.instance_endpoints):
self.log.warn(
"unable to find vm %s, cannot update virtual env config like guacamole" % ctx.virtual_machine_name)
else:
for endpoint in vm_role.instance_endpoints:
azure_resource.public_ip = endpoint.vip
if endpoint.name == ctx.remote_endpoint_name: # endpoint for remote desktop
ve.remote_provider = VERemoteProvider.Guacamole
ve.remote_paras = get_remote_parameters(
ctx.raw_system_config,
ctx.remote,
ctx.virtual_machine_name,
endpoint.vip,
endpoint.public_port)
else:
try:
aep = self.__get_persistable_endpoint(endpoint, ctx.raw_network_config)
azure_resource.end_points.append(aep)
except Exception as e:
self.log.error(e)
ve.azure_resource = azure_resource
azure_resource.save()
expr.save()
self.log.debug(
"azure virtual environment %d vm success callback done, step to next" % sctx.current_job_index)
# step to config next unit
sctx.current_job_index += 1
self.__schedule_setup(sctx)
except Exception as e:
self.log.error("azure virtual environment %d failed on vm_done: %r" % (sctx.current_job_index, e.message))
self._on_virtual_environment_failed(sctx)
示例8: heart_beat
def heart_beat(self, expr_id):
expr = Experiment.objects(id=expr_id, status=EStatus.RUNNING).first()
if expr is None:
return not_found('Experiment is not running')
expr.last_heart_beat_time = self.util.get_now()
expr.save()
return ok()
示例9: _on_virtual_environment_stopped
def _on_virtual_environment_stopped(self, context):
expr = Experiment.objects(id=context.experiment_id).no_dereference() \
.only("status", "virtual_environments").first()
ve = expr.virtual_environments.get(name=context.virtual_environment_name)
ve.status = VEStatus.STOPPED
if all(ve.status == VEStatus.STOPPED for ve in expr.virtual_environments):
expr.status = VEStatus.STOPPED
expr.save()
示例10: _on_virtual_environment_success
def _on_virtual_environment_success(self, context):
expr = Experiment.objects(id=context.experiment_id).no_dereference() \
.only("status", "virtual_environments").first()
if all(ve.status == VEStatus.RUNNING for ve in expr.virtual_environments):
expr.status = EStatus.RUNNING
expr.save()
self._on_expr_started(context)
self._hooks_on_virtual_environment_success(context)
示例11: get_expr_list_by_hackathon_id
def get_expr_list_by_hackathon_id(self, hackathon, context):
# get a list of all experiments' detail
user_name = context.user_name if "user_name" in context else None
status = context.status if "status" in context else None
page = int(context.page) if "page" in context else 1
per_page = int(context.per_page) if "per_page" in context else 10
users = User.objects(name=user_name).all() if user_name else []
if user_name and status:
experiments_pagi = Experiment.objects(hackathon=hackathon, status=status, user__in=users).paginate(page, per_page)
elif user_name and not status:
experiments_pagi = Experiment.objects(hackathon=hackathon, user__in=users).paginate(page, per_page)
elif not user_name and status:
experiments_pagi = Experiment.objects(hackathon=hackathon, status=status).paginate(page, per_page)
else:
experiments_pagi = Experiment.objects(hackathon=hackathon).paginate(page, per_page)
return self.util.paginate(experiments_pagi, self.__get_expr_with_detail)
示例12: pre_allocate_expr
def pre_allocate_expr(self, context):
# TODO: too complex, not check
hackathon_id = context.hackathon_id
self.log.debug("executing pre_allocate_expr for hackathon %s " % hackathon_id)
hackathon = Hackathon.objects(id=hackathon_id).first()
hackathon_templates = hackathon.templates
for template in hackathon_templates:
try:
template = template
pre_num = int(hackathon.config.get("pre_allocate_number", 1))
query = Q(status=EStatus.STARTING) | Q(status=EStatus.RUNNING)
curr_num = Experiment.objects(user=None, hackathon=hackathon, template=template).filter(query).count()
if template.provider == VE_PROVIDER.AZURE:
if curr_num < pre_num:
remain_num = pre_num - curr_num
start_num = Experiment.objects(user=None, template=template, status=EStatus.STARTING).count()
if start_num > 0:
self.log.debug("there is an azure env starting, will check later ... ")
return
else:
self.log.debug(
"no starting template: %s , remain num is %d ... " % (template.name, remain_num))
self.start_expr(None, template.name, hackathon.name)
break
elif template.provider == VE_PROVIDER.DOCKER:
if hackathon.config.get('cloud_provider') == CLOUD_PROVIDER.ALAUDA:
# don't create pre-env if alauda used
continue
self.log.debug(
"template name is %s, hackathon name is %s" % (template.name, hackathon.name))
if curr_num < pre_num:
remain_num = pre_num - curr_num
start_num = Experiment.objects(user=None, template=template, status=EStatus.STARTING).count()
if start_num > 0:
self.log.debug("there is an docker container starting, will check later ... ")
return
self.log.debug("no idle template: %s, remain num is %d ... " % (template.name, remain_num))
self.start_expr(None, template.name, hackathon.name)
break
except Exception as e:
self.log.error(e)
self.log.error("check default experiment failed")
示例13: get_certificates_by_expr
def get_certificates_by_expr(self, expr_id):
"""Get certificates by experiment id
"""
# expr = self.db.get_object(Experiment, expr_id)
expr = Experiment.objects(id=expr_id)
# hak = self.db.find_all_objects_by(HackathonAzureKey, hackathon_id=expr.hackathon_id)
hak = Hackathon.objects(id=expr.hackathon_id).first().azure_keys[0]
if not hak:
raise Exception("no azure key configured")
return map(lambda key: self.db.get_object(AzureKey, key.azure_key_id), hak)
示例14: getConnectInfo
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
示例15: start_expr
def start_expr(self, context):
"""To start a new Experiment asynchronously
:type context: Context
:param context: the execution context.
"""
expr = Experiment(status=EStatus.INIT,
template=context.template,
user=context.user,
virtual_environments=[],
hackathon=context.hackathon)
expr.save()
template_content = self.template_library.load_template(context.template)
expr.status = EStatus.STARTING
expr.save()
# context contains complex object, we need create another serializable one with only simple fields
new_context = Context(template_content=template_content,
template_name=context.template.name,
hackathon_id=context.hackathon.id,
experiment_id=expr.id)
if context.get("user", None):
new_context.user_id = context.user.id
self._internal_start_expr(new_context)
new_context.experiment = expr
return new_context