def stop_expr(self, expr_id, force=0): """ :param expr_id: experiment id :param force: 0: only stop container and release ports, 1: force stop and delete container and release ports. :return: """ self.log.debug("begin to stop %d" % expr_id) expr = self.db.find_first_object_by(Experiment, id=expr_id, status=EStatus.RUNNING) if expr is not None: # Docker if expr.template.provider == VE_PROVIDER.DOCKER: # stop containers for c in expr.virtual_environments.all(): try: self.log.debug("begin to stop %s" % c.name) docker = self.__get_docker(expr.hackathon, c) if force: docker.delete(c.name, virtual_environment=c, container=c.container, expr_id=expr_id) c.status = VEStatus.DELETED else: docker.stop(c.name, virtual_environment=c, container=c.container, expr_id=expr_id) c.status = VEStatus.STOPPED except Exception as e: self.log.error(e) self.__roll_back(expr_id) return internal_server_error( 'Failed stop/delete container') if force: expr.status = EStatus.DELETED else: expr.status = EStatus.STOPPED self.db.commit() else: try: # todo support delete azure vm hosted_docker = RequiredFeature("hosted_docker") af = AzureFormation( hosted_docker.load_azure_key_id(expr_id)) af.stop(expr_id, AVMStatus.STOPPED_DEALLOCATED) except Exception as e: self.log.error(e) return internal_server_error('Failed stopping azure') self.log.debug("experiment %d ended success" % expr_id) return ok('OK') else: return ok()
def __start_new_expr(self, hackathon, template, user_id): # new expr expr = self.db.add_object_kwargs(Experiment, user_id=user_id, hackathon_id=hackathon.id, status=EStatus.INIT, template_id=template.id) self.db.commit() curr_num = self.db.count(Experiment, Experiment.user_id == ReservedUser.DefaultUserID, Experiment.template == template, (Experiment.status == EStatus.STARTING) | (Experiment.status == EStatus.RUNNING)) if template.provider == VE_PROVIDER.DOCKER: try: template_dic = self.template_library.load_template(template) virtual_environments_list = template_dic[TEMPLATE.VIRTUAL_ENVIRONMENTS] if curr_num != 0 and curr_num >= self.util.get_config("pre_allocate.docker"): return expr.status = EStatus.STARTING self.db.commit() map(lambda virtual_environment_dic: self.__remote_start_container(hackathon, expr, virtual_environment_dic), virtual_environments_list) expr.status = EStatus.RUNNING self.db.commit() except Exception as e: self.log.error(e) self.log.error("Failed starting containers") self.__roll_back(expr.id) return internal_server_error('Failed starting containers') else: if curr_num != 0 and curr_num >= self.util.get_config("pre_allocate.azure"): return expr.status = EStatus.STARTING self.db.commit() try: af = AzureFormation(self.hosted_docker.load_azure_key_id(expr.id)) af.create(expr.id) except Exception as e: self.log.error(e) return internal_server_error('Failed starting azure vm') # after everything is ready, set the expr state to running # response to caller return self.__report_expr_status(expr)
def stop_expr(self, expr_id, force=0): """ :param expr_id: experiment id :param force: 0: only stop container and release ports, 1: force stop and delete container and release ports. :return: """ self.log.debug("begin to stop %d" % expr_id) expr = self.db.find_first_object_by(Experiment, id=expr_id, status=EStatus.RUNNING) if expr is not None: # Docker if expr.template.provider == VE_PROVIDER.DOCKER: # stop containers for c in expr.virtual_environments.all(): try: self.log.debug("begin to stop %s" % c.name) docker = self.__get_docker(expr.hackathon, c) if force: docker.delete(c.name, virtual_environment=c, container=c.container, expr_id=expr_id) c.status = VEStatus.DELETED else: docker.stop(c.name, virtual_environment=c, container=c.container, expr_id=expr_id) c.status = VEStatus.STOPPED except Exception as e: self.log.error(e) self.__roll_back(expr_id) return internal_server_error('Failed stop/delete container') if force: expr.status = EStatus.DELETED else: expr.status = EStatus.STOPPED self.db.commit() else: try: # todo support delete azure vm hosted_docker = RequiredFeature("hosted_docker") af = AzureFormation(hosted_docker.load_azure_key_id(expr_id)) af.stop(expr_id, AVMStatus.STOPPED_DEALLOCATED) except Exception as e: self.log.error(e) return internal_server_error('Failed stopping azure') self.log.debug("experiment %d ended success" % expr_id) return ok('OK') else: return ok()
def __start_new_expr(self, hackathon, template, user_id): # new expr expr = self.db.add_object_kwargs(Experiment, user_id=user_id, hackathon_id=hackathon.id, status=EStatus.INIT, template_id=template.id) self.db.commit() if template.provider == VE_PROVIDER.DOCKER: try: template_content = self.template_library.load_template(template) virtual_environments_units = template_content.units expr.status = EStatus.STARTING self.db.commit() map(lambda unit: self.__remote_start_container(hackathon, expr, unit), virtual_environments_units) expr.status = EStatus.RUNNING self.db.commit() self.template_library.template_verified(template.id) except Exception as e: self.log.error(e) self.log.error("Failed starting containers") self.__roll_back(expr.id) return internal_server_error('Failed starting containers') else: expr.status = EStatus.STARTING self.db.commit() try: af = AzureFormation(self.hosted_docker.load_azure_key_id(expr.id)) af.create(expr.id) except Exception as e: self.log.error(e) return internal_server_error('Failed starting azure vm') # after everything is ready, set the expr state to running # response to caller return self.__report_expr_status(expr)
def __start_new_expr(self, hackathon, template, user_id): # new expr expr = self.db.add_object_kwargs(Experiment, user_id=user_id, hackathon_id=hackathon.id, status=EStatus.INIT, template_id=template.id, create_time=self.util.get_now()) self.db.commit() if template.provider == VE_PROVIDER.DOCKER: try: template_content = self.template_library.load_template( template) virtual_environments_units = template_content.units expr.status = EStatus.STARTING self.db.commit() map( lambda unit: self.__remote_start_container( hackathon, expr, unit), virtual_environments_units) except Exception as e: self.log.error(e) self.log.error("Failed starting containers") self.__roll_back(expr.id) return internal_server_error('Failed starting containers') else: expr.status = EStatus.STARTING self.db.commit() try: af = AzureFormation( self.hosted_docker.load_azure_key_id(expr.id)) af.create(expr.id) except Exception as e: self.log.error(e) return internal_server_error('Failed starting azure vm') # after everything is ready, set the expr state to running # response to caller return self.__report_expr_status(expr)