How to use the pyinfra.logger function in pyinfra

To help you get started, we’ve selected a few pyinfra examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github Fizzadar / pyinfra / pyinfra / api / connectors / local.py View on Github external
logger.debug('--> Running command on localhost: {0}'.format(command))

    if print_output:
        print('{0}>>> {1}'.format(host.print_prefix, command))

    process = Popen(command, shell=True, stdout=PIPE, stderr=PIPE)

    combined_output = read_buffers_into_queue(
        host,
        process.stdout,
        process.stderr,
        timeout=timeout,
        print_output=print_output,
    )

    logger.debug('--> Waiting for exit status...')
    process.wait()
    logger.debug('--> Command exit status: {0}'.format(process.returncode))

    # Close any open file descriptors
    process.stdout.close()
    process.stderr.close()

    status = process.returncode == 0

    if return_combined_output:
        return status, combined_output

    stdout, stderr = split_combined_output(combined_output)
    return status, stdout, stderr
github Fizzadar / pyinfra / pyinfra_cli / prints.py View on Github external
def print_meta(state):
    group_combinations = _get_group_combinations(state.inventory)
    rows = []

    for i, (groups, hosts) in enumerate(six.iteritems(group_combinations), 1):
        hosts = [
            host for host in hosts
            if state.is_host_in_limit(host)
        ]

        if not hosts:
            continue  # pragma: no cover

        if groups:
            rows.append((logger.info, 'Groups: {0}'.format(
                click.style(' / '.join(groups), bold=True),
            )))
        else:
            rows.append((logger.info, 'Ungrouped:'))

        for host in hosts:
            meta = state.meta[host]

            # Didn't connect to this host?
            if host not in state.activated_hosts:
                rows.append((logger.info, [
                    host.style_print_prefix('red', bold=True),
                    click.style('No connection', 'red'),
                ]))
                continue
github Fizzadar / pyinfra / pyinfra_cli / inventory.py View on Github external
def _get_group_data(deploy_dir):
    group_data = {}
    group_data_directory = path.join(deploy_dir, 'group_data')

    if path.exists(group_data_directory):
        files = listdir(group_data_directory)

        for file in files:
            if not file.endswith('.py'):
                continue

            group_data_file = path.join(group_data_directory, file)
            group_name = path.basename(file)[:-3]

            logger.debug('Looking for group data in: {0}'.format(group_data_file))

            # Read the files locals into a dict
            attrs = exec_file(group_data_file, return_locals=True)

            group_data[group_name] = {
                key: value
                for key, value in six.iteritems(attrs)
                if _is_group_data(key, value)
            }

    return group_data
github Fizzadar / pyinfra / pyinfra / api / connectors / ssh.py View on Github external
# which did complete. So if both haven't completed, we kill them and fail
    # with a timeout.
    if len(greenlets) != 2:
        stdout_reader.kill()
        stderr_reader.kill()

        raise timeout_error()

    # Read the buffers into a list of lines
    stdout = stdout_reader.get()
    stderr = stderr_reader.get()

    logger.debug('Waiting for exit status...')
    exit_status = channel.recv_exit_status()

    logger.debug('Command exit status: {0}'.format(exit_status))
    return exit_status == 0, stdout, stderr
github Fizzadar / pyinfra / pyinfra_cli / util.py View on Github external
# Copy the inventory hosts (some might be removed during deploy)
    hosts = list(state.inventory)

    for host in hosts:
        # Don't load for anything within our (top level, --limit) limit
        if (
            isinstance(state.limit_hosts, list)
            and host not in state.limit_hosts
        ):
            continue

        pseudo_host.set(host)

        exec_file(filename)

        logger.info('{0} {1} {2}'.format(
            host.print_prefix,
            click.style('Ready:', 'green'),
            click.style(filename, bold=True),
        ))

    # Remove any pseudo host
    pseudo_host.reset()
github Fizzadar / pyinfra / pyinfra / api / connectors / docker.py View on Github external
def show_warning():
    logger.warning('The @docker connector is in Alpha!')
github Fizzadar / pyinfra / pyinfra / modules / python.py View on Github external
def execute(state, host, callback, *args, **kwargs):
    '''
    [DEPRECATED], please use ``python.call``.
    '''

    # COMPAT w/ <0.4
    # TODO: remove this function

    logger.warning((
        'Use of `python.execute` is deprecated, '
        'please use `python.call` instead.'
    ))

    # Pre pyinfra 0.4 the operation execution passed (state, host, host.name)
    # as args, so here we replicate that - hence ``python.call`` which replaces
    # this function going forward.
    args = (host.name,) + args

    yield (callback, args, kwargs)
github Fizzadar / pyinfra / pyinfra_cli / log.py View on Github external
stderr_handler = logging.StreamHandler(sys.stderr)

    # Setup filters to push different levels to different streams
    stdout_filter = LogFilter(*STDOUT_LOG_LEVELS)
    stdout_handler.addFilter(stdout_filter)

    stderr_filter = LogFilter(*STDERR_LOG_LEVELS)
    stderr_handler.addFilter(stderr_filter)

    # Setup a formatter
    formatter = LogFormatter()
    stdout_handler.setFormatter(formatter)
    stderr_handler.setFormatter(formatter)

    # Add the handlers
    logger.addHandler(stdout_handler)
    logger.addHandler(stderr_handler)