chmury-loadbalancer/clean.py

53 lines
1.4 KiB
Python
Raw Normal View History

2023-01-12 17:27:41 +01:00
#!/usr/bin/env python3
import sys
from hcloud import Client
if len(sys.argv) > 1:
VERBOSE = True
else:
VERBOSE = False
with open('.credentials/token', 'r') as file:
client = Client(token=str(file.readline()).strip())
index = 's444501'
ssh_name = index + '-ssh'
network_name = index + '-network'
server_base_name = index + '-server'
load_balancer_name = index + '-lb'
SERVER_COUNT = 5
print("Clearing SSH key... ", end='') if VERBOSE else None
try:
action = client.ssh_keys.delete(client.ssh_keys.get_by_name(ssh_name))
action.wait_until_finished()
except AttributeError:
pass
print('\t\tdone.') if VERBOSE else None
print("Clearing network... ", end='') if VERBOSE else None
try:
action = client.networks.delete(client.networks.get_by_name(network_name))
action.wait_until_finished()
except AttributeError:
pass
print('\t\tdone.') if VERBOSE else None
print("Clearing servers... ", end='') if VERBOSE else None
for i in range(SERVER_COUNT):
try:
action = client.servers.delete(client.servers.get_by_name(server_base_name + '-' + str(i + 1)))
action.wait_until_finished()
except AttributeError:
pass
print('\tdone.') if VERBOSE else None
print("Clearing load balancer... ", end='') if VERBOSE else None
try:
2023-01-12 17:42:56 +01:00
action = client.load_balancers.delete(client.load_balancers.get_by_name(load_balancer_name))
2023-01-12 17:27:41 +01:00
action.wait_until_finished()
except AttributeError:
pass
print('\tdone.') if VERBOSE else None