def create(self, worker_api): """ 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 worker_api: worker_api of the host :return: True or False """ if check_daemon(worker_api): logger.warning("The worker_api is active:" + worker_api) else: logger.warning("The worker_api is inactive:" + worker_api) return False detected_type = detect_daemon_type(worker_api) if detected_type != self.host_type: logger.warning("Host type={} should be same with the initialized \ type={}".format(detected_type, self.host_type)) return False if detected_type not in ['docker', 'swarm']: logger.warning("Detected type={} is not docker or swarm".format( detected_type)) return False if setup_container_host(detected_type, worker_api): return True else: logger.warning( "Cannot setup Docker host worker_api={}".format(worker_api)) return False
def create(self, worker_api): """ 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 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 :return: True or False """ if check_daemon(worker_api): logger.warning("The worker_api is active:" + worker_api) else: logger.warning("The worker_api is inactive:" + worker_api) return False detected_type = detect_daemon_type(worker_api) if detected_type != self.host_type: logger.warning("Host type={} should be same with the initialized \ type={}".format(detected_type, self.host_type)) return False if detected_type not in ['docker', 'swarm']: logger.warning("Detected type={} is not docker or swarm". format(detected_type)) return False if setup_container_host(detected_type, worker_api): return True else: logger.warning("Cannot setup Docker host worker_api={}" .format(worker_api)) return False
def create(self, worker_api): """ 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 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 :return: True or False """ if check_daemon(worker_api): logger.warning("The worker_api is active:" + worker_api) else: logger.warning("The worker_api is inactive:" + worker_api) return False detected_type = detect_daemon_type(worker_api) if detected_type != self.host_type: logger.warning("Host type={} should be same with the initialized \ type={}".format(detected_type, self.host_type)) return False if detected_type not in ['docker', 'swarm']: logger.warning("Detected type={} is not docker or swarm".format( detected_type)) return False if setup_container_host(detected_type, worker_api): return True else: logger.warning( "Cannot setup Docker host worker_api={}".format(worker_api)) return False
def create(self, name, daemon_url, capacity=1, log_level=CLUSTER_LOG_LEVEL[0], log_type=CLUSTER_LOG_TYPES[0], log_server="", autofill="false", schedulable="false", serialization=True): """ 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 daemon_url: daemon_url of the host :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 :return: True or False """ logger.debug("Create host: name={}, daemon_url={}, capacity={}, " "log={}/{}, autofill={}, schedulable={}" .format(name, daemon_url, capacity, log_type, log_server, autofill, schedulable)) if not daemon_url.startswith("tcp://"): daemon_url = "tcp://" + daemon_url if self.col.find_one({"daemon_url": daemon_url}): logger.warning("{} already existed in db".format(daemon_url)) return {} if "://" not in log_server: log_server = "udp://" + log_server if log_type == CLUSTER_LOG_TYPES[0]: log_server = "" if check_daemon(daemon_url): logger.warning("The daemon_url is active:" + daemon_url) status = "active" else: logger.warning("The daemon_url is inactive:" + daemon_url) status = "inactive" detected_type = detect_daemon_type(daemon_url) if not setup_container_host(detected_type, daemon_url): logger.warning("{} cannot be setup".format(name)) return {} h = { 'id': '', 'name': name, 'daemon_url': daemon_url, 'create_ts': datetime.datetime.now(), 'capacity': capacity, 'status': status, 'clusters': [], 'type': detected_type, 'log_level': log_level, 'log_type': log_type, 'log_server': log_server, 'autofill': autofill, 'schedulable': schedulable } hid = self.col.insert_one(h).inserted_id # object type host = self.db_update_one( {"_id": hid}, {"$set": {"id": str(hid)}}) if capacity > 0 and autofill == "true": # should autofill it self.fillup(str(hid)) if serialization: return self._serialize(host) else: return host
def setup_vm(self, connection, params): """ setup a new vritualmachine. :param connection: vc connection return by initializesi :param params: :return: """ # vmname comes from upstream layer. vm = params.get(utils.VIRTUAL_MACHINE) vc = params.get(utils.VCENTER) # Get vm params vmname = vm.get(utils.VMNAME) vmmem = vm.get(utils.VMMEMORY) vmcpunum = vm.get(utils.VMCPU) vmip = vm.get(utils.VMIP) vmnetmask = vm.get(utils.VMNETMASK) vmdns = vm.get(utils.VMDNS) vmgateway = vm.get(utils.VMGATEWAY) # Get vc params template = vc.get(utils.TEMPLATE) datacenter = vc.get(utils.VC_DATACENTER) cluster = vc.get(utils.VC_CLUSTER) datastore = vc.get(utils.VC_DATASTORE) network = vc.get(utils.NETWORK) destfolder = datacenter.vmFolder vmconf = vim.vm.ConfigSpec(numCPUs=vmcpunum, memoryMB=vmmem * 1024) # nic nic = vim.vm.device.VirtualDeviceSpec() nic.operation = vim.vm.device.VirtualDeviceSpec.Operation.edit nic.device = vim.vm.device.VirtualVmxnet3() nic.device.wakeOnLanEnabled = True nic.device.addressType = utils.NIC_DEVICE_ADDRESS_TYPE nic.device.key = 4000 nic.device.deviceInfo = vim.Description() nic.device.backing = vim.vm.device. \ VirtualEthernetCard.NetworkBackingInfo() nic.device.backing.network = network nic.device.backing.useAutoDetect = False nic.device.connectable = vim.vm.device.VirtualDevice.ConnectInfo() nic.device.connectable.startConnected = True nic.device.connectable.allowGuestControl = True # guest_map guest_map = vim.vm.customization.AdapterMapping() guest_map.adapter = vim.vm.customization.IPSettings() guest_map.adapter.ip = vim.vm.customization.FixedIp() guest_map.adapter.ip.ipAddress = vmip guest_map.adapter.subnetMask = vmnetmask guest_map.adapter.gateway = vmgateway globalip = vim.vm.customization.GlobalIPSettings(dnsServerList=vmdns) name = vim.vm.customization.FixedName(name=utils.VM_DEFAULT_HOSTNAME) ident = vim.vm.customization.LinuxPrep(hostName=name) customspec = vim.vm.customization. \ Specification(nicSettingMap=[guest_map], globalIPSettings=globalip, identity=ident) # relocateSpec relocatespec = vim.vm.RelocateSpec() relocatespec.datastore = datastore relocatespec.pool = cluster.resourcePool clonespec = vim.vm.CloneSpec() clonespec.location = relocatespec clonespec.customization = customspec clonespec.config = vmconf clonespec.powerOn = True host = { utils.VMUUID: '', utils.VMIP: vmip, utils.VMNETMASK: vmnetmask, utils.VMDNS: vmdns, utils.VMGATEWAY: vmgateway, utils.VMCPU: vmcpunum, utils.VMMEMORY: vmmem, utils.VC_CLUSTER: cluster.name, utils.VC_DATASTORE: datastore.name, utils.VC_DATACENTER: datacenter.name, } try: task = template.Clone(folder=destfolder, name=vmname, spec=clonespec) # Block here for vm creation. self.wait_for_task(task) vm = self.check_object(connection, [vim.VirtualMachine], vmname) workerapi = "tcp://" + vmip + ":2375" uuid = vm.summary.config.uuid host.update({utils.VMUUID: uuid}) if self.check_isport_open(vmip, utils.WORKER_API_PORT, utils.DEFAULT_TIMEOUT): if (self.pull_images(workerapi) and setup_container_host( utils.WORKER_TYPE_DOCKER, workerapi)): host['status'] = 'active' logger.info(host) else: host["status"] = 'error' logger.error("Failed to setup container host") else: host["status"] = 'error' logger.error("Failed to ping docker daemon:{}:{}".format( vmip, "2375")) self.col.find_one_and_update({utils.VMNAME: vmname}, {"$set": host}) # Should be safe to delete vm though VsphereHost layer. except Exception as e: logger.error(e) self.col.delete_one({utils.VMNAME: vmname}) return
def create(self, name, daemon_url, capacity=1, log_level=CLUSTER_LOG_LEVEL[0], log_type=CLUSTER_LOG_TYPES[0], log_server="", autofill="false", schedulable="false", serialization=True): """ 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 daemon_url: daemon_url of the host :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 :return: True or False """ logger.debug("Create host: name={}, daemon_url={}, capacity={}, " "log={}/{}, autofill={}, schedulable={}".format( name, daemon_url, capacity, log_type, log_server, autofill, schedulable)) if not daemon_url.startswith("tcp://"): daemon_url = "tcp://" + daemon_url if self.col.find_one({"daemon_url": daemon_url}): logger.warning("{} already existed in db".format(daemon_url)) return {} if "://" not in log_server: log_server = "udp://" + log_server if log_type == CLUSTER_LOG_TYPES[0]: log_server = "" if check_daemon(daemon_url): logger.warning("The daemon_url is active:" + daemon_url) status = "active" else: logger.warning("The daemon_url is inactive:" + daemon_url) status = "inactive" detected_type = detect_daemon_type(daemon_url) if not setup_container_host(detected_type, daemon_url): logger.warning("{} cannot be setup".format(name)) return {} h = { 'id': '', 'name': name, 'daemon_url': daemon_url, 'create_ts': datetime.datetime.now(), 'capacity': capacity, 'status': status, 'clusters': [], 'type': detected_type, 'log_level': log_level, 'log_type': log_type, 'log_server': log_server, 'autofill': autofill, 'schedulable': schedulable } hid = self.col.insert_one(h).inserted_id # object type host = self.db_update_one({"_id": hid}, {"$set": {"id": str(hid)}}) if capacity > 0 and autofill == "true": # should autofill it self.fillup(str(hid)) if serialization: return self._serialize(host) else: return host
def setup_vm(self, connection, params, hid): """ setup a new vritualmachine. :param connection: vc connection return by initializesi :param params: :return: """ # vmname comes from upstream layer. vm = params.get(utils.VIRTUAL_MACHINE) vc = params.get(utils.VCENTER) # Get vm params vmname = vm.get(utils.VMNAME) vmmem = vm.get(utils.VMMEMORY) vmcpunum = vm.get(utils.VMCPU) vmip = vm.get(utils.VMIP) vmnetmask = vm.get(utils.VMNETMASK) vmdns = vm.get(utils.VMDNS) vmgateway = vm.get(utils.VMGATEWAY) # Get vc params vcip = vc.get(utils.VCIP) vcusername = vc.get(utils.VCUSERNAME) vcpwd = vc.get(utils.VCPWD) vcport = vc.get(utils.VCPORT) template = vc.get(utils.TEMPLATE) datacenter = vc.get(utils.VC_DATACENTER) cluster = vc.get(utils.VC_CLUSTER) datastore = vc.get(utils.VC_DATASTORE) network = vc.get(utils.NETWORK) destfolder = datacenter.vmFolder vmconf = vim.vm.ConfigSpec(numCPUs=vmcpunum, memoryMB=vmmem * 1024) # nic nic = vim.vm.device.VirtualDeviceSpec() nic.operation = vim.vm.device.VirtualDeviceSpec.Operation.edit nic.device = vim.vm.device.VirtualVmxnet3() nic.device.wakeOnLanEnabled = True nic.device.addressType = utils.NIC_DEVICE_ADDRESS_TYPE nic.device.key = 4000 nic.device.deviceInfo = vim.Description() nic.device.backing = vim.vm.device. \ VirtualEthernetCard.NetworkBackingInfo() nic.device.backing.network = network nic.device.backing.useAutoDetect = False nic.device.connectable = vim.vm.device.VirtualDevice.ConnectInfo() nic.device.connectable.startConnected = True nic.device.connectable.allowGuestControl = True # guest_map guest_map = vim.vm.customization.AdapterMapping() guest_map.adapter = vim.vm.customization.IPSettings() guest_map.adapter.ip = vim.vm.customization.FixedIp() guest_map.adapter.ip.ipAddress = vmip guest_map.adapter.subnetMask = vmnetmask guest_map.adapter.gateway = vmgateway globalip = vim.vm.customization.GlobalIPSettings( dnsServerList=vmdns) name = vim.vm.customization.FixedName(name=utils.VM_DEFAULT_HOSTNAME) ident = vim.vm.customization.LinuxPrep(hostName=name) customspec = vim.vm.customization. \ Specification(nicSettingMap=[guest_map], globalIPSettings=globalip, identity=ident) # relocateSpec relocatespec = vim.vm.RelocateSpec() relocatespec.datastore = datastore relocatespec.pool = cluster.resourcePool clonespec = vim.vm.CloneSpec() clonespec.location = relocatespec clonespec.customization = customspec clonespec.config = vmconf clonespec.powerOn = True try: host = HostModel.Query.get(id=hid) except Exception: logger.error("No vsphere host found with id=" + hid) return host.vcparam = { utils.VMUUID: '', utils.VMIP: vmip, utils.VMNETMASK: vmnetmask, utils.VMDNS: vmdns, utils.VMGATEWAY: vmgateway, utils.VMCPU: vmcpunum, utils.VMMEMORY: vmmem, utils.VC_CLUSTER: cluster.name, utils.VC_DATASTORE: datastore.name, utils.VC_DATACENTER: datacenter.name, utils.VCIP: vcip, utils.VCUSERNAME: vcusername, utils.VCPWD: vcpwd, utils.VCPORT: vcport } try: task = template.Clone(folder=destfolder, name=vmname, spec=clonespec) # Block here for vm creation. self.wait_for_task(task) vm = self.check_object(connection, [vim.VirtualMachine], vmname) workerapi = "tcp://" + vmip + ":2375" host.worker_api = workerapi uuid = vm.summary.config.uuid host.vcparam[utils.VMUUID] = uuid host.save() if self.check_isport_open(vmip, utils.WORKER_API_PORT, utils.DEFAULT_TIMEOUT): if (self.pull_images(workerapi) and setup_container_host(utils.WORKER_TYPE_DOCKER, workerapi)): host.status = 'active' logger.info(host) else: host.status = 'error' logger.error("Failed to setup container host") else: host.status = 'error' logger.error("Failed to ping docker daemon:{}:{}" .format(vmip, "2375")) host.save() # Should be safe to delete vm though VsphereHost layer. except Exception as e: logger.error(e) if self.check_isport_open(vmip, utils.WORKER_API_PORT, utils.DEFAULT_TIMEOUT): host = HostModel.Query.get(id=hid) uuid = host.vcparam[utils.VMUUID] self.delete_vm(vcip, vcusername, vcpwd, vcport, uuid) host.delete() return