This commit is contained in:
MikolajPaterka 2023-01-03 09:28:06 +01:00
parent 11b3a7638e
commit 7b862b9dab
3 changed files with 21 additions and 37 deletions

View File

@ -4,15 +4,13 @@ api_token = ""
with open("token.txt", "r") as file:
api_token = file.read().strip()
client = Client(
token=api_token
)
PREFIX = "s444455"
INDEKS = "s444455"
servers = client.servers.get_all()
print(f"Usuwanie {len(servers)} serwerów")
for s in servers:
if s.data_model.name.startswith(PREFIX):
action = client.servers.delete(s)
print(f"Usuwanie serwera {s.data_model.name} ({s.data_model.public_net.ipv4.ip}): {action.data_model.status}")
for i in servers:
if i.data_model.name.startswith(INDEKS):
action = client.servers.delete(i)
print("Cleaning servers: done")

View File

@ -5,37 +5,35 @@ from hcloud.images.domain import Image
from hcloud.server_types.domain import ServerType
PREFIX = "s444455"
INDEKS = "s444455"
api_token = ""
ssh_name = f"{PREFIX}-key"
db_server_name = f"{PREFIX}-db"
gitea_server_name = f"{PREFIX}-gitea"
vnet_name = f"{PREFIX}-pzc-vnet"
hetzner_token = ""
ssh_name = f"{INDEKS}-key"
db_server_name = f"{INDEKS}-db"
gitea_server_name = f"{INDEKS}-gitea"
vnet_name = f"{INDEKS}-pzc-vnet"
with open("token.txt", "r") as file:
api_token = file.read().strip()
hetzner_token = file.read().strip()
client = Client(token=api_token)
client = Client(token=hetzner_token)
vnet = client.networks.get_by_name(vnet_name) or None
print(f"Sieć wirtualna {vnet.data_model.name} ({vnet.data_model.ip_range})")
vnet = client.networks.get_by_name(vnet_name)
print("Vnet: done")
ssh_key = client.ssh_keys.get_by_name(ssh_name) or None
print(f"Klucz {ssh_key.data_model.name}")
ssh_key = client.ssh_keys.get_by_name(ssh_name)
print("Key: checked")
cloud_init_db=r'''
#cloud-config
# lista podstawowych pakietów, które należy zainstalować
packages:
- apt-transport-https
- ca-certificates
- curl
- gnupg-agent
- software-properties-common
# tworzymy plik docker-compose.yml
write_files:
- path: /root/docker-compose.yml
content: |
@ -56,12 +54,10 @@ write_files:
phpmyadmin:
image: phpmyadmin
restart: always
# przekierowanie portów zostawione tylko dla przykładu, nie należy tak robić na produkcji
ports:
- "8080:80"
volumes:
db_data: {}
# instalujemy docker i docker-compose, a następnie uruchamiamy naszą bazę danych
runcmd:
- curl -fsSL https://download.docker.com/linux/ubuntu/gpg | apt-key add -
- add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
@ -75,14 +71,12 @@ runcmd:
'''
gitea_cloud_init=r'''#cloud-config
# lista podstawowych pakietów, które należy zainstalować
packages:
- apt-transport-https
- ca-certificates
- curl
- gnupg-agent
- software-properties-common
# tworzymy plik docker-compose.yml
write_files:
- path: /root/docker-compose.yml
content: |
@ -95,7 +89,7 @@ write_files:
GITEA_database_HOST: 10.10.10.2:3306
GITEA_database_NAME: gitea
GITEA_database_USER: gitea
GITEA_database_PASSWD: 6XigvpPYf0N0MjO7WW5LmwwGgaDFJ
GITEA_database_PASSWD: gitea
restart: always
volumes:
- ./data:/root/gitea
@ -106,7 +100,6 @@ write_files:
ports:
- "3000:3000"
- "222:22"
# instalujemy docker, docker-compose a następnie uruchamiamy naszą bazę danych
runcmd:
- curl -fsSL https://download.docker.com/linux/ubuntu/gpg | apt-key add -
- add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
@ -129,13 +122,9 @@ db_server = client.servers.create(
user_data=cloud_init_db
)
db_server.action.wait_until_finished()
print(f"Serwer db: {db_server.action.complete}")
db_server = client.servers.get_by_name(db_server_name)
print(f"Serwer: {db_server.data_model.name}\n\tpubliczne IP: {db_server.data_model.public_net.ipv4.ip}\n\tprywatne IP: {db_server.data_model.private_net[0].ip}")
print("Serwer db: done")
gitea_server = client.servers.create(
@ -149,7 +138,5 @@ gitea_server = client.servers.create(
)
gitea_server.action.wait_until_finished()
print(f"Serwera gitea: {gitea_server.action.complete}")
gitea_server = client.servers.get_by_name(gitea_server_name)
print(f"Serwer: {gitea_server.data_model.name}\n\tpubliczne IP: {gitea_server.data_model.public_net.ipv4.ip}\n\tprywatne IP: {gitea_server.data_model.private_net[0].ip}")
print("Serwer gitea: done")

View File

@ -1 +0,0 @@
KccUEiddxtzGoLWSNC3V8tylq7MYHCjdnShtgasQ8jSbHqCjGoaa6Rq7yoz4uS23