A Pythonic toolkit to manage infrastructure.
Project description
Opslib
Opslib is a Python infrastructure-as-code framework which offers powerful abstractions to make deployment straightforward and fun.
Installing
$ pip install -U pyopslib
A Simple Example
# stack.py
from opslib import SshHost, Stack
from opslib.ansible import AnsibleAction
from opslib.terraform import TerraformProvider
stack = Stack(__name__)
stack.hetzner = TerraformProvider(
name="hcloud",
source="hetznercloud/hcloud",
version="~> 1.38.2",
)
stack.server = stack.hetzner.resource(
type="hcloud_server",
args=dict(
name="opslib-example",
server_type="cx11",
image="debian-11",
location="hel1",
ssh_keys=["my-key"],
),
output=["ipv4_address"],
)
stack.vm = SshHost(
hostname=stack.server.output["ipv4_address"],
username="root",
)
stack.install_docker = AnsibleAction(
host=stack.vm,
module="ansible.builtin.shell",
args=dict(
cmd="curl -s https://get.docker.com | bash",
creates="/opt/bin/docker",
),
)
Deploy the stack. It's called -
because it's the top level Stack object:
export HCLOUD_TOKEN="..." # https://docs.hetzner.com/cloud/api/getting-started/generating-api-token
opslib - deploy
Now we can interact with the VPS host, which we named vm
above:
opslib vm run 'set -x; whoami; uptime'
opslib vm run docker run --rm hello-world
Finally, we tear down the stack:
opslib - destroy
Features
Opslib does its best to enable readable code so you can reason about your stack. This means making use of descriptors, typing and other Python language features where it makes sense.
- Defining components
- Define the stack in terms of nested components with typed props.
- Extensible cli: any component in the stack can be referenced directly and may define its own commands.
- Batteries included
- Terraform: invoke any provider from the Terraform Registry.
- Ansible: invoke any module from Ansible Collections.
- Places: native modeling of local/remote hosts, files, directories and shell commands.
- A handful of specific integrations (e.g. systemd, restic). I plan to spin them off into a library of reusable components.
- Operations model
- Most actions boil down to invoking subprocess commands or HTTP requests. All actions return Results, which are understood by the reporting layer.
- Many components, after being successfully deployed, are skipped on subsequent deployments, unless their props change, or are manually refreshed to account for remote state changes.
- Commands may track deployment of other components (e.g. configuration files) and only get re-run when needed.
- Lazy variables, that wrap values which are available after another component is deployed, get evaluated when they are needed.
Links
- Documentation: https://pyopslib.readthedocs.io
- Examples: https://github.com/mgax/opslib/tree/main/examples
- Discord: https://discord.gg/TbaFKfusS6
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.2.tar.gz
(179.5 kB
view details)
Built Distribution
pyopslib-0.2-py3-none-any.whl
(29.6 kB
view details)
File details
Details for the file pyopslib-0.2.tar.gz
.
File metadata
- Download URL: pyopslib-0.2.tar.gz
- Upload date:
- Size: 179.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: python-requests/2.31.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 826f1bc5afbcf3346a5256c14186be81090f559e651082ea135f509c9d0dbf8b |
|
MD5 | 679c4bbf00dc0d4281b1d9b85cbfc889 |
|
BLAKE2b-256 | 86612638f0c503ec53f9c7157e9b7b0ab819be59a1924aef4a0681a52f74171d |
File details
Details for the file pyopslib-0.2-py3-none-any.whl
.
File metadata
- Download URL: pyopslib-0.2-py3-none-any.whl
- Upload date:
- Size: 29.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: python-requests/2.31.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ebe8e600a2083664daa26a0eccddcb8c92fe9aa9a6fa5f82fd4a57e37f845427 |
|
MD5 | 8a6485aaa9200e3a569e83e4c949dcc0 |
|
BLAKE2b-256 | ca912da2b2b9e03fea200802a511534e4a581c3a0d1fc072b17cf2a65d4c1647 |