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 set_solidity_version(version): """ set the solidity version, now support 0.5 and 0.6 """ Status.solidity_version = version Status.solidity_version_specified = True LOGGER.debug('expected solidity version is %s', Status.solidity_version)
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 file_must_not_exists(_file): """[utils] Arguments: _file {[type]} -- [description] Raises: MCError -- [description] """ if os.path.exists(_file) and os.path.isfile(_file): LOGGER.error(' %s exist! pls delete it!', _file) raise MCError(' %s exist! pls delete it!' % _file)
def dir_must_not_exists(_dir): """[utils] Arguments: _dir {[type]} -- [description] Raises: MCError -- [description] """ if os.path.exists(_dir): LOGGER.error(' %s existed! pls delete it!', _dir) raise MCError(' %s existed! pls delete it!' % _dir)
def delete_data(path): """[delete data_dir] Arguments: path {[get_dir]} -- [description] """ if os.path.isfile(path): os.remove(path) elif os.path.isdir(path): shutil.rmtree(path) else: LOGGER.error(' path not exisited ! path => %s', path)
def generate_root_ca(_dir): """[generate root cert] Arguments: dir {[path]} -- [root cert path] """ try: ca_dir = os.path.abspath(_dir) if utils.Status.gm_option: os.chdir('{}/scripts/gm/'.format(path.get_path())) (status, result) = utils.getstatusoutput('./cts.sh gen_chain_cert {}' .format(ca_dir)) os.chdir('{}'.format(path.get_path())) else: os.chdir('{}/scripts'.format(path.get_path())) (status, result) = utils.getstatusoutput('./cts.sh gen_chain_cert {}' .format(ca_dir)) os.chdir('{}'.format(path.get_path())) if bool(status): LOGGER.error( ' cts.sh failed! status is %d, output is %s, dir is %s.', status, result, ca_dir) raise MCError('cts.sh failed! status is %d, output is %s, dir is %s.' % ( status, result, ca_dir)) LOGGER.info( ' cts.sh success! status is %d, output is %s, dir is %s.', status, result, ca_dir) LOGGER.info(' Generate root cert success, dir is %s', ca_dir) CONSOLER.info(' Generate root cert success, dir is %s', ca_dir) except MCError as cert_exp: console_error(' %s ' % cert_exp) except Exception as gen_cert_exp: console_error( ' Generate root cert failed! excepion is %s.' % gen_cert_exp) LOGGER.error(' Generate root cert failed! Result is %s', result) raise MCError( 'Generate root agency failed! Result is %s' % gen_cert_exp)
def valid_genesis(_file): """[summary] Arguments: _file {[type]} -- [description] """ group_genesis = _file LOGGER.info("group genesis file is %s", group_genesis) pack = group_genesis.split('.') if len(pack) == 3: if pack[0] == 'group' and int(pack[1]) and pack[2] == 'genesis': LOGGER.info("valid_genesis is %s", pack) return int(pack[1]) return 0
def download_fisco(_dir): """[download fisco-bcos] Arguments: _dir {[type]} -- [description] """ bin_path = _dir # bcos_bin_name = 'fisco-bcos' if Status.gm_option: package_name = "fisco-bcos-gm.tar.gz" else: package_name = "fisco-bcos.tar.gz" (status, version)\ = getstatusoutput('curl -s https://raw.githubusercontent.com/' 'FISCO-BCOS/FISCO-BCOS/master/release_note.txt | sed "s/^[vV]//"') if bool(status): LOGGER.error( ' get fisco-bcos verion failed, result is %s.', version) raise MCError(' get fisco-bcos verion failed, result is %s.' % version) download_link = 'https://github.com/FISCO-BCOS/FISCO-BCOS/releases/download/v{}/{}'.format( version.strip('\n'), package_name.strip('\n')) # filename = package_name LOGGER.info("Downloading fisco-bcos binary from %s", download_link) CONSOLER.info("Downloading fisco-bcos binary from %s", download_link) # (status, result) = getstatusoutput('curl -LO {}'.format(download_link)) # subprocess.call('curl -LO {}'.format(download_link), shell=True) download_bin(download_link, package_name) # if bool(status): # LOGGER.error( # ' download fisco-bcos failed, result is %s.', result) # raise MCError( # ' download fisco-bcos failed, result is %s.' % result) (status, result)\ = getstatusoutput('tar -zxf {} && mv fisco-bcos {} && rm {}'.format(package_name, bin_path, package_name)) if bool(status): LOGGER.error( ' Decompress fisco-bcos failed, result is %s.', result) raise MCError( ' Decompress fisco-bcos failed, result is %s.' % result) (status, result) = getstatusoutput('chmod a+x {}'.format(bin_path)) if bool(status): LOGGER.error( ' exec fisco-bcos failed, result is %s.', result) raise MCError( ' exec fisco-bcos failed, result is %s.' % result) LOGGER.info("Downloading fisco-bcos successful, fisco-bcos at %s", bin_path) CONSOLER.info( "Downloading fisco-bcos successful, fisco-bcos at %s", bin_path)
def get_console_cert(_dir): """get console certs Arguments: _dir {[type]} -- [description] """ LOGGER.info("get console in %s!", _dir) CONSOLER.info("get console in %s!", _dir) meta = '{}/meta'.format(path.get_path()) data = _dir get_sdk_cert() utils.dir_must_exists(data) shutil.copyfile('{}/ca.crt'.format(meta), '{}/ca.crt'.format(data)) shutil.copyfile('{}/sdk/node.key'.format(meta), '{}/node.key'.format(data)) shutil.copyfile('{}/sdk/node.crt'.format(meta), '{}/node.crt'.format(data))
def download_console(_dir): """[summary] Arguments: _dir {[type]} -- [description] Raises: MCError -- [description] MCError -- [description] """ dir_must_exists(_dir) bin_path = _dir meta = '{}/meta'.format(path.get_path()) # file_must_exists('{}/ca.crt'.format(meta)) # file_must_exists('{}/agency.crt'.format(meta)) # file_must_exists('{}/agency.key'.format(meta)) download_console_command = "bash {}/tpl/{}".format( path.get_path(), Status.download_console_shell_script) cdn_option = "" if Status.use_cdn: CONSOLER.info("download_console: use cdn") cdn_option = "-n" download_console_command = "{} {}".format(download_console_command, cdn_option) if (Status.download_console_version_specified is True): download_console_command = "{} -c {}".format( download_console_command, Status.download_console_version) if (Status.solidity_version_specified is True): download_console_command = "{} -v {}".format(download_console_command, Status.solidity_version) CONSOLER.info("The download_console_command is %s", download_console_command) # execute the download_console_command (status, result) = getstatusoutput(download_console_command) if bool(status): LOGGER.error(' download console failed, result is %s.', result) raise MCError(' download console failed, result is %s.' % result) chmod_command = 'chmod a+x console/start.sh' if bin_path != "." and bin_path != "./": chmod_command = "{} && mv console {}".format(chmod_command, bin_path) (status, result) = getstatusoutput(chmod_command) if bool(status): LOGGER.error('chmod console failed! status is %d,' ' output is %s.', status, result) raise MCError('chmod console failed!' ' status is %d, output is %s.' % (status, result))
def get_all_nodes_dir(_dir): """[summary] Arguments: _dir {[type]} -- [description] """ data_path = _dir node_dir_list = [] dir_must_exists(data_path) LOGGER.info("get all nodes_dir from %s", data_path) for node_file in os.listdir(data_path): file_path = os.path.join(data_path, node_file) if os.path.isdir(file_path) and valid_node_dir(node_file): node_dir_list.append(file_path) LOGGER.info("all nodes_dir is %s", node_dir_list) return node_dir_list
def port_in_use(port): """using cmd nc to check if the port is occupied. Arguments: port {string} -- port number Returns: bool -- True or False. """ cmd = 'nc -z 127.0.0.1' + (' %d' % port) status, output = getstatusoutput(cmd) LOGGER.debug('port is %s, status is %s, output is %s', port, status, output) return status == 0
def valid_peer(peer): """[Determine if the peer is valid] Arguments: peer {[str]} -- [peers] Returns: [bool] -- [true or false] """ try: peer = peer.split(':') if not valid_ip(peer[0]): return False if valid_port(peer[1]): return True return False except Exception as utils_exp: LOGGER.error('%s is not a valid peer', utils_exp) raise MCError('%s is not a valid peer' % utils_exp)
def valid_chain_id(chain_id): """[Determine if the chain id is valid] Arguments: ip {[string]} -- [chain id] Returns: [bool] -- [true or false] """ try: int(chain_id) return True except ValueError as utils_exp: LOGGER.error('%s is not a valid chain_id', utils_exp) except Exception as utils_exp: LOGGER.error('%s is not a valid chain_id', utils_exp) raise MCError('%s is not a valid chain_id' % utils_exp)
def read_peers(data_path): """[read peers] Arguments: data_path {[file]} -- [peers file] """ # read and parser peer file try: for line in open(data_path): peer = line.strip('\n') if utils.valid_peer(peer): MchainConf.peers.append(peer) except Exception as ini_exp: LOGGER.error(' open %s file failed, exception is %s', data_path, ini_exp) raise MCError(' open %s file failed, exception is %s' % (data_path, ini_exp)) MchainConf.peers = list(set(MchainConf.peers)) LOGGER.info('peers is %s', MchainConf.peers)
def replace(filepath, old, new): """[replace old string to new from filepath] Arguments: filepath {[path]} -- [file path that needs to be replaced] old {[string]} -- [old string] new {[string]} -- [new string] """ if not os.path.exists(filepath): return False cmd = "sed -i 's|%s|%s|g' %s " % (old, new, filepath) status, output = getstatusoutput(cmd) if status != 0: LOGGER.error(' replace failed,' 'new is %s, old is %s, file is %s, status is %s, output is %s ', new, old, filepath, str(status), output) return False return True
def valid_expand_package(pkg): """[Determine if the string->package is valid] Arguments: pkg {[string]} -- [a string outside the function] Returns: [bool] -- [true or false] """ try: pack = pkg.split() if not valid_ip(pack[0]): return False if pack[1]: return True return False except ValueError as utils_exp: LOGGER.error(' invalied package , exception is %s', utils_exp) except Exception as utils_exp: LOGGER.error(' invalied package , exception is %s', utils_exp) raise MCError('invalied package , exception is %s' % utils_exp)
def download_bin(_download_link, _package_name): """dowloand """ try: if sys.version > '3': urllib.request.urlretrieve(_download_link, _package_name, _hook_func) else: if os.environ.get('https_proxy') or os.environ.get('http_proxy'): url = _download_link response = urllib2.urlopen(url) data = chunk_read(response, report_hook=chunk_report) # data = response.read() with open(_package_name, 'wb') as code: code.write(data) else: urllib.urlretrieve(_download_link, _package_name, _hook_func) except Exception as download_err: LOGGER.error('download %s failed! exception is %s', _package_name, download_err) raise MCError('download %s failed! exception is %s' % (_package_name, download_err))
def getstatusoutput(cmd): """replace commands.getstatusoutput Arguments: cmd {[string]} """ get_cmd = subprocess.Popen( cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) ret = get_cmd.communicate() out = ret[0] err = ret[1] output = '' if not out is None: output = output + out.decode('utf-8') if not err is None: output = output + err.decode('utf-8') LOGGER.debug(' cmd is %s, status is %s, output is %s', cmd, str(get_cmd.returncode), output) return (get_cmd.returncode, output)
def generator_node_ca(_dir, agent, node): """[generate node cert ] Arguments: agent {[path]} -- [agency cert path] node {[string]} -- [node name] dir {[path]} -- [node cert path] """ node_dir = os.path.abspath(_dir) agent = os.path.abspath(agent) try: if utils.Status.gm_option: os.chdir('{}/scripts/gm/'.format(path.get_path())) (status, result) = utils.getstatusoutput( './cts.sh' ' gen_node_cert {} {}/{}'.format(agent, node_dir, node)) os.chdir('{}'.format(path.get_path())) else: os.chdir('{}/scripts/'.format(path.get_path())) (status, result) = utils.getstatusoutput( './cts.sh' ' gen_node_cert {} {}/{}'.format(agent, node_dir, node)) os.chdir('{}'.format(path.get_path())) if not bool(status): LOGGER.info(' Generate %s cert successful! dir is %s/%s.', node, node_dir, node) os.chdir('{}'.format(path.get_path())) if utils.Status.gm_option: (status, result) = utils.getstatusoutput( 'cat {}/{}/gmagency.crt ' '>> {}/{}/gmnode.crt'.format(_dir, node, _dir, node)) os.remove('{}/{}/gmagency.crt'.format(_dir, node)) os.remove('{}/{}/gmnode.serial'.format(_dir, node)) else: (status, result) = utils.getstatusoutput( 'cat {}/{}/agency.crt ' '>> {}/{}/node.crt'.format(_dir, node, _dir, node)) os.remove('{}/{}/agency.crt'.format(_dir, node)) os.remove('{}/{}/node.ca'.format(_dir, node)) os.remove('{}/{}/node.json'.format(_dir, node)) os.remove('{}/{}/node.private'.format(_dir, node)) os.remove('{}/{}/node.serial'.format(_dir, node)) os.remove('{}/{}/node.param'.format(_dir, node)) os.remove('{}/{}/node.pubkey'.format(_dir, node)) else: # console_error( # ' Generate node cert failed! Please check your network,' # ' and try to check your opennssl version.') LOGGER.error(' Generate %s cert failed! Result is %s', node, result) raise MCError(' Generate %s cert failed! Result is %s' % (node, result)) except MCError as cert_exp: console_error(' %s ' % cert_exp) except Exception as gen_cert_exp: console_error(' Generate node cert failed! excepion is %s.' % gen_cert_exp) LOGGER.error(' Generate node cert failed! Result is %s', result) raise MCError('Generate node failed! Result is %s' % gen_cert_exp)
def check_java(self): """[check java] Raises: MCError -- [java not install] """ cmd = 'java -version' status, output = utils.getstatusoutput(cmd) if status != 0: LOGGER.error(' java -version failed , status is %d, output is %s', status, output) raise MCError(' java -version failed , java not installed.') version_str = output.split("\"") if not len(version_str) > 1: LOGGER.error( ' cannot get java version, status is %d, output is %s', status, output) raise MCError( ' cannot get java version, oracle jdk need >=1.8, please try \'java -version\'. ' ) version_arr = version_str[1].split('.') if not len(version_arr) > 2: LOGGER.error( ' cannot get java version, status is %d, output is %s', status, output) raise MCError( ' cannot get java version, oracle jdk need >=1.8, please try \'java -version\' ' ) self.major = version_arr[0] self.minor = version_arr[1] self.openjdk = bool(output.lower().find('openjdk') != -1) if not self.is_suitable(): raise MCError( ' invalid java version, oracle jdk need >=1.8, now %s ' % self) LOGGER.info(' java version is %s ', self)
def check_fisco(_file): """checkfisco """ bin_fisco = _file CONSOLER.info(" Checking fisco-bcos binary...") LOGGER.info(" Checking fisco-bcos binary...") (status, bin_version)\ = getstatusoutput('{} -v'.format(bin_fisco)) if bool(status): LOGGER.error( 'Checking fisco-bcos failed! status is %d,' ' output is %s, dir is %s.', status, bin_version, bin_fisco) raise MCError('Checking fisco-bcos failed!' ' status is %d, output is %s, dir is %s.' % (status, bin_version, bin_fisco)) if not 'FISCO-BCOS' in bin_version: LOGGER.error("%s is wrong. Please correct it and try again.", bin_fisco) raise Exception("%s is wrong. Please correct it and try again." % bin_fisco) # if Status.gm_option: # if not 'gm' in bin_version: # LOGGER.error( # 'Checking fisco-bcos failed! %s isn\'t ' # 'gm version. Please correct it and try again.', bin_fisco) # raise MCError( # 'Checking fisco-bcos failed! %s isn\'t ' # 'gm version. Please correct it and try again' % bin_version) # else: # if 'gm' in bin_version: # LOGGER.error( # 'Checking fisco-bcos failed! %s isn\'t ' # 'standard version. Please correct it and try again.', bin_fisco) # raise MCError( # 'Checking fisco-bcos failed! %s isn\'t ' # 'standard version. Please correct it and try again.' % bin_version) CONSOLER.info(' Binary check passed.') LOGGER.info(' Binary check passed.')
def generator_agent_ca(_dir, _ca, agent): """[generate agency cert] Arguments: dir {[path]} -- [agency cert path] ca {[path]} -- [root cert path] agent {[string]} -- [agency name] """ try: ca_dir = os.path.abspath(_ca) agency_dir = os.path.abspath(_dir) if utils.Status.gm_option: os.chdir('{}/scripts/gm/'.format(path.get_path())) (status, result) = utils.getstatusoutput('./cts.sh' ' gen_agency_cert {} {}/{}' .format(ca_dir, agency_dir, agent)) os.chdir('{}'.format(path.get_path())) else: os.chdir('{}/scripts'.format(path.get_path())) (status, result) = utils.getstatusoutput('./cts.sh' ' gen_agency_cert {} {}/{}' .format(ca_dir, agency_dir, agent)) os.chdir('{}'.format(path.get_path())) if not bool(status): LOGGER.info(' Generate %s cert successful! dir is %s/%s.', agent, agency_dir, agent) else: # console_error( # ' Generate cert failed! Please check your network,' # ' and try to check your opennssl version.') LOGGER.error(' Generate %s cert failed! Result is %s', agent, result) raise MCError(' Generate %s cert failed! Result is %s' % (agent, result)) except MCError as cert_exp: console_error(' %s ' % cert_exp) except Exception as gen_cert_exp: console_error( ' Generate agency cert failed! excepion is %s.' % gen_cert_exp) LOGGER.error(' Generate agency cert failed! Result is %s', result) raise MCError( 'Generate agency agency failed! Result is %s' % gen_cert_exp)
def download_console(_dir): """[summary] Arguments: _dir {[type]} -- [description] Raises: MCError -- [description] MCError -- [description] """ bin_path = _dir package_name = "console.tar.gz" dir_must_not_exists('{}/console'.format(bin_path)) (status, version) = getstatusoutput('curl -s https://raw.githubusercontent.com/' 'FISCO-BCOS/console/master/release_note.txt' ' | sed "s/^[vV]//"') if bool(status): LOGGER.error( ' get fisco-bcos verion failed, result is %s.', version) raise MCError(' get fisco-bcos verion failed, result is %s.' % version) download_link = 'https://github.com/FISCO-BCOS/console/releases/download/v{}/{}'.format( version.strip('\n'), package_name.strip('\n')) LOGGER.info("Downloading console binary %s", download_link) CONSOLER.info("Downloading console binary %s", download_link) download_bin(download_link, package_name) # subprocess.call('curl -LO {}'.format(download_link), shell=True) (status, result)\ = getstatusoutput('tar -zxf {} -C {} && ' 'rm {}'.format(package_name, bin_path, package_name)) if bool(status): LOGGER.error( ' Decompress console failed, result is %s.', result) raise MCError( ' Decompress console failed, result is %s.' % result) (status, result) = getstatusoutput( 'chmod a+x {}/console/start.sh'.format(bin_path))
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 create_group_genesis(data_dir='{}/meta'.format(path.get_path())): ''' create group in meta ''' LOGGER.info('create_group_genesis start') package_dir = data_dir gm_opr = utils.Status.gm_option group_id = mgroup.MgroupConf.group_id p2p_ip = mgroup.MgroupConf.p2p_ip p2p_listen_port = mgroup.MgroupConf.p2p_listen_port utils.file_must_not_exists('{}/group.{}.genesis'.format( data_dir, group_id)) if not os.path.exists(package_dir): LOGGER.warning(' %s not existed!', package_dir) os.mkdir(data_dir) shutil.copy('{}/tpl/group.i.genesis'.format(path.get_path()), '{}/group.{}.genesis'.format(package_dir, group_id)) shutil.copy('{}/tpl/group.i.ini'.format(path.get_path()), '{}/group.{}.ini'.format(package_dir, group_id)) # uptes second to ms (status, time_stamp) = utils.getstatusoutput('echo $(date +%s"000")') if not bool(status): CONSOLER.info('generate %s/group.%s.genesis, successful', package_dir, group_id) else: LOGGER.error( ' Generate %s/group.%s.genesis failed! Please check your network.', package_dir, group_id) raise MCError( ' Generate %s/group.%s.genesis failed, timestamp is %s! Please check your network.' % (package_dir, group_id, time_stamp)) # CONSOLER.info('generate %s/group.%s.ini', package_dir, group_id) group_cfg = configparser.ConfigParser() with open('{}/group.{}.genesis'.format(package_dir, group_id), 'r') as config_file: group_cfg.readfp(config_file) for node_idx, _in in enumerate(p2p_ip): try: if gm_opr: node_id = config.get_nodeid_str( '{}/meta/gmcert_{}_{}.crt'.format( path.get_path(), p2p_ip[node_idx], p2p_listen_port[node_idx])) node_id.strip('\n').strip( 'WARNING: can\'t open config file: /home/asherli/TASSL/ssl/openssl.cnf' ) LOGGER.info('resolve %s/meta/gmcert_%s_%s.crt', path.get_path(), p2p_ip[node_idx], p2p_listen_port[node_idx]) LOGGER.info("nodeid -> %s", node_id) group_cfg.set("consensus", "node.{}".format(node_idx), node_id) else: node_id = config.get_nodeid_str( '{}/meta/cert_{}_{}.crt'.format(path.get_path(), p2p_ip[node_idx], p2p_listen_port[node_idx])) LOGGER.info('resolve %s/meta/cert_%s_%s.crt', path.get_path(), p2p_ip[node_idx], p2p_listen_port[node_idx]) LOGGER.info("nodeid -> %s", node_id) group_cfg.set("consensus", "node.{}".format(node_idx), node_id) except Exception as group_exp: LOGGER.error('create group genesis failed! exception is %s', group_exp) raise MCError('create group genesis failed! exception is %s' % group_exp) group_cfg.set("group", "id", group_id) group_cfg.set("group", "timestamp", time_stamp) with open('{}/group.{}.genesis'.format(package_dir, group_id), 'w') as config_file: group_cfg.write(config_file) shutil.copy('{}/group.{}.genesis'.format(package_dir, group_id), '{}/meta/group.{}.genesis'.format(path.get_path(), group_id)) LOGGER.info('create_group_genesis end')
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() 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) 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 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 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!")