Exemple #1
0
    def delete_host_server(self, host_server_id):
        """
        delete a docker host_server for a hackathon.

        :param host_server_id: the id of host_server in DB
        :type host_server_id: Integer

        :return: ok() if succeeds or this host_server doesn't exist
                 precondition_failed() if there are still some containers running
        """
        DockerHostServer.objects(id=host_server_id).delete()
        return ok()
    def delete_host_server(self, host_server_id):
        """
        delete a docker host_server for a hackathon.

        :param host_server_id: the id of host_server in DB
        :type host_server_id: Integer

        :return: ok() if succeeds or this host_server doesn't exist
                 precondition_failed() if there are still some containers running
        """
        DockerHostServer.objects(id=host_server_id).delete()
        return ok()
    def update_host_server(self, args):
        """
        update a docker host_server's information for a hackathon.

        :param hackathon_id: the id of hackathon in DB
        :type hackathon_id: Integer

        :return: ok() if succeed.
                 not_found(...) if fail to update the docker_host's information
        """
        vm = DockerHostServer.objects(id=args.id).first()
        if vm is None:
            self.log.warn('delete docker_host fail, not find hostserver_id:' + args.id)
            return not_found("", "host_server not found")

        vm.vm_name = args.get("vm_name", vm.vm_name)
        vm.public_dns = args.get("public_dns", vm.public_dns)
        vm.public_ip = args.get("public_ip", vm.public_ip)
        vm.private_ip = args.get("private_ip", vm.private_ip)
        vm.container_max_count = int(args.get("container_max_count", vm.container_max_count))
        vm.public_docker_api_port = int(args.get("public_docker_api_port", vm.public_docker_api_port))
        vm.private_docker_api_port = int(args.get("private_docker_api_port", vm.private_docker_api_port))
        vm.disabled = args.get("disabled", vm.disabled)
        if self.docker.ping(vm):
            vm.state = DockerHostServerStatus.DOCKER_READY
        else:
            vm.state = DockerHostServerStatus.UNAVAILABLE

        vm.save()
        return self.__check_docker_host_server(vm).dic()
    def get_and_check_host_server(self, host_server_id):
        """
        first get the docker host DB object for a hackathon,
        and check whether the container_count is correct through "Docker Restful API",
        if not, update this value in the database.

        :param host_server_id: the id of host_server in DB
        :type host_server_id: Integer

        :return: A object of docker_host_server
        :rtype: DockerHostServer object or None
        """
        host_server = DockerHostServer.objects(id=host_server_id).first()
        if host_server is None:
            self.log.warn('get docker_host fail, not find host server by id:' + host_server_id)
            return not_found("docker host server not found")

        ping = self.docker.ping(host_server)
        if not ping:
            host_server.state = DockerHostServerStatus.UNAVAILABLE
            host_server.save()
        else:
            try:
                containers = self.docker.list_containers(host_server)
                if len(containers) != host_server.container_count:
                    host_server.container_count = len(containers)
                    host_server.save()
            except Exception as e:
                self.log.error("Failed in sync container count")
                self.log.error(e)

        return self.__check_docker_host_server(host_server).dic()
Exemple #5
0
    def get_and_check_host_server(self, host_server_id):
        """
        first get the docker host DB object for a hackathon,
        and check whether the container_count is correct through "Docker Restful API",
        if not, update this value in the database.

        :param host_server_id: the id of host_server in DB
        :type host_server_id: Integer

        :return: A object of docker_host_server
        :rtype: DockerHostServer object or None
        """
        host_server = DockerHostServer.objects(id=host_server_id).first()
        if host_server is None:
            self.log.warn('get docker_host fail, not find host server by id:' +
                          host_server_id)
            return not_found("docker host server not found")

        ping = self.docker.ping(host_server)
        if not ping:
            host_server.state = DockerHostServerStatus.UNAVAILABLE
            host_server.save()
        else:
            try:
                containers = self.docker.list_containers(host_server)
                if len(containers) != host_server.container_count:
                    host_server.container_count = len(containers)
                    host_server.save()
            except Exception as e:
                self.log.error("Failed in sync container count")
                self.log.error(e)

        return self.__check_docker_host_server(host_server).dic()
Exemple #6
0
 def query_vm_status_for_release(self, context):
     vm_adapter = self.__get_azure_vm_adapter(context)
     try:
         context.trial = context.get("trial", 0) + 1
         result = vm_adapter.get_virtual_machine_instance_status(
             context.cloud_service_name, DEPLOYMENT_SLOT,
             context.virtual_machine_name)
         if result is None:
             self.log.error("cannot find vm or deployment slot")
             self._on_virtual_environment_unexpected_error(context)
         elif result == AVMStatus.READY_ROLE:
             host_server = DockerHostServer.objects(
                 id=context.host_server_id).first()
             self.__stop_docker_container(context, host_server)
         elif context.trial > MAX_TRIAL:
             self._on_virtual_environment_unexpected_error(context)
         else:
             self.log.debug(
                 'wait for virtual machine [%r] loop count [%d]' %
                 (context.virtual_machine_name, context.trial))
             self.scheduler.add_once(FEATURE,
                                     "query_vm_status",
                                     context,
                                     seconds=TRIAL_INTERVAL_SECONDS)
     except Exception as e:
         self.log.error(e)
         self.scheduler.add_once(FEATURE,
                                 "query_vm_status_for_release",
                                 context,
                                 seconds=TRIAL_INTERVAL_SECONDS)
Exemple #7
0
    def update_host_server(self, args):
        """
        update a docker host_server's information for a hackathon.

        :param hackathon_id: the id of hackathon in DB
        :type hackathon_id: Integer

        :return: ok() if succeed.
                 not_found(...) if fail to update the docker_host's information
        """
        vm = DockerHostServer.objects(id=args.id).first()
        if vm is None:
            self.log.warn('delete docker_host fail, not find hostserver_id:' +
                          args.id)
            return not_found("", "host_server not found")

        vm.vm_name = args.get("vm_name", vm.vm_name)
        vm.public_dns = args.get("public_dns", vm.public_dns)
        vm.public_ip = args.get("public_ip", vm.public_ip)
        vm.private_ip = args.get("private_ip", vm.private_ip)
        vm.container_max_count = int(
            args.get("container_max_count", vm.container_max_count))
        vm.public_docker_api_port = int(
            args.get("public_docker_api_port", vm.public_docker_api_port))
        vm.private_docker_api_port = int(
            args.get("private_docker_api_port", vm.private_docker_api_port))
        vm.disabled = args.get("disabled", vm.disabled)
        if self.docker.ping(vm):
            vm.state = DockerHostServerStatus.DOCKER_READY
        else:
            vm.state = DockerHostServerStatus.UNAVAILABLE

        vm.save()
        return self.__check_docker_host_server(vm).dic()
Exemple #8
0
    def add_host_server(self, hackathon, args):
        """
        create a docker host DB object for a hackathon and insert record into the database.
        param-"args" contain all necessary infos to new a docker_host

        :return: return True if succeed, otherwise return False
        :rtype: bool
        """

        host_server = DockerHostServer(
            vm_name=args.vm_name,
            public_dns=args.public_dns,
            public_ip=args.public_ip,
            public_docker_api_port=args.public_docker_api_port,
            private_ip=args.private_ip,
            private_docker_api_port=args.private_docker_api_port,
            container_count=0,
            container_max_count=args.container_max_count,
            is_auto=False,
            disabled=args.get("disabled", False),
            hackathon=hackathon)

        if self.docker.ping(host_server):
            host_server.state = DockerHostServerStatus.DOCKER_READY
        else:
            host_server.state = DockerHostServerStatus.UNAVAILABLE

        host_server.save()
        return host_server.dic()
Exemple #9
0
    def __update_virtual_environment_cfg(self, context):
        experiment = Experiment.objects(
            id=context.experiment_id).no_dereference().first()
        virtual_environment = experiment.virtual_environments.get(
            name=context.virtual_environment_name)
        host_server = DockerHostServer.objects(
            id=context.host_server_id).first()

        # azure_key
        if not self.util.is_local():
            experiment.azure_key = AzureKey.objects(
                id=context.azure_key_id).first()

        # update port binding
        for cfg in context.port_config:
            public_port = cfg[
                DOCKER_UNIT.
                PORTS_PUBLIC_PORT] if DOCKER_UNIT.PORTS_PUBLIC_PORT in cfg else None
            port_binding = PortBinding(
                name=cfg[DOCKER_UNIT.PORTS_NAME],
                is_public=bool(cfg[DOCKER_UNIT.PORTS_PUBLIC]),
                public_port=public_port,
                host_port=cfg[DOCKER_UNIT.PORTS_HOST_PORT],
                container_port=cfg[DOCKER_UNIT.PORTS_PORT])
            if DOCKER_UNIT.PORTS_URL in cfg:
                port_binding.url = cfg[DOCKER_UNIT.PORTS_URL]
            virtual_environment.docker_container.port_bindings.append(
                port_binding)

        # guacamole config
        guacamole = context.unit.get_remote()
        port_cfg = filter(
            lambda p: p[DOCKER_UNIT.PORTS_PORT] == guacamole[
                DOCKER_UNIT.REMOTE_PORT], context.port_config)
        if len(port_cfg) > 0:
            virtual_environment.remote_provider = VERemoteProvider.Guacamole
            gc = {
                "displayname": context.virtual_environment_name,
                "name": context.virtual_environment_name,
                "protocol": guacamole[DOCKER_UNIT.REMOTE_PROTOCOL],
                "hostname": host_server.public_ip,
                "port": port_cfg[0][DOCKER_UNIT.PORTS_PUBLIC_PORT],
                "enable-sftp": True
            }
            if DOCKER_UNIT.REMOTE_USERNAME in guacamole:
                gc["username"] = guacamole[DOCKER_UNIT.REMOTE_USERNAME]
            if DOCKER_UNIT.REMOTE_PASSWORD in guacamole:
                gc["password"] = guacamole[DOCKER_UNIT.REMOTE_PASSWORD]

            # save guacamole config into DB
            virtual_environment.remote_paras = gc

        experiment.save()

        # start container
        self.__start_docker_container(context, experiment, host_server)
    def get_host_server_by_id(self, id_):
        """
        Search host server in DB by id

        :param id_: the id of host server in DB
        :type id_: integer

        :return: the found host server information in DB
        :rtype: DockerHostServer object
        """
        return DockerHostServer.objects(id=id_).first()
Exemple #11
0
    def get_host_server_by_id(self, id_):
        """
        Search host server in DB by id

        :param id_: the id of host server in DB
        :type id_: integer

        :return: the found host server information in DB
        :rtype: DockerHostServer object
        """
        return DockerHostServer.objects(id=id_).first()
Exemple #12
0
    def get_docker_hosts_list(self, hackathon):
        """
        Get all host servers of a hackathon
        :param hackathon_id: the id of this hackathon, on_success callback function
        :type hackathon_id: integer

        :return: a list of all docker hosts
        :rtype: list
        """
        host_servers = DockerHostServer.objects(hackathon=hackathon)
        return [host_server.dic() for host_server in host_servers]
    def get_docker_hosts_list(self, hackathon):
        """
        Get all host servers of a hackathon
        :param hackathon_id: the id of this hackathon, on_success callback function
        :type hackathon_id: integer

        :return: a list of all docker hosts
        :rtype: list
        """
        host_servers = DockerHostServer.objects(hackathon=hackathon)
        return [host_server.dic() for host_server in host_servers]
    def add_host_server(self, hackathon, args):
        """
        create a docker host DB object for a hackathon and insert record into the database.
        param-"args" contain all necessary infos to new a docker_host

        :return: return True if succeed, otherwise return False
        :rtype: bool
        """

        host_server = DockerHostServer(vm_name=args.vm_name,
                                       public_dns=args.public_dns,
                                       public_ip=args.public_ip,
                                       public_docker_api_port=args.public_docker_api_port,
                                       private_ip=args.private_ip,
                                       private_docker_api_port=args.private_docker_api_port,
                                       container_count=0,
                                       container_max_count=args.container_max_count,
                                       is_auto=False,
                                       disabled=args.get("disabled", False),
                                       hackathon=hackathon)

        if self.docker.ping(host_server):
            host_server.state = DockerHostServerStatus.DOCKER_READY
        else:
            host_server.state = DockerHostServerStatus.UNAVAILABLE

        host_server.save()
        return host_server.dic()
 def __create_default_data_for_local(self, hackathon):
     """
     create test data for new hackathon. It's for local development only
     :param hackathon:
     :return:
     """
     try:
         # test docker host server
         host = DockerHostServer(vm_name="localhost",
                                 public_dns="localhost",
                                 public_ip="127.0.0.1",
                                 public_docker_api_port=4243,
                                 private_ip="127.0.0.1",
                                 private_docker_api_port=4243,
                                 container_count=0,
                                 container_max_count=100,
                                 disabled=False,
                                 state=DockerHostServerStatus.DOCKER_READY,
                                 hackathon=hackathon)
         host.save()
     except Exception as e:
         self.log.error(e)
         self.log.warn("fail to create test data")
 def __create_default_data_for_local(self, hackathon):
     """
     create test data for new hackathon. It's for local development only
     :param hackathon:
     :return:
     """
     try:
         # test docker host server
         host = DockerHostServer(vm_name="localhost",
                                 public_dns="localhost",
                                 public_ip="127.0.0.1",
                                 public_docker_api_port=4243,
                                 private_ip="127.0.0.1",
                                 private_docker_api_port=4243,
                                 container_count=0,
                                 container_max_count=100,
                                 disabled=False,
                                 state=DockerHostServerStatus.DOCKER_READY,
                                 hackathon=hackathon)
         host.save()
     except Exception as e:
         self.log.error(e)
         self.log.warn("fail to create test data")
    def __update_virtual_environment_cfg(self, context):
        experiment = Experiment.objects(id=context.experiment_id).no_dereference().first()
        virtual_environment = experiment.virtual_environments.get(name=context.virtual_environment_name)
        host_server = DockerHostServer.objects(id=context.host_server_id).first()

        # azure_key
        if not self.util.is_local():
            experiment.azure_key = AzureKey.objects(id=context.azure_key_id).first()

        # update port binding
        for cfg in context.port_config:
            public_port = cfg[DOCKER_UNIT.PORTS_PUBLIC_PORT] if DOCKER_UNIT.PORTS_PUBLIC_PORT in cfg else None
            port_binding = PortBinding(name=cfg[DOCKER_UNIT.PORTS_NAME],
                                       is_public=bool(cfg[DOCKER_UNIT.PORTS_PUBLIC]),
                                       public_port=public_port,
                                       host_port=cfg[DOCKER_UNIT.PORTS_HOST_PORT],
                                       container_port=cfg[DOCKER_UNIT.PORTS_PORT])
            if DOCKER_UNIT.PORTS_URL in cfg:
                port_binding.url = cfg[DOCKER_UNIT.PORTS_URL]
            virtual_environment.docker_container.port_bindings.append(port_binding)

        # guacamole config
        guacamole = context.unit.get_remote()
        port_cfg = filter(lambda p:
                          p[DOCKER_UNIT.PORTS_PORT] == guacamole[DOCKER_UNIT.REMOTE_PORT],
                          context.port_config)
        if len(port_cfg) > 0:
            virtual_environment.remote_provider = VERemoteProvider.Guacamole
            gc = {
                "displayname": context.virtual_environment_name,
                "name": context.virtual_environment_name,
                "protocol": guacamole[DOCKER_UNIT.REMOTE_PROTOCOL],
                "hostname": host_server.public_ip,
                "port": port_cfg[0][DOCKER_UNIT.PORTS_PUBLIC_PORT],
                "enable-sftp": True
            }
            if DOCKER_UNIT.REMOTE_USERNAME in guacamole:
                gc["username"] = guacamole[DOCKER_UNIT.REMOTE_USERNAME]
            if DOCKER_UNIT.REMOTE_PASSWORD in guacamole:
                gc["password"] = guacamole[DOCKER_UNIT.REMOTE_PASSWORD]

            # save guacamole config into DB
            virtual_environment.remote_paras = gc

        experiment.save()

        # start container
        self.__start_docker_container(context, experiment, host_server)
 def query_vm_status_for_release(self, context):
     vm_adapter = self.__get_azure_vm_adapter(context)
     try:
         context.trial = context.get("trial", 0) + 1
         result = vm_adapter.get_virtual_machine_instance_status(context.cloud_service_name,
                                                                 DEPLOYMENT_SLOT,
                                                                 context.virtual_machine_name)
         if result is None:
             self.log.error("cannot find vm or deployment slot")
             self._on_virtual_environment_unexpected_error(context)
         elif result == AVMStatus.READY_ROLE:
             host_server = DockerHostServer.objects(id=context.host_server_id).first()
             self.__stop_docker_container(context, host_server)
         elif context.trial > MAX_TRIAL:
             self._on_virtual_environment_unexpected_error(context)
         else:
             self.log.debug(
                 'wait for virtual machine [%r] loop count [%d]' % (context.virtual_machine_name, context.trial))
             self.scheduler.add_once(FEATURE, "query_vm_status", context, seconds=TRIAL_INTERVAL_SECONDS)
     except Exception as e:
         self.log.error(e)
         self.scheduler.add_once(FEATURE, "query_vm_status_for_release", context, seconds=TRIAL_INTERVAL_SECONDS)