chmury_gitea/clean.py
2022-12-07 01:14:22 +01:00

74 lines
1.7 KiB
Python

#!/usr/bin/env python3
from hcloud import Client, APIException
import sys
if len(sys.argv) > 1:
VERBOSE = True
else:
VERBOSE = False
with open('.credentials/token', 'r') as file:
client = Client(token=str(file.readline()).strip())
ssh_name = "ssh-s444501"
network_name = "s444501-gitea"
volume_name = "s444501-gitea"
db_name = "s444501-gitea-db"
gitea_name = "s444501-gitea-server"
if VERBOSE:
print("Clearing SSH key... ", end='')
try:
action = client.ssh_keys.delete(client.ssh_keys.get_by_name(ssh_name))
action.wait_until_finished()
except AttributeError:
pass
if VERBOSE:
print('\t\tdone.')
if VERBOSE:
print("Clearing network... ", end='')
try:
action = client.networks.delete(client.networks.get_by_name(network_name))
action.wait_until_finished()
except AttributeError:
pass
if VERBOSE:
print('\t\tdone.')
if VERBOSE:
print("Clearing volume... ", end='')
try:
volume = client.volumes.get_by_name(volume_name)
try:
action = client.volumes.detach(volume)
action.wait_until_finished()
except APIException:
pass
action = client.volumes.delete(volume)
action.wait_until_finished()
except AttributeError:
pass
if VERBOSE:
print('\t\tdone.')
if VERBOSE:
print("Clearing db server... ", end='')
try:
action = client.servers.delete(client.servers.get_by_name(db_name))
action.wait_until_finished()
except AttributeError:
pass
if VERBOSE:
print('\t\tdone.')
if VERBOSE:
print("Clearing gitea server... ", end='')
try:
action = client.servers.delete(client.servers.get_by_name(gitea_name))
action.wait_until_finished()
except AttributeError:
pass
if VERBOSE:
print('\tdone.')