def publish_server(chain_id, chain_version, force=False): """publish one chain. Arguments: chain_id {string} -- chain id. chain_version {string} -- chain version. force {bool} """ chain = Chain(chain_id, chain_version) dir = chain.data_dir() if not os.path.isdir(dir): consoler.info( ' No build version exist for chain_id:%s chain_version:%s, do nothing.', chain_id, chain_version) logger.warn( ' No build version exist for chain_id:%s chain_version:%s, do nothing.', chain_id, chain_version) return mm = meta.Meta(chain_id) if force: # force is set, publish this chain again. mm.clear() mm.set_chain_version(chain_version) else: if mm.exist(): if chain_version != mm.get_chain_version(): consoler.error( ' \033[1;31m chain %s already publish %s version, if you want publish annother version, --force/-f need to be set.\033[0m ', chain_id, mm.get_chain_version()) return else: mm.set_chain_version(chain_version) consoler.info(' publish package for chain %s version %s begin.', chain_id, chain_version) for host in os.listdir(dir): if not utils.valid_ip(host): logger.debug(' skip, not invalid host_ip ' + host) continue ret = push_package(dir, host, chain_id, chain_version, mm, force) if ret: hp = HostPort(chain_id, chain_version, host) for node_dir, p in hp.get_ports().items(): logger.debug(' node_dir is %s, port is %s', node_dir, p) if not mm.host_node_exist(host, node_dir): mm.append(meta.MetaNode(host, p.get_rpc_port(), p.get_p2p_port(), p.get_channel_port(), node_dir)) consoler.info(' \t push package : %s success.', host) else: consoler.error(' \033[1;31m \t push package : %s failed. \033[0m', host) # record meta info, write meta.json file mm.write_to_file() consoler.info(' publish package for chain %s version %s end.', chain_id, chain_version)
def publish_chain(chains, force = False): """Publish a chain to designated servers using publish_server Arguments: chains {list} -- chain which you wanto published, type chain_id:chain_version. Returns: null """ pchains = [] for i in range(len(chains)): chain = chains[i].split(':') if len(chain) != 2: logger.error('not chain_id:chain_version format, str is %s', chains[i]) consoler.error(' \033[1;31m skip, invalid publish format, chain_id:chain_version should require, chain is %s \033[0m', chain) continue chain_id = chain[0] chain_version = chain[1] pchains.append(Chain(chain_id, chain_version)) # consoler.info('\t append publish chain, chain_id %s:chain_version %s', chain_id, chain_version) logger.debug('chain_id is %s, chain_version is %s', chain_id, chain_version) if len(pchains) != 0: for chain in pchains: logger.info('publish, chain_id is %s, chain_version is %s', chain_id, chain_version) publish_server(chain.get_id(), chain.get_version(), force)
def remove(self): if self.exist(): host_dir = Chain( self.chain_id, self.chain_version).data_dir() + '/' + self.host + '/' shutil.rmtree(host_dir) return not self.exist()
def create(self): if not self.exist(): host_dir = Chain( self.chain_id, self.chain_version).data_dir() + '/' + self.host + '/' os.makedirs(host_dir) return True return self.exist()
def load(self): self.clear() hn = HostNodeDirs(self.chain_id, self.chain_version, self.host) host_dir = Chain(self.chain_id, self.chain_version).data_dir() + '/' + self.host + '/' for node in hn.get_node_dirs(): cfg_json = host_dir + '/' + node + '/config.json' if not os.path.exists(cfg_json): logger.error(' config not exist, config is %s .', cfg_json) continue cf = Config(self.chain_id) if cf.fromJson(cfg_json): p = Port(int(cf.get_rpc_port()), int(cf.get_p2p_port()), int(cf.get_channel_port())) self.ports[node] = p logger.debug(' append node, node is %s, port is %s', node, p) logger.info(' load end, hp ports is %s', self)
def load(self): self.clear() host_dir = Chain(self.chain_id, self.chain_version).data_dir() + '/' + self.host + '/' if not os.path.exists(host_dir): logger.info( ' host dir not exist, chain_id is %s, chain_version is %s, host is %s', self.chain_id, self.chain_version, self.host) return logger.debug( 'load begin, chain_id is %s, chain_version is %s, host is %s', self.chain_id, self.chain_version, self.host) for list_dir in os.listdir(host_dir): if 'node' in list_dir: self.node_dirs.append(list_dir) index = int(list_dir[4:]) if index > self.max_index: self.max_index = index logger.debug(' append node%d, dir is %s', index, list_dir) logger.info(' load end, info %s', self)
def exist(self): dir = Chain(self.chain_id, self.chain_version).data_dir() return os.path.exists(dir)
def __init__(self, chain_id, chain_version): self.chain_id = chain_id self.chain_version = chain_version self.chain = Chain(self.chain_id, self.chain_version) self.pkg_list = [] self.load()
class VerHosts: """all package of chain of the version """ def __init__(self, chain_id, chain_version): self.chain_id = chain_id self.chain_version = chain_version self.chain = Chain(self.chain_id, self.chain_version) self.pkg_list = [] self.load() def __repr__(self): return 'chain is %s, list = %s' % (self.chain, self.pkg_list) def get_chain_id(self): return self.chain_id def get_pkg_list(self): return self.pkg_list def get_chain_version(self): return self.chain_version def get_version_dir(self): ver_dir = Chain(self.chain_id, self.chain_version).data_dir() return ver_dir def get_host_dir(self, host): host_dir = Chain(self.chain_id, self.chain_version).data_dir() + '/' + host + '/' return host_dir def append(self, host): self.pkg_list.append(host) def clear(self): self.pkg_list = [] def empty(self): return len(self.pkg_list) == 0 def exist(self): dir = self.chain.data_dir() return os.path.exists(dir) def load(self): self.clear() if not self.exist(): logger.info('dir not exist, chain_id is %s, chain_version is %s', self.chain_id, self.chain_version) return dir = self.chain.data_dir() logger.debug( 'load begin, chain_id is %s, chain_version is %s, dir is %s', self.chain_id, self.chain_version, dir) for host in os.listdir(dir): if utils.valid_ip(host): self.append(host) logger.debug(' chain id %s, chain version %s, host is %s', self.chain_id, self.chain_version, host) else: logger.debug( ' skip, not invalid host_ip, chain id is %s, chain version is %s, host is %s', self.chain_id, self.chain_version, host) logger.info('load end, len is %d', len(self.get_pkg_list()))
def exist(self): host_dir = Chain(self.chain_id, self.chain_version).data_dir() + '/' + self.host + '/' return os.path.exists(host_dir)
def get_host_dir(self): return Chain(self.chain_id, self.chain_version).data_dir() + '/' + self.host + '/'
def get_node_dir(self, index): return Chain(self.chain_id, self.chain_version).data_dir( ) + '/' + self.host + '/' + str(index) + '/'
def get_host_dir(self, host): host_dir = Chain(self.chain_id, self.chain_version).data_dir() + '/' + host + '/' return host_dir
def get_version_dir(self): ver_dir = Chain(self.chain_id, self.chain_version).data_dir() return ver_dir
def parser(self): ''' resolve config.conf, return object[Config.conf] ''' cfg = self.cfg logger.info('cfg parser %s', cfg) # read and parser config file cf = configparser.ConfigParser() with codecs.open(cfg, 'r', encoding='utf-8') as f: cf.readfp(f) chain_id = cf.get('chain', 'chainid') if not utils.valid_string(chain_id): raise Exception('chain_id empty.') if not utils.valid_chain_id(chain_id): raise Exception('invalid chain_id, ', chain_id) chain_version = cf.get('chain', 'version') if not utils.valid_string(chain_id): raise Exception('chain_version empty.') chain_name = str(chain_id) try: chain_name = cf.get('chain', 'chainname') if not utils.valid_string(chain_name): chain_name = str(id) except Exception as e: pass self.set_chain(Chain(chain_id, chain_version, chain_name)) rpc_port = cf.getint('ports', 'rpc_port') if not utils.valid_port(rpc_port): raise Exception('invalid rpc_port, ', rpc_port) p2p_port = cf.getint('ports', 'p2p_port') if not utils.valid_port(p2p_port): raise Exception('invalid p2p_port, ', p2p_port) channel_port = cf.getint('ports', 'channel_port') if not utils.valid_port(channel_port): raise Exception('invalid channel_port, ', channel_port) port = Port(rpc_port, p2p_port, channel_port) if port.check_port(): raise Exception('port config dup, ', port) self.set_port(port) index = 0 while True: try: n = NodeEle(cf.get('pkgs', 'pkg%u' % index)) index += 1 n.do_parser() except Exception as e: logger.info('cfg parser end, e is %s, result is %s', e, self) break else: if not self.add_node(n): raise Exception(' duplicate host ip, host is ', n.get_host_ip()) if len(self.get_nodes()) == 0: raise Exception('invalid cfg format, nodes empty') logger.info('cfg parser end, result is %s', self)