This commit is contained in:
s464863 2024-11-23 10:29:35 +01:00
parent 8b1c611e37
commit c7883caf2b
3 changed files with 54 additions and 51 deletions

View File

@ -2,4 +2,4 @@
chmod +x delete_resources_script.py chmod +x delete_resources_script.py
pip install -r requirements.txt pip install -r requirements.txt
python delete_resources_script.py python delete_resources_script.py $1

View File

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

View File

@ -2,4 +2,4 @@
chmod +x deploy_script.py chmod +x deploy_script.py
pip install -r requirements.txt pip install -r requirements.txt
python deploy_script.py $1 python deploy_script.py