def delete(self, network): """ Delete a cluster instance Clean containers, remove db entry. Only operate on active host. :param id: id of the cluster to delete :param forced: Whether to removing user-using cluster, for release :return: """ logger.debug("Delete cluster: id={}".format(network.id)) network.update(set__status='deleting') net_id = network.id try: #self.host_agents[host.type].delete(network) # remove cluster info from host logger.info("remove network from host, network:{}".format( network.id)) # if org has referenced network, remove for org_id in network.peer_orgs: peer_org = org_handler().schema( org_handler().get_by_id(org_id)) host_id = peer_org['host_id'] host_handler.refresh_status(host_id) host = host_handler.get_active_host_by_id(host_id) host.update(pull__clusters=network.id) self.host_agents[host.type].delete_peer_org( peer_org, host, net_id) org_obj = modelv2.Organization.objects.get(id=org_id) org_obj.update(unset__network=network.id) for org_id in network.orderer_orgs: orderer_org = org_handler().schema( org_handler().get_by_id(org_id)) host_id = orderer_org['host_id'] host_handler.refresh_status(host_id) host = host_handler.get_active_host_by_id(host_id) consensus_type = network.consensus_type host.update(pull__clusters=network.id) self.host_agents[host.type].delete_orderer_org( orderer_org, consensus_type, host, net_id) org_obj = modelv2.Organization.objects.get(id=org_id) org_obj.update(unset__network=network.id) #从Userdashboard的mongo中删除该network相关的数据 self.userdashboard_mongo_delete(network.id) network.delete() filepath = '{}{}'.format(CELLO_MASTER_FABRIC_DIR, network.id) os.system('rm -rf {}'.format(filepath)) return except Exception as e: logger.info("remove network {} fail from host".format(network.id)) network.update(set__status='error') raise e
def _update_network_for_addpeers(self, network_config, request_host_ports): net_id = network_config['id'] network = modelv2.BlockchainNetwork.objects.get(id=net_id) try: #self.host_agents[host.type].update(network_config, request_host_ports) # # service urls can only be calculated after service is create # if host.type == WORKER_TYPE_K8S: # service_urls = self.host_agents[host.type] \ # .get_services_urls(net_id) # else: # service_urls = self.gen_service_urls(net_id) net_id = network_config['id'] net_name = network_config['name'] peer_num = network_config['peer_num'] peer_org = network_config['peer_org_dict'] couchdb_enabled = False if network_config['db_type'] == 'couchdb': couchdb_enabled = True fabric_version = fabric_image_version[ network_config['fabric_version']] portid = [] portid.append(0) host_id = peer_org['host_id'] host = host_handler.get_active_host_by_id(host_id) host_handler.refresh_status(host_id) self.host_agents[host.type].create_peer_org( peer_org, couchdb_enabled, host, net_id, net_name, fabric_version, request_host_ports, portid, peer_num) network.update(set__status='running') logger.info("Update network OK, id={}".format(net_id)) except Exception as e: logger.error("network {} update failed for {}".format(net_id, e)) # will not call self.delete(network) in case of nested exception #self.delete(network) raise e
def host_check(host_id, retries=3, period=3): """ Run check on specific host. Check status and check each chain's health. :param host_id: id of the checked host :param retries: how many retries before thnking it's inactive :param period: retry wait :return: """ for _ in range(retries): if host_handler.refresh_status(host_id): # host is active logger.debug("Host {}/{} is active, start checking".format( host_handler.get_by_id(host_id).get('name'), host_id)) host_check_chains(host_id) time.sleep(period) host_check_fillup(host_id) break time.sleep(period)
def host_check(host_id, retries=3, period=3): """ Run check on specific host. Check status and check each chain's health. :param host_id: id of the checked host :param retries: how many retries before thnking it's inactive :param period: retry wait :return: """ for _ in range(retries): if host_handler.refresh_status(host_id): # host is active logger.debug("Host {}/{} is active, start checking".format( host_handler.get_by_id(host_id).get('name'), host_id)) host_check_chains(host_id) time.sleep(period) host_check_fillup(host_id) break time.sleep(period)
def _create_network(self, network_config, request_host_ports): net_id = network_config['id'] network = modelv2.BlockchainNetwork.objects.get(id=net_id) try: #self.host_agents[host.type].create(network_config, request_host_ports) # # service urls can only be calculated after service is create # if host.type == WORKER_TYPE_K8S: # service_urls = self.host_agents[host.type] \ # .get_services_urls(net_id) # else: # service_urls = self.gen_service_urls(net_id) net_id = network_config['id'] net_name = network_config['name'] couchdb_enabled = False if network_config['db_type'] == 'couchdb': couchdb_enabled = True fabric_version = fabric_image_version[ network_config['fabric_version']] consensus_type = network_config['consensus_type'] portid = [] portid.append(0) for orderer_org in network_config['orderer_org_dicts']: host_id = orderer_org['host_id'] host_handler.refresh_status(host_id) host = host_handler.get_active_host_by_id(host_id) host.update(add_to_set__clusters=[net_id]) self.host_agents[host.type].create_orderer_org( orderer_org, consensus_type, host, net_id, net_name, fabric_version, request_host_ports, portid) time.sleep(5) for peer_org in network_config['peer_org_dicts']: host_id = peer_org['host_id'] peer_num = peer_org['peerNum'] host_handler.refresh_status(host_id) host = host_handler.get_active_host_by_id(host_id) host.update(add_to_set__clusters=[net_id]) self.host_agents[host.type].create_peer_org( peer_org, couchdb_enabled, host, net_id, net_name, fabric_version, request_host_ports, portid, peer_num) network.update(set__status='running') # zsh修改,为解决网络创建过程中,还可以继续操作组织的问题,将给组织增加网络的动作放到前面 # for peer_org in network_config['peer_org_dicts']: # org_obj = modelv2.Organization.objects.get(id=peer_org['id']) # org_obj.update(set__network=network) # for orderer_org in network_config['orderer_org_dicts']: # org_obj = modelv2.Organization.objects.get(id=orderer_org['id']) # org_obj.update(set__network=network) logger.info("Create network OK, id={}".format(net_id)) def check_health_work(network): time.sleep(180) self.refresh_health(network) t = Thread(target=check_health_work, args=(network, )) t.start() except Exception as e: logger.error("network {} create failed for {}".format(net_id, e)) # will not call self.delete(network) in case of nested exception self.delete(network) raise e