How to use the pyinfra.api.operation 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 / modules / postgresql.py View on Github external
@operation
def dump(
    state, host,
    remote_filename, database=None,
    # Details for speaking to PostgreSQL via `psql` CLI
    postgresql_user=None, postgresql_password=None,
    postgresql_host=None, postgresql_port=None,
):
    '''
    Dump a PostgreSQL database into a ``.sql`` file. Requires ``mysqldump``.

    + database: name of the database to dump
    + remote_filename: name of the file to dump the SQL to
    + postgresql_*: global module arguments, see above
    '''

    yield '{0} > {1}'.format(make_psql_command(
github Fizzadar / pyinfra / pyinfra / modules / files.py View on Github external
@operation(pipeline_facts={
    'file': 'destination',
})
def download(
    state, host, source_url, destination,
    user=None, group=None, mode=None, cache_time=None, force=False,
):
    '''
    Download files from remote locations.

    + source_url: source URl of the file
    + destination: where to save the file
    + user: user to own the files
    + group: group to own the files
    + mode: permissions of the files
    + cache_time: if the file exists already, re-download after this time (in s)
    + force: always download the file, even if it already exists
github Fizzadar / pyinfra / pyinfra / modules / linux.py View on Github external
@operation
def file(name, present=True, user=None, group=None, permissions=None, touch=False):
    '''Manage the state of files.'''
    info = server.file(name)
    commands = []

    # It's a directory?!
    if info is False:
        raise CommandError('{} is a directory'.format(name))

    # Doesn't exist & we want it
    if info is None and present:
        commands.append('touch {}'.format(name))
        if permissions:
            commands.append(_chmod(name, permissions))
        if user or group:
            commands.append(_chown(name, user, group))
github Fizzadar / pyinfra / pyinfra / modules / mysql.py View on Github external
@operation
def privileges(
    state, host,
    user, privileges,
    user_hostname='localhost',
    database='*', table='*',
    present=True,
    flush=True,
    # Details for speaking to MySQL via `mysql` CLI
    mysql_user=None, mysql_password=None,
    mysql_host=None, mysql_port=None,
):
    '''
    Add/remove MySQL privileges for a user, either global, database or table specific.

    + user: name of the user to manage privileges for
    + privileges: list of privileges the user should have
github Fizzadar / pyinfra / pyinfra / modules / files.py View on Github external
@operation
def line(state, host, name, line, present=True, replace=None, flags=None):
    '''
    Ensure lines in files using grep to locate and sed to replace.

    + name: target remote file to edit
    + line: string or regex matching the target line
    + present: whether the line should be in the file
    + replace: text to replace entire matching lines when ``present=True``
    + flags: list of flags to pass to sed when replacing/deleting

    Regex line matching:
        Unless line matches a line (starts with ^, ends $), pyinfra will wrap it such that
        it does, like: ``^.*LINE.*$``. This means we don't swap parts of lines out. To
        change bits of lines, see ``files.replace``.

    Regex line escaping:
github Fizzadar / pyinfra / pyinfra / modules / postgresql.py View on Github external
@operation
def role(
    state, host, name,
    present=True,
    password=None, login=True, superuser=False, inherit=False,
    createdb=False, createrole=False, replication=False, connection_limit=None,
    # Details for speaking to PostgreSQL via `psql` CLI
    postgresql_user=None, postgresql_password=None,
    postgresql_host=None, postgresql_port=None,
):
    '''
    Add/remove PostgreSQL roles.

    + name: name of the role
    + present: whether the role should be present or absent
    + password: the password for the role
    + login: whether the role can login
github Fizzadar / pyinfra / pyinfra / modules / vzctl.py View on Github external
@operation
def stop(state, host, ctid):
    '''
    Stop OpenVZ containers.

    + ctid: CTID of the container to stop
    '''

    args = ['{0}'.format(ctid)]

    yield 'vzctl stop {0}'.format(' '.join(args))
github Fizzadar / pyinfra / pyinfra / modules / server.py View on Github external
@operation
def crontab(
    state, host, command, present=True, user=None, name=None,
    minute='*', hour='*', month='*', day_of_week='*', day_of_month='*',
):
    '''
    Add/remove/update crontab entries.

    + command: the command for the cron
    + present: whether this cron command should exist
    + user: the user whose crontab to manage
    + name: name the cronjob so future changes to the command will overwrite
    + minute: which minutes to execute the cron
    + hour: which hours to execute the cron
    + month: which months to execute the cron
    + day_of_week: which day of the week to execute the cron
    + day_of_month: which day of the month to execute the cron
github Fizzadar / pyinfra / pyinfra / modules / linux.py View on Github external
@operation
def user(name, present=True, home=None, shell=None, public_keys=None, delete_keys=False):
    '''Manage Linux users & their ssh `authorized_keys`.'''
    commands = []
    is_present = name in server.fact('Users')

    def do_keys():
        if delete_keys:
            commands.append('rm -f {0}/.ssh/authorized_keys*'.format(home))

        if not public_keys: return

        # Ensure .ssh directory & authorized_keys file
        commands.extend(
            directory.inline(name='{}/.ssh'.format(home), present=True)
        )
        commands.extend(
github Fizzadar / pyinfra / pyinfra / modules / vzctl.py View on Github external
@operation
def set(state, host, ctid, save=True, **settings):
    '''
    Set OpenVZ container details.

    + ctid: CTID of the container to set
    + save: whether to save the changes
    + settings: settings/arguments to apply to the container

    Settings/arguments:
        these are mapped directly to ``vztctl`` arguments, eg
        ``hostname='my-host.net'`` becomes ``--hostname my-host.net``.
    '''

    args = ['{0}'.format(ctid)]

    if save: