Repository navigation
Expand file tree
/
Copy pathupdate.py
More file actions
54 lines (43 loc) 路 1.8 KB
/
Copy pathupdate.py
File metadata and controls
54 lines (43 loc) 路 1.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# SPDX-License-Identifier: BSD-3-Clause
# Copyright (c) 2026, Unikraft GmbH. All rights reserved.
#
# Changing an instance's properties. Run with:
# UKC_TOKEN=... uv run examples/update.py
from __future__ import annotations
import asyncio
import sys
from unikraft_cloud import REMOVE, PatchItem, UnikraftCloud, UnikraftCloudError
async def main() -> None:
async with UnikraftCloud() as ukc:
fra = ukc.metro("fra")
# Properties are keyword arguments: a value sets the property, REMOVE
# clears it, and anything omitted is left alone. One request, whatever
# you pass.
updated = await fra.instances.get(name="web").update(
memory_mb=512,
vcpus=2,
env={"LOG_LEVEL": "debug"},
autokill=REMOVE,
)
print(f"{updated.name} in {updated.metro}: {updated.status}")
# When `set` is not what you mean -- merge into a property, or remove
# single members -- stage the operations and apply them together.
await (
fra.instances.get(name="web")
.edit()
.set(hostname="web-1")
.add(env={"FEATURE_X": "1"}, tags=["prod"])
.delete(env=["LOG_LEVEL"], tags=["staging"])
.apply()
)
# `apply()` returns a handle, so the chain continues.
await fra.instances.get(name="web").edit().set(memory_mb=1024).apply().wait(state="running")
# The raw triples remain available for anything keyword arguments cannot
# express.
await fra.instances.get(name="web").patch([PatchItem("memory_mb", "set", 256)])
if __name__ == "__main__":
try:
asyncio.run(main())
except UnikraftCloudError as err:
print(f"API error ({err.status or '?'}): {err}", file=sys.stderr)
raise SystemExit(1) from err