コード例 #1
0
    def __start_virtual_environment(self, context, docker_template_unit):
        origin_name = docker_template_unit.get_name()
        prefix = str(context.experiment_id)[0:9]
        suffix = "".join(random.sample(string.ascii_letters + string.digits,
                                       8))
        new_name = '%s-%s-%s' % (prefix, origin_name, suffix.lower())
        docker_template_unit.set_name(new_name)
        self.log.debug("starting to start container: %s" % new_name)

        # db document for VirtualEnvironment
        ve = VirtualEnvironment(
            provider=VE_PROVIDER.DOCKER,
            name=new_name,
            image=docker_template_unit.get_image_with_tag(),
            status=VEStatus.INIT,
            remote_provider=VERemoteProvider.Guacamole)
        # create a new context for current ve only
        context = context.copy()
        experiment = Experiment.objects(
            id=context.experiment_id).no_dereference().only(
                "virtual_environments").first()
        experiment.virtual_environments.append(ve)
        experiment.save()

        # start container remotely , use hosted docker or alauda docker
        context.virtual_environment_name = ve.name
        context.unit = docker_template_unit
        self._internal_start_virtual_environment(context)
コード例 #2
0
    def _internal_start_expr(self, context):
        hackathon = Hackathon.objects.get(id=context.hackathon_id)
        experiment = Experiment.objects.get(id=context.experiment_id)
        if not experiment or not hackathon:
            return internal_server_error(
                'Failed starting k8s: experiment or hackathon not found.')
        user = experiment.user or None
        _virtual_envs = []
        try:
            if user:
                _virtual_envs = experiment.virtual_environments
            if not _virtual_envs:
                # Get None VirtualEnvironment, create new one:
                for template_unit in context.template_content.units:
                    k8s_dict = self.__create_useful_k8s_dict(
                        hackathon, experiment, template_unit)
                    experiment.virtual_environments.append(
                        VirtualEnvironment(
                            provider=VE_PROVIDER.K8S,
                            name=k8s_dict['name'],
                            k8s_resource=k8s_dict,
                            status=VEStatus.INIT,
                            remote_provider=VERemoteProvider.Guacamole))

                    # save constructed experiment, and execute from first job content
                    experiment.save()
                    self.__schedule_create(context)
            else:
                self.__schedule_start(context)
        except Exception as e:
            self.log.error(e)
            experiment.status = EStatus.FAILED
            experiment.save()
            return internal_server_error('Failed starting k8s')
コード例 #3
0
    def _internal_start_expr(self, context):
        hackathon = Hackathon.objects.get(id=context.hackathon_id)
        experiment = Experiment.objects.get(id=context.experiment_id)
        template_content = context.template_content

        if not experiment or not hackathon:
            return internal_server_error(
                'Failed starting k8s: experiment or hackathon not found.')

        user = experiment.user or None
        _virtual_envs = []
        _env_name = str(hackathon.name + "-" + template_content.name).lower()
        if user:
            _virtual_envs = experiment.virtual_environments
            _env_name += str("-" + user.name).lower()
        _env_name = "{}-{}".format(_env_name,
                                   "".join(random.sample(string.lowercase, 6)))

        try:
            if not _virtual_envs:
                # Get None VirtualEnvironment, create new one:
                labels = {
                    "hacking.kaiyuanshe.cn/hackathon": str(hackathon.id),
                    "hacking.kaiyuanshe.cn/experiment": str(experiment.id),
                    "hacking.kaiyuanshe.cn/virtual_environment": _env_name,
                }
                k8s_env = self.__create_useful_k8s_resource(
                    _env_name, template_content, labels)

                experiment.virtual_environments.append(
                    VirtualEnvironment(
                        provider=VE_PROVIDER.K8S,
                        name=_env_name,
                        k8s_resource=k8s_env,
                        status=VEStatus.INIT,
                        remote_provider=VERemoteProvider.Guacamole))

                experiment.status = EStatus.INIT
                experiment.save()
                self.log.debug(
                    "virtual_environments %s created, creating k8s..." %
                    _env_name)
            self.__schedule_start(context)
        except Exception as e:
            self.log.error(e)
            experiment.status = EStatus.FAILED
            experiment.save()
            return False

        return True
コード例 #4
0
    def __start_vm(self, experiment, hackathon, template_units):
        azure_keys = hackathon.azure_keys
        # TODO: which key to use?
        azure_key = azure_keys[0]
        experiment.azure_key = azure_key

        # job context
        job_ctxs = []
        ctx = Context(
            job_ctxs=job_ctxs,
            current_job_index=0,
            subscription_id=azure_key.subscription_id,
            pem_url=azure_key.get_local_pem_url(),
            management_host=azure_key.management_host,
            experiment_id=experiment.id,

            # remote_created is used to store the resources we create we create remote
            # so we can do rollback
            # TODO: if the user create a virtual machine with vm_image, we have to config the network of it
            #       but so far we have no way to rollback the network settings of it
            remote_created=[])

        # create virtual environments for units
        # and setup setup job contenxt
        # the setup of each unit must be SERLIALLY EXECUTED
        # to avoid the creation of same resource in same time
        # TODO: we still have't avoid the parrallel excution of the setup of same template
        for i in xrange(len(template_units)):
            unit = template_units[i]
            vm_name = self.__get_virtual_machine_name(
                unit.get_virtual_machine_name(), experiment.id)

            # set up virtual environment
            experiment.virtual_environments.append(
                VirtualEnvironment(provider=VE_PROVIDER.AZURE,
                                   name=vm_name,
                                   image=unit.get_image_name(),
                                   status=VEStatus.INIT,
                                   remote_provider=VERemoteProvider.Guacamole))

            # construct job context
            job_ctxs.append(
                self.__construct_setup_job_context(unit, azure_key, vm_name))

        # save constructed experiment, and execute from first job content
        experiment.save()
        self.__schedule_setup(ctx)