Exemplo n.º 1
0
    def db_set_by_id(self, id, **kwargs):
        """
        Set the key:value pairs to the data
        :param id: Which host to update
        :param kwargs: kv pairs
        :return: The updated host json dict
        """
        kwargs = dict(
            ('set__' + k, v) for (k, v) in locals().get("kwargs", {}).items())
        HostModel.objects(id=id).update(upsert=True, **kwargs)

        return HostModel.objects.get(id=id)
Exemplo n.º 2
0
    def db_set_by_id(self, id, **kwargs):
        """
        Set the key:value pairs to the data
        :param id: Which host to update
        :param kwargs: kv pairs
        :return: The updated host json dict
        """
        kwargs = dict(('set__' + k, v)
                      for (k, v) in locals().get("kwargs", {}).items())
        HostModel.objects(id=id).update(
            upsert=True,
            **kwargs
        )

        return HostModel.objects.get(id=id)
Exemplo n.º 3
0
    def delete(self):
        args = host_user_add_parser.parse_args()
        host_id = args.get('host_id')
        user_id = args.get('user_id')
        if not all([host_id, user_id]):
            return make_fail_resp(error='参数缺失')

        try:
            host = HostModel.objects(id=host_id)
        except Exception as e:
            logger.error(e)
            return make_fail_resp(error='db error')
        if not host:
            return make_fail_resp(error='主机不存在')

        try:
            user = UserModel.objects(id=user_id)
        except Exception as e:
            logger.error(e)
            return make_fail_resp(error='db error')
        try:
            user.update(host=None)
        except Exception as e:
            logger.error(e)
            return make_fail_resp(error='db error')

        return
Exemplo n.º 4
0
    def db_set_by_id(self, id, **kwargs):
        """
        Set the key:value pairs to the data
        :param id: Which host to update
        :param kwargs: kv pairs
        :return: The updated host json dict
        """
        try:
            host = HostModel.Query.get(id=id)
            host = HostModel(objectId=host.objectId, **kwargs)
            host.save()
            host = HostModel.Query.get(id=id)
        except Exception:
            return None

        return host
Exemplo n.º 5
0
    def db_set_by_id(self, id, **kwargs):
        """
        Set the key:value pairs to the data
        :param id: Which host to update
        :param kwargs: kv pairs
        :return: The updated host json dict
        """
        try:
            host = HostModel.Query.get(id=id)
            host = HostModel(objectId=host.objectId, **kwargs)
            host.save()
            host = HostModel.Query.get(id=id)
        except Exception:
            return None

        return host
Exemplo n.º 6
0
 def get(self):
     hosts = HostModel.objects(status='active')
     available_host_list = []
     for host in hosts:
         if len(host.clusters) < int(host.capacity):
             available_host_list.append(host)
     result = {'data': available_host_list}
     return result
Exemplo n.º 7
0
    def list(self, filter_data={}):
        """ List hosts with given criteria

        :param filter_data: Image with the filter properties
        :return: iteration of serialized doc
        """
        logger.info("filter data {}".format(filter_data))
        hosts = HostModel.objects(__raw__=filter_data)
        return self._schema(hosts, many=True)
Exemplo n.º 8
0
    def list(self, filter_data={}):
        """ List hosts with given criteria

        :param filter_data: Image with the filter properties
        :return: iteration of serialized doc
        """
        logger.info("filter data {}".format(filter_data))
        hosts = HostModel.objects(__raw__=filter_data)
        return self._schema(hosts, many=True)
Exemplo n.º 9
0
    def list(self, filter_data=None):
        """ List hosts with given criteria

        :param filter_data: Image with the filter properties
        :return: iteration of serialized doc
        """
        if filter_data is None:
            filter_data = {}
        hosts = HostModel.objects(__raw__=filter_data)
        return self._schema(hosts, many=True)
Exemplo n.º 10
0
    def create(self, name, worker_api, host_type, capacity=1,
               log_level=CLUSTER_LOG_LEVEL[0],
               log_type=CLUSTER_LOG_TYPES[0], log_server="", autofill="false",
               schedulable="false", params=None):
        """ Create a new docker host node

        A docker host is potentially a single node or a swarm.
        Will full fill with clusters of given capacity.

        :param name: name of the node
        :param worker_api: worker_api of the host
        :param host_type: docker host type docker or swarm
        :param capacity: The number of clusters to hold
        :param log_type: type of the log
        :param log_level: level of the log
        :param log_server: server addr of the syslog
        :param autofill: Whether automatically fillup with chains
        :param schedulable: Whether can schedule cluster request to it
        :param serialization: whether to get serialized result or object
        :param params: extra data for vSphere host type
        :return: True or False
        """
        logger.debug("Create host: name={}, worker_api={}, host_type={}, "
                     "capacity={}, log={}/{}, autofill={}, schedulable={}"
                     .format(name, worker_api, host_type, capacity, log_type,
                             log_server, autofill, schedulable))

        if params is None and not worker_api.startswith("tcp://"):
            # params is None when host_type is either docker or swarm.
            worker_api = "tcp://" + worker_api

        if HostModel.objects(worker_api=worker_api).count():
            logger.warning("{} already existed in db".format(worker_api))
            return {}

        if "://" not in log_server:
            log_server = "udp://" + log_server
        if log_type == CLUSTER_LOG_TYPES[0]:
            log_server = ""

        if not host_type:
            logger.warning("Host {} cannot be setup".format(name))
            return {}

        hid = uuid4().hex
        host = HostModel(id=hid,
                         name=name,
                         worker_api=worker_api,
                         capacity=capacity,
                         type=host_type,
                         log_level=log_level,
                         log_type=log_type,
                         log_server=log_server,
                         autofill=autofill == "true",
                         schedulable=schedulable == "true"
                         )

        if (host_type == WORKER_TYPE_DOCKER or
           host_type == WORKER_TYPE_SWARM):
            if not self.host_agents[host_type].create(worker_api):
                logger.warning("Host {} cannot be setup".format(name))
                return {}

        if host_type == WORKER_TYPE_VSPHERE:

            vc = params.get(VCENTER)
            vm = params.get(VIRTUAL_MACHINE)

            vc_ip = vc.get(VCIP)
            vc_username = vc.get(VCUSERNAME)
            vc_passwd = vc.get(VCPWD)
            vc_port = vc.get(VCPORT)

            h_update = {
                VMNAME: vm.get(VMNAME),
                VMMEMORY: vm.get(VMMEMORY),
                VMCPU: vm.get(VMCPU),
                VMIP: vm.get(VMIP),
                VMNETMASK: vm.get(VMNETMASK),
                VMDNS: vm.get(VMDNS),
                VMGATEWAY: vm.get(VMGATEWAY),
                TEMPLATE: vc.get(TEMPLATE),
                VC_DATACENTER: vc.get(VC_DATACENTER),
                VC_CLUSTER: vc.get(VC_CLUSTER),
                VC_DATASTORE: vc.get(VC_DATASTORE),
                NETWORK: vc.get(NETWORK),
                VCUSERNAME: vc_username,
                VCPWD: vc_passwd,
                VCPORT: vc_port,
                HOST_STATUS: HOST_STATUS_PENDING
            }
            logger.debug("update {}".format(h_update))
            host.status = HOST_STATUS_PENDING
            try:
                if self.host_agents[host_type].create(vc_ip,
                                                      vc_username,
                                                      vc_passwd, vc_port,
                                                      params, hid):
                    logger.info("Creating vSphere host{}".format(name))

            except Exception as e:  # Catch failure while connecting to vc.
                logger.error("Host {} cannot be setup".format(name))
                logger.error("{}".format(e))
                return {"msg": "{}".format(e)}

        if host_type == WORKER_TYPE_K8S:
            try:
                if self.host_agents[host_type].create(params):
                    logger.info("Successfully created Kubernetes \
                                 host{}".format(name))
            except Exception as e:
                logger.error("Failed to setup Host {}".format(name))
                logger.error("{}".format(e))
                return {"msg": "{}".format(e)}
            logger.debug("Storing K8S data")
            host.k8s_param = params

        host.save()

        if capacity > 0 and autofill == "true":  # should autofill it
            self.fillup(str(hid))

        return self._schema(host)
Exemplo n.º 11
0
    def post(self):
        request_debug(r, logger)
        args = host_create_parser.parse_args()
        name = args.get('host_name')
        worker_api = args.get('worker_api')
        capacity = args.get('capacity')
        host_type = args.get('host_type')

        if host_type != "docker":
            return {'stat': 20001, 'msg': '后续开放'}
            # vcaddress = args['vc_address']
            # if vcaddress.find(":") == -1:
            #     address = vcaddress
            #     port = "443"
            # else:
            #     address = vcaddress.split(':')[0]
            #     port = vcaddress.split(':')[1]
            # logger.debug("address={}, port={}".format(address, port))
            #
            # vmname = "cello-vsphere-" + str(uuid.uuid1())
            # vsphere_param = {
            #     'vc': {
            #         'address': address,
            #         'port': port,
            #         'username': args['vc_user'],
            #         'password': args['vc_password'],
            #         'network': args['vc_network'],
            #         'vc_datastore': args['datastore'],
            #         'vc_datacenter': args['datacenter'],
            #         'vc_cluster': args['cluster'],
            #         'template': args['vm_template']},
            #     'vm': {
            #         'vmname': vmname,
            #         'ip': args['vm_ip'],
            #         'gateway': args['vm_gateway'],
            #         'netmask': args['vm_netmask'],
            #         'dns': args['vm_dns'],
            #         'vcpus': int(args['vm_cpus']),
            #         'memory': int(args['vm_memory'])}}
            #
            # vsphere_must_have_params = {
            #     'Name': name,
            #     'Capacity': capacity,
            #     'VCAddress': address,
            #     'VCUser': args['vc_user'],
            #     'VCPassword': args['vc_password'],
            #     'VCNetwork': args['vc_network'],
            #     'Datastore': args['datastore'],
            #     'Datacenter': args['datacenter'],
            #     'Cluster': args['cluster'],
            #     'VMIp': args['vm_ip'],
            #     'VMGateway': args['vm_gateway'],
            #     'VMNetmask': args['vm_netmask']}
            # for key in vsphere_must_have_params:
            #     if vsphere_must_have_params[key] == '':
            #         error_msg = "host POST without {} data".format(key)
            #         logger.warning(error_msg)
            #         return make_fail_resp(error=error_msg, data=args)
            # result = host_handler.create(name=name, worker_api=worker_api,
            #                              capacity=int(capacity),
            #                              autofill=autofill,
            #                              schedulable=schedulable,
            #                              log_level=log_level,
            #                              log_type=log_type,
            #                              log_server=log_server,
            #                              host_type=host_type,
            #                              params=vsphere_param)

        # elif host_type == 'kubernetes':
        #     return {'stat': 20001, 'msg': '后续开放'}
        #
        #     worker_api = body['worker_api']
        #     k8s_param = create_k8s_host(name, capacity, log_type, body)
        #     if len(k8s_param) == 0:
        #         return make_fail_resp(error=error_msg, data=r.form)
        #
        #     logger.debug("name={}, worker_api={},  capacity={},"
        #                  "fillup={}, schedulable={}, log={}/{}, k8s_param={}".
        #                  format(name, worker_api, capacity, autofill,
        #                         schedulable, log_type, log_server, k8s_param))
        #
        #     result = host_handler.create(name=name, worker_api=worker_api,
        #                                  capacity=int(capacity),
        #                                  autofill=autofill,
        #                                  schedulable=schedulable,
        #                                  log_level=log_level,
        #                                  log_type=log_type,
        #                                  log_server=log_server,
        #                                  host_type=host_type,
        #                                  params=k8s_param)

        else:
            logger.debug(
                "name={}, worker_api={}, capacity={},host_type={}".format(
                    name, worker_api, capacity,
                    host_type if host_type else 'docker'))
            if not name or not worker_api or not capacity:
                error_msg = "参数缺失"
                logger.warning(error_msg)
                return make_fail_resp(error=error_msg, data=args)

            url = urlparse(worker_api)
            if not url.scheme:
                worker_api = "tcp://" + worker_api  # worker node listen on tcp port
            segs = worker_api.split(":")
            if len(segs) != 3:
                logger.error("Invalid daemon url = ", worker_api)
                return make_fail_resp(error='服务器地址格式错误',
                                      data={"worker_api": worker_api})

            host = HostModel.objects(name=name)
            if host:
                error_msg = '主机名已存在'
                return make_fail_resp(error=error_msg, data={'name': name})

            host = HostModel.objects(worker_api=worker_api)
            if host:
                error_msg = '服务器地址已存在'
                return make_fail_resp(error=error_msg,
                                      data={"worker_api": worker_api})
            if not check_worker_api(worker_api):
                error_msg = '服务器地址异常'
                logger.error(error_msg)
                # return make_fail_resp(error=error_msg, data={'worker_api': worker_api})
                return {'msg': error_msg, 'stat': 400}

            host_type = host_type if host_type else detect_daemon_type(
                worker_api)
            result = host_handler.create(name=name,
                                         worker_api=worker_api,
                                         capacity=int(capacity),
                                         host_type=host_type)

        logger.debug("result.msg={}".format(result.get('msg')))
        if (host_type == "vsphere") and ('msg' in result):
            vsphere_errmsg = result.get('msg')
            error_msg = "Failed to create vsphere host {}".format(
                vsphere_errmsg)
            logger.warning(error_msg)
            return make_fail_resp(error=error_msg)
        elif result:
            logger.debug("host creation successfully")
            return make_ok_resp()
        else:
            error_msg = "Failed to create host {}".format(args["host_name"])
            logger.warning(error_msg)
            return make_fail_resp(error=error_msg)
Exemplo n.º 12
0
    def create(self, name, worker_api, host_type, capacity=1,
               log_level=CLUSTER_LOG_LEVEL[0],
               log_type=CLUSTER_LOG_TYPES[0], log_server="", autofill="false",
               schedulable="false", params=None):
        """ Create a new docker host node

        A docker host is potentially a single node or a swarm.
        Will full fill with clusters of given capacity.

        :param name: name of the node
        :param worker_api: worker_api of the host
        :param host_type: docker host type docker or swarm
        :param capacity: The number of clusters to hold
        :param log_type: type of the log
        :param log_level: level of the log
        :param log_server: server addr of the syslog
        :param autofill: Whether automatically fillup with chains
        :param schedulable: Whether can schedule cluster request to it
        :param serialization: whether to get serialized result or object
        :param params: extra data for vSphere host type
        :return: True or False
        """
        logger.debug("Create host: name={}, worker_api={}, host_type={}, "
                     "capacity={}, log={}/{}, autofill={}, schedulable={}"
                     .format(name, worker_api, host_type, capacity, log_type,
                             log_server, autofill, schedulable))

        if params is None and not worker_api.startswith("tcp://"):
            # params is None when host_type is either docker or swarm.
            worker_api = "tcp://" + worker_api

        if HostModel.objects(worker_api=worker_api).count():
            logger.warning("{} already existed in db".format(worker_api))
            return {}

        if "://" not in log_server:
            log_server = "udp://" + log_server
        if log_type == CLUSTER_LOG_TYPES[0]:
            log_server = ""

        if not host_type:
            logger.warning("Host {} cannot be setup".format(name))
            return {}

        if (host_type == WORKER_TYPE_DOCKER or
           host_type == WORKER_TYPE_SWARM):
            if not self.host_agents[host_type].create(worker_api):
                logger.warning("Host {} cannot be setup".format(name))
                return {}

        if host_type == WORKER_TYPE_VSPHERE:

            vc = params.get(VCENTER)
            vm = params.get(VIRTUAL_MACHINE)

            worker_api = vc.get(VCIP)
            vc_username = vc.get(VCUSERNAME)
            vc_passwd = vc.get(VCPWD)
            vc_port = vc.get(VCPORT)

            h_update = {
                VMNAME: vm.get(VMNAME),
                VMMEMORY: vm.get(VMMEMORY),
                VMCPU: vm.get(VMCPU),
                VMIP: vm.get(VMIP),
                VMNETMASK: vm.get(VMNETMASK),
                VMDNS: vm.get(VMDNS),
                VMGATEWAY: vm.get(VMGATEWAY),
                TEMPLATE: vc.get(TEMPLATE),
                VC_DATACENTER: vc.get(VC_DATACENTER),
                VC_CLUSTER: vc.get(VC_CLUSTER),
                VC_DATASTORE: vc.get(VC_DATASTORE),
                NETWORK: vc.get(NETWORK),
                VCUSERNAME: vc_username,
                VCPWD: vc_passwd,
                VCPORT: vc_port,
                HOST_STATUS: HOST_STATUS_PENDING
            }
            logger.debug("update {}".format(h_update))
            try:
                if self.host_agents[host_type].create(worker_api,
                                                      vc_username,
                                                      vc_passwd, vc_port,
                                                      params):
                    logger.info("Creating vSphere host{}".format(name))

            except Exception as e:  # Catch failure while connecting to vc.
                logger.error("Host {} cannot be setup".format(name))
                logger.error("{}".format(e))
                return {"msg": "{}".format(e)}

        hid = uuid4().hex
        host = HostModel(id=hid,
                         name=name,
                         worker_api=worker_api,
                         capacity=capacity,
                         type=host_type,
                         log_level=log_level,
                         log_type=log_type,
                         log_server=log_server,
                         autofill=autofill == "true",
                         schedulable=schedulable == "true"
                         )
        host.save()

        if capacity > 0 and autofill == "true":  # should autofill it
            self.fillup(str(hid))

        return self._schema(host)