示例#1
0
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))
示例#2
0
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')
示例#3
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!")