Ejemplo n.º 1
0
def uninstall(state=None, host=None):
    supported_schema_versions = [
        v1beta3.HttpData,
    ]

    validate_schema_version(host.data.http, supported_schema_versions)

    if 'apache2.service' in host.fact.systemd_status:
        systemd.service(
            name='Stop apache2',
            service='apache2',
            running=False,
            sudo=True,
            state=state,
            host=host,
        )

    files.file(
        name='Remove custom config',
        path=str(Path('/etc') / 'apache2' / 'conf-available' / 'root.conf'),
        present=False,
        sudo=True,
        state=state,
        host=host,
    )

    apt.packages(
        name='Ensure apache2 package is not present',
        packages=['apache2'],
        present=False,
        sudo=True,
        state=state,
        host=host,
    )
Ejemplo n.º 2
0
def uninstall(state=None, host=None):
    supported_schemas = [
        v1beta3.DnsmasqData
    ]

    validate_schema_version(host.data.dnsmasq, supported_schemas)

    if 'dnsmasq.service' in host.fact.systemd_status:
        systemd.service(
            name='Stop dnsmasq',
            service='dnsmasq',
            running=False,
            sudo=True,

            state=state, host=host,
        )

    files.file(
        name='Remove dnsmasq config',
        path=str(Path('/etc') / 'dnsmasq.conf'),
        present=False,
        sudo=True,

        state=state, host=host,
    )

    if host.data.dnsmasq.tftp is not None:
        files.directory(
            name=f'Remove TFTP root dir {host.data.dnsmasq.tftp.root_dir}',
            path=str(host.data.dnsmasq.tftp.root_dir),
            present=False,
            recursive=False,
            sudo=True,

            state=state, host=host,
        )

    apt.packages(
        name='Ensure dnsmasq package is not present',
        packages=['dnsmasq'],
        present=False,
        sudo=True,

        state=state, host=host,
    )
Ejemplo n.º 3
0
from pyinfra.operations import files, server

SUDO = True
FAIL_PERCENT = 0

server.user(
    name='Create the pyinfra user',
    user='******',
)

files.file(
    name='Create a file as the pyinfra user using sudo',
    path='/home/pyinfra/sudo_testfile',
    sudo_user='******',
)

files.file(
    name='Create a file as the pyinfra user using su',
    path='/home/pyinfra/su_testfile',
    su_user='******',
)
Ejemplo n.º 4
0
from pyinfra.operations import apt, brew, files, git, server, systemd

# Add/remove/add same file
files.file(path="/somefile", )

files.file(
    path="/somefile",
    present=False,
)

files.file(path="/somefile", )

# Add/remove/add same directory
files.directory(path="/somedir", )

files.directory(
    path="/somedir",
    present=False,
)

files.directory(path="/somedir", )

# Add/remove/add same link
files.link(
    path="/somelink",
    target="/elsewhere",
)

files.link(
    path="/somelink",
    present=False,
Ejemplo n.º 5
0
def install_hashicorp_products(hashicorp_products: List[HashicorpProduct],
                               state=None,
                               host=None):
    apt.packages(
        name="Ensure unzip is installed",
        packages=["unzip"],
        update=True,
        state=state,
        host=host,
    )
    for product in hashicorp_products:
        server.user(
            name=f"Create system user for {product.name}",
            user=product.name,
            system=True,
            shell="/bin/false",  # noqa: S604
            state=state,
            host=host,
        )
        if linux_family(host.fact.linux_name).lower == "debian":
            cpu_arch = host.fact.debian_cpu_arch
        elif linux_family(host.fact.linux_name).lower == "redhat":
            cpu_arch = host.fact.redhat_cpu_arch
        else:
            cpu_arch = "amd64"
        file_download = f"{product.name}_{product.version}_linux_{cpu_arch}.zip"
        file_hashes = (
            httpx.get(
                "https://releases.hashicorp.com/{product_name}/{product_version}/{product_name}_{product_version}_SHA256SUMS"
                .format(  # noqa: E501
                    product_name=product.name,
                    product_version=product.version)).read().decode(
                        "utf8").strip("\n").split("\n"))
        file_hash_map = {
            file_hash.split()[1]: file_hash.split()[0]
            for file_hash in file_hashes
        }
        download_destination = f"/tmp/{product.name}.zip"  # noqa: S108
        target_directory = product.install_directory or "/usr/local/bin/"
        download_binary = files.download(
            name=f"Download {product.name} archive",
            src=
            f"https://releases.hashicorp.com/{product.name}/{product.version}/{file_download}",  # noqa: WPS221,E501
            dest=download_destination,
            sha256sum=file_hash_map[file_download],
            state=state,
            host=host,
        )
        server.shell(
            name=f"Unzip {product.name}",
            commands=[
                f"unzip -o {download_destination} -d {target_directory}"
            ],
            state=state,
            host=host,
        )
        files.file(
            name=f"Ensure {product.name} binary is executable",
            path=Path(target_directory).joinpath(product.name),
            assume_present=download_binary.changed,
            user=product.name,
            group=product.name,
            mode="755",
            state=state,
            host=host,
        )
        files.directory(
            name=f"Ensure configuration directory for {product.name}",
            path=product.configuration_directory
            or product.configuration_file.parent,
            present=True,
            user=product.name,
            group=product.name,
            recursive=True,
            state=state,
            host=host,
        )
        if hasattr(product, "data_directory"):  # noqa: WPS421
            files.directory(
                name=f"Create data directory for {product.name}",
                path=product.data_directory,
                present=True,
                user=product.name,
                group=product.name,
                recursive=True,
                state=state,
                host=host,
            )
Ejemplo n.º 6
0
from pyinfra import config
from pyinfra.operations import files, server

config.SUDO = True
config.FAIL_PERCENT = 0

server.user(
    name="Create the pyinfra user",
    user="******",
)

files.file(
    name="Create a file as the pyinfra user using sudo",
    path="/home/pyinfra/sudo_testfile",
    sudo_user="******",
)

files.file(
    name="Create a file as the pyinfra user using su",
    path="/home/pyinfra/su_testfile",
    su_user="******",
)
Ejemplo n.º 7
0
    dest="/tmp/foo",
    foo_variable=foo_variable,
)

files.link(
    name="Create link /etc/issue2 that points to /etc/issue",
    path="/etc/issue2",
    target="/etc/issue",
)

# Note: The directory /tmp/secret will get created with the default umask.
files.file(
    name="Create /tmp/secret/file",
    path="/tmp/secret/file",
    mode="600",
    user="******",
    group="root",
    touch=True,
    create_remote_dir=True,
)

files.directory(
    name="Ensure the /tmp/dir_that_we_want_removed is removed",
    path="/tmp/dir_that_we_want_removed",
    present=False,
)

# multiple directories
dirs = ["/tmp/aaa", "/tmp/bbb"]
for dir in dirs:
    files.directory(
Ejemplo n.º 8
0
if is_server:
    config_type = "01-server"
    # Find the public IPv6 address of the machine if we are a server.
    for dev, dev_config in host.get_fact(NetworkDevices).items():
        if dev_config["ipv4"]:
            dev_v4_addr = ipaddress.IPv4Address(dev_config["ipv4"]["address"])
            if dev_v4_addr.is_global:
                v4_addr = dev_v4_addr
                break
else:
    config_type = "01-client"

# Arch specific config
files.file(
    name="Delete /etc/nomad.d/nomad.hcl",
    path="/etc/nomad.d/nomad.hcl",
    present=False,
)

files.sync(
    name="Sync Nomad template configs",
    src="common/consul_template/nomad/",
    dest="/etc/consul-template/templates/nomad/",
)

files.sync(
    name="Sync Nomad static configs",
    src="files/nomad/config/",
    dest="/etc/nomad.d/",
)
Ejemplo n.º 9
0
    '/tmp/foo',
    foo_variable=foo_variable,
)

files.link(
    {'Create link /etc/issue2 that points to /etc/issue'},
    '/etc/issue2',
    '/etc/issue',
)

# Note: The directory /tmp/secret will get created with the default umask.
files.file(
    {'Create /tmp/secret/file'},
    '/tmp/secret/file',
    mode='600',
    user='******',
    group='root',
    touch=True,
    create_remote_dir=True,
)

files.directory(
    {'Ensure the /tmp/dir_that_we_want_removed is removed'},
    '/tmp/dir_that_we_want_removed',
    present=False,
)

# multiple directories
dirs = ['/tmp/aaa', '/tmp/bbb']
for dir in dirs:
    files.directory(
Ejemplo n.º 10
0
elif host.name == 'anotherhost':
    local.include('tasks/a_task.py')

# Include the whole file again, but for all hosts
local.include('tasks/a_task.py')

# Do a loop which will generate duplicate op hashes
for i in range(2):
    server.shell(
        {'Loop-{0} main operation'.format(i)},
        'echo loop_{0}_main_operation'.format(i),
    )

files.file(
    {'Third main operation'},
    'files/a_file',
    '/a_file',
)

with state.preserve_loop_order([1, 2]) as loop_items:
    for item in loop_items():
        server.shell(
            {'Order loop {0}'.format(item)},
            'echo loop_{0}'.format(item),
        )
        server.shell(
            {'2nd Order loop {0}'.format(item)},
            'echo loop_{0}'.format(item),
        )

if host.name == 'somehost':
Ejemplo n.º 11
0
        'apt-add-repository "deb [arch=amd64] https://apt.releases.hashicorp.com $(lsb_release -cs) main"',
    )

    apt.packages(
        name="Install Consul.",
        packages=[
            "consul",
        ],
        update=True,
    )

    for cert_file in ["server.crt", "server.key", "ca.crt"]:
        files.file(
            name="Touch Consul TLS files.",
            path="/etc/consul.d/" + cert_file,
            present=True,
            user="******",
            group="consul",
            touch=True,
        )

files.sync(
    name="Sync Consul consul-template configs",
    src="files/consul_vault/",
    dest="/etc/consul-template/templates/consul/",
)

files.template(
    name="Create Consul consul-template config.",
    src="templates/consul/consul_template.j2",
    dest="/etc/consul-template/config/10-consul.hcl",
    mode="644",