def init_ansible(hosts_conf, add_opr=False):
    try:
        if not os.path.exists(hosts_conf):
            raise MCError('hosts_conf not exisits! ')
        if add_opr:
            src = '/etc/ansible/hosts'
            dst = '/etc/ansible/hosts.bak'
            if not os.path.exists(src):
                raise MCError('/etc/ansible/hosts not exisits! ')
            os.rename(src, dst)
            f = open(src, 'w')
            f.close()
        for line in open(hosts_conf):
            line = line.strip()
            host_value = line.split()
            if len(host_value) != 4:
                raise Exception('hosts_conf type error ,host_line -> %s',
                                host_value)
            user = host_value[0]
            ip = host_value[1]
            port = host_value[2]
            passwd = host_value[3]
            if not utils.valid_string(user):
                raise Exception(
                    'user type error ,user -> %s, host_line -> %s' %
                    (user, host_value))
            if not utils.valid_ip(ip):
                raise Exception('ip type error ,ip -> %s, host_line -> %s' %
                                (ip, host_value))
            if not utils.valid_port(int(port)):
                raise Exception(
                    'port type error ,port -> %s, host_line -> %s' %
                    (port, host_value))
            if not utils.valid_string(passwd):
                raise Exception(
                    'passwd type error ,passwd -> %s, host_line -> %s' %
                    (passwd, host_value))
            (status,
             result) = utils.getstatusoutput('bash ' + path.get_path() +
                                             '/scripts/ansible_init.sh' + ' ' +
                                             user + ' ' + ip + ' ' + port +
                                             ' ' + passwd)
            if status != 0:
                logger.warn(
                    ' ansible_init failed! status is %d, output is %s.',
                    status, result)
                raise MCError(
                    'ansible_init failed! status is %d, output is %s.' %
                    (status, result))
            result = []
            result.append(ip)
            opr_tools.telnet_ansible(result)
            logger.info(' ansible_init success! status is %d, output is %s',
                        status, result)
    except MCError as me:
        consoler.error(' \033[1;31m %s \033[0m', me)
    except Exception as e:
        consoler.error(
            ' \033[1;31m ansible_init failed! excepion is %s.\033[0m', e)
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())
Example #3
0
def parser(mgroup):
    """resolve group_genesis.ini

    Arguments:
        mgroup {string} -- path of group_genesis.ini

    Raises:
        MCError -- exception description
    """

    LOGGER.info('group_genesis.ini is %s', mgroup)
    # resolve configuration
    if not utils.valid_string(mgroup):
        LOGGER.error(
            ' group_genesis.ini not invalid path, group_genesis.ini is %s', mgroup)
        raise MCError(
            ' group_genesis.ini not invalid path, group_genesis.ini is %s' % mgroup)

    # read and parser config file
    config_parser = configparser.ConfigParser(allow_no_value=True)
    try:
        with codecs.open(mgroup, 'r', encoding='utf-8') as file_mchain:
            config_parser.readfp(file_mchain)
    except Exception as ini_exp:
        LOGGER.error(
            ' open group_genesis.ini file failed, exception is %s', ini_exp)
        raise MCError(
            ' open group_genesis.ini file failed, exception is %s' % ini_exp)

    if config_parser.has_section('group'):
        MgroupConf.group_id = config_parser.get('group', 'group_id')
    else:
        LOGGER.error(
            ' invalid group_genesis.ini format, group id is %s', MgroupConf.group_id)
        raise MCError(
            ' invalid group_genesis.ini format, group id is %s' % MgroupConf.group_id)

    if not config_parser.has_section('nodes'):
        LOGGER.error(
            ' invalid group_genesis.ini format, nodes not existed!')
        raise MCError(
            ' invalid group_genesis.ini format, nodes not existed!')

    group_nodes = config_parser.options('nodes')

    for node in group_nodes:
        p2p_section = config_parser.get('nodes', node)
        utils.valid_package(p2p_section)
        MgroupConf.p2p_ip.append(p2p_section.split(':')[0])
        MgroupConf.p2p_listen_port.append(p2p_section.split(':')[1])

    LOGGER.info('group_id is %s', MgroupConf.group_id)
    LOGGER.info('p2p_ip is %s', MgroupConf.p2p_ip)
    LOGGER.info('p2p_listen_port is %s', MgroupConf.p2p_listen_port)

    LOGGER.info('group_genesis.ini end, result is %s', MgroupConf())
Example #4
0
def parser(mchain):
    """resolve node_installation.ini

    Arguments:
        mchain {string} -- path of node_installation.ini

    Raises:
        MCError -- exception description
    """

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

    # read and parser config file
    config_parser = configparser.ConfigParser(allow_no_value=True)
    try:
        with codecs.open(mchain, 'r', encoding='utf-8') as file_mchain:
            config_parser.readfp(file_mchain)
    except Exception as ini_exp:
        LOGGER.error(
            ' open node_installation.ini file failed, exception is %s',
            ini_exp)
        raise MCError(
            ' open node_installation.ini file failed, exception is %s' %
            ini_exp)
    for idx in range(0, 128):
        node_index = ('node{}'.format(idx))
        if config_parser.has_section('group'):
            MchainConf.group_id = config_parser.get('group', 'group_id')
        else:
            LOGGER.error(
                ' invalid node_installation.ini format, group id is %s',
                MchainConf.group_id)
            raise MCError(
                ' invalid node_installation.ini format, group id is %s' %
                MchainConf.group_id)

        if config_parser.has_section(node_index):
            p2p_ip = config_parser.get(node_index, 'p2p_ip')
            rpc_ip = config_parser.get(node_index, 'rpc_ip')
            if not utils.valid_ip(p2p_ip):
                LOGGER.error(
                    ' invalid node_installation.ini format, p2p_ip is %s',
                    p2p_ip)
                raise MCError(
                    ' invalid node_installation.ini format, p2p_ip is %s' %
                    p2p_ip)
            # if  rpc_ip == "0.0.0.0" and utils.Status.allow_unsecure_cfg:
            if rpc_ip == "0.0.0.0":
                LOGGER.warning('Your rpc_ip is %s, this is an unsecurity way',
                               rpc_ip)
                CONSOLER.warning(
                    ' \033[1;31m Your rpc_ip is %s, this is an unsecurity way \033[0m',
                    rpc_ip)
            elif not utils.valid_ip(rpc_ip):
                LOGGER.error(
                    ' invalid node_installation.ini format, rpc_ip is %s',
                    rpc_ip)
                raise MCError(
                    ' invalid node_installation.ini format, rpc_ip is %s' %
                    rpc_ip)
            p2p_listen_port = config_parser.get(node_index, 'p2p_listen_port')
            jsonrpc_listen_port = config_parser.get(node_index,
                                                    'jsonrpc_listen_port')
            channel_listen_port = config_parser.get(node_index,
                                                    'channel_listen_port')
            if not (utils.valid_string(p2p_listen_port)
                    and utils.valid_string(jsonrpc_listen_port)
                    and utils.valid_string(channel_listen_port)):
                LOGGER.error(
                    'mchain bad format, p2p_listen_port is %s, '
                    'jsonrpc_port is %s, channel_port is %s', p2p_listen_port,
                    jsonrpc_listen_port, channel_listen_port)
                raise MCError('mchain bad format, p2p_listen_port is %s, '
                              'jsonrpc_port is %s, channel_port is %s' %
                              (p2p_listen_port, jsonrpc_listen_port,
                               channel_listen_port))
            MchainConf.p2p_ip.append(p2p_ip)
            MchainConf.rpc_ip.append(rpc_ip)
            MchainConf.p2p_listen_port.append(p2p_listen_port)
            MchainConf.jsonrpc_listen_port.append(jsonrpc_listen_port)
            MchainConf.channel_listen_port.append(channel_listen_port)
        else:
            LOGGER.warning(' node%s not existed, break!', idx)
            break
    # if config_parser.has_section('peers'):
    #     for peer in config_parser.items('peers'):
    #         MchainConf.peers.append(peer[1])
    # else:
    #     LOGGER.warning(' section peers not existed!')

    LOGGER.info('group_id is %s', MchainConf.group_id)
    LOGGER.info('p2p_ip is %s', MchainConf.p2p_ip)
    LOGGER.info('rpc_ip is %s', MchainConf.rpc_ip)
    LOGGER.info('p2p_listen_port is %s', MchainConf.p2p_listen_port)
    LOGGER.info('jsonrpc_listen_port is %s', MchainConf.jsonrpc_listen_port)
    LOGGER.info('channel_listen_port is %s', MchainConf.channel_listen_port)
    LOGGER.info('peers is %s', MchainConf.peers)

    LOGGER.info('node_installation.ini end, result is %s', MchainConf())
Example #5
0
    def parser(self):
        '''
        resolve config.conf, return object[Config.conf]
        '''
        cfg = self.cfg
        logger.info('cfg parser %s', cfg)

        # read and parser config file
        cf = configparser.ConfigParser()
        with codecs.open(cfg, 'r', encoding='utf-8') as f:
            cf.readfp(f)

        chain_id = cf.get('chain', 'chainid')
        if not utils.valid_string(chain_id):
            raise Exception('chain_id empty.')
        
        if not utils.valid_chain_id(chain_id):
            raise Exception('invalid chain_id, ', chain_id)

        chain_version = cf.get('chain', 'version')
        if not utils.valid_string(chain_id):
            raise Exception('chain_version empty.')
        
        chain_name = str(chain_id)
        try:
            chain_name = cf.get('chain', 'chainname')
            if not utils.valid_string(chain_name):
                chain_name = str(id)
        except Exception as e:
            pass
        
        self.set_chain(Chain(chain_id, chain_version, chain_name))

        rpc_port = cf.getint('ports', 'rpc_port')
        if not utils.valid_port(rpc_port):
            raise Exception('invalid rpc_port, ', rpc_port)
        p2p_port = cf.getint('ports', 'p2p_port')
        if not utils.valid_port(p2p_port):
            raise Exception('invalid p2p_port, ', p2p_port)
        channel_port = cf.getint('ports', 'channel_port')
        if not utils.valid_port(channel_port):
            raise Exception('invalid channel_port, ', channel_port)
        port = Port(rpc_port, p2p_port, channel_port)
        if port.check_port():
            raise Exception('port config dup, ', port)
        self.set_port(port)

        index = 0
        while True:
            try:
                n = NodeEle(cf.get('pkgs', 'pkg%u' % index))
                index += 1
                n.do_parser()
            except Exception as e:
                logger.info('cfg parser end, e is %s, result is %s', e, self)
                break
            else:
                if not self.add_node(n):
                    raise Exception(' duplicate host ip, host is ', n.get_host_ip())
        
        if len(self.get_nodes()) == 0:
            raise Exception('invalid cfg format, nodes empty')

        logger.info('cfg parser end, result is %s', self)