Python SDK
The floodwall Python package: options, guard, submit, wait, report, errors, signing, failing closed or open.
The floodwall package asks a Floodwall Cloud plane before your agent changes production, and tells it how the change went. Standard library only, Python 3.9+; signing intents needs the optional cryptography package. The source is in sdk/python.
pip install floodwall
pip install "floodwall[signing]" # to sign intentsimport os
import subprocess
from floodwall import Floodwall
fw = Floodwall(api_key=os.environ["FLOODWALL_KEY"], plane=os.environ["FLOODWALL_PLANE"], agent="deployer")
# Runs the callable only if the plane admits the change, then reports
# "succeeded", or "failed" with the exception if it raises.
fw.guard(
{"action": {"kind": "scale", "resource": "web", "replicas": 5}, "priority": "normal"},
lambda: subprocess.run(["kubectl", "scale", "deploy/web", "--replicas=5"], check=True),
)Options#
Floodwall(api_key, plane, agent, ...) is a connection to one plane, as one agent.
| Argument | Default | What it does |
|---|---|---|
api_key | required | An agent (or operator) key, fw_live_… or fw_test_…. Left out of repr(). |
plane | required | The plane's id. |
agent | required | The agent's name, as the plane's rules and ledger know it. |
base_url | "https://api.floodwall.ai" | Must be https://, except for localhost, 127.0.0.1 and [::1]. |
when_unreachable | "closed" | "closed" refuses a change the plane can't be asked about; "open" applies it unchecked. |
on_fail_open | warnings.warn | Called with (intent, error) for each change applied unchecked; the default never includes the manifest. |
signing_key | None | The agent's Ed25519 private key: 32 seed bytes, or a cryptography Ed25519PrivateKey. |
on_report_error | warnings.warn | Called with (error, key) when reporting a successful change failed; guard still returns the result. |
timeout | 30.0 | Seconds for each HTTP request. |
opener | urllib | For tests: how a request is sent. |
Changes#
A change is a dict:
{
"action": {"kind": "apply", "resource": "api", "manifest": "registry.internal/api:v42"},
# or {"kind": "scale", "resource": "web", "replicas": 5}
# or {"kind": "destroy", "resource": "old-worker"}
"priority": "normal", # bulk | normal | urgent | pager
"blast_radius": "service", # cell | service | region | global ("blastRadius" works too)
"id": 1731600000000000, # optional: defaults to a new id above every id used
}An invalid change raises ValueError before anything is sent. See Intents.
Methods#
guard(change, apply, timeout_seconds=300)#
Submits the change; waits while it is queued or held for a person; calls apply() only if it is admitted; then reports succeeded, or failed with the exception's text if apply raises, and re-raises it. Raises NotAdmitted if the change is rejected, expires or lapses, or still waits when time runs out. Returns what apply returns.
submit(change)#
Offers a change; returns the plane's first answer as a decision dict (admit, held, rejected or queued).
wait(key, timeout_seconds=300)#
Long-polls until the intent leaves the queue and the hold queue, or time runs out, and returns its latest status. A timeout leaves the intent held. Polls are at least a second apart.
report(key, outcome, reason=None)#
Reports "succeeded" or "failed" for an admitted change, which frees its resource. Reasons are sent with whitespace collapsed, at most 1,024 characters.
prepare(change), intent(change), next_id()#
intent builds the plane's form, prepare also signs it, and next_id gives an id above every id used: the time in microseconds, or one more.
first = fw.submit({"action": {"kind": "apply", "resource": "api", "manifest": "v42"}})
decision = fw.wait(first["key"]) if first["status"] in ("queued", "held") else first
if decision["status"] not in ("admit", "in_flight"):
raise NotAdmitted(decision)
try:
deploy()
except Exception as error:
fw.report(first["key"], "failed", str(error))
raise
fw.report(first["key"], "succeeded")Decisions#
{
"key": {"agent": "deployer", "id": "1731600000000000"},
"status": "held", # admit | in_flight | queued | held | rejected | expired | lapsed | done
"verdict": "defer", # admit | defer | reject | None
"reason": "'billing' needs a person to sign off",
"policies": [{"policy": "guardrails", "verdict": "defer", "reason": "..."}],
}Errors#
| Exception | When |
|---|---|
NotAdmitted | Rejected, expired, lapsed, or still waiting at the timeout; error.decision says which. |
FloodwallError | The plane answered with an error: error.status and error.body. |
ValueError | An invalid change or option. |
RuntimeError | Signing without the cryptography package. |
If apply raises and the failure report fails too, your exception is still the one raised, with the report's error on its report_error attribute.
Failing closed or open#
With when_unreachable="open", a change is applied unchecked when the plane can't be reached and on_fail_open(intent, error) is called. Only URLError, timeouts, connection errors and 5xx answers count. Invalid changes, signing errors, a missing cryptography package, 4xx answers and rejections always raise.
Retries and rate limits#
A 429 is waited out as its Retry-After says (seconds or an HTTP date, a minute at most), up to five times. Redirects are never followed, and the key is sent only to base_url.
Signing#
from cryptography.hazmat.primitives.asymmetric.ed25519 import Ed25519PrivateKey
from cryptography.hazmat.primitives import serialization
key = Ed25519PrivateKey.generate() # keep it in your secret store
public_hex = key.public_key().public_bytes(serialization.Encoding.Raw, serialization.PublicFormat.Raw).hex()
print(public_hex) # goes in the plane's keyring under this agent's name
fw = Floodwall(api_key=..., plane=..., agent="deployer", signing_key=key)intent_bytes(intent) and intent_digest(intent) are exported too: the exact bytes and SHA-256 the plane verifies.
Examples#
The examples include a Terraform apply per workspace and a remediation bot in Python.