Beispiel #1
0
def push_package(dir, host, chain_id, version, meta, force = True):
    """push install package of one server
    
    Arguments:
        dir {string} -- package install dir       
        host {string} -- server host
        chain_id {string} -- chain id
        version {string} -- chain version
        force {string} -- is push all node dir or not published
    
    Returns:
        [bool] -- success return True, if not False will return.
    """
    # check if common dir exist.
    if not os.path.exists(dir + '/common'):
        logger.warn(' common dir is not exist, dir is %s, host is %s', dir, host)
        return False

    # check if host dir exist.
    if not os.path.exists(dir + '/' + host):
        logger.warn(' host dir is not exist, dir is %s, host is %s', dir, host)
        return False
    
    try:
        if meta.get_host_nodes(host):
            pass
    except MCError as me:
        # create dir on the target server
        ret = ansible.mkdir_module(host, ansible.get_dir() + '/' + chain_id)
        if not ret:
            return ret
        
        # push common package
        ret = ansible.copy_module(host, dir + '/common/', ansible.get_dir() + '/' + chain_id)
        if not ret:
            return ret
    
    if force:
        logger.debug(' force is set, push all package, chain_id is %s, chain_version is %s, host is %s',chain_id, version, host)
        # push host dir
        ret = ansible.copy_module(host, dir + '/' + host + '/', ansible.get_dir() + '/' + chain_id)
        if not ret:
            return ret
    else:
        # push node${index} dir in host dir not published
        hnd = HostNodeDirs(chain_id, version, host)
        for node_dir in hnd.get_node_dirs():
            if meta.host_node_exist(host, node_dir):
                logger.info(' %s already published, skip', node_dir)
                continue
            logger.info(' publish nodedir, chain_id is %s, chain_version is %s, node is %s', chain_id, version, node_dir)
            # push host dir
            ret = ansible.copy_module(host, dir + '/' + host + '/' + node_dir, ansible.get_dir() + '/' + chain_id)
            if not ret:
                return ret

    
    logger.info('push package success, dir is %s, host is %s, chain_id is %s, chain_version is %s', dir, host, chain_id, version)
    
    return True  
Beispiel #2
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 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)
Beispiel #4
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)
Beispiel #5
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)
Beispiel #6
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)