def get_console_file(_file): """[get console file] Arguments: _file {[type]} -- [description] """ data = _file utils.file_must_exists(data) p2p_ip = mconf.MchainConf.p2p_ip channel_listen_port = mconf.MchainConf.channel_listen_port channel_addr = [] group_id = mconf.MchainConf.group_id utils.replace(data, '"group1', '"group{}'.format(group_id)) utils.replace(data, 'name="groupId" value="1"', 'name="groupId" value="{}"'.format(group_id)) for ip_idx, p2p_get in enumerate(p2p_ip): channel_addr.append('{}:{}'.format(p2p_get, channel_listen_port[ip_idx])) cmd = "cat {} | grep -n connectionsStr | awk '{{print $1}}'".format(data) (status, result) = utils.getstatusoutput(cmd) result = result.strip('\n').strip(':') if bool(status): LOGGER.error(' append console channel_addr failed, result is %s.', result) raise MCError(' append console channel_addr failed, result is %s.' % result) line_num = int(result) + 1 for channel in channel_addr: (status, result) \ = utils.getstatusoutput('sed -i "{} a' '<value>{}</value>" {}' .format(line_num, channel, data)) line_num = line_num + 1 CONSOLER.info('get console file end')
def replace(self): (address, publicKey, privateKey) = ('', '', '') if self.fisco.is_gm(): god_file = get_gm_god_path() + '/godInfo.txt' (address, publicKey, privateKey) = self.fromGod(god_file) genesis_path = path.get_path() + '/tpl/GM_temp_node/genesis.json' utils.replace(genesis_path, self.address, address) else: god_file = get_god_path() + '/godInfo.txt' (address, publicKey, privateKey) = self.fromGod(god_file) genesis_path = path.get_path() + '/tpl/temp_node/genesis.json' utils.replace(genesis_path, self.address, address)
def web3_conf_by_chain(chain, gm=False, web3_conf_path=None): cvp = ChainVerPort(chain.get_id(), chain.get_version()) web3_connect_nodes_list = '' for host, hp in cvp.get_ports().items(): for node, port in hp.get_ports().items(): web3_connect_nodes_list += ( '<value>%s_%s@%s:%d</value>' % (host, node, host, port.get_channel_port())) if (web3_conf_path is None) or (not os.path.exists(web3_conf_path)): if gm: shutil.copy( chain.data_dir() + '/' + 'common/web3sdk/conf/applicationContext_GM.xml', chain.data_dir() + '/' + 'common/web3sdk/conf/applicationContext.xml') else: shutil.copy( chain.data_dir() + '/' + 'common/web3sdk/conf/applicationContext_NB.xml', chain.data_dir() + '/' + 'common/web3sdk/conf/applicationContext.xml') web3_conf_path = chain.data_dir( ) + '/' + 'common/web3sdk/conf/applicationContext.xml' logger.info(' web3sdk config file is %s', web3_conf_path) return utils.replace(web3_conf_path, 'WEB3SDK_NODES_LIST', web3_connect_nodes_list)
def build(self): logger.info(' build temp dir, dir is %s', self.dir()) try: os.makedirs(self.dir()) shutil.copytree(path.get_path() + '/tpl/web3sdk', self.dir() + '/web3sdk') if self.is_gm(): shutil.move(self.dir() + '/web3sdk/conf/applicationContext_GM.xml', self.dir() + '/web3sdk/conf/applicationContext.xml') shutil.copytree(path.get_path() + '/tpl/GM_temp_node', self.dir() + '/node') shutil.copy(self.dir() + '/node/data/sdk/ca.crt', self.dir() + '/web3sdk/conf') shutil.copy(self.dir() + '/node/data/sdk/client.keystore', self.dir() + '/web3sdk/conf') else: shutil.move(self.dir() + '/web3sdk/conf/applicationContext_NB.xml', self.dir() + '/web3sdk/conf/applicationContext.xml') shutil.copytree(path.get_path() + '/tpl/temp_node', self.dir() + '/node') shutil.copy(self.dir() + '/node/sdk/ca.crt', self.dir() + '/web3sdk/conf') shutil.copy(self.dir() + '/node/sdk/client.keystore', self.dir() + '/web3sdk/conf') # copy fisco-bcos shutil.copy(self.fisco.get_fisco_path(), self.dir() + '/node/') # config.json for temp node Config('12345', self.port.get_rpc_port(), self.port.get_p2p_port(), self.port.get_channel_port(), self.is_gm()).writeFile(self.dir() + '/node/config.json') # web3sdk config for temp node utils.replace(self.dir() + '/web3sdk/conf/applicationContext.xml', 'WEB3SDK_NODES_LIST', '<value>[email protected]:%s</value>' % str(self.port.get_channel_port())) except Exception as e: logger.error( ' temp node build opr failed , chain is %s, exception is %s .', self.chain, e) raise MCError( ' build temp node failed, chain is %s, exception is %s ' % (self.chain, e))
def build_package_only(_data_dir): """[-- build create config_ini] Keyword Arguments: _meta_dir {[PATH]} -- [input dir] (default: {meta}) _data_dir {[PATH]} -- [output dir] (default: {data}) Raises: MCError -- [description] MCError -- [description] MCError -- [description] MCError -- [description] """ LOGGER.info("build_package_only start ") p2p_listen_port = mconf.MchainConf.p2p_listen_port jsonrpc_listen_port = mconf.MchainConf.jsonrpc_listen_port channel_listen_port = mconf.MchainConf.channel_listen_port p2p_ip = mconf.MchainConf.p2p_ip rpc_ip = mconf.MchainConf.rpc_ip channel_ip = mconf.MchainConf.channel_ip peers = mconf.MchainConf.peers meta_dir = '{}/meta'.format(path.get_path()) conf_dir = meta_dir package_dir = _data_dir gm_opr = utils.Status.gm_option if os.path.exists(package_dir): LOGGER.error(' %s existed, maybe u had created it!', package_dir) raise MCError(' %s existed, maybe u had created it!' % package_dir) os.mkdir(package_dir) if gm_opr: shutil.copy('{}/tpl/config.ini.gm'.format(path.get_path()), '{}/.config.ini'.format(conf_dir)) else: shutil.copy('{}/tpl/config.ini'.format(path.get_path()), '{}/.config.ini'.format(conf_dir)) if utils.Status.gm_ssl: utils.replace('{}/.config.ini'.format(conf_dir), 'sm_crypto_channel=false', 'sm_crypto_channel=true') fin_p2p_ip = [] if not peers: LOGGER.warning('section peers not existed!') CONSOLER.warn('section peers not existed!') else: for _, peer in enumerate(peers): fin_p2p_ip.append(peer) # init config.ini & node package for my_node_index, node_ip in enumerate(p2p_ip): LOGGER.info("p2p_ip -> %s", node_ip) CONSOLER.info(' Generate %s/node_%s_%s ', package_dir, node_ip, p2p_listen_port[my_node_index]) node_dir = '{}/node_{}_{}'.format(package_dir, node_ip, p2p_listen_port[my_node_index]) os.mkdir(node_dir) os.mkdir('{}/scripts'.format(node_dir)) shutil.copy('{}/tpl/start.sh'.format(path.get_path()), '{}/start.sh'.format(node_dir)) shutil.copy('{}/tpl/stop.sh'.format(path.get_path()), '{}/stop.sh'.format(node_dir)) shutil.copy('{}/tpl/load_new_groups.sh'.format(path.get_path()), '{}/scripts/load_new_groups.sh'.format(node_dir)) shutil.copy('{}/tpl/reload_whitelist.sh'.format(path.get_path()), '{}/scripts/reload_whitelist.sh'.format(node_dir)) shutil.copy('{}/fisco-bcos'.format(meta_dir), '{}/fisco-bcos'.format(node_dir)) os.mkdir('{}/conf'.format(node_dir)) try: # cp config.ini shutil.copy('{}/.config.ini'.format(conf_dir), '{}/config.ini'.format(node_dir)) except Exception as build_exp: LOGGER.error(' exception is %s', build_exp) utils.delete_data(package_dir) raise MCError(' exception is %s' % build_exp) node_cfg = configparser.ConfigParser(allow_no_value=True) try: with codecs.open('{}/config.ini'.format(node_dir), 'r', encoding='utf-8') as config_file: node_cfg.readfp(config_file) except Exception as build_exp: LOGGER.error(' open config.ini file failed, exception is %s', build_exp) utils.delete_data(package_dir) raise MCError(' open config.ini file failed, exception is %s' % build_exp) if len(rpc_ip) > my_node_index: node_cfg.set("rpc", "jsonrpc_listen_ip", rpc_ip[my_node_index]) else: node_cfg.set("rpc", "jsonrpc_listen_ip", "127.0.0.1") if len(channel_ip) > my_node_index: node_cfg.set("rpc", "channel_listen_ip", channel_ip[my_node_index]) else: node_cfg.set("rpc", "channel_listen_ip", "0.0.0.0") node_cfg.set("rpc", "channel_listen_port", channel_listen_port[my_node_index]) node_cfg.set("rpc", "jsonrpc_listen_port", jsonrpc_listen_port[my_node_index]) node_cfg.set("p2p", "listen_port", p2p_listen_port[my_node_index]) with open('{}/config.ini'.format(node_dir), 'w') as config_file: node_cfg.write(config_file) config_file.close() # set p2p ip in config.ini for my_node_index, ip_item in enumerate(p2p_ip): node_cfg = configparser.ConfigParser(allow_no_value=True) if not utils.valid_ip(ip_item): LOGGER.error(' init config.ini file failed, found ip => %s', ip_item) utils.delete_data(package_dir) raise MCError(' init config.ini file failed, found ip => %s' % ip_item) node_dir = '{}/node_{}_{}'.format(package_dir, ip_item, p2p_listen_port[my_node_index]) try: with codecs.open('{}/config.ini'.format(node_dir), 'r', encoding='utf-8') as config_file: node_cfg.readfp(config_file) except Exception as build_exp: LOGGER.error(' open config.ini file failed, exception is %s', build_exp) utils.delete_data(package_dir) raise MCError(' open config.ini file failed, exception is %s' % build_exp) # write p2pip:port into config.ini for ip_idx, set_item in enumerate(p2p_ip): fin_p2p_ip.append("{}:{}".format(set_item, p2p_listen_port[ip_idx])) fin_p2p_ip = list(set(fin_p2p_ip)) for index, p2p_section in enumerate(fin_p2p_ip): node_cfg.set("p2p", "node.{}".format(index), '{}'.format(p2p_section)) node_cfg.set('certificate_whitelist', '; cal.0 should be nodeid, nodeid\'s length is 128') node_cfg.set('certificate_whitelist', ';cal.0=') node_cfg.set('certificate_blacklist', '; crl.0 should be nodeid, nodeid\'s length is 128') node_cfg.set('certificate_blacklist', ';crl.0=') with open('{}/config.ini'.format(node_dir), 'w') as config_file: node_cfg.write(config_file) os.mkdir(package_dir + '/scripts/') shutil.copy('{}/scripts/install.sh'.format(path.get_path()), package_dir + '/scripts/') shutil.copy('{}/scripts/pack.sh'.format(path.get_path()), package_dir + '/scripts/') shutil.copy('{}/tpl/start_all.sh'.format(path.get_path()), package_dir) shutil.copy('{}/tpl/stop_all.sh'.format(path.get_path()), package_dir) shutil.copytree('{}/scripts/monitor'.format((path.get_path())), '{}/monitor'.format(package_dir)) LOGGER.info("build_package_only end!")