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 add_peers2cfg(_peers, _node): """[summary] Arguments: _peers {[type]} -- [description] _node {[type]} -- [description] """ data_path = _peers p2p_list = [] node_send = [] utils.file_must_exists(data_path) try: for line in open(data_path): peer = line.strip('\n') utils.valid_peer(peer) p2p_list.append(peer) except Exception as ini_exp: LOGGER.error( ' add peers %s file failed, exception is %s', data_path, ini_exp) raise MCError( ' add peers %s file failed, exception is %s' % (data_path, ini_exp)) LOGGER.info('merge peers is %s', p2p_list) p2p_list = list(set(p2p_list)) node_send = utils.get_all_nodes_dir(_node) for node_file in node_send: utils.file_must_exists('{}/config.ini'.format(node_file)) merge_cfg(p2p_list, '{}/config.ini'.format(node_file))
def add_group(_group, _node): """ Arguments: _group {[type]} -- [description] _node {[type]} -- [description] """ data_path = _group node_send = [] utils.file_must_exists(data_path) file_name = os.path.basename(data_path) group_id = utils.valid_genesis(file_name) if group_id == 0: raise MCError(' paser %s file failed' % (data_path)) node_name = os.path.basename(os.path.normpath(_node)) if utils.valid_node_dir(node_name): utils.file_must_not_exists('{}/conf/{}'.format(_node, file_name)) shutil.copyfile(data_path, '{}/conf/{}'.format(_node, file_name)) shutil.copyfile('{}/tpl/group.i.ini'.format(path.get_path()), '{}/conf/group.{}.ini'.format(_node, group_id)) else: node_send = utils.get_all_nodes_dir(_node) for node_file in node_send: utils.file_must_not_exists('{}/conf/{}'.format( node_file, file_name)) shutil.copyfile(data_path, '{}/conf/{}'.format(node_file, file_name)) shutil.copyfile('{}/tpl/group.i.ini'.format(path.get_path()), '{}/conf/group.{}.ini'.format(node_file, group_id))
def concatenate_cfg(cfg_file, cfg_file_get): """[combine two config.ini] Arguments: cfg_file {[type]} -- [description] cfg_file_get {[type]} -- [description] Raises: MCError -- [description] """ LOGGER.info("concatenate two config.ini now!") meta = cfg_file data = cfg_file_get utils.file_must_exists(meta) utils.file_must_exists(data) p2p_get = [] p2p_get_ip = [] p2p_send = [] p2p_send_ip = [] p2p_cfg = configparser.ConfigParser() try: with codecs.open(meta, 'r', encoding='utf-8') as config_file: p2p_cfg.readfp(config_file) except Exception as build_exp: LOGGER.error( ' open config.ini file failed, exception is %s', build_exp) raise MCError( ' open config.ini file failed, exception is %s' % build_exp) p2p_get = p2p_cfg.items('p2p') p2p_get.pop(0) p2p_get.pop(0) LOGGER.info("get node is %s!", p2p_get) for node_tuple in p2p_get: p2p_get_ip.append(node_tuple[1]) LOGGER.info("get node ip is %s!", p2p_get_ip) try: with codecs.open(data, 'r', encoding='utf-8') as config_file: p2p_cfg.readfp(config_file) except Exception as build_exp: LOGGER.error( ' open config.ini file failed, exception is %s', build_exp) raise MCError( ' open config.ini file failed, exception is %s' % build_exp) p2p_send = p2p_cfg.items('p2p') p2p_send.pop(0) p2p_send.pop(0) LOGGER.info("send node is %s!", p2p_send) for node_tuple in p2p_send: p2p_send_ip.append(node_tuple[1]) LOGGER.info("get node ip is %s!", p2p_send_ip) p2p_send_ip = list(set(p2p_get_ip + p2p_send_ip)) LOGGER.info("final node ip is %s!", p2p_send_ip) for ip_idx, p2p_ip in enumerate(p2p_send_ip): p2p_cfg.set("p2p", "node.{}".format(ip_idx), p2p_ip) with open(data, 'w') as config_file: p2p_cfg.write(config_file) LOGGER.info( "concatenate two config.ini now! output => %s/conf/config.ini", data)
def package(data_path, peer_path): utils.file_must_exists('{}/meta/fisco-bcos'.format(path.get_path())) utils.check_fisco('{}/meta/fisco-bcos'.format(path.get_path())) if (os.path.exists(peer_path) and os.path.isfile(peer_path)): mconf.read_peers(peer_path) else: mconf.default_peers() config.build_package_only(data_path)
def merge_cfg(p2p_list, cfg_file): """[combine config.ini] Arguments: p2p_list {[type]} -- [list] cfg_file {[type]} -- [file] Raises: MCError -- [description] """ LOGGER.info("merge peers to config.ini now!") data = cfg_file utils.file_must_exists(data) p2p_get = p2p_list p2p_send = [] p2p_cfg = configparser.ConfigParser(allow_no_value=True) try: with codecs.open(data, 'r', encoding='utf-8') as config_file: p2p_cfg.readfp(config_file) except Exception as build_exp: LOGGER.error(' open config.ini file failed, exception is %s', build_exp) raise MCError(' open config.ini file failed, exception is %s' % build_exp) if p2p_cfg.has_section('p2p'): p2p_send_opt = p2p_cfg.options('p2p') else: LOGGER.error(' open config.ini file failed, exception is %s', build_exp) raise MCError(' open config.ini file failed, exception is %s' % build_exp) for node in p2p_send_opt: p2p_section = p2p_cfg.get('p2p', node) p2p_send.append(p2p_section) p2p_send.pop(0) p2p_send.pop(0) LOGGER.info("send node is %s!", p2p_send) # for node_tuple in p2p_send: # p2p_send.append(node_tuple) LOGGER.info("get node ip is %s!", p2p_get) p2p_send = list(set(p2p_send + p2p_get)) LOGGER.info("final node ip is %s!", p2p_send) for ip_idx, p2p_ip in enumerate(p2p_send): p2p_cfg.set("p2p", "node.{}".format(ip_idx), p2p_ip) p2p_cfg.set('certificate_whitelist', '; cal.0 should be nodeid, nodeid\'s length is 128') p2p_cfg.set('certificate_whitelist', ';cal.0=') p2p_cfg.set('certificate_blacklist', '; crl.0 should be nodeid, nodeid\'s length is 128') p2p_cfg.set('certificate_blacklist', ';crl.0=') with open(data, 'w') as config_file: p2p_cfg.write(config_file) LOGGER.info("concatenate config.ini now! output => %s/conf/config.ini", data) return True
def build(peer_path, data_path): """[--build] """ utils.file_must_exists('{}/meta/fisco-bcos'.format(path.get_path())) utils.check_fisco('{}/meta/fisco-bcos'.format(path.get_path())) if utils.Status.gm_option: utils.file_must_exists('{}/meta/gmca.crt'.format(path.get_path())) else: utils.file_must_exists('{}/meta/ca.crt'.format(path.get_path())) utils.file_must_exists(peer_path) mconf.read_peers(peer_path) config.build_config_ini(data_path) opr_cert.deploy_key('{}/meta'.format(path.get_path()), data_path)
def config_console_toml_file(_file): """ config config.toml of the console """ utils.file_must_exists(_file) # Note: the default ip the console connects to is 127.0.0.1 rpc_ip = mconf.MchainConf.rpc_ip channel_listen_port = mconf.MchainConf.channel_listen_port with open(_file, mode='rb') as fp: content = fp.read() if content.startswith(b'\xef\xbb\xbf'): content = content[3:] config = toml.loads(content.decode('utf8')) for ip_idx, rpc_get in enumerate(rpc_ip): config["network"]["peers"][ip_idx] = "{}:{}".format( rpc_get, channel_listen_port[ip_idx]) CONSOLER.info("configure the channel connections to %s", config["network"]["peers"]) with open(_file, 'w') as fd: toml.dump(config, fd) CONSOLER.info("config_console_toml_file success")
def build_config_ini(_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_config_ini 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 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 group_id = mconf.MchainConf.group_id utils.file_must_exists('{}/group.{}.genesis'.format(meta_dir, group_id)) 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) default_cfg = configparser.ConfigParser() 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)) try: with codecs.open('{}/.config.ini'.format(conf_dir), 'r', encoding='utf-8') as config_file: default_cfg.readfp(config_file) except Exception as build_exp: LOGGER.error( ' open config.ini file failed, exception is %s', build_exp) raise MCError( ' open config.ini file failed, exception is %s' % build_exp) 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) # default_cfg.set("p2p", "node.{}".format(node_id + len(p2p_listen_port)), # peer) # with open('{}/.config.ini'.format(conf_dir), 'w') as config_file: # default_cfg.write(config_file) # init config.ini & node package for my_node_index, node_ip in enumerate(p2p_ip): LOGGER.info("p2p_ip -> %s", node_ip) try: if utils.Status.gm_option: utils.file_must_exists('{}/gmcert_{}_{}.crt'.format(conf_dir, node_ip, p2p_listen_port[my_node_index])) else: utils.file_must_exists('{}/cert_{}_{}.crt'.format(conf_dir, node_ip, p2p_listen_port[my_node_index])) except Exception as build_exp: LOGGER.error('%s', build_exp) raise MCError('%s' % build_exp) 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) 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('{}/fisco-bcos'.format(meta_dir), '{}/fisco-bcos'.format(node_dir)) os.mkdir('{}/conf'.format(node_dir)) try: # get node cert shutil.copy('{}/.config.ini'.format(conf_dir), '{}/config.ini'.format(node_dir)) shutil.copy('{}/group.{}.genesis'.format(conf_dir, group_id), '{}/conf/group.{}.genesis'.format(node_dir, group_id)) shutil.copy('{}/tpl/group.i.ini'.format(path.get_path()), '{}/conf/group.{}.ini'.format(node_dir, group_id)) if gm_opr: get_node_cert('{}/gmcert_{}_{}.crt'.format(meta_dir, node_ip, p2p_listen_port[my_node_index]), '{}/conf/gmnode.crt'.format(node_dir)) # get_nodeid('{}/conf/gmnode.crt'.format(node_dir), # '{}/conf/gmnode.nodeid'.format(node_dir)) shutil.copyfile('{}/gmca.crt'.format(meta_dir), '{}/conf/gmca.crt'.format(node_dir)) else: get_node_cert('{}/cert_{}_{}.crt'.format(meta_dir, node_ip, p2p_listen_port[my_node_index]), '{}/conf/node.crt'.format(node_dir)) # get_nodeid('{}/conf/node.crt'.format(node_dir), # '{}/conf/node.nodeid'.format(node_dir)) shutil.copyfile('{}/ca.crt'.format(meta_dir), '{}/conf/ca.crt'.format(node_dir)) except Exception as build_exp: LOGGER.error(' get node.crt failed ! exception is %s', build_exp) utils.delete_data(package_dir) raise MCError(' get node.crt failed! exception is %s' % build_exp) node_cfg = configparser.ConfigParser() 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) node_cfg.set("rpc", "listen_ip", rpc_ip[my_node_index]) 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_ip", p2p_ip[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() 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)) 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_config_ini end!")
def get_sdk_cert(): """[summary] Arguments: _dir {[type]} -- [description] """ LOGGER.info("get sdk cert in meta!") CONSOLER.info("get sdk cert in meta!") meta = '{}/meta'.format(path.get_path()) utils.file_must_exists('{}/ca.crt'.format(meta)) utils.file_must_exists('{}/agency.crt'.format(meta)) utils.file_must_exists('{}/agency.key'.format(meta)) if os.path.isdir('{}/sdk'.format(meta)): utils.file_must_exists('{}/sdk/ca.crt'.format(meta)) utils.file_must_exists('{}/sdk/node.crt'.format(meta)) utils.file_must_exists('{}/sdk/node.key'.format(meta)) LOGGER.info("sdk cert existed!") CONSOLER.info("sdk cert existed!") else: LOGGER.info("generate console cert!") CONSOLER.info("generate console cert!") ca.generator_node_ca(meta, meta, 'sdk')
def deploy_key(_get_dir, _send_dir): """[deploy_key] Arguments: _get_dir {[PATH]} -- [description] _send_dir {[PATH]} -- [description] """ utils.dir_must_exists(_get_dir) utils.dir_must_exists(_send_dir) meta_path = _get_dir data_path = _send_dir get_node_list = [] send_node_list = [] for _, dirs, _ in os.walk(meta_path, topdown=True, onerror=None, followlinks=False): for name in dirs: get_node_list.append(name) for _, dirs, _ in os.walk(data_path, topdown=True, onerror=None, followlinks=False): for name in dirs: send_node_list.append(name) LOGGER.info("get cert in %s!", get_node_list) LOGGER.info("send cert to %s!", send_node_list) for node_dir in get_node_list: if not utils.valid_node_dir(node_dir): continue if utils.Status.gm_option: utils.file_must_exists('{}/{}/gmnode.key'.format( meta_path, node_dir)) utils.file_must_exists('{}/{}/gmnode.nodeid'.format( meta_path, node_dir)) if os.path.exists('{}/{}/conf'.format(data_path, node_dir)): LOGGER.info("send cert from %s to %s", data_path, node_dir) shutil.copyfile( '{}/{}/gmnode.key'.format(meta_path, node_dir), '{}/{}/conf/gmnode.key'.format(data_path, node_dir)) shutil.copyfile( '{}/{}/gmnode.nodeid'.format(meta_path, node_dir), '{}/{}/conf/gmnode.nodeid'.format(data_path, node_dir)) shutil.copyfile( '{}/{}/gmennode.key'.format(meta_path, node_dir), '{}/{}/conf/gmennode.key'.format(data_path, node_dir)) shutil.copyfile( '{}/{}/gmennode.crt'.format(meta_path, node_dir), '{}/{}/conf/gmennode.crt'.format(data_path, node_dir)) shutil.copytree( '{}/{}/origin_cert'.format(meta_path, node_dir), '{}/{}/conf/origin_cert'.format(data_path, node_dir)) else: utils.file_must_exists('{}/{}/node.key'.format( meta_path, node_dir)) utils.file_must_exists('{}/{}/node.nodeid'.format( meta_path, node_dir)) if os.path.exists('{}/{}/conf'.format(data_path, node_dir)): LOGGER.info("send cert from %s to %s", data_path, node_dir) shutil.copyfile( '{}/{}/node.key'.format(meta_path, node_dir), '{}/{}/conf/node.key'.format(data_path, node_dir)) shutil.copyfile( '{}/{}/node.nodeid'.format(meta_path, node_dir), '{}/{}/conf/node.nodeid'.format(data_path, node_dir))