50 lines
1.4 KiB
Python
50 lines
1.4 KiB
Python
|
from hcloud import Client
|
||
|
from dotenv import load_dotenv
|
||
|
import os
|
||
|
import sys
|
||
|
|
||
|
load_dotenv()
|
||
|
|
||
|
API_KEY = os.getenv("API_KEY")
|
||
|
PREFIX = "s464863"
|
||
|
|
||
|
client = Client(
|
||
|
token=API_KEY
|
||
|
)
|
||
|
|
||
|
delete_volumes = False
|
||
|
sys.argv = sys.argv[1:]
|
||
|
if len(sys.argv) > 0:
|
||
|
if sys.argv[0] == "--delete-volumes":
|
||
|
delete_volumes = True
|
||
|
|
||
|
def delete_all_servers():
|
||
|
servers = client.servers.get_all()
|
||
|
for server in servers:
|
||
|
if server.name.startswith(PREFIX):
|
||
|
action = server.volumes[0].detach()
|
||
|
action.wait_until_finished()
|
||
|
action = client.servers.delete(server)
|
||
|
print(
|
||
|
f"Usuwanie serwera {server.data_model.name} ({server.data_model.public_net.ipv4.ip}): {action.data_model.status}")
|
||
|
|
||
|
def delete_all_volumes():
|
||
|
volumes = client.volumes.get_all()
|
||
|
for volume in volumes:
|
||
|
if volume.name.startswith(PREFIX):
|
||
|
action = client.volumes.detach(volume)
|
||
|
action.wait_until_finished()
|
||
|
client.volumes.delete(volume)
|
||
|
print(f"Usuwanie woluminu {volume.name}")
|
||
|
|
||
|
def delete_all_networks():
|
||
|
networks = client.networks.get_all()
|
||
|
for network in networks:
|
||
|
if network.name.startswith(PREFIX):
|
||
|
action = client.networks.delete(network)
|
||
|
print(f"Usuwanie sieci {network.data_model.name}")
|
||
|
|
||
|
delete_all_servers()
|
||
|
if delete_volumes:
|
||
|
delete_all_volumes()
|
||
|
delete_all_networks()
|