This commit is contained in:
Krystian Wasilewski 2022-12-07 01:14:22 +01:00
parent e2549f61c5
commit f4005bfc67
5 changed files with 121 additions and 56 deletions

13
README.md Normal file
View File

@ -0,0 +1,13 @@
## Przed uruchomieniem
W głównym katalogu repozytorium utwórz katalog ``.credentials/``
Umieść w nim plik ``token`` zawierający token do Hetznera.
Upewnij się, że w katalogu domowym ``~/.ssh/`` posiadasz klucz ``id_ed25519.pub``
Upewnij się, że zainstalowany jest ``Python3.9`` lub nowszy oraz ``pip``
## Uruchamianie
Maszyny tworzone są poprzez wywołanie pliku ``deploy.sh``
Maszyny usuwane są przez wywołanie pliku ``clean.sh``

73
clean.py Normal file
View File

@ -0,0 +1,73 @@
#!/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.')

6
clean.sh Normal file
View File

@ -0,0 +1,6 @@
chmod +x clean.py
echo "Creating virtual environment..."
python3 -m venv venv
source venv/bin/activate
pip install -q hcloud
./clean.py 1

7
deploy.sh Normal file
View File

@ -0,0 +1,7 @@
chmod +x main.py clean.py
echo "Creating virtual environment..."
python3 -m venv venv
source venv/bin/activate
pip install -q hcloud
./clean.py
./main.py

78
main.py
View File

@ -1,4 +1,5 @@
from hcloud import Client, APIException #!/usr/bin/env python3
from hcloud import Client
from hcloud.images.domain import Image from hcloud.images.domain import Image
from hcloud.networks.domain import NetworkSubnet from hcloud.networks.domain import NetworkSubnet
from hcloud.locations.domain import Location from hcloud.locations.domain import Location
@ -14,20 +15,11 @@ with open(f'{str(Path.home())}/.ssh/id_ed25519.pub', 'r') as file:
ssh_name = "ssh-s444501" ssh_name = "ssh-s444501"
network_name = "s444501-gitea" network_name = "s444501-gitea"
volume_name = "s444501-gitea" volume_name = "s444501-gitea"
db_name = "s444501-gitea-db"
try: gitea_name = "s444501-gitea-server"
client.ssh_keys.delete(client.ssh_keys.get_by_name(ssh_name))
except AttributeError:
pass
ssh_key = client.ssh_keys.create(name=ssh_name, public_key=ssh_raw) ssh_key = client.ssh_keys.create(name=ssh_name, public_key=ssh_raw)
try:
client.networks.delete(client.networks.get_by_name(network_name))
except AttributeError:
pass
vnet = client.networks.create( vnet = client.networks.create(
name=network_name, name=network_name,
ip_range="10.10.10.0/24", ip_range="10.10.10.0/24",
@ -36,18 +28,6 @@ vnet = client.networks.create(
] ]
) )
try:
v = client.volumes.get_by_name(volume_name)
try:
action = client.volumes.detach(v)
action.wait_until_finished()
except APIException:
pass
client.volumes.delete(v)
except AttributeError:
pass
volume = client.volumes.create( volume = client.volumes.create(
name=volume_name, name=volume_name,
size=10, size=10,
@ -94,7 +74,7 @@ runcmd:
''' '''
db_server = client.servers.create( db_server = client.servers.create(
name="s444501-gitea-db", name=db_name,
server_type=ServerType("cpx11"), server_type=ServerType("cpx11"),
image=Image(name="ubuntu-22.04"), image=Image(name="ubuntu-22.04"),
ssh_keys=[ssh_key], ssh_keys=[ssh_key],
@ -120,11 +100,6 @@ write_files:
- path: /root/docker-compose.yml - path: /root/docker-compose.yml
content: | content: |
version: "2" version: "2"
volumes:
gitea-data:
driver: local
gitea-config:
driver: local
services: services:
server: server:
image: gitea/gitea:1.17.3-rootless image: gitea/gitea:1.17.3-rootless
@ -137,14 +112,15 @@ write_files:
- GITEA__database__PASSWD=gitea - GITEA__database__PASSWD=gitea
restart: always restart: always
volumes: volumes:
- gitea-data:/var/lib/gitea - /mnt/s444501-gitea/data:/var/lib/gitea
- gitea-config:/etc/gitea - /mnt/s444501-gitea/config:/etc/gitea
- /etc/timezone:/etc/timezone:ro - /etc/timezone:/etc/timezone:ro
- /etc/localtime:/etc/localtime:ro - /etc/localtime:/etc/localtime:ro
ports: ports:
- "3000:3000" - "3000:3000"
- "2222:2222" - "2222:2222"
'''
runcmd = f'''
runcmd: runcmd:
- curl -fsSL https://download.docker.com/linux/ubuntu/gpg | apt-key add - - 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" - add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
@ -157,17 +133,26 @@ runcmd:
- cd /root/ - cd /root/
- IP=$(hostname -I | cut -d ' ' -f 1) - IP=$(hostname -I | cut -d ' ' -f 1)
- echo "DOMAIN=$IP" >> .env - echo "DOMAIN=$IP" >> .env
- sudo mkfs.ext4 -F /dev/disk/by-id/scsi-0HC_Volume_{volume.volume.id}
- mkdir /mnt/{volume_name}
- mount -o discard,defaults /dev/disk/by-id/scsi-0HC_Volume_{volume.volume.id} /mnt/{volume_name}
- echo "/dev/disk/by-id/scsi-0HC_Volume_{volume.volume.id} /mnt/{volume_name} ext4 discard,nofail,defaults 0 0" >> /etc/fstab
- mkdir /mnt/{volume_name}/data
- mkdir /mnt/{volume_name}/config
- sudo chown 1000:1000 /mnt/{volume_name}/config/ /mnt/{volume_name}/data
- docker-compose up -d - docker-compose up -d
''' '''
cloud_init_gitea = cloud_init_gitea + runcmd
gitea_server = client.servers.create( gitea_server = client.servers.create(
name="s444501-gitea-server", name=gitea_name,
server_type=ServerType("cpx11"), server_type=ServerType("cpx11"),
image=Image(name="ubuntu-22.04"), image=Image(name="ubuntu-22.04"),
ssh_keys=[ssh_key], ssh_keys=[ssh_key],
networks=[vnet], networks=[vnet],
volumes=[volume.volume], volumes=[volume.volume],
automount=True, automount=True, # automount nie działa z poziomu skryptu a działa z przeglądarki
location=Location("hel1"), location=Location("hel1"),
user_data=cloud_init_gitea user_data=cloud_init_gitea
) )
@ -175,24 +160,5 @@ gitea_server = client.servers.create(
gitea_server.action.wait_until_finished() gitea_server.action.wait_until_finished()
print(f"Tworzenie serwera gitea: {gitea_server.action.complete}") print(f"Tworzenie serwera gitea: {gitea_server.action.complete}")
# automount dirty fix print(f"Serwer dostępny pod adresem http://{gitea_server.server.data_model.public_net.ipv4.ip}:3000")
# v = client.volumes.get_by_name(volume_name) print("Uruchumienie GITEA może zająć 2-3 minuty")
# client.volumes.attach(v, gitea_server.server, automount=True)
# HC_Volume_id
# print(f"Usuwanie volumenu")
# v = client.volumes.get_by_name(volume_name)
# try:
# action = client.volumes.detach(v)
# action.wait_until_finished()
# except APIException:
# pass
#
# client.volumes.delete(v)
#
# servers = client.servers.get_all()
# print(f"Usuwanie {len(servers)} serwerów")
# for s in servers:
# action = client.servers.delete(s)
# print(f"Usuwanie serwera {s.data_model.name} ({s.data_model.public_net.ipv4.ip}): {action.data_model.status}")