Ejemplo n.º 1
0
def remove_hosts(group_name,exclude=[]):
    """
    Args:
    group_name is a string representing the name of a group of hosts.
    exclude is a list of string representing host keys.
    Removes, from Logentries, hosts that belong to group with name 'group_name' except for the one whose key belong to 'exclude'.
    """
    global _GROUP_HOST_LIST
    log_client = logclient.Client(constants.get_account_key())
    if log_client is None or constants.get_account_key() is None:
        logger.error('Could not retrieve account information. account_key=%s','%s-xxxx-xxxx-xxxx-xxxxxxxxxxxx'%constants.get_account_key().split('-')[0])
        return
    hosts = log_client.get_hosts()
    for host in hosts:
        if host.get_key() not in _GROUP_HOST_LIST and host.get_location() == group_name:
            logger.debug('Removing Logentries host. host=%s', host)
            log_client.remove_host(host)
    return
Ejemplo n.º 2
0
def set_instance_host_keys():
    """
    Args:
    Collects host keys associated to instance in fabric host_list.
    """
    instance_id, log_filter = utils.get_log_filter(env.host)
    host_name = '%s_%s'%(constants.get_group_name(), instance_id)
    log_client = logclient.Client(constants.get_account_key())

    global _GROUP_HOST_LIST
    log_conf = get_instance_log_conf(instance_id)
    if log_conf is None:
        return
    conf_host = log_conf.get_host()
    logger.debug('Checking if host should be kept. host=%s', conf_host)
    if conf_host is not None:
        logger.info('Host found in ssh configuration. host=%s', conf_host.to_json())
        _GROUP_HOST_LIST.append(conf_host.get_key())
Ejemplo n.º 3
0
def deprovision():
    """
    Deprovisions the instance by removing the logentries rsyslog config file from it, restarting rsyslog and removing the corresponding host from the logentries system.
    """
    instance_id, log_filter = utils.get_log_filter(env.host)
    host_name = '%s_%s'%(constants.get_group_name(), instance_id)

    log_conf_file = get_instance_log_conf_file(instance_id)
    if log_conf_file is None:
        logger.debug('Cannot deprovision instance as it has not been provisioned. hostname=%s', host_name)
        return
    log_conf = load_conf_file(log_conf_file, instance_id)

    if log_conf is None:
        logger.info('No existing logentries rsyslog configuration file was found on instance %s',instance_id)
        return

    if remove_log_conf(instance_id):
        restart_rsyslog(instance_id)

    conf_host = log_conf.get_host()
    if conf_host is None:
        logger.error('Error. This instance configuration is missing the corresponding model!! instance_id=%s',instance_id)
        return
    
    if conf_host.get_key() is None:
        logger.error('Host has a logentries-rsyslog config file but no account key, host=%s!!',host.to_json())
    else:
        log_client = logclient.Client(constants.get_account_key())
        logentries_host = get_logentries_host(log_client,conf_host)

        # If there is no matching host, then it is assumed that it was deleted from Logentries and that no configuration should be associated to this instance.
        if logentries_host is not None:
                succeeded = log_client.remove_host(logentries_host)
                if succeeded:
                    logger.warning('Host removed from Logentries. host=%s', logentries_host.to_json())
                else:
                    logger.error('Could not remove host from Logentries. host=%s', logentries_host.to_json())
        else:
            logger.error('Could not remove host from Logentries. hostname=%s', host_name)
    return