Exemple #1
0
def gen_build_cert(_dir):
    """[gen_build_cert]

    Arguments:
        _dir {[PATH]} -- [cert output]

    Raises:
        MCError -- [description]
    """

    meta_path = '{}/meta'.format(path.get_path())
    cert_path = _dir
    data_path = meta_path

    p2p_listen_port = mconf.MchainConf.p2p_listen_port
    p2p_ip = mconf.MchainConf.p2p_ip
    utils.file_must_not_exists('{}/peers.txt'.format(cert_path))
    if not os.path.exists(cert_path):
        os.mkdir(cert_path)
    if utils.Status.gm_option:
        if not os.path.exists('{}/gmca.crt'.format(meta_path)):
            CONSOLER.error(" gmca.crt not existed")
            utils.delete_data(cert_path)
            raise MCError(' gmca.crt not founded!')
    else:
        if not os.path.exists('{}/ca.crt'.format(meta_path)):
            CONSOLER.error(" ca.crt not existed")
            utils.delete_data(cert_path)
            raise MCError(' ca.crt not founded!')
    if utils.Status.gm_option:
        if not (os.path.exists('{}/gmagency.key'.format(meta_path))
                and os.path.exists(('{}/gmagency.crt'.format(meta_path)))):
            CONSOLER.error(" gmagency.crt or gmagency.key not existed")
            utils.delete_data(cert_path)
            raise MCError(' gmagency.crt or gmagency.key not found in %s!' %
                          meta_path)
    else:
        if not (os.path.exists('{}/agency.key'.format(meta_path))
                and os.path.exists(('{}/agency.crt'.format(meta_path)))):
            CONSOLER.error(" agency.crt or agency.key not existed")
            utils.delete_data(cert_path)
            raise MCError(' agency.crt or agency.key not found in %s!' %
                          meta_path)
    for my_node_index, node_ip in enumerate(p2p_ip):
        LOGGER.info("p2p_ip -> %s", node_ip)
        CONSOLER.info(' Generate %s/node_%s_%s ', data_path, node_ip,
                      p2p_listen_port[my_node_index])
        if utils.Status.gm_option:
            utils.file_must_not_exists('{}/gmcert_{}_{}.crt'.format(
                meta_path, node_ip, p2p_listen_port[my_node_index]))
        else:
            utils.file_must_not_exists('{}/cert_{}_{}.crt'.format(
                meta_path, node_ip, p2p_listen_port[my_node_index]))
        ca.generator_node_ca(
            data_path, '{}/'.format(meta_path),
            'node_{}_{}'.format(node_ip, p2p_listen_port[my_node_index]))
        if utils.Status.gm_option:
            utils.off_gm()
            if os.path.isdir('./.origin_cert'):
                shutil.rmtree('./.origin_cert')
            ca.generator_node_ca('./', meta_path, '.origin_cert')
            shutil.copytree(
                './.origin_cert', '{}/node_{}_{}/origin_cert'.format(
                    data_path, node_ip, p2p_listen_port[my_node_index]))
            shutil.rmtree('./.origin_cert')
            utils.set_gm()
            shutil.copyfile(
                '{}/node_{}_{}/gmnode.crt'.format(
                    data_path, node_ip, p2p_listen_port[my_node_index]),
                '{}/gmcert_{}_{}.crt'.format(meta_path, node_ip,
                                             p2p_listen_port[my_node_index]))
            shutil.copyfile(
                '{}/gmcert_{}_{}.crt'.format(meta_path, node_ip,
                                             p2p_listen_port[my_node_index]),
                '{}/gmcert_{}_{}.crt'.format(cert_path, node_ip,
                                             p2p_listen_port[my_node_index]))
        else:
            shutil.copyfile(
                '{}/node_{}_{}/node.crt'.format(
                    data_path, node_ip, p2p_listen_port[my_node_index]),
                '{}/cert_{}_{}.crt'.format(meta_path, node_ip,
                                           p2p_listen_port[my_node_index]))
            shutil.copyfile(
                '{}/cert_{}_{}.crt'.format(meta_path, node_ip,
                                           p2p_listen_port[my_node_index]),
                '{}/cert_{}_{}.crt'.format(cert_path, node_ip,
                                           p2p_listen_port[my_node_index]))
        (status, result) = \
            utils.getstatusoutput('echo {}:{} >> {}/peers.txt'
                                  .format(node_ip,
                                          p2p_listen_port[my_node_index],
                                          cert_path))
        LOGGER.info(" status is %s, result is %s", status, result)
    CONSOLER.info(" Generate cert by node_installation.ini successful!")
Exemple #2
0
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!")
Exemple #3
0
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))
    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!")