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
        """
        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.º 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
        """
        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.º 3
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.º 4
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)