Пример #1
0
def unregister_module(ip, dest, index):
    """Using ansible.sh unregister_module, start nodes
    
    Arguments:
        ip {string} -- corresponding server host ip
        dest {string} -- corresponding server dir path
        index {int} -- node index to be register
    
    Returns:
        bool -- true or false
    """

    (status, result) = utils.getstatusoutput('bash ' + path.get_path() +
                                                '/scripts/ansible.sh unregister ' + ip + ' ' + dest + ' ' + str(index))
    logger.debug(' unregister action, status %s, output %s' % (status, result))
    
    if status:
        consoler.warn(' ansible unregister  failed, host is %s, index is %s, dst is %s, status is %s, output is %s.', ip, str(index), dest, status, result)
    elif result.find('SUCCESS') == -1 and result.find('CHANGED') == -1:
        consoler.warn(' ansible unregister failed, host is %s, index is %s, dst is %s, status is %s, output is %s.', ip, str(index), dest, status, result)
    elif not (result.find('success') + 1):
        consoler.warn(' ansible register failed, host is %s, index is %s, dst is %s, status is %s, output is %s.', ip, str(index), dest, status, result)
    else:
        consoler.info(' ansible unregister success, host is %s, index is %s, output is %s.', ip, str(index), result)
        return True
    return False
Пример #2
0
def check_module(ip, dest):
    """Using ansible.sh check_module, check chain status
    
    Arguments:
        ip {string} -- corresponding server host ip
        dest {string} -- corresponding server dir path
    
    Returns:
        bool -- true or false
    """

    (status, result) = utils.getstatusoutput('bash ' + path.get_path() +
                                                '/scripts/ansible.sh check ' + ip + ' ' + dest)
    logger.debug('check action , status %s, output %s' % (status, result))
    
    if status:
        logger.warn('check action failed, status %s' % (status))
        consoler.warn(' ansible check failed, host is %s, dst is %s, status is %s, output is %s.', ip, dest, status, result)
    elif result.find('SUCCESS') == -1 and result.find('CHANGED') == -1:
        logger.warn('check action failed, output %s' % (result))
        consoler.warn(' ansible check failed, host is %s, dst is %s, status is %s, output is %s.', ip, dest, status, result)
    else:
        consoler.info(' ansible check success, host is %s, output is %s.', ip, result)
        return True
    
    return False
Пример #3
0
    def parser(self):

        self.clear()

        if os.path.exists(self.cfg) and os.path.isfile(self.cfg):

            logger.info(' single config is %s', self.cfg)
            # resolve one config.json
            try:
                self.append(ConfigConf(self.cfg))
            except Exception as e:
                logger.warn('parser cfg %s end exception, e is %s ', self.cfg, e)
                raise MCError(' parser config failed, invalid format, config is %s, exception is %s' % (self.cfg, e))

        elif os.path.isdir(self.cfg):
            logger.info(' config dir is %s', self.cfg)
            # resolve dir, if not config.json goto next
            for c in os.listdir(self.cfg):
                try:
                    logger.debug(' config dir is %s, config file is %s', self.cfg, c)
                    cc = ConfigConf(self.cfg + '/' + c)
                    chain = cc.get_chain()
                    if not self.append(cc):
                        cc = self.get_cc(chain)
                        logger.error(' chain_id: %s and chain_version: %s duplicate, config is %s:%s', chain.get_id(), chain.get_version(), cc.get_cfg(), c)
                        raise MCError(' chain_id: %s and chain_version: %s duplicate, config is %s:%s' % (chain.get_id(), chain.get_version(), cc.get_cfg(), c))
                    logger.debug(' append cc, cc is %s', cc)
                    consoler.info(' parser config %s success, chain_id is %s, chain_version is %s', c, chain.get_id(), chain.get_version())
                except Exception as e:
                    consoler.error(
                        ' \033[1;31m skip config %s, invalid config format parser failed, exception is %s \033[0m', c, e)
                    logger.warn(' parser cfg %s end exception, e %s ', c, e)

        else:
            raise MCError(' invalid config, %s not exist' % self.cfg)
Пример #4
0
def check_chain(chain):
    """[Check if the nodes is running normally.]
    
    Arguments:
        chain {[list]} -- [get chain_id:host_ip from command Line]
    """
    
    if chain[0] == 'all':
        dir = data.meta_dir_base()
        if os.path.exists(dir):
            for chain_id in os.listdir(dir):
                check_server(chain_id)
        else:
            consoler.info(' No published chain exist, do nothing.')
    else:
        for i in range(len(chain)):
            chain_get = chain[i].split(':')
            if len(chain_get) == 1:
                if utils.valid_chain_id(chain_get[0]):
                    check_server(chain_get[0])
                else:
                    consoler.info(' skip, invalid chain_id, chain_id is %s', chain_get[0])
            elif len(chain_get) == 2:
                if utils.valid_chain_id(chain_get[0]):
                    if utils.valid_ip(chain_get[1]):
                        ansible.check_module(chain_get[1], ansible.get_dir() + '/' + chain_get[0])
                    else:
                        consoler.info(' skip, invalid host, chain_id is %s, host is %s', chain_get[0], chain_get[1])
                else:
                    consoler.info(' skip, invalid chain_id, chain_id is %s, host is %s', chain_get[0], chain_get[1])

            else:
                consoler.info(' skip, invalid format, not chain_id:host, input %s', chain_get)
def ls_host(hosts):
    """[show in host_ip which port used (published fisco-bcos) ]
    
    Arguments:
        hosts {[string]} -- [host_ip]
    """

    am = AllMeta()
    ns = Names()

    for host in hosts:
        consoler.info(' => host is %s', host)
        if not utils.valid_ip(host):
            consoler.info(' \t => Invalid host ip, host is %s.', host)
            continue

        metas = get_meta_ports_by_host(host, am)
        if len(metas) == 0:
            consoler.info(' \t => No chain published to this host.')
            continue

        for meta in metas:
            consoler.info(
                ' \t => chain id :%s  chain name : %s  published version : %s',
                meta.get_chain_id(), ns.get_name(meta.get_chain_id()),
                meta.get_chain_version())
            nodes = meta.get_host_nodes(host)
            for node in nodes:
                consoler.info(
                    ' \t\t %s, rpc_port:%s, p2p_port:%s, channel_port:%s',
                    node.get_node(), str(node.get_rpc()), str(node.get_p2p()),
                    str(node.get_channel()))
Пример #6
0
def cmd_module(ip, cmd):
    """Using ansible.sh cmd_module, execute commands on the corresponding server.

    Arguments:
        ip {string} -- server host ip

    Keyword Arguments:
        msg {string} -- execute commands

    Returns:
        [bool] -- true or false
    """
    cmd = '"' + cmd + '"'
    
    (status, result) = utils.getstatusoutput('bash ' + path.get_path() +
                                                '/scripts/ansible.sh cmd ' + ip + ' ' + cmd)
    logger.debug(' cmd action , status %s, output %s' % (status, result))
    if status:
        consoler.error(' \033[1;31m  ansible cmd failed, host is %s, output is %s \033[0m', ip, result)
    elif result.find('SUCCESS') == -1 and result.find('CHANGED') == -1:
        consoler.error(' \033[1;31m  ansible cmd failed, host is %s, output is %s \033[0m', ip, result)
    else:
        consoler.info(' ansible cmd success, host is %s, cmd is %s.', ip, cmd)
        return True
    return False
def do_cmd(dst, cmd):
    """do cmd on remote server, dst can be one server, one chain or all server
    
    Arguments:
        dst {string} -- host ip or chain id or 'all'
        cmd {string} -- shell cmd or shell file
    """

    if dst == 'all':
        ansible.cmd_module('all', cmd)
    elif utils.valid_chain_id(dst):
        mm = Meta(dst)
        if not mm.exist():
            consoler.error(
                ' \033[1;31m chain is not published, can not cmd action, chain_id is %s \033[0m',
                dst)
        else:
            consoler.info(' => do cmd, chain id is %s', dst)
            for k in mm.get_nodes().keys():
                logger.debug('host ip is ' + k)
                ansible.cmd_module(k, cmd)
    elif utils.valid_ip(dst):
        ansible.cmd_module(dst, cmd)
    else:
        consoler.error(
            ' \033[1;31m invalid docmd dst, dst is %s, dst should be invalid chain_id or invali host ip or \'all\'. \033[0m',
            dst)
Пример #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)
def register(chain_id, host, node):

    logger.debug(' chain_id is %s, host is %s, node is %s', chain_id, host,
                 node)
    meta = Meta(chain_id)
    if not meta.exist():
        consoler.error(
            ' \033[1;31m register failed, chain not published, chain id is %s \033[0m',
            chain_id)
        return

    try:
        meta.get_host_node(host, node)
    except MCError as me:
        consoler.error(' \033[1;31m register failed, %s  \033[0m', me)
    else:
        ret = ansible.register_module(host,
                                      ansible.get_dir() + '/' + chain_id,
                                      int(node[4:]))
        if ret:
            consoler.info(
                ' register success, chain_id is %s, host is %s, node is %s. \033[0m',
                chain_id, host, node)
        else:
            consoler.error(
                ' \033[1;31m register failed, chain_id is %s, host is %s, node is %s. \033[0m',
                chain_id, host, node)
Пример #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)
Пример #11
0
def unarchive_module(ip, src, dest):
    """[Using ansible.sh unarchive_module, compress files to the corresponding server and extract it]
    
    Arguments:
        ip {[string]} -- [corresponding server host ip]
        src {[string]} -- [files dir path]
        dest {[string]} -- [corresponding server dir path]
    
    Returns:
        [bool] -- [true or false]
    """


    (status, result) = utils.getstatusoutput('bash ' + path.get_path() +
                                                '/scripts/ansible.sh unarchive ' + ip + ' ' + src + ' ' + dest)
    logger.debug('unarchive action , status %s, output %s' % (status, result))
   
    if status:
        logger.warn('unarchive action failed, status %s' % (status))
        consoler.warn(' ansible unarchive  failed, host is %s, src is %s, dst is %s, status is %s, output is %s.', ip, src, dest, status, result)
    elif result.find('SUCCESS') == -1 and result.find('CHANGED') == -1:
        logger.warn('unarchive action failed, output %s' % (result))
        consoler.warn(' ansible unarchive failed, host is %s, src is %s, dst is %s, status is %s, output is %s.', ip, src, dest, status, result)
    else:
        consoler.info(' ansible unarchive success, host is %s, src is %s, dst is %s.', ip, src, dest)
        return True
    return False
Пример #12
0
def copy_module(ip, src, dest):
    """[Using ansible.sh copy_module, push package to servers]
    
    Arguments:
        ip {string} -- corresponding server host ip
        src {string} -- files which push
        dest {string} -- corresponding server dir path

    Returns:
        bool -- true or false.
    """

    (status, result) = utils.getstatusoutput('bash ' + path.get_path() +
                                                '/scripts/ansible.sh copy ' + ip + ' ' + src + ' ' + dest)
    logger.debug('copy action , status %s, output %s' % (status, result))
   
    if status:
        logger.warn('copy action failed, status %s' % (status))
        consoler.warn(' ansible copy failed, host is %s, src is %s, dst is %s, status is %s, output is %s.', ip, src, dest, status, result)
    elif result.find('SUCCESS') == -1 and result.find('CHANGED') == -1:
        consoler.warn(' ansible copy failed, host is %s, src is %s, dst is %s, status is %s, output is %s.', ip, src, dest, status, result)
        logger.warn('copy action failed, output %s' % (result))
    else:
        consoler.info(' ansible copy success, host is %s, src is %s, dst is %s.', ip, src, dest)
        return True
    return False
Пример #13
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
Пример #14
0
def diagnose_module(ip, dest):
    """Using ansible.sh diagnose_module, call script -> monotor.sh, Check status of nodes
    Arguments:
        ip {string} -- corresponding server host ip
        dest {string} -- corresponding server dir path
    """

    (status, result) = utils.getstatusoutput('bash ' + path.get_path() +
                                                '/scripts/ansible.sh diagnose ' + ip + ' ' + dest)
    logger.debug('diagnose action , status %s, output %s' % (status, result))
    if status:
        consoler.error(' \033[1;31m  ansible diagnose failed, host is %s, output is %s \033[0m', ip, result)
    elif result.find('SUCCESS') == -1 and result.find('CHANGED') == -1:
        consoler.error(' \033[1;31m  ansible diagnose failed, host is %s, output is %s \033[0m', ip, result)
    else:
        consoler.info(' ansible diagnose success, host is %s, result is %s.', ip, result)
        return True
    return False
Пример #15
0
def check_server(chain_id):
    """[Using scheck.sh check all nodes of a chain]
    
    Arguments:
        chain_id {[string]} -- [chain_id:version]
    """

    mm = Meta(chain_id)
    if not mm.exist():
        logger.warn('chain meta is not exist, maybe the chain is not published, chain_id is %s', chain_id)
        consoler.warn('chain is not published, can not check action, chain_id is %s', chain_id)
        return 

    logger.info('check action, chain_id is ' + chain_id)
    consoler.info(' => check all node of chain %s', chain_id)
    for k in mm.get_nodes().keys():
        logger.debug('host ip is ' + k)
        ansible.check_module(k, ansible.get_dir() + '/' + chain_id)
Пример #16
0
def publish_server(chain_id, chain_version, force=False):
    """publish one chain.

    Arguments:
        chain_id {string} -- chain id.
        chain_version {string} -- chain version.
        force {bool}
    """

    chain = Chain(chain_id, chain_version)
    dir = chain.data_dir()
    if not os.path.isdir(dir):
        consoler.info(
            ' No build version exist for chain_id:%s chain_version:%s, do nothing.', chain_id, chain_version)
        logger.warn(
            ' No build version exist for chain_id:%s chain_version:%s, do nothing.', chain_id, chain_version)
        return

    mm = meta.Meta(chain_id)
    if force:
        # force is set, publish this chain again.
        mm.clear()
        mm.set_chain_version(chain_version)
    else:
        if mm.exist():
            if chain_version != mm.get_chain_version():
                consoler.error(
                    ' \033[1;31m chain %s already publish %s version, if you want publish annother version, --force/-f need to be set.\033[0m ', chain_id, mm.get_chain_version())
                return
        else:
            mm.set_chain_version(chain_version)

    consoler.info(' publish package for chain %s version %s begin.',
                  chain_id, chain_version)

    for host in os.listdir(dir):
        if not utils.valid_ip(host):
            logger.debug(' skip, not invalid host_ip ' + host)
            continue
        ret = push_package(dir, host, chain_id, chain_version, mm, force)
        if ret:
            hp = HostPort(chain_id, chain_version, host)
            for node_dir, p in hp.get_ports().items():
                logger.debug(' node_dir is %s, port is %s', node_dir, p)
                if not mm.host_node_exist(host, node_dir):
                    mm.append(meta.MetaNode(host, p.get_rpc_port(), p.get_p2p_port(), p.get_channel_port(), node_dir))
            consoler.info(' \t push package : %s success.', host)
        else:
            consoler.error(' \033[1;31m \t push package : %s  failed. \033[0m', host)
    # record meta info, write meta.json file
    mm.write_to_file()
    consoler.info(' publish package for chain %s version %s end.', chain_id, chain_version)
def telnet_ansible(server):
    """[Test ansible operation to all server in operation server]
    
    Arguments:
        server {[list]} -- [host ip]
    """

    if server[0] == 'all':
        server = ['all']

    for i in range(len(server)):
        if utils.valid_ip(server[i]) or server[i] == 'all':
            if ansible.telnet_module(server[i]):
                consoler.info(' telnet test success, host is %s.\n', server[i])
            else:
                consoler.error(
                    ' \033[1;31m telnet test failed, host is %s. \033[0m',
                    server[i])
        else:
            consoler.error(
                ' \033[1;31m Not invalid host, skip, host is %s. \033[0m',
                server[i])
def pub_list(chains):
    """[List the nodes in package corresponding to the --publishi chain:version]
    
    Arguments:
        chains {[list]} -- [chain id]
    """

    logger.info('list begin, chains is %s', chains)
    consoler.info(' chains is %s' % chains)
    ns = Names()
    meta_list = []
    if chains[0] == 'all':
        dir = data.meta_dir_base()
        if os.path.exists(dir):
            for chain_id in os.listdir(dir):
                m = Meta(chain_id)
                if not m.empty():
                    meta_list.append(m)
        else:
            consoler.info(' No published chain exist, do nothing.')
    else:
        for chain_id in chains:
            m = Meta(chain_id)
            if not m.empty():
                meta_list.append(m)

    for m in meta_list:
        consoler.info(
            ' => chain id :%s  chain name : %s  published version : %s',
            m.get_chain_id(), ns.get_name(m.get_chain_id()),
            m.get_chain_version())
        nodes = m.get_nodes()
        for host, nodes in nodes.items():
            consoler.info('\t host => %s', host)
            for node in nodes:
                consoler.info('\t\t node => %s', node.get_node())

    logger.info('list end.')
def expand(cfg, dir):
    """expand operation 
    
    Arguments:
        cfg {string} -- config file path
        dir {string} -- dir with fisco-bcos, genesis.json, bootstrapsnode.json
    """

    logger.debug(' cfg is %s, dir is %s', cfg, dir)

    try:

        # check java env
        java = JAVA()

        try:
            # parser and check config if valid
            cc = ConfigConf(cfg)
        except Exception as e:
            raise MCError(
                ' parser config failed, invalid format, config is %s, exception is %s'
                % (cfg, e))

        if os.path.exists(cc.get_chain().data_dir()):
            ns = Names()
            expand_chain.expand_on_exist_chain(cc)
            ns.write()
        else:
            expand_chain.expand_on_nonexist_chain(cc, dir)

        consoler.info(
            ' expand install package for chain %s version %s success.',
            cc.get_chain().get_id(),
            cc.get_chain().get_version())

    except MCError as me:
        consoler.error(me)
        consoler.error(' \033[1;31m \t %s \033[0m', me)
Пример #20
0
def diagnose_server(chain_id):
    """[Using diagnose.sh diagnose all nodes of a chain]
    
    Arguments:
        chain_id {[string]} -- [chain_id:version]
    """

    mm = Meta(chain_id)
    if not mm.exist():
        logger.warn(
            'chain meta is not exist, maybe the chain is not published, chain_id is %s',
            chain_id)
        consoler.warn(
            ' chain is not published, can not diagnose action, chain_id is %s',
            chain_id)
        return

    consoler.info(' ==> diagnose chain %s', chain_id)

    logger.info('diagnose_server action, chain_id is ' + chain_id)
    for k in mm.get_nodes().keys():
        logger.debug('host ip is ' + k)
        ansible.diagnose_module(k, ansible.get_dir() + '/' + chain_id)
Пример #21
0
def telnet_module(ip, msg='HelloWorld!'):
    """using ansible.sh telnet module, echo 'HelloWorld' to screen to check ansible is useful.

    Arguments:
        ip {string} -- host ip

    Keyword Arguments:
        msg {string} -- test string (default: {'HelloWorld!'})

    Returns:
        [bool] -- ansible useful echo return True, else False.
    """

    (status, result) = utils.getstatusoutput('bash ' + path.get_path() +
                                                '/scripts/ansible.sh telnet ' + ip + ' ' + msg)
    logger.debug('telnet action , status %s, output %s' % (status, result))
    if status:
        consoler.error(' \033[1;31m  ansible telnet failed, host is %s, output is %s \033[0m', ip, result)
    elif result.find('SUCCESS') == -1 and result.find('CHANGED') == -1:
        consoler.error(' \033[1;31m  ansible telnet failed, host is %s, output is %s \033[0m', ip, result)
    else:
        consoler.info(' ansible telnet success, host is %s, output is %s', ip, result)
        return True
    return False
Пример #22
0
 def export(self):
     try:
         if self.fisco.is_gm():
             shutil.move(get_gm_god_path() + '/godInfo.txt',
             get_gm_god_path() + '/godInfo.txt.bak')
             cmd = self.fisco.get_fisco_path() + ' --newaccount ' + get_gm_god_path() + '/godInfo.txt'
             status, result = utils.getstatusoutput(cmd)
             logger.debug(' start status, status is %d, output is %s', status, result)
         else:
             shutil.move(get_god_path() + '/godInfo.txt',
             get_god_path() + '/godInfo.txt.bak')
             cmd = self.fisco.get_fisco_path() + ' --newaccount ' + get_god_path() + '/godInfo.txt'
             status, result = utils.getstatusoutput(cmd)
             logger.debug(' start status, status is %d, output is %s', status, result)
         if status != 0:
             logger.warn(' export godInfo.txt failed! status is %d, output is %s.', status, result)
             raise MCError('godInfo.txt failed! status is %d, output is %s.' % (status, result))
         logger.info(' export godInfo.txt status is %d, output is %s.', status, result)
         consoler.info(' export godInfo.txt success')
     except MCError as me:
         consoler.error(' \033[1;31m %s \033[0m', me)
     except Exception as e:
         consoler.error(' \033[1;31m export godInfo.txt failed! excepion is %s.\033[0m', e)
         logger.error('  export godInfo.txt failed!')
Пример #23
0
def build(cfg, fisco_path):
    """parser input config file, build install pacakge by 

    Arguments:
        cfg {string} --config, maybe a config.json or a dictionary of conf,eg: ./conf/config.conf or ./conf
        fisco_path {string} -- path of fisco-bcos, eg: /usr/local/bin/fisco-bcos
    """

    logger.info('build cfg is %s, fisco is %s ', cfg, fisco_path)
    try:

        # check java env
        java = JAVA()
        fisco = Fisco(fisco_path)

        # parser and check config if valid
        ccs = ConfigConfs(cfg).get_ccs()
        ns = Names()
        # build all chain
        if len(ccs) != 0:
            for cc in ccs.values():
                try:
                    chain_id = cc.get_chain().get_id()
                    chain_version = cc.get_chain().get_version()
                    chain_name = cc.get_chain().get_name()
                    consoler.info(
                        ' build install package for chain %s version %s.',
                        chain_id, chain_version)
                    build_chain.build(cc, fisco)
                    ns.append(chain_id, chain_name)
                    consoler.info(
                        ' \t build install package for chain %s version %s success.',
                        chain_id, chain_version)
                except MCError as me:
                    logger.error(me)
                    consoler.error(' \033[1;31m \t %s \033[0m', me)

                ns.write()
        else:
            consoler.info(' build operation will do nothing.')

    except MCError as me:
        logger.error(me)
        consoler.error(' \033[1;31m \t %s \033[0m', me)

    logger.info(' chain build end.')
Пример #24
0
def stop_chain(chain):
    """[stop nodes]
    
    Arguments:
        chain {[list]} -- [get chain_id:host_ip from command Line]
    """

    if chain[0] == 'all':
        consoler.info('You want to stop all node,are you sure? yes or no? y/n')
        consoler.info('Your choice is : ')
        choice = sys.stdin.readline().strip('\n')
        if ((choice == 'yes') | (choice == 'Yes') | (choice == 'Y') |
            (choice == 'y')):
            dir = data.meta_dir_base()
            if os.path.exists(dir):
                for chain_id in os.listdir(dir):
                    stop_server(chain_id)
            else:
                consoler.info(' No published chain exist, do nothing.')
        else:
            consoler.info(' input No, and will do nothing.')
            logger.info('refuse stop all node')
    else:
        for i in range(len(chain)):
            chain_get = chain[i].split(':')
            if len(chain_get) == 1:
                if utils.valid_chain_id(chain_get[0]):
                    stop_server(chain_get[0])
                else:
                    consoler.info(' skip, invalid chain_id, chain_id is %s',
                                  chain_get[0])
            elif len(chain_get) == 2:
                if utils.valid_chain_id(chain_get[0]):
                    if utils.valid_ip(chain_get[1]):
                        ansible.stop_module(
                            chain_get[1],
                            ansible.get_dir() + '/' + chain_get[0])
                    else:
                        consoler.info(
                            ' skip, invalid host, chain_id is %s, host is %s',
                            chain_get[0], chain_get[1])
                else:
                    consoler.info(
                        ' skip, invalid chain_id, chain_id is %s, host is %s',
                        chain_get[0], chain_get[1])
            else:
                consoler.info(
                    ' skip, invalid format, not chain_id:host, input %s',
                    chain_get)
def pkg_list(chains):
    """list all version and all pacakge of the chain
    
    Arguments:
        chains {[type]} -- all chains
    """

    logger.info(' chains is %s', chains)

    consoler.info(' chains is %s' % chains)

    ns = Names()

    if chains[0] == 'all':
        ac = AllChain()
        chains = ac.get_chains()
        if len(chains) == 0:
            consoler.info(' No build chain exist, do nothing.')

    for chain in chains:
        logger.debug(' chain id is %s, chain name is %s', chain,
                     ns.get_name(chain))
        consoler.info(' ==> chain id : %s ,chain name is %s', chain,
                      ns.get_name(chain))
        cv = ChainVers(chain)
        if len(cv.get_ver_list()) == 0:
            consoler.info(' No build version exist for chain %s, do nothing.',
                          chain)

        for version in cv.get_ver_list():
            consoler.info('\t\t => chain version : %s', version)
            logger.debug(' chain id is %s, chain version is %s', chain,
                         version)
            vh = VerHosts(chain, version)
            for pkg in vh.get_pkg_list():
                consoler.info('\t\t\t => package :%s', pkg)
                hn = HostNodeDirs(chain, version, pkg)
                hp = HostPort(chain, version, pkg)
                for node_dir in hn.get_node_dirs():
                    consoler.info('\t\t\t\t => %s %s ', node_dir,
                                  hp.get_by_index(node_dir))

    logger.info('load end')
Пример #26
0
 def logout(self):
     consoler.info('%s', self.msg)
Пример #27
0
def version():
    """load release_node.txt, print version number.
    """

    with open(path.get_path() + '/release_note.txt', 'r') as f:
        consoler.info(f.read())
Пример #28
0
def Usage():
    """cmd usage
    """

    parser = argparse.ArgumentParser(
        description=
        ' Build fisco-bcos install pkg for multi chain and manage the chain  package with ansible. '
    )

    parser.add_argument('--version',
                        action='store_true',
                        help='show OWMC\'s version. ')
    parser.add_argument(
        '--ansibleinit',
        nargs=1,
        metavar=(' hosts config file '),
        help=' initialize ansible configuration file, need sudo permissions. ')
    parser.add_argument('--cainit',
                        action='store_true',
                        help=' initialize cert. ')

    pkg_group = parser.add_argument_group(
        ' Build, Expand, Export, List Chain Package Options ')
    pkg_group.add_argument(
        '--build',
        nargs=2,
        metavar=('./config.conf or ./conf/', 'fisco_path'),
        help=' build chain packages with the specified configuration file')
    pkg_group.add_argument('--expand',
                           nargs=2,
                           metavar=('./config.conf, dir'),
                           help='build chain packages on exist chain')
    pkg_group.add_argument('--export',
                           nargs=3,
                           metavar=('chain_id', 'chain_version', 'dest_path'),
                           help='export build package out.')
    pkg_group.add_argument('--pkglist',
                           nargs='+',
                           metavar=('all or chain_id'),
                           help='list build packages info.')
    pkg_group.add_argument(
        '--direct',
        action='store_true',
        help=
        'follow --export/-E, package of chain will export without reordering')

    mgr_group = parser.add_argument_group(
        ' Manage Published Chain With Ansible Options ')
    mgr_group.add_argument('--publish',
                           nargs='+',
                           metavar=('chain_id:version'),
                           help='publish packages')
    mgr_group.add_argument('--start',
                           nargs='+',
                           metavar=('all or chain_id or', 'chain_id:host_ip'),
                           help='start node')
    mgr_group.add_argument('--stop',
                           nargs='+',
                           metavar=('all or chain_id or', 'chain_id:host_ip'),
                           help='stop node')
    mgr_group.add_argument('--register',
                           nargs=3,
                           metavar=('chain_id ', 'host_ip', 'node'),
                           help='register node on node with expand opr build')
    mgr_group.add_argument('--unregister',
                           nargs=3,
                           metavar=('chain_id ', 'host_ip', 'node'),
                           help='unregister node')
    mgr_group.add_argument('--diagnose',
                           nargs='+',
                           metavar=('all or chain_id or', 'chain_id:host_ip'),
                           help='diagnose node')
    mgr_group.add_argument('--check',
                           nargs='+',
                           metavar=('all or chain_id or', 'chain_id:host_ip'),
                           help='check servers status')
    mgr_group.add_argument('--publist',
                           nargs='+',
                           metavar=('all or chain_id or'),
                           help='list published packages info.')
    mgr_group.add_argument('--lshost',
                           nargs='+',
                           metavar=('host_ip'),
                           help='ls published packages\' host')
    mgr_group.add_argument(
        '--force',
        action='store_true',
        help='follow --publish, all package of chain will be republished')

    tools_group = parser.add_argument_group(' Other Tools Options ')
    tools_group.add_argument('--envcheck',
                             nargs='+',
                             metavar=('all or host_ip'),
                             help='check build environment')
    tools_group.add_argument('--telnet',
                             nargs='+',
                             metavar=('\'all\' or host_ip or chain_id'),
                             help='test ansible')
    tools_group.add_argument(
        '--docmd',
        nargs=2,
        metavar=(' host ip or chain id or \'all\'',
                 'shell cmd or shell file, e.g. \'ls -lt\' test.sh'),
        help='execute a shell command or shell file on remote server')
    tools_group.add_argument('--pushfile',
                             nargs=3,
                             metavar=('host ip or chain id or \'all\'',
                                      'file or dir to be push.', 'dst dir.'),
                             help='push one file or dir to remote server.')
    tools_group.add_argument('--chainca',
                             nargs=1,
                             metavar=('chain_dir', ),
                             help='generate root cert')
    tools_group.add_argument('--agencyca',
                             nargs=3,
                             metavar=('agency_dir', 'chain_dir',
                                      ' agency_name'),
                             help='generate agency cert')
    tools_group.add_argument('--nodeca',
                             nargs=3,
                             metavar=('agency_dir', 'node _dir', 'node_name'),
                             help='generate node cert')
    tools_group.add_argument('--sdkca',
                             nargs=2,
                             metavar=('sdk_dir', 'agency_dir'),
                             help='generate sdk cert')
    tools_group.add_argument(
        '--gm',
        action='store_true',
        help=
        'follow ----chainca/--agencyca/--agencyca/--sdkca, is gm ca operation.'
    )
    tools_group.add_argument('--god',
                             nargs=1,
                             metavar=('fiscopath'),
                             help='god module.')

    args = parser.parse_args()
    os.path.exists
    if args.version:
        version.version()
    elif args.god:
        consoler.info(' god operation begin.')
        opr_god.opr_god(args.god[0])
        consoler.info(' god operation end.')
    elif args.build:
        consoler.info(' build operation begin.')
        opr_build.build(args.build[0], args.build[1])
        consoler.info(' build operation end.')
    elif args.expand:
        consoler.info(' expand operation begin.')
        opr_expand.expand(args.expand[0], args.expand[1])
        consoler.info(' expand operation end.')
    elif args.check:
        consoler.info(' check operation begin.')
        chain = args.check
        opr_check.check_chain(chain)
        consoler.info(' check operation end.')
    elif args.diagnose:
        consoler.info(' diagnose operation begin.')
        chain = args.diagnose
        opr_diagnose.diagnose_chain(chain)
        consoler.info(' diagnose operation end.')
    elif args.publish:
        consoler.info(' publish operation begin.')
        chain = args.publish
        opr_publish.publish_chain(chain, args.force)
        consoler.info(' publish operation end.')
    elif args.register:
        consoler.info(' register operation begin.')
        opr_register.register(args.register[0], args.register[1],
                              args.register[2])
        consoler.info(' register operation end.')
    elif args.unregister:
        consoler.info(' unregister operation begin.')
        opr_register.unregister(args.unregister[0], args.unregister[1],
                                args.unregister[2])
        consoler.info(' unregister operation end.')
    elif args.start:
        consoler.info(' start operation begin.')
        chain = args.start
        opr_start.start_chain(chain)
        consoler.info(' start operation end.')
    elif args.stop:
        consoler.info(' stop operation begin.')
        chain = args.stop
        opr_stop.stop_chain(chain)
        consoler.info(' stop operation end.')
    elif args.publist:
        consoler.info(' publist operation begin.')
        chain = args.publist
        opr_list.pub_list(chain)
        consoler.info(' publist operation end.')
    elif args.pkglist:
        consoler.info(' pkglist operation begin.')
        chain = args.pkglist
        opr_list.pkg_list(chain)
        consoler.info(' pkglist operation end.')
    elif args.docmd:
        consoler.info(' docmd operation begin.')
        params = args.docmd
        opr_tools.do_cmd(params[0], params[1])
        consoler.info(' docmd operation end.')
    elif args.pushfile:
        consoler.info(' pushfile operation begin.')
        params = args.pushfile
        opr_tools.push_file(params[0], params[1], params[2])
        consoler.info(' pushfile operation end.')
    elif args.chainca:
        consoler.info(' chain cert begin.')
        chain_dir = args.chainca[0]
        ca.generate_root_ca(chain_dir, args.gm)
        consoler.info(' chain cert end.')
    elif args.agencyca:
        consoler.info(' agency cert begin.')
        agency_dir = args.agencyca[0]
        chain_dir = args.agencyca[1]
        agency_name = args.agencyca[2]
        ca.generator_agent_ca(agency_dir, chain_dir, agency_name, args.gm)
        consoler.info(' agency cert end.')
    elif args.nodeca:
        consoler.info(' agency cert begin.')
        agency_dir = args.nodeca[0]
        node_dir = args.nodeca[1]
        node_name = args.nodeca[2]
        ca.generator_node_ca(agency_dir, node_dir, node_name, args.gm)
        consoler.info(' agency cert end.')
    elif args.sdkca:
        consoler.info(' sdk cert begin.')
        sdk_dir = args.sdkca[0]
        agency_dir = args.sdkca[1]
        ca.generator_sdk_ca(agency_dir, sdk_dir, args.gm)
        consoler.info(' sdk cert end.')
    elif args.envcheck:
        consoler.info(' envcheck operation begin.')
        hosts = args.envcheck
        opr_env_check.env_check(hosts)
        consoler.info(' envcheck operation end.')
    elif args.telnet:
        consoler.info(' telnet operation begin.')
        telnet_list = args.telnet
        opr_tools.telnet_ansible(telnet_list)
        consoler.info(' telnet operation end.')
    elif args.ansibleinit:
        opr_init_chain.init_ansible(args.ansibleinit[0], False)
        consoler.info(' ansible init end.')
    elif args.cainit:
        consoler.info(' cert init begin.')
        ca.init_ca(args.cainit[0])
        consoler.info(' cert init end.')
    elif args.export:
        consoler.info(' export operation begin.')
        opr_export.export_package(args.export[0], args.export[1],
                                  args.export[2], args.direct)
        consoler.info(' export operation end.')
    elif args.lshost:
        consoler.info(' lshost operation begin.')
        opr_list.ls_host(args.lshost)
        consoler.info(' lshost operation end.')
    else:
        consoler.error(
            '\033[1;31m invalid operation,  \"owmc -h\" can be used to show detailed usage. \033[0m'
        )
    return 0
def push_file(host, src, dst):
    """push file to remote server
    
    Arguments:
        host {string} -- host ip or chain id or 'all'
        src {string} -- file or dir
        dst {string} -- dst dir
    """

    if not os.path.exists(src):
        consoler.error(' \033[1;31m src is not exist, src is %s. \033[0m', src)
        return

    logger.info(' host is %s, src is %s, dst is %s', host, src, dst)

    if host == 'all':
        if mkdir_and_push(host, src, dst):
            consoler.info(' push %s to %s of all server success.', src, dst)
    elif utils.valid_chain_id(host):
        consoler.info(' => push %s to %s of chain %s.', src, dst, host)
        mm = Meta(host)
        if not mm.exist():
            consoler.error(
                ' \033[1;31m chain is not published, can not push file action, chain_id is %s \033[0m',
                host)
        else:
            consoler.info(' => do cmd, chain id is %s', host)
            for k in mm.get_nodes().keys():
                logger.debug(' host is %s', k)
                if mkdir_and_push(k, src, dst):
                    consoler.info(' \t\t push %s to %s of %s server success.',
                                  src, dst, k)
        consoler.info(' => push %s to %s of chain %s end.', src, dst, host)
    elif utils.valid_ip(host):
        if mkdir_and_push(host, src, dst):
            consoler.info(' push %s to %s of %s server success.', src, dst,
                          host)
    else:
        consoler.error(
            ' \033[1;31m invalid push file host, host is %s, dst should be invalid chain_id or invali host ip or \'all\'. \033[0m',
            host)