Files
computer-playbook/roles/svc-ai-litellm/files/python/ensure_keys.py
kevinveenbirkenbachandClaude Opus 5 dfe7a7fdd4 refactor(roles): sort role scripts into per-language folders
The merged branch moved every role-local script out of files/ into a folder
named for its language (files/python/, files/php/, files/shell/), but the
moves lost their staging and never landed. HEAD still carried the old paths
while the worktree had the new ones.

Move the 24 remaining scripts and update the six places that resolve them:
the mcp-adapter instance/isolation/probe tasks, the litellm key task, the
adapter README and the endpoint-path lint. Every changed line in those
files is a path.

Also drop roles/sys-svc-compose/files/swarm/stack_ready.sh, a stale
duplicate of the copy already tracked under files/shell/swarm/.

The added comments are the branch's own `nocheck: mirrored-unit-test`
directives, which state why a script whose measurement only exists inside
its container cannot have a mirrored unit test.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-28 12:00:59 +02:00

68 lines
2.1 KiB
Python

import contextlib
import json
import os
import urllib.error
import urllib.request
TIMEOUT = 30
def ensure_key(entry, call):
"""Give the consumer the key it was issued, whatever holds the alias now.
An inventory re-initialization mints a new key while the proxy database
keeps the previous one under the same alias, and the alias is unique, so
creating the new key is refused until the stale one is gone.
Args:
entry: mapping with an ``alias`` and the ``key`` it must carry.
call: ``(method, path, body=None)`` performing one HTTP request.
Returns:
True when the key had to be created, False when it already existed.
"""
with contextlib.suppress(urllib.error.HTTPError):
call("GET", "/key/info?key=" + entry["key"])
return False
with contextlib.suppress(urllib.error.HTTPError):
call("POST", "/key/delete", {"key_aliases": [entry["alias"]]})
call("POST", "/key/generate", {"key": entry["key"], "key_alias": entry["alias"]})
return True
def http_call(base, headers):
"""Return a ``call`` bound to one proxy.
Args:
base: scheme and authority of the proxy, without a trailing slash.
headers: headers sent with every request, carrying the master key.
"""
def call(method, path, body=None):
request = urllib.request.Request( # noqa: S310 fixed internal http origin
base + path,
data=(json.dumps(body).encode() if body is not None else None),
headers=headers,
method=method,
)
return urllib.request.urlopen(request, timeout=TIMEOUT) # noqa: S310 fixed internal http origin
return call
def main():
call = http_call(
"http://localhost:" + os.environ["LITELLM_PORT"],
{
"Authorization": "Bearer " + os.environ["LITELLM_MK"],
"Content-Type": "application/json",
},
)
for entry in json.loads(os.environ["LITELLM_KEYS_PAYLOAD"]):
if ensure_key(entry, call):
print("CHANGED " + entry["alias"])
if __name__ == "__main__":
main()