first commit
This commit is contained in:
commit
0ffcbcec6b
5
README.md
Normal file
5
README.md
Normal file
@ -0,0 +1,5 @@
|
||||
1. Before running the scripts setup TOKEN in ```create_server.sh``` and ```remove_server.sh``` to hetzner cloud
|
||||
|
||||
2. Run ```sh create_server.sh``` to create gitea server
|
||||
|
||||
3. Run ```sh remove_server.sh``` to remove gitea server
|
BIN
analiza_kosztow.pdf
Normal file
BIN
analiza_kosztow.pdf
Normal file
Binary file not shown.
5
create_server.sh
Normal file
5
create_server.sh
Normal file
@ -0,0 +1,5 @@
|
||||
TOKEN="PhjbOkYk19zOWOiRfhZpFIWuCrr7gFRpCJssYEOqM2dk2XXr4NhqobVpKvnoms6B"
|
||||
|
||||
pip install hcloud
|
||||
|
||||
python deploy.py create $TOKEN
|
249
deploy.py
Normal file
249
deploy.py
Normal file
@ -0,0 +1,249 @@
|
||||
from hcloud import Client, APIException
|
||||
from hcloud.images.domain import Image
|
||||
from hcloud.networks.domain import NetworkSubnet
|
||||
from hcloud.server_types.domain import ServerType
|
||||
from hcloud.locations.domain import Location
|
||||
import sys
|
||||
import time
|
||||
|
||||
|
||||
PREFIX = "s495731"
|
||||
KEY_SSH_NAME = f"{PREFIX}-ssh"
|
||||
NETWORK_NAME = f"{PREFIX}-gitea-net"
|
||||
VOLUME_NAME = f"{PREFIX}-gitea-volume"
|
||||
DB_NAME = f"{PREFIX}-gitea-db"
|
||||
SERVER_NAME = f"{PREFIX}-gitea-server"
|
||||
|
||||
VOLUME_SIZE = 10
|
||||
IP_RANGE="10.10.10.0/24"
|
||||
LOCATION = Location("hel1")
|
||||
SSH_KEY= "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIJ2Ez5k2Mpt/oPFpjR59OxDOTqEMg/QFbkgXSOHh99e/ adaste4@st.amu.edu.pl"
|
||||
NETWORK_ZONE= "eu-central"
|
||||
TYPE="cloud"
|
||||
IMAGE_NAME= "ubuntu-24.04"
|
||||
SERVER_TYPE_NAME="cx22"
|
||||
|
||||
class GiteaHetzner:
|
||||
def __init__(self, token):
|
||||
self.client = Client(token=token)
|
||||
|
||||
|
||||
def create_key(self, key_name, new_ssh_key):
|
||||
ssh_key = self.client.ssh_keys.get_by_name(name=key_name)
|
||||
if ssh_key is None:
|
||||
ssh_key = self.client.ssh_keys.create(name=key_name, public_key=new_ssh_key)
|
||||
print("The ssh key has been created")
|
||||
else:
|
||||
print("The ssh key existed before")
|
||||
|
||||
return ssh_key
|
||||
|
||||
def create_network(self, net_name, ip_range, network_zone, type):
|
||||
network = self.client.networks.get_by_name(NETWORK_NAME)
|
||||
if not network:
|
||||
network = self.client.networks.create(
|
||||
name=net_name,
|
||||
ip_range=ip_range,
|
||||
subnets=[
|
||||
NetworkSubnet(ip_range=ip_range, network_zone=network_zone, type=type)
|
||||
],
|
||||
)
|
||||
print(f"Network {network.data_model.name} ({network.data_model.ip_range}) has been created")
|
||||
|
||||
else:
|
||||
print(f"Network {network.data_model.name} ({network.data_model.ip_range}) existed before")
|
||||
|
||||
return network
|
||||
|
||||
def create_volume(self, volume_name, location, size):
|
||||
volume = self.client.volumes.get_by_name(VOLUME_NAME)
|
||||
if not volume:
|
||||
volume = self.client.volumes.create(
|
||||
name=volume_name, size=size, location=location
|
||||
)
|
||||
print("Volume has been created")
|
||||
|
||||
return volume.volume
|
||||
|
||||
else:
|
||||
print("Volume existed before")
|
||||
|
||||
return volume
|
||||
|
||||
def create_db(self, db_name, ssh_key, network, location, server_type, image):
|
||||
init_db_config = f"""#cloud-config
|
||||
packages:
|
||||
- apt-transport-https
|
||||
- ca-certificates
|
||||
- curl
|
||||
- gnupg-agent
|
||||
- software-properties-common
|
||||
|
||||
write_files:
|
||||
- path: /root/docker-compose.yml
|
||||
content: |
|
||||
version: "3.9"
|
||||
services:
|
||||
db:
|
||||
image: postgres:14
|
||||
restart: always
|
||||
ports:
|
||||
- "10.10.10.2:5432:5432"
|
||||
environment:
|
||||
- POSTGRES_USER=gitea
|
||||
- POSTGRES_PASSWORD=gitea
|
||||
- POSTGRES_DB=gitea
|
||||
volumes:
|
||||
- ./postgres:/var/lib/postgresql/data
|
||||
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"
|
||||
- apt-get update -y
|
||||
- apt-get install -y docker-ce docker-ce-cli containerd.io
|
||||
- curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
|
||||
- chmod +x /usr/local/bin/docker-compose
|
||||
- systemctl start docker
|
||||
- systemctl enable docker
|
||||
- cd /root/ && docker-compose up -d
|
||||
"""
|
||||
db_server = self.client.servers.create(
|
||||
name=db_name,
|
||||
server_type=ServerType(server_type),
|
||||
image=Image(name=image),
|
||||
ssh_keys=[ssh_key],
|
||||
networks=[network],
|
||||
location=location,
|
||||
user_data=init_db_config,
|
||||
)
|
||||
db_server.action.wait_until_finished()
|
||||
print(f"Server: {db_server.server.data_model.name}\n\tpublic IP: {db_server.server.data_model.public_net.ipv4.ip}\n\tprivate IP: {db_server.server.data_model.private_net[0].ip}")
|
||||
return db_server
|
||||
|
||||
def create_gitea_server(self, server_name, ssh_key, network, volume, location, server_type, image, dbIP):
|
||||
init_gitea_config=f'''#cloud-config
|
||||
packages:
|
||||
- apt-transport-https
|
||||
- ca-certificates
|
||||
- curl
|
||||
- gnupg-agent
|
||||
- software-properties-common
|
||||
write_files:
|
||||
- path: /root/docker-compose.yml
|
||||
content: |
|
||||
version: "3"
|
||||
services:
|
||||
server:
|
||||
image: gitea/gitea:1.14.5-rootless
|
||||
environment:
|
||||
- GITEA__database__DB_TYPE=postgres
|
||||
- GITEA__database__HOST={dbIP}
|
||||
- GITEA__database__NAME=gitea
|
||||
- GITEA__database__USER=gitea
|
||||
- GITEA__database__PASSWD=gitea
|
||||
restart: always
|
||||
volumes:
|
||||
- /mnt/{VOLUME_NAME}/data:/var/lib/gitea
|
||||
- /mnt/{VOLUME_NAME}/config:/etc/gitea
|
||||
- /etc/timezone:/etc/timezone:ro
|
||||
- /etc/localtime:/etc/localtime:ro
|
||||
ports:
|
||||
- "3000:3000"
|
||||
- "2222:2222"
|
||||
|
||||
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"
|
||||
- apt-get update -y
|
||||
- apt-get install -y docker-ce docker-ce-cli containerd.io
|
||||
- curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
|
||||
- chmod +x /usr/local/bin/docker-compose
|
||||
- systemctl start docker
|
||||
- systemctl enable docker
|
||||
- cd /root/
|
||||
- IP=$(hostname -I | cut -d ' ' -f 1)
|
||||
- echo "DOMAIN=$IP" >> .env
|
||||
- sudo mkfs.ext4 -F /dev/disk/by-id/scsi-0HC_Volume_{volume.id}
|
||||
- mkdir /mnt/{VOLUME_NAME}
|
||||
- mount -o discard,defaults /dev/disk/by-id/scsi-0HC_Volume_{volume.id} /mnt/{VOLUME_NAME}
|
||||
- echo "/dev/disk/by-id/scsi-0HC_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
|
||||
'''
|
||||
|
||||
gitea_server = self.client.servers.create(
|
||||
name=server_name,
|
||||
server_type=ServerType(server_type),
|
||||
image=Image(name=image),
|
||||
ssh_keys=[ssh_key],
|
||||
networks=[network],
|
||||
volumes=[volume],
|
||||
location=location,
|
||||
user_data=init_gitea_config,
|
||||
)
|
||||
gitea_server.action.wait_until_finished()
|
||||
print(f"The gitea server was created: {gitea_server.action.complete}")
|
||||
return gitea_server
|
||||
|
||||
def remove_everything(self):
|
||||
servers = self.client.servers.get_all()
|
||||
print(f"Usuwanie {len(servers)} serwerów")
|
||||
for s in servers:
|
||||
if s.data_model.name.startswith(PREFIX):
|
||||
action = self.client.servers.delete(s)
|
||||
print(f"\tUsuwanie serwera {s.data_model.name} ({s.data_model.public_net.ipv4.ip}): {action.data_model.status}")
|
||||
|
||||
|
||||
ssh_keys = self.client.ssh_keys.get_all()
|
||||
print(f"Usuwanie {len(ssh_keys)} kluczy SSH")
|
||||
for s in ssh_keys:
|
||||
if s.data_model.name.startswith(PREFIX):
|
||||
action = self.client.ssh_keys.delete(s)
|
||||
print(f"\tUsuwanie klucza {s.name}: {action}")
|
||||
|
||||
vnets = self.client.networks.get_all()
|
||||
print(f"Usuwanie {len(vnets)} sieci wirtualnych")
|
||||
for s in vnets:
|
||||
if s.data_model.name.startswith(PREFIX):
|
||||
action = self.client.networks.delete(s)
|
||||
print(f"\tUsuwanie sieci wirtualnej {s.name}: {action}")
|
||||
|
||||
time.sleep(6)
|
||||
try:
|
||||
|
||||
volumes = self.client.volumes.get_all()
|
||||
print(f"Usuwanie {len(volumes)} wolumenów")
|
||||
for v in volumes:
|
||||
if s.data_model.name.startswith(PREFIX):
|
||||
action = self.client.volumes.delete(v)
|
||||
print(f"\tUsuwanie wolumenu {v.name}: {action}")
|
||||
except :
|
||||
print('Problem with removing volume, try to run script one more time')
|
||||
|
||||
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
args = sys.argv[1]
|
||||
token = sys.argv[2]
|
||||
client = GiteaHetzner(token=token)
|
||||
|
||||
if args == "create":
|
||||
ssh_key = client.create_key(KEY_SSH_NAME, SSH_KEY)
|
||||
network = client.create_network(NETWORK_NAME, IP_RANGE, NETWORK_ZONE, TYPE)
|
||||
|
||||
db = client.create_db(DB_NAME, ssh_key, network, LOCATION, SERVER_TYPE_NAME, IMAGE_NAME)
|
||||
|
||||
volume = client.create_volume(VOLUME_NAME, LOCATION, VOLUME_SIZE)
|
||||
|
||||
server = client.create_gitea_server(
|
||||
SERVER_NAME, ssh_key, network, volume, LOCATION, SERVER_TYPE_NAME, IMAGE_NAME,
|
||||
db.server.data_model.private_net[0].ip,
|
||||
)
|
||||
print(
|
||||
f"You can access the server (in a few minutes) under the address: http://{server.server.data_model.public_net.ipv4.ip}:3000"
|
||||
)
|
||||
|
||||
if args == 'everything_delete':
|
||||
client.remove_everything()
|
5
remove_server.sh
Normal file
5
remove_server.sh
Normal file
@ -0,0 +1,5 @@
|
||||
TOKEN="PhjbOkYk19zOWOiRfhZpFIWuCrr7gFRpCJssYEOqM2dk2XXr4NhqobVpKvnoms6B"
|
||||
|
||||
pip install hcloud
|
||||
|
||||
python deploy.py everything_delete $TOKEN
|
Loading…
Reference in New Issue
Block a user