Skip to content

About

Python SDK for operations.do — machines, DNS and GitLab through one scoped key, with no cloud credential in your app

Topics

Resources

Contributing

Security policy

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

operations.do

operationsdo-sdk-python

operations.do · Machines, DNS and GitLab CI from Python — with no cloud or GitLab token in your app.

version: 0.6.2 python: 3.10+ license: MIT operations.do: SDK

The Python SDK for operations.do: create and destroy machines, manage DNS, and drive GitLab CI without your application ever holding a cloud or GitLab token. Your app holds one operations.do API key, scoped to exactly what it needs (one client's DNS zone, the droplets scope, one GitLab project); operations.do holds the provider credentials and does the call.

Needs an operations.do account. Every call goes to the operations.do API — the hosted one at https://api.operations.do or a self-run instance. Nothing runs locally on its own.

from operationsdo_sdk import OperationsdoApiClient

operationsdo = OperationsdoApiClient.from_env()   # OPERATIONS_API_URL / OPERATIONS_API_KEY / OPERATIONS_CLIENT_ID
# or OperationsdoApiClient.cloud(api_key)  /  OperationsdoApiClient.local(api_key)

for record in operationsdo.dns.records(type="TXT"):
    print(record["fqdn"], record["data"])

machine = operationsdo.machines.create("worker-1", size="s-1vcpu-1gb", region="fra1", tags=["batch"])
machine = operationsdo.machines.wait_until_active(machine["id"])
operationsdo.machines.destroy_verified(machine["id"])

Versioning: 0.x — the API may still change between minor versions; every change is in the CHANGELOG. OperationsClient and the operations_sdk import name are kept as aliases until 1.0.

Why this exists

A DigitalOcean (or AWS, or GCP) token that can create a machine can usually also read DNS, managed databases and object-store credentials. An application holding one turns a single compromised container into a compromised account.

So applications hold an operations.do API key scoped to droplets instead, and ask operations.do for a machine. Which provider answers is operations.do's business. When that changes, this package and every caller stay exactly as they are — which is the other half of the point.

Install

pip install "operationsdo-sdk-python @ git+https://github.com/pamfilico/operationsdo-sdk-python.git@v0.6.2"

Poetry:

[tool.poetry.dependencies]
operationsdo-sdk-python = { git = "https://github.com/pamfilico/operationsdo-sdk-python.git", tag = "v0.6.2" }

Import it as operationsdo_sdk.

Configuration

Reads the same names the operationsdo CLI reads from .operationsdorc, so a host that already runs the CLI needs no second set of variables:

variable what
OPERATIONS_API_URL e.g. https://operations.do
OPERATIONS_API_KEY an operations.do key with the droplets scope
OPERATIONS_CLIENT_ID optional; carried for logging and for callers that record it

OperationsdoApiClient.from_env() raises naming exactly what is missing, rather than returning a half-configured client that 401s deep inside a provisioning sequence.

Surface

operationsdo.dns

One client's DNS, with no DigitalOcean token in the app. The key is minted with a grant naming the zone (or name) it may touch, and sees nothing else: not the rest of the account, not a suite sibling's domains.

scope on the key reaches
client:<slug>:dns:<zone-or-name>:read records at or under that name, for that client
client:<slug>:dns:<zone-or-name>:write the same, plus create / update / delete
operationsdo = OperationsdoApiClient(os.environ["OPERATIONS_API_URL"], os.environ["OPERATIONS_DNS_API_KEY"],
                       client_id="example-app")
operationsdo.dns.zones()                              # [{"zone": "example.com", "names": ["example.com"], "access": "write"}]
operationsdo.dns.records(type="TXT")                  # each with id, fqdn, data, ttl, writable
operationsdo.dns.upsert("TXT", "_verify", "abc123")   # create, update, or leave alone
operationsdo.dns.delete(record_id)
method notes
zones() the zones this key may look into
records(domain=, type=, name=) all granted zones when domain is omitted
find(type, name) records of one type at one name
create(type, name, data, ttl=300, priority=…) names relative (api, @) or absolute (api.example.com)
update(id, data=…, ttl=…) / delete(id) a record outside the grant answers OperationsNotFound
upsert(type, name, data) idempotent; refuses when several records share the name

SOA records and the zone's own NS records are never writable, and a name in another zone (x.example.org sent to example.com) is refused rather than created as x.example.org.example.com.

operationsdo.machines

method notes
list(tag=None) tag narrows server-side — how a fleet finds its OWN machines and not the production boxes
get(id) / exists(id) exists is the honest way to confirm a destroy
sizes(region=None) cheapest first, with hourly_usd — use this rather than a hard-coded price table
create(name, size=…, region=…, image=…, key_ids=…, tags=…, user_data=…) returns immediately; the machine is not reachable yet
destroy(id) response carries verified_gone — check it
destroy_verified(id) destroys and confirms absence, retrying the confirmation
act(id, action, **extras) reboot, power_*, shutdown, rebuild(image=), resize(size=), snapshot(name=), rename(name=)
action_status(id, action_id) in-progress | completed | errored
wait_until_active(id) waits for status and an IP
wait_for_action(id, action_id) raises on errored

operationsdo.keys

list(), create(name, public_key), delete(id), find_by_name(name).

A fleet that mints a keypair per machine — so a lost box's key is worthless — must delete, or the account collects one dead key per machine forever.

operationsdo.gitlab — CI without a GitLab token

operations.do holds the GitLab token (a connector) and binds each client to a connector + project. With only the client's operations.do key:

operationsdo = OperationsdoApiClient.from_env()               # OPERATIONS_CLIENT_ID names the client

operationsdo.gitlab.variables.set("DEPLOY_TAG", "v1.2.3", masked=True)        # create or update
operationsdo.gitlab.variables.bulk_set({"A": "1", "B": "2"})
run = operationsdo.gitlab.pipelines.trigger(variables={"DEPLOY": "1"})        # default branch
run = operationsdo.gitlab.pipelines.wait(run["id"])
if run["status"] != "success":
    failed = [j for j in run["jobs"] if j["status"] == "failed"][0]
    print(operationsdo.gitlab.pipelines.trace(failed["id"], tail=50)["trace"])

operationsdo.gitlab.branches.create("fix/x")
mr = operationsdo.gitlab.merge_requests.create("fix/x", "Fix x", draft=True)
operationsdo.gitlab.merge_requests.close(mr["iid"])
namespace methods key needs
connectors list get create update delete verify projects gitlab:read/gitlab:write
bindings list get(live=) bind unbind read: client scope; bind/unbind: gitlab:write
variables list(reveal=) get create update set bulk_set delete client:<slug>:read; values and writes need :write
pipelines list latest get jobs trigger retry cancel trace retry_job play_job wait client:<slug>:read/:write
merge_requests list get create update close reopen merge client:<slug>:read/:write
branches list create delete client:<slug>:read/:write
deploy_tokens mint(purpose, scopes, days=) list revoke client:<slug>:write — for a machine that must clone or pull
pages site unpublish project set_access domains domain add_domain verify_domain remove_domain api client:<slug>:read/:write — GitLab's own JSON, through the proxy

A Pages custom domain is useless until verified — while it is not, a members-only site's sign-in fails on it (401 after signing in). Create the CNAME and the TXT (_gitlab-pages-verification-code.<domain> = gitlab-pages-verification-code=<code>), then operationsdo.gitlab.pages.verify_domain(domain).

operationsdo.api_keys — scopes of an existing key (owner only)

owner = OperationsdoApiClient.cloud(session_token)                   # the dashboard session, not a key
owner.api_keys.update_scopes("ci:example", add=["client:example:dns:www.example.com:write"])

local = OperationsdoApiClient.local("local-operator", auth_header="X-Ops-Internal-Secret")
local.api_keys.update_scopes_local("owner@example.com", "ops_1a2b3c4d", remove=["…"])

An API key can never change scopes (it must not widen itself). key is the name, the prefix or the id. list, create and revoke take the session too.

Every client-addressed method takes client= (id or slug) and defaults to client_id.

Three things that will bite you otherwise

1. Every action is asynchronous. create returns a machine id and the box is still booting; resize returns in-progress and the machine has not changed yet. Use the wait_* helpers. The most common bug in code like this is treating the POST as completion.

2. OperationsTimeout does not mean failure. It means "not finished yet, and I stopped watching". A caller that treats it as failure and provisions a replacement is paying for two machines.

3. An underscore in a name is a 422. Hostnames allow letters, digits, dots and hyphens. safe_name() exists because a name built from something like businessportal.gr/gr/software_wide looks fine and is rejected.

Errors

type when retry?
OperationsAuthError 401/403 — key wrong, inactive, or missing the scope never
OperationsNotFound 404 no
OperationsProviderError 502 — the provider refused or wobbled yes, and the SDK already does
OperationsTimeout a wait_* gave up see #2 above
OperationsError everything else depends

429/502/503/504 and connection failures are retried internally with backoff. 4xx never is.

Tests

python -m venv .venv && .venv/bin/pip install -e ".[dev]"
.venv/bin/python -m pytest tests/ -q

29 tests, all against a fake transport — no network, no operations.do, no cloud credential. Anything needing a real machine is an integration concern and deliberately not in a suite that must pass on a laptop with no credentials.

About

Python SDK for operations.do — machines, DNS and GitLab through one scoped key, with no cloud credential in your app

Topics

Resources

Contributing

Security policy

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages