DPZC-Hetzner/zad4/clean.py
2023-01-22 22:56:41 +01:00

22 lines
697 B
Python

from hcloud import Client
with open("../token.txt", "r") as file:
api_token = file.read().strip()
client = Client(token=api_token)
PREFIX = "s478873"
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)
print(f"Usuwanie serwera {s.data_model.name} ({s.data_model.public_net.ipv4.ip}): {action.data_model.status}")
loadbalancer = client.load_balancers.get_by_name(f"{PREFIX}-loadbalancer") or None
if loadbalancer:
print("Usuwanie loadbalancera...")
action = client.load_balancers.delete(loadbalancer)
print("Usunięto loadbalancer")