예제 #1
0
def install_node_exporter(state, host):
    if not host.data.node_exporter_version:
        raise DeployError(
            'No node_exporter_version set for this host, refusing to install node_exporter!',
        )

    server.user(
        state,
        host,
        {'Create the node_exporter user (Called prometheus by default)'},
        '{{ host.data.node_exporter_user }}',
        shell='/sbin/nologin',
    )

    files.directory(
        state,
        host,
        {'Ensure the node_exporter install directory exists'},
        '{{ host.data.node_exporter_install_dir }}',
        user=host.data.node_exporter_user,
        group=host.data.node_exporter_user,
    )

    # Work out the filename
    host.data.node_exporter_version_name = (
        'node_exporter-{0}.linux-'
        'amd64' if host.fact.arch == 'x86_64' else host.fact.arch).format(
            host.data.node_exporter_version)

    host.data.node_exporter_temp_filename = state.get_temp_filename(
        'node_exporter-{0}'.format(host.data.node_exporter_version), )

    download_node_exporter = files.download(
        state,
        host,
        {'Download node_exporter'},
        ('{{ host.data.node_exporter_download_base_url }}/'
         'v{{ host.data.node_exporter_version }}/'
         '{{ host.data.node_exporter_version_name }}.tar.gz'),
        '{{ host.data.node_exporter_temp_filename }}',
    )

    # If we downloaded node_exporter, extract it!
    if download_node_exporter.changed:
        server.shell(
            state,
            host,
            {'Extract node_exporter'},
            'tar -xzf {{ host.data.node_exporter_temp_filename }}'
            ' -C {{ host.data.node_exporter_install_dir }}',
        )

    files.link(
        state,
        host,
        {'Symlink node_exporter to /usr/bin'},
        '{{ host.data.node_exporter_bin_dir }}/node_exporter',  # link
        '{{ host.data.node_exporter_install_dir }}/'
        '{{ host.data.node_exporter_version_name }}/node_exporter',
    )
예제 #2
0
    def test_no_invalid_op_call(self):
        inventory = make_inventory()
        state = State(inventory, Config())
        connect_all(state)
        pseudo_state.set(state)

        state.in_op = True
        with self.assertRaises(PyinfraError):
            server.user('someuser')

        state.in_op = False
        state.in_deploy = True
        with self.assertRaises(PyinfraError):
            server.user('someuser')
예제 #3
0
    def test_op_line_numbers(self):
        inventory = make_inventory()
        state = State(inventory, Config())
        connect_all(state)

        # Add op to both hosts
        add_op(state, server.shell, 'echo "hi"')

        # Add op to just the second host - using the pseudo modules such that
        # it replicates a deploy file.
        pseudo_state.set(state)
        pseudo_host.set(inventory['anotherhost'])
        first_pseudo_hash = server.user('anotherhost_user').hash
        first_pseudo_call_line = getframeinfo(currentframe()).lineno - 1

        # Add op to just the first host - using the pseudo modules such that
        # it replicates a deploy file.
        pseudo_state.set(state)
        pseudo_host.set(inventory['somehost'])
        second_pseudo_hash = server.user('somehost_user').hash
        second_pseudo_call_line = getframeinfo(currentframe()).lineno - 1

        pseudo_state.reset()
        pseudo_host.reset()

        # Ensure there are two ops
        op_order = state.get_op_order()
        self.assertEqual(len(op_order), 3)

        # And that the two ops above were called in the expected order
        self.assertEqual(op_order[1], first_pseudo_hash)
        self.assertEqual(op_order[2], second_pseudo_hash)

        # And that they have the expected line numbers
        self.assertEqual(
            state.op_line_numbers_to_hash.get((0, first_pseudo_call_line)),
            first_pseudo_hash,
        )
        self.assertEqual(
            state.op_line_numbers_to_hash.get((0, second_pseudo_call_line)),
            second_pseudo_hash,
        )

        # Ensure somehost has two ops and anotherhost only has the one
        self.assertEqual(len(state.ops[inventory.get_host('somehost')]), 2)
        self.assertEqual(len(state.ops[inventory.get_host('anotherhost')]), 2)
예제 #4
0
def install_prometheus(state, host):
    if not host.data.prometheus_version:
        raise DeployError(
            'No prometheus_version set for this host, refusing to install prometheus!',
        )

    server.user(
        state,
        host,
        {'Create the prometheus user'},
        '{{ host.data.prometheus_user }}',
        shell='/sbin/nologin',
    )

    files.directory(
        state,
        host,
        {'Ensure the prometheus data directory exists'},
        '{{ host.data.prometheus_data_dir }}',
        user=host.data.prometheus_user,
        group=host.data.prometheus_user,
    )

    files.directory(
        state,
        host,
        {'Ensure the prometheus install directory exists'},
        '{{ host.data.prometheus_install_dir }}',
        user=host.data.prometheus_user,
        group=host.data.prometheus_user,
    )

    # Work out the filename
    host.data.prometheus_version_name = ('prometheus-{0}.linux-'
                                         'amd64' if host.fact.arch == 'x86_64'
                                         else host.fact.arch).format(
                                             host.data.prometheus_version)

    host.data.prometheus_temp_filename = state.get_temp_filename(
        'prometheus-{0}'.format(host.data.prometheus_version), )

    download_prometheus = files.download(
        state,
        host,
        {'Download prometheus'},
        ('{{ host.data.prometheus_download_base_url }}/'
         'v{{ host.data.prometheus_version }}/'
         '{{ host.data.prometheus_version_name }}.tar.gz'),
        '{{ host.data.prometheus_temp_filename }}',
    )

    # If we downloaded prometheus, extract it!
    server.shell(
        state,
        host,
        {'Extract prometheus'},
        'tar -xzf {{ host.data.prometheus_temp_filename }} -C {{ host.data.prometheus_install_dir }}',
        when=download_prometheus.changed,
    )

    files.link(
        state,
        host,
        {'Symlink prometheus to /usr/bin'},
        '{{ host.data.prometheus_bin_dir }}/prometheus',  # link
        '{{ host.data.prometheus_install_dir }}/{{ host.data.prometheus_version_name }}/prometheus',
    )
예제 #5
0
    print('Success on {0} for OP: {1}!'.format(host.name, op_hash))


def on_pyinfra_error(state, host, op_hash):
    print('Error on {0} for OP: {1}!'.format(host.name, op_hash))


# Ensure the state of a user
server.user(
    'pyinfra',
    shell='/bin/sh',
    ensure_home=True,

    # Options available for all operations
    sudo=True,
    sudo_user='******',
    ignore_errors=False,
    serial=False,
    run_once=False,
    get_pty=False,
    timeout=30,  # only applies to commands on the remote host (not SFTP, local Python)
    on_success=on_pyinfra_success,
)

# And groups
server.group(
    {'Ensure pyinfra2 group exists'},  # use a set as the first arg to set the op name
    'pyinfra2',
    sudo=True,
    run_once=True,  # run only on one host
)
예제 #6
0
from pyinfra import host
from pyinfra.modules import server

SUDO = True

if host.name == '@vagrant/two':

    key_file = open('/tmp/one_vagrant_id_rsa.pub', 'r')
    key = key_file.read().strip()
    server.user(
        {'Add the vagrant public key from one on to two'},
        'vagrant',
        public_keys=[key],
    )
예제 #7
0
def install_etcd(state, host):
    if not host.data.etcd_version:
        raise DeployError(
            'No etcd_version set for this host, refusing to install etcd!', )

    server.user(
        state,
        host,
        {'Create the etcd user'},
        'etcd',
        shell='/sbin/nologin',
    )

    files.directory(
        state,
        host,
        {'Ensure the etcd data directory exists'},
        '{{ host.data.etcd_data_dir }}',
        user=host.data.etcd_user,
        group=host.data.etcd_user,
    )

    files.directory(
        state,
        host,
        {'Ensure the etcd install directory exists'},
        host.data.etcd_install_dir,
        user=host.data.etcd_user,
        group=host.data.etcd_user,
    )

    # Work out the filename
    host.data.etcd_version_name = ('etcd-{0}-linux-'
                                   'amd64' if host.fact.arch == 'x86_64' else
                                   host.fact.arch).format(
                                       host.data.etcd_version)

    host.data.etcd_temp_filename = state.get_temp_filename(
        'etcd-{0}'.format(host.data.etcd_version), )

    download_etcd = files.download(
        state,
        host,
        {'Download etcd'},
        ('{{ host.data.etcd_download_base_url }}/'
         '{{ host.data.etcd_version }}/'
         '{{ host.data.etcd_version_name }}.tar.gz'),
        '{{ host.data.etcd_temp_filename }}',
    )

    # If we downloaded etcd, extract it!
    server.shell(
        state,
        host,
        {'Extract etcd'},
        'tar -xzf {{ host.data.etcd_temp_filename }} -C {{ host.data.etcd_install_dir }}',
        when=download_etcd.changed,
    )

    files.link(
        state,
        host,
        {'Symlink etcd to /usr/bin'},
        '{{ host.data.etcd_bin_dir }}/etcd',  # link
        '{{ host.data.etcd_install_dir }}/{{ host.data.etcd_version_name }}/etcd',
    )

    files.link(
        state,
        host,
        {'Symlink etcdctl to {0}'.format(host.data.etcd_bin_dir)},
        '{{ host.data.etcd_bin_dir }}/etcdctl',
        '{{ host.data.etcd_install_dir }}/{{ host.data.etcd_version_name }}/etcdctl',
    )
예제 #8
0
# pyinfra Performance
# File: deploy/deploy.py
# Desc: the pyinfra deploy!

from pyinfra.modules import files, server

CONNECT_TIMEOUT = 1
FAIL_PERCENT = 0

server.user(
    {'Add pyinfra user'},
    'pyinfra',
    home='/home/pyinfra',
    shell='/bin/bash',
)

files.file(
    {'Add log file'},
    '/var/log/pyinfra.log',
    user='******',
    mode=777,
)

files.put(
    {'Copy a file'},
    '../files/test_file.txt',
    '/home/pyinfra/test_file.txt',
    user='******',
)

server.shell(
예제 #9
0
files.sync(
    {'Sync the files directory'},
    'files',
    '/home/vagrant/example_files',
    delete=True,
)

# Executing with sudo
#

# Ensure the state of a user
server.user(
    {'Ensure pyinfra user exists'},
    'pyinfra',
    shell='/bin/sh',

    # Global arguments available in all operations, for the full list see:
    # https://pyinfra.readthedocs.io/page/deploys.html#global-arguments
    sudo=True,
)

# And groups
server.group(
    {'Ensure pyinfra2 group exists'
     },  # use a set as the first arg to set the op name
    'pyinfra2',
    sudo=True,
)

# Ensure the state of files
files.file(
예제 #10
0

# Hooks inside deploy file
@hook.before_connect
def before_connect(data, state):
    print('inventory hosts!: ', [host.name for host in inventory])


# Ensure the state of a user
server.user(
    'pyinfra',
    home='/home/pyinfra',
    shell='/bin/sh',

    # Options available for all operations
    sudo=True,
    sudo_user='******',
    ignore_errors=False,
    serial=False,
    run_once=False,
    timeout=
    30  # only applies to commands on the remote host (not SFTP, local Python)
)

# And groups
server.group(
    {'Ensure pyinfra2 group exists'
     },  # use a set as the first arg to set the op name
    'pyinfra2',
    sudo=True,
    run_once=True  # run only on one host
)
예제 #11
0
    server.sysctl(
        {'Change the fs.file-max value'},
        'fs.file-max',
        '100000',
        persist=True,
    )

    if host.fact.linux_name in ['CentOS', 'RedHat']:
        server.modprobe(
            {'Silly example for modprobe'},
            'floppy',
        )

server.user(
    {'Ensure user is removed'},
    'kevin',
    present=False,
)

# multiple users
for user in ['kevin', 'bob']:
    server.user(
        {'Ensure user {} is removed'.format(user)},
        user,
        present=False,
    )

server.group(
    {'Create uberadmin group'},
    'uberadmin',
)
예제 #12
0
import os
from subprocess import check_call

from pyinfra import host
from pyinfra.modules import apt, files, init, postgresql, server

os.chdir(os.path.dirname('./' + __file__))
cwd = os.path.abspath('.')
project_root = os.path.abspath('../..')

server.user(
    'hockeypuck',
    home='/var/lib/hockeypuck',
    system=True,
    sudo=True,
)

create_install_dir = files.directory(
    '/var/lib/hockeypuck',
    user='******',
    group='hockeypuck',
    sudo=True,
)

files.directory(
    '/etc/hockeypuck',
    user='******',
    group='root',
    sudo=True,
)
예제 #13
0
from pyinfra import host
from pyinfra.modules import files, init, server

SUDO = True

if host.fact.linux_name in ['Ubuntu']:

    server.user(
        {'Ensure myweb user exists'},
        'myweb',
        shell='/bin/bash',
    )

    files.directory(
        {'Ensure /web exists'},
        '/web',
        user='******',
        group='myweb',
    )

    files.template(
        {'Create script to run inside the service'},
        'templates/myweb.sh.j2',
        '/usr/local/bin/myweb.sh',
        mode='755',
        user='******',
        group='myweb',
    )

    files.template(
        {'Create service file'},