From c7883caf2b70f2b48e4deef29ca869d99b290c28 Mon Sep 17 00:00:00 2001 From: s464863 Date: Sat, 23 Nov 2024 10:29:35 +0100 Subject: [PATCH] Fix --- delete_resources.sh | 2 +- delete_resources_script.py | 101 +++++++++++++++++++------------------ deploy.sh | 2 +- 3 files changed, 54 insertions(+), 51 deletions(-) diff --git a/delete_resources.sh b/delete_resources.sh index 174d8e8..be5805c 100644 --- a/delete_resources.sh +++ b/delete_resources.sh @@ -2,4 +2,4 @@ chmod +x delete_resources_script.py pip install -r requirements.txt -python delete_resources_script.py \ No newline at end of file +python delete_resources_script.py $1 \ No newline at end of file diff --git a/delete_resources_script.py b/delete_resources_script.py index e654efa..60a0c97 100644 --- a/delete_resources_script.py +++ b/delete_resources_script.py @@ -1,50 +1,53 @@ -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() +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 +if len(sys.argv) > 1: + if sys.argv[1] == "--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.power_off() + action.wait_until_finished() + if server.volumes and server.volumes[0]: + 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): + if volume.server is not None: + 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() \ No newline at end of file diff --git a/deploy.sh b/deploy.sh index 65e4a25..903b032 100644 --- a/deploy.sh +++ b/deploy.sh @@ -2,4 +2,4 @@ chmod +x deploy_script.py pip install -r requirements.txt -python deploy_script.py $1 \ No newline at end of file +python deploy_script.py \ No newline at end of file