add main.py
This commit is contained in:
parent
bd12c5ddf1
commit
9b04cde31b
166
main.py
Normal file
166
main.py
Normal file
@ -0,0 +1,166 @@
|
|||||||
|
from hcloud import Client
|
||||||
|
from hcloud.images import Image
|
||||||
|
from hcloud.networks import Network, NetworkSubnet
|
||||||
|
from hcloud.volumes import Volume
|
||||||
|
from hcloud.server_types import ServerType
|
||||||
|
from hcloud._exceptions import APIException
|
||||||
|
from hcloud.servers.domain import ServerCreatePublicNetwork
|
||||||
|
from os import getenv
|
||||||
|
from dotenv import load_dotenv
|
||||||
|
|
||||||
|
load_dotenv()
|
||||||
|
|
||||||
|
HETZNER_API_TOKEN = getenv('HETZNER_API_TOKEN')
|
||||||
|
NETWORK_NAME = "s444510-priv-net"
|
||||||
|
NETWORK_TYPE = "server"
|
||||||
|
NETWORK_ZONE = "eu-central"
|
||||||
|
IP_RANGE = "10.0.0.0/8"
|
||||||
|
IP_RANGE_SUBNET = "10.0.0.0/24"
|
||||||
|
LOCATION = "hel1"
|
||||||
|
VOLUME_NAME = "s444510-volume"
|
||||||
|
SERVER_TYPE = "cx11"
|
||||||
|
SERVER_IMAGE = "ubuntu-22.04"
|
||||||
|
|
||||||
|
DB_SERVER_NAME = "s444510-db"
|
||||||
|
GITEA_SERVER_NAME = "s444510-gitea"
|
||||||
|
|
||||||
|
|
||||||
|
print("Create Hetzner client")
|
||||||
|
client = Client(token=f"{HETZNER_API_TOKEN}")
|
||||||
|
|
||||||
|
print("Get location")
|
||||||
|
location = client.locations.get_by_name(LOCATION)
|
||||||
|
|
||||||
|
|
||||||
|
print("Creating DB volume")
|
||||||
|
try:
|
||||||
|
volume = client.volumes.create(size=10, name=VOLUME_NAME, location=location)
|
||||||
|
volume = volume.volume
|
||||||
|
print(f"Volume created: {volume.name}")
|
||||||
|
except APIException:
|
||||||
|
volume = client.volumes.get_by_name(name=VOLUME_NAME)
|
||||||
|
print(f"Volume already exists: {volume.name}")
|
||||||
|
|
||||||
|
|
||||||
|
print("Creating Network")
|
||||||
|
try:
|
||||||
|
network = client.networks.create(name=NETWORK_NAME, ip_range=IP_RANGE)
|
||||||
|
print(f"network created: {network.name}")
|
||||||
|
except APIException:
|
||||||
|
network = client.networks.get_by_name(name=NETWORK_NAME)
|
||||||
|
print(f"Network already exists: {network.name}")
|
||||||
|
|
||||||
|
|
||||||
|
print("Creating Subnet")
|
||||||
|
try:
|
||||||
|
subnet = client.networks.add_subnet(
|
||||||
|
network=network, subnet=NetworkSubnet(ip_range=IP_RANGE_SUBNET, network_zone=NETWORK_ZONE, type=NETWORK_TYPE)
|
||||||
|
)
|
||||||
|
except APIException:
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
print("Creating DB server")
|
||||||
|
try:
|
||||||
|
response = client.servers.create(
|
||||||
|
name=DB_SERVER_NAME,
|
||||||
|
server_type=ServerType(name=SERVER_TYPE),
|
||||||
|
image=Image(name=SERVER_IMAGE),
|
||||||
|
location=location,
|
||||||
|
volumes=[Volume(id=volume.id)],
|
||||||
|
networks=[Network(id=network.id)],
|
||||||
|
public_net=ServerCreatePublicNetwork(enable_ipv4=False, enable_ipv6=False),
|
||||||
|
user_data="""
|
||||||
|
#cloud-config
|
||||||
|
package_update: true
|
||||||
|
package_upgrade: true
|
||||||
|
|
||||||
|
groups:
|
||||||
|
- docker
|
||||||
|
|
||||||
|
system_info:
|
||||||
|
default_user:
|
||||||
|
groups: [ docker ]
|
||||||
|
|
||||||
|
packages:
|
||||||
|
- apt-transport-https
|
||||||
|
- ca-certificates
|
||||||
|
- curl
|
||||||
|
- gnupg
|
||||||
|
- lsb-release
|
||||||
|
- unattended-upgrades
|
||||||
|
|
||||||
|
runcmd:
|
||||||
|
- mkdir -p /etc/apt/keyrings
|
||||||
|
- curl -fsSL https://download.docker.com/linux/ubuntu/gpg | gpg --dearmor -o /etc/apt/keyrings/docker.gpg
|
||||||
|
- echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | tee /etc/apt/sources.list.d/docker.list > /dev/null
|
||||||
|
- apt-get update
|
||||||
|
- apt-get install -y docker-ce docker-ce-cli containerd.io docker-compose-plugin
|
||||||
|
- systemctl enable docker
|
||||||
|
- systemctl start docker
|
||||||
|
- git clone https://git.wmi.amu.edu.pl/s444510/zpzc-2023.git /zpzc-2023
|
||||||
|
- docker compose -f /zpzc-2023/db.yml up -d
|
||||||
|
|
||||||
|
bootcmd:
|
||||||
|
- docker compose -f /zpzc-2023/db.yml up -d
|
||||||
|
|
||||||
|
final_message: "The system is finally up, after $UPTIME seconds"
|
||||||
|
"""
|
||||||
|
)
|
||||||
|
db_server = response.server
|
||||||
|
print(f"{db_server.id=} {db_server.name=} {db_server.status=}")
|
||||||
|
except APIException:
|
||||||
|
print("Server already exists:")
|
||||||
|
db_server = client.servers.get_by_name(name=DB_SERVER_NAME)
|
||||||
|
|
||||||
|
print("Creating Gitea server")
|
||||||
|
try:
|
||||||
|
gitea_server = client.servers.create(
|
||||||
|
name=GITEA_SERVER_NAME,
|
||||||
|
server_type=ServerType(name=SERVER_TYPE),
|
||||||
|
image=Image(name=SERVER_IMAGE),
|
||||||
|
location=location,
|
||||||
|
networks=[Network(id=network.id)],
|
||||||
|
user_data="""
|
||||||
|
#cloud-config
|
||||||
|
package_update: true
|
||||||
|
package_upgrade: true
|
||||||
|
|
||||||
|
groups:
|
||||||
|
- docker
|
||||||
|
|
||||||
|
system_info:
|
||||||
|
default_user:
|
||||||
|
groups: [ docker ]
|
||||||
|
|
||||||
|
packages:
|
||||||
|
- apt-transport-https
|
||||||
|
- ca-certificates
|
||||||
|
- curl
|
||||||
|
- gnupg
|
||||||
|
- lsb-release
|
||||||
|
- unattended-upgrades
|
||||||
|
|
||||||
|
runcmd:
|
||||||
|
- mkdir -p /etc/apt/keyrings
|
||||||
|
- curl -fsSL https://download.docker.com/linux/ubuntu/gpg | gpg --dearmor -o /etc/apt/keyrings/docker.gpg
|
||||||
|
- echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | tee /etc/apt/sources.list.d/docker.list > /dev/null
|
||||||
|
- apt-get update
|
||||||
|
- apt-get install -y docker-ce docker-ce-cli containerd.io docker-compose-plugin
|
||||||
|
- systemctl enable docker
|
||||||
|
- systemctl start docker
|
||||||
|
- git clone https://git.wmi.amu.edu.pl/s444510/zpzc-2023.git /zpzc-2023
|
||||||
|
- docker compose -f /zpzc-2023/gitea.yml up -d
|
||||||
|
|
||||||
|
bootcmd:
|
||||||
|
- docker compose -f /zpzc-2023/gitea.yml up -d
|
||||||
|
|
||||||
|
final_message: "The system is finally up, after $UPTIME seconds"
|
||||||
|
|
||||||
|
"""
|
||||||
|
)
|
||||||
|
_gitea_server = gitea_server.server
|
||||||
|
print(f"{_gitea_server.id=} {_gitea_server.name=} {_gitea_server.status=}, {gitea_server.root_password} ")
|
||||||
|
except APIException:
|
||||||
|
gitea_server = client.servers.get_by_name(name=DB_SERVER_NAME)
|
||||||
|
print(f"Server already exists: {gitea_server.name}")
|
Loading…
Reference in New Issue
Block a user