def _create_cluster(self, cluster, cid, mapped_ports, worker, config, user_id, peer_ports, ca_ports, orderer_ports, explorer_ports): # start compose project, failed then clean and return logger.debug("Start compose project with name={}".format(cid)) containers = self.cluster_agents[worker.type] \ .create(cid, mapped_ports, self.host_handler.schema(worker), config=config, user_id=user_id) if not containers: logger.warning("failed to start cluster={}, then delete" .format(cluster.name)) self.delete(id=cid, record=False, forced=True) return None # creation done, update the container table in db for k, v in containers.items(): container = Container(id=v, name=k, cluster=cluster) container.save() # service urls can only be calculated after service is created if worker.type == WORKER_TYPE_K8S: service_urls = self.cluster_agents[worker.type]\ .get_services_urls(cid) else: service_urls = self.gen_service_urls(cid, peer_ports, ca_ports, orderer_ports, explorer_ports) # update the service port table in db for k, v in service_urls.items(): service_port = ServicePort(name=k, ip=v.split(":")[0], port=int(v.split(":")[1]), cluster=cluster) service_port.save() # update api_url, container, user_id and status self.db_update_one( {"id": cid}, { "user_id": user_id, 'api_url': service_urls.get('rest', ""), 'service_url': service_urls, 'status': NETWORK_STATUS_RUNNING } ) def check_health_work(cid): time.sleep(60) self.refresh_health(cid) t = Thread(target=check_health_work, args=(cid,)) t.start() host = HostModel.objects.get(id=worker.id) host.update(add_to_set__clusters=[cid]) logger.info("Create cluster OK, id={}".format(cid))
def create(self, name, host_id, config, start_port=0, user_id=""): """ Create a cluster based on given data TODO: maybe need other id generation mechanism Args: name: name of the cluster host_id: id of the host URL config: network configuration start_port: first service port for cluster, will generate if not given user_id: user_id of the cluster if start to be applied return: Id of the created cluster or None """ logger.info("Create cluster {}, host_id={}, config={}, start_port={}, " "user_id={}".format(name, host_id, config.get_data(), start_port, user_id)) worker = self.host_handler.get_active_host_by_id(host_id) if not worker: logger.error("Cannot find available host to create new network") return None if ClusterModel.objects(host=worker).count() >= worker.capacity: logger.warning("host {} is already full".format(host_id)) return None if worker.type == WORKER_TYPE_VSPHERE: vm_params = self.host_handler.get_vm_params_by_id(host_id) docker_daemon = vm_params.get(VMIP) + ":2375" worker.update({"worker_api": "tcp://" + docker_daemon}) logger.info(worker) peer_num = int(config.get_data().get("size", 4)) ca_num = 2 if peer_num > 1 else 1 cid = uuid4().hex mapped_ports, peer_ports, ca_ports, orderer_ports, explorer_ports = \ self.gen_ports_mapping(peer_num, ca_num, start_port, host_id) if not mapped_ports: logger.error("mapped_ports={}".format(mapped_ports)) return None env_mapped_ports = dict(((k + '_port').upper(), str(v)) for (k, v) in mapped_ports.items()) network_type = config['network_type'] net = { # net is a blockchain network instance 'id': cid, 'name': name, 'user_id': user_id, 'worker_api': worker.worker_api, 'network_type': network_type, # e.g., fabric-1.0 'env': env_mapped_ports, 'status': NETWORK_STATUS_CREATING, 'mapped_ports': mapped_ports, 'service_url': {}, # e.g., {rest: xxx:7050, grpc: xxx:7051} } net.update(config.get_data()) # try to start one cluster at the host cluster = ClusterModel(**net) cluster.host = worker cluster.save() # start compose project, failed then clean and return logger.debug("Start compose project with name={}".format(cid)) containers = self.cluster_agents[worker.type] \ .create(cid, mapped_ports, self.host_handler.schema(worker), config=config, user_id=user_id) if not containers: logger.warning("failed to start cluster={}, then delete" .format(name)) self.delete(id=cid, record=False, forced=True) return None # creation done, update the container table in db for k, v in containers.items(): container = Container(id=v, name=k, cluster=cluster) container.save() # service urls can only be calculated after service is created service_urls = self.gen_service_urls(cid, peer_ports, ca_ports, orderer_ports, explorer_ports) # update the service port table in db for k, v in service_urls.items(): service_port = ServicePort(name=k, ip=v.split(":")[0], port=int(v.split(":")[1]), cluster=cluster) service_port.save() # update api_url, container, user_id and status self.db_update_one( {"id": cid}, { "user_id": user_id, 'api_url': service_urls.get('rest', ""), 'service_url': service_urls, 'status': NETWORK_STATUS_RUNNING } ) def check_health_work(cid): time.sleep(5) self.refresh_health(cid) t = Thread(target=check_health_work, args=(cid,)) t.start() host = HostModel.objects.get(id=host_id) host.update(add_to_set__clusters=[cid]) logger.info("Create cluster OK, id={}".format(cid)) return cid