Skip to main content

A Pythonic toolkit to manage infrastructure.

Project description

Opslib

Tired of describing your infrastructure with declarative configuration files that make any non-trivial logic awkward? Opslib is a Pythonic toolkit to manage infrastructure, inspired by AWS CDK.

Opslib is tiny but it stands on the shoulders of giants. You can use any Terraform Provider, Ansible Module, or directly execute shell commands.

Example

The code below creates a VPS using the Hetzner Cloud Terraform provider and installs Docker using the Ansible shell module. It also defines a custom ssh command to log into the server.

# stack.py
import click
from opslib.components import Component, Stack
from opslib.places import SshHost
from opslib.props import Prop
from opslib.terraform import TerraformProvider

class VPS(Component):
    class Props:
        hetzner = Prop(TerraformProvider)
        name = Prop(str)

    def build(self):
        self.server = self.props.hetzner.resource(
            type="hcloud_server",
            body=dict(
                name=self.props.name,
                server_type="cx11",
                image="debian-11",
                location="hel1",
                ssh_keys=["my-key"],
            ),
            output=["ipv4_address"],
        )

        self.install_docker = self.host.ansible_action(
            module="ansible.builtin.shell",
            args=dict(
                cmd="curl -s https://get.docker.com | bash",
                creates="/opt/bin/docker",
            ),
        )

    @property
    def host(self):
        return SshHost(
            hostname=self.server.output["ipv4_address"],
            username="root",
        )

    def add_commands(self, cli):
        @cli.command(context_settings=dict(ignore_unknown_options=True))
        @click.argument("args", nargs=-1, type=click.UNPROCESSED)
        def ssh(args):
            self.host.run(*args, capture_output=False, exit=True)

class Example(Stack):
    def build(self):
        self.hetzner = TerraformProvider(
            name="hcloud",
            source="hetznercloud/hcloud",
            version="~> 1.36.2",
        )

        self.vps = VPS(
            hetzner=self.hetzner,
            name="mycodeforge",
        )

def get_stack():
    return Example()

To deploy the stack, simply run:

opslib - init
opslib - deploy

Then, check if Docker works, by running the hello-world image. opslib vps ssh invokes the custom command created in add_commands.

opslib vps ssh docker run --rm hello-world

Documentation

https://pyopslib.readthedocs.io

A good place to start is the tutorial.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pyopslib-0.0.3.tar.gz (21.1 kB view details)

Uploaded Source

Built Distribution

pyopslib-0.0.3-py3-none-any.whl (26.6 kB view details)

Uploaded Python 3

File details

Details for the file pyopslib-0.0.3.tar.gz.

File metadata

  • Download URL: pyopslib-0.0.3.tar.gz
  • Upload date:
  • Size: 21.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.4.1 CPython/3.11.3 Darwin/22.4.0

File hashes

Hashes for pyopslib-0.0.3.tar.gz
Algorithm Hash digest
SHA256 507f3530b6d68de8242aab15c3542c8b52c51bd032cc72165a43f1b05daf1b95
MD5 6c61510a0ddeee49ac4131bd47c1cc3b
BLAKE2b-256 f7605214ca8e549e67c6a0ef5dc1e542fc8aa3235667e4cc4e1ccddf37b01888

See more details on using hashes here.

File details

Details for the file pyopslib-0.0.3-py3-none-any.whl.

File metadata

  • Download URL: pyopslib-0.0.3-py3-none-any.whl
  • Upload date:
  • Size: 26.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.4.1 CPython/3.11.3 Darwin/22.4.0

File hashes

Hashes for pyopslib-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 2d43c8504af04a0236dd9b5409429996ef98db3dd4d61466b5c6dea8e3b19f31
MD5 e09db0c62dc9207c55db5e2eecb573fb
BLAKE2b-256 ca13e851dc7524e7f549e465ae5f2909e9c317bcffc83446b366b9604a6d9479

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page