dpzc-hetzner/clean.py

23 lines
730 B
Python
Raw Normal View History

2022-12-29 23:32:21 +01:00
from hcloud import Client
with open("token.txt", "r") as file:
api_token = file.read().strip()
client = Client(token=api_token)
PREFIX = "s478839"
servers = client.servers.get_all()
print(f"Usuwanie {len(servers)} serwerów")
for s in servers:
if s.data_model.name.startswith(PREFIX):
action = client.servers.delete(s)
2023-01-08 20:47:07 +01:00
print(f"Usuwanie serwera {s.data_model.name} ({s.data_model.public_net.ipv4.ip}): {action.data_model.status}")
volume = client.volumes.get_by_name(f"{PREFIX}-pzc-test-volume") or None
if volume:
print("Usuwanie wolumenu...")
action = client.volumes.detach(volume)
action.wait_until_finished()
action = client.volumes.delete(volume)
print("Usunięto wolumen")