Repository navigation
Expand file tree
/
Copy pathquickstart.py
More file actions
52 lines (42 loc) · 1.99 KB
/
Copy pathquickstart.py
File metadata and controls
52 lines (42 loc) · 1.99 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
"""Order a small server, give it a DNS name, then delete it.
export NODERINGS_TOKEN=... # a service account token
export NODERINGS_PLAN_ID=... # an offer's plan ID, from client.marketplace.list_providers
export NODERINGS_REGION_ID=... # the offer's region ID
export NODERINGS_SSH_KEY_ID=... # one of client.ssh_key.list_ssh_keys()
export NODERINGS_ZONE_ID=... # optional: a DNS zone for an A record
python examples/quickstart.py
The server is billed while it exists; on usage-billed (1 vCPU) plans that is a
fraction of a cent for this run.
"""
from __future__ import annotations
import os
from noderings import NodeRings
from noderings.models import Distro, DNSRecord, OperationStatus
def main() -> None:
with NodeRings() as client:
created = client.virtual_machine.create_virtual_machine(
hostname="quickstart-1",
plan_id=os.environ["NODERINGS_PLAN_ID"],
region_id=os.environ["NODERINGS_REGION_ID"],
distro=Distro.UBUNTU_26_04,
ssh_key_ids=[os.environ["NODERINGS_SSH_KEY_ID"]],
tags=["quickstart"],
)
assert created.virtual_machine
vm_id = created.virtual_machine.id
try:
vm = client.wait_for_virtual_machine_status(vm_id, OperationStatus.STARTED)
print(f"{vm.hostname} is running: ssh root@{vm.ipv4}")
zone_id = os.environ.get("NODERINGS_ZONE_ID")
if zone_id:
rec = client.dns_record.upsert_dns_record(
zone_id, record=DNSRecord(name=vm.hostname, type="A", ttl=300, rdata=[vm.ipv4])
)
print("A record:", rec.name, rec.rdata)
client.dns_record.delete_dns_record(zone_id, name=vm.hostname, type="A")
finally:
client.virtual_machine.delete_virtual_machine(vm_id)
client.wait_for_virtual_machine_deleted(vm_id)
print("deleted")
if __name__ == "__main__":
main()