Exemplo n.º 1
0
def download_fisco(_dir):
    """[download fisco-bcos]

    Arguments:
        _dir {[type]} -- [description]
    """
    dir_must_exists(_dir)
    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(r'curl -s https://api.github.com/repos/FISCO-BCOS/FISCO-BCOS/releases '
                          r'| grep "tag_name" | grep "\"v2\.[0-9]\.[0-9]\""'
                          r' | sort -u | tail -n 1 | cut -d \" -f 4 | 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'))
    cnd_link = 'https://www.fisco.com.cn/cdn/fisco-bcos/releases/download/v{}/{}'.format(
        version.strip('\n'), package_name.strip('\n'))
    if valid_url(cnd_link):
        LOGGER.info("Downloading fisco-bcos binary from %s", cnd_link)
        CONSOLER.info("Downloading fisco-bcos binary from %s", cnd_link)
        download_bin(cnd_link, package_name)
    elif valid_url(download_link):
        LOGGER.info("Downloading fisco-bcos binary from %s", download_link)
        CONSOLER.info("Downloading fisco-bcos binary from %s", download_link)
        download_bin(download_link, package_name)
    else:
        LOGGER.error(' Download fisco-bcos failed, Please check your network!')
        raise MCError(
            ' Download fisco-bcos failed, Please check your network!')
    (status, result)\
        = getstatusoutput('tar -zxf {} -C {} && 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)
Exemplo n.º 2
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)
Exemplo n.º 3
0
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
Exemplo n.º 4
0
def get_nodeid_str(get_path):
    """[get nodeid string]

    Arguments:
        get_path {[file]} -- [description]

    Raises:
        MCError -- [description]

    Returns:
        [string] -- [nodeid]
    """

    # openssl x509  -text -in ./node.crt |  sed -n '15,20p' |  sed 's/://g' |
    #  tr "\n" " " | sed 's/ //g' | sed 's/pub//g' | cut -c 3-130
    LOGGER.info("get_nodeid start! get path is %s", get_path)
    if not os.path.isfile(get_path):
        LOGGER.error(' node cert doesn\'t existed! Need %s', get_path)
        raise MCError(' node cert doesn\'t existed! Need %s' % get_path)
    try:
        if utils.Status.gm_option:
            (status, result) = utils.getstatusoutput(
                '~/.tassl x509  -text -in {}'
                ' | sed -n "15,20p" |  sed '
                '"s/://g" | sed "s/pub//g" |'
                ' tr "\n" " " | sed "s/ //g"'
                ' | cut -c 3-130'.format(get_path))
            result = result.split('\n')[0]
        else:
            (status, result) = utils.getstatusoutput(
                'openssl x509  -text -in {}'
                ' | sed -n "15,20p" |  sed '
                '"s/://g" | sed "s/pub//g" |'
                ' tr "\n" " " | sed "s/ //g"'
                ' | cut -c 3-130'.format(get_path))

        if status != 0:
            LOGGER.error(
                ' create nodeid failed! status is %d, output is %s, dir is %s.',
                status, result, get_path)
        LOGGER.info(
            ' create nodeid success! status is %d, output is %s, dir is %s.',
            status, result, get_path)
    except Exception as node_id_exp:
        LOGGER.error(
            ' create nodeid failed! status is %d, output is %s, dir is %s.',
            status, result, get_path)
        raise MCError(' create nodeid failed! excepion is %s.' % node_id_exp)
    LOGGER.info("get_nodeid success! get path is %s", get_path)
    return result
Exemplo n.º 5
0
    def port_check(self):
        # rpc port check
        if utils.port_in_use(self.port.get_rpc_port()):
            raise MCError(' temp node rpc port(%s) is in use.' %
                          self.port.get_rpc_port())

        # p2p port check
        if utils.port_in_use(self.port.get_p2p_port()):
            raise MCError(' temp node p2p port(%s) is in use.' %
                          self.port.get_p2p_port())

        # channel port
        if utils.port_in_use(self.port.get_channel_port()):
            raise MCError(' temp node channel port(%s) is in use.' %
                          self.port.get_channel_port())
Exemplo n.º 6
0
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))
Exemplo n.º 7
0
def parser(mchain):
    """resolve mchain.conf
    
    Arguments:
        mchain {string} -- path of mchain.conf
    
    Raises:
        MCError -- exception description
    """

    logger.info('mchain.conf is %s', mchain)
    # resolve configuration
    if not utils.valid_string(mchain):
        logger.error(' mchain.conf not invalid path, mchain.conf is %s',
                     mchain)
        raise MCError(' mchain.conf not invalid path, mchain.conf is %s' %
                      mchain)

    # read and parser config file
    cf = configparser.ConfigParser()
    try:
        with codecs.open(mchain, 'r', encoding='utf-8') as f:
            cf.readfp(f)
    except Exception as e:
        logger.error(' open mchain.conf file failed, exception is %s', e)
        raise MCError(' open mchain.conf file failed, exception is %s' % e)

    agent_name = cf.get('agent', 'agent_name')
    if not utils.valid_string(agent_name):
        logger.error(
            ' invalid mchain.conf format, agent_name empty, agent_name is %s',
            agent_name)
        raise MCError(
            ' invalid mchain.conf format, agent_name empty, agent_name is %s' %
            agent_name)
    MchainConf.agent_name = agent_name

    ansible_dir = cf.get('ansible', 'dir')
    if not utils.valid_string(ansible_dir):
        logger.error(
            ' invalid mchain.conf format, ansible_dir empty, ansible_dir is %s',
            ansible_dir)
        raise MCError(
            ' invalid mchain.conf format, ansible_dir empty, ansible_dir is %s'
            % ansible_dir)
    MchainConf.ansible_dir = ansible_dir

    logger.info('mchain.conf end, result is %s', MchainConf())
Exemplo n.º 8
0
def generate_root_ca(dir, gm=False):
    """[generate root cert]
    
    Arguments:
        dir {[path]} -- [root cert path]
    """
    try:
        dir = os.path.abspath(dir)
        if gm:
            (status, result) = utils.getstatusoutput(
                'bash ' + path.get_path() +
                '/scripts/ca/gm/cts.sh gen_chain_cert ' + dir)
        else:
            (status, result
             ) = utils.getstatusoutput('bash ' + path.get_path() +
                                       '/scripts/ca/cts.sh gen_chain_cert ' +
                                       dir)
        if status != 0:
            logger.warn(
                ' cts.sh failed! status is %d, output is %s, dir is %s.',
                status, result, dir)
            raise MCError(
                'cts.sh failed! status is %d, output is %s, dir is %s.' %
                (status, result, dir))
        logger.info(' cts.sh success! status is %d, output is %s, dir is %s.',
                    status, result, dir)
        logger.info(' Generate root cert success, dir is %s', dir)
        consoler.info(' Generate root cert success, dir is %s' % dir)
    except MCError as me:
        consoler.error(' \033[1;31m %s \033[0m', me)
    except Exception as e:
        consoler.error(
            ' \033[1;31m Generate root cert failed! excepion is %s.\033[0m', e)
        logger.error('  Generate root cert failed! Result is %s' % result)
Exemplo n.º 9
0
    def load(self):
        self.clear()
        if not self.exist():
            logger.info(' meta.json not exist, chain_id is ' + self.chain_id)
            return

        try:
            with open(data.meta_dir(self.chain_id) + '/meta.json', 'r') as f:
                jsondata = json.load(f)

                if 'chain_version' in jsondata:
                    self.chain_version = str(jsondata['chain_version'])

                if 'nodes' in jsondata:
                    for hm in jsondata['nodes'].values():
                        for v in hm:
                            mn = MetaNode(v['host_ip'], v['rpc_port'], v['p2p_port'], v['channel_port'], v['node'])
                            logger.info(
                                'load from meta.json, meta node is %s', mn)
                            self.append(mn)
            logger.info(' Meta is %s', self.to_json())
        except Exception as e:
            logger.error(
                ' load meta failed, chaind id is %s, exception is %s', self.chain_id, e)
            # raise or not ???
            raise MCError(' load meta.json data failed, chain id is %s, exception is %s' % (self.chain_id, e))
Exemplo n.º 10
0
def generator_sdk_ca(agency_dir, sdk_dir, gm=False):
    """[generate sdkcert]
    
    Arguments:
        dir {[path]} -- [agency cert path]
        If operation success, dir will generate sdk dir under the target path, the content is sdk_cert.
    """
    dir = os.path.abspath(agency_dir)
    sdk_dir = os.path.abspath(sdk_dir)
    try:
        if gm:
            (status, result
             ) = utils.getstatusoutput('bash ' + path.get_path() +
                                       '/scripts/ca/gm/cts.sh gen_sdk_cert ' +
                                       dir + ' ' + sdk_dir)
        else:
            (status, result
             ) = utils.getstatusoutput('bash ' + path.get_path() +
                                       '/scripts/ca/cts.sh gen_sdk_cert ' +
                                       dir + ' ' + sdk_dir)
        if not status:
            consoler.info(' Generate sdk cert successful! dir is %s.',
                          sdk_dir + '/sdk')
            logger.info(' Generate sdk cert successful! dir is %s.',
                        sdk_dir + '/sdk')
        else:
            logger.error('  Generate sdk cert failed! Result is %s' % result)
            raise MCError(' Generate sdk cert failed! Result is %s' % result)
    except MCError as me:
        consoler.error(' \033[1;31m %s \033[0m', me)
    except Exception as e:
        consoler.error(
            ' \033[1;31m Generate root cert failed! excepion is %s.\033[0m', e)
        logger.error('  Generate root cert failed! Result is %s' % result)
Exemplo n.º 11
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))
Exemplo n.º 12
0
def check_cert_sdk(path):
    """[check sdk cert exist]
    
    Arguments:
        path {[dir -> sdk cert]} -- [where sdk cert put]
    
    Raises:
        MCError -- [catch sdk Init status]
    
    Returns:
        [bool] -- [true or false]
    """

    result = os.path.exists(path + '/sdk/sdk.crt') and os.path.exists(path + '/sdk/ca.crt')  and os.path.exists(path + '/sdk/client.keystore')\
        and os.path.exists(path + '/sdk/keystore.p12') and os.path.exists(path + '/sdk/sdk.csr') and os.path.exists(path + '/sdk/sdk.key') and os.path.exists(path + '/sdk/sdk.param')\
        and os.path.exists(path + '/sdk/sdk.private') and os.path.exists(path + '/sdk/sdk.pubkey')
    try:
        if result:
            consoler.info('  Init sdk cert success!')
        else:
            logger.error('  Init sdk cert failed!')
            raise MCError('  Init sdk cert failed!')
    except MCError as me:
        logger.error(me)
        consoler.error(me)
    return result
Exemplo n.º 13
0
    def is_running(self):
        check_command = 'bash ' + self.check_shell_file()
        status, output = utils.getstatusoutput(check_command)
        logger.info('check status, status is %d, output is %s', status, output)

        if (output.find('is running') == -1):
            raise MCError(' temp node is not running, outpus is %s' % output)
Exemplo n.º 14
0
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))
Exemplo n.º 15
0
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')
Exemplo n.º 16
0
 def register(self, node_json):
     register_command = 'bash %s NodeAction registerNode file:%s' % (
         self.register_shell_file(), node_json)
     # node_all_command = 'bash %s NodeAction all'
     status, output = utils.getstatusoutput(register_command)
     if status != 0:
         logger.error(
             ' register node failed, node_json is %s,status is %d, output is %s', node_json, status, output)
         raise MCError(' register opr failed, status is %d ' % status)
Exemplo n.º 17
0
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)
Exemplo n.º 18
0
def get_nodeid(get_path, send_path):
    """[get nodeid into file]

    Arguments:
        get_path {[file]} -- [description]
        send_path {[file]} -- [description]

    Raises:
        MCError -- [description]
    """

    LOGGER.info("get_nodeid start! get path is %s", get_path)
    LOGGER.info("get_nodeid start! send path is %s", send_path)
    if not os.path.isfile(get_path):
        LOGGER.error(' node cert doesn\'t existed! Need %s', get_path)
        raise MCError(' node cert doesn\'t existed! Need %s' % get_path)
    try:
        if utils.Status.gm_option:
            (status, result) = utils.getstatusoutput('~/.tassl x509  -text -in {}'
                                                     ' | sed -n "15,20p" |  sed '
                                                     '"s/://g" | sed "s/pub//g" |'
                                                     ' tr "\n" " " | sed "s/ //g"'
                                                     ' cut -c 3-130| cat >{}'
                                                     .format(get_path, send_path))
        else:
            (status, result) = utils.getstatusoutput('openssl x509  -text -in {}'
                                                     ' | sed -n "15,20p" |  sed "s/://g"'
                                                     ' | tr "\n" " " | sed "s/ //g" |'
                                                     ' cut -c 3-130| cat >{}'
                                                     .format(get_path, send_path))
        if status != 0:
            LOGGER.error(
                ' create nodeid failed! status is %d, output is %s, dir is %s.',
                status, result, get_path)
        LOGGER.info(
            ' create nodeid success! status is %d, output is %s, dir is %s.',
            status, result, get_path)
    except Exception as node_id_exp:
        LOGGER.error(
            ' create nodeid failed! status is %d, output is %s, dir is %s.',
            status, result, get_path)
        raise MCError(' create nodeid failed! excepion is %s.' % node_id_exp)
    LOGGER.info("get_nodeid success! get path is %s", get_path)
    LOGGER.info("get_nodeid success! send path is %s", send_path)
Exemplo n.º 19
0
    def get_host_nodes(self, host_ip):

        if host_ip in self.nodes:
            host_nodes = self.nodes[host_ip]
            logger.info(' get host nodes, host is %s, hm is %s',
                        host_ip, host_nodes)
            return host_nodes
        # raise or not ???
        logger.info(' get host nodes failed, host is %s', host_ip)
        raise MCError(' not found meta node, chain_id is %s, host is %s' % (self.chain_id, host_ip))
Exemplo n.º 20
0
 def start(self):
     # check if port temp node will use aleady used by others.
     self.port_check()
     # start temp node.
     start_command = 'bash ' + self.start_shell_file()
     status, output = utils.getstatusoutput(start_command)
     if status != 0:
         raise MCError(
             ' temp node start not success, output is %s ' % output)
     logger.info(' start status, status is %d, output is %s',
                 status, output)
Exemplo n.º 21
0
    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)
Exemplo n.º 22
0
 def export(self):
     self.stop()
     export_command = 'bash ' + self.export_shell_file() + ' ' + \
         self.chain.data_dir() + '/genesis.json'
     status, output = utils.getstatusoutput(export_command)
     if not os.path.exists(self.dir() + '/../genesis.json'):
         logger.error('export genesis.json failed, output is %s', output)
         raise MCError(
             ' export genesis.json failed, output is %s.' % output)
     else:
         logger.debug(
             'export status, status is %d, output is %s', status, output)
Exemplo n.º 23
0
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.')
Exemplo n.º 24
0
    def check_java(self):
        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 or openjdk need >= 1.9, 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 or openjdk need >= 1.9, please try \'java -version\' '
            )

        self.major = version_arr[0]
        self.minor = version_arr[1]
        if output.lower().find('openjdk') != -1:
            self.openjdk = True
        else:
            self.openjdk = False

        if not self.is_suitable():
            raise MCError(
                ' invalid java version, oracle jdk need >=1.8 or openjdk need >= 1.9, now %s '
                % self)

        logger.info(' java version is %s ', self)
Exemplo n.º 25
0
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)
Exemplo n.º 26
0
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 expand_on_exist_chain(cc):

    chain = cc.get_chain()
    port = cc.get_port()
    chain_id = chain.get_id()
    chain_version = chain.get_version()

    # expand on exist chain, check common、 genesis.json、 bootstrapnodes.json file exist.
    if not os.path.exists(chain.data_dir() + '/common'):
        raise MCError(' chain dir exist ,but common dir not exist, chain_id %s、chain_version %s' % (
            chain_id, chain_version))
    if not os.path.exists(chain.data_dir() + '/genesis.json'):
        raise MCError(' chain dir exist ,but genesis.json not exist, chain_id %s、chain_version %s' % (
            chain_id, chain_version))
    if not os.path.exists(chain.data_dir() + '/bootstrapnodes.json'):
        raise MCError(' chain dir exist ,but bootstrapnodes.json not exist, chain_id %s、and chain_version %s' % (
            chain_id, chain_version))
    
    acp = AllChainPort()
    # port check
    for node in cc.get_nodes():
        for index in range(node.get_node_num()):
            # create dir for every node on the server
            acp.port_conflicts_outside_chain(chain.get_id(), node.get_host_ip(), port.to_port(index))
            acp.port_conflicts_inside_chain(node.get_host_ip(), port.to_port(index) ,chain.get_id(), chain.get_version())

    fisco = Fisco(chain.data_dir() + '/' + 'common' + '/' + 'fisco-bcos')
    
    # expand install dir for every server
    for node in cc.get_nodes():
        try:
            build_pkg.expand_host_dir(chain, node, port, fisco)
        except Exception as e:
            logger.error(' expand failed, chain id is %s, chain version is %s, exception is %s.',
                     chain_id, chain_version, e)
            raise MCError(' expand failed, chain id is %s, chain version is %s, exception is %s' % (
            chain_id, chain_version, e))
    
    web3_conf_by_chain(chain, fisco.is_gm())
Exemplo n.º 28
0
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))
Exemplo n.º 29
0
def get_node_cert(get_path, send_path):
    """[get node crt to conf/]

    Arguments:
        get_path {[PATH]} -- [input file]
        send_path {[PATH]} -- [output file]

    Raises:
        MCError -- [description]
        MCError -- [description]
        MCError -- [description]
    """

    LOGGER.info("get node.crt in %s", get_path)
    LOGGER.info("send node.crt in %s", send_path)
    if not os.path.isfile(get_path):
        LOGGER.error(' node cert doesn\'t existed! Need %s', get_path)
        raise MCError(' node cert doesn\'t existed! Need %s' % get_path)
    if os.path.isfile(send_path):
        LOGGER.error(' node.crt existed! path is %s', send_path)
        raise MCError(' node.crt existed! path is %s' % send_path)

    with open(get_path) as cert_file:
        node_crt = cert_file.read()
        cert_begin = node_crt.count(
            '-----BEGIN CERTIFICATE-----', 0, len(node_crt))
        cert_end = node_crt.count(
            '-----END CERTIFICATE-----', 0, len(node_crt))
        if (cert_begin != 2) or (cert_end != 2):
            LOGGER.error(
                ' node cert format checked failed! path is %s', get_path)
            raise MCError(
                ' node cert format checked failed! path is %s' % get_path)
        cert_file.close()
    shutil.copy(get_path, send_path)
    LOGGER.info("get_node_cert success! get path is %s", get_path)
    LOGGER.info("get_node_cert success! send path is %s", send_path)
Exemplo n.º 30
0
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)