zpzc-3/hertzner.py

169 lines
4.8 KiB
Python
Raw Normal View History

2024-02-05 15:09:27 +01:00
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
2024-02-08 03:56:29 +01:00
from hcloud.servers.domain import ServerCreatePublicNetwork
2024-02-05 15:09:27 +01:00
2024-02-07 20:03:38 +01:00
YOUR_API_TOKEN = ""
2024-02-05 15:09:27 +01:00
NETWORK_PRIV = "zpzc-s434686-priv-net"
NETWORK_TYPE = "server"
NETWORK_ZONE = "eu-central"
IP_RANGE = "10.0.0.0/8"
IP_RANGE_SUBNET = "10.0.0.0/24"
DB_SERVER_NAME = "zpzc-s434686-db-script"
GITEA_SERVER_NAME = "zpzc-s434686-gitea-script"
LOCATION = "hel1"
VOLUME_NAME = "s434686-volume"
PROC = "cx11"
SERVER_SYS = "ubuntu-22.04"
print("Bound connection")
client = Client(token=f"{YOUR_API_TOKEN}")
print("Get location")
loc = client.locations.get_by_name(LOCATION)
print("Creating DB volume")
try:
volume = client.volumes.create(size=10, name=VOLUME_NAME, location=loc)
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_PRIV, ip_range=IP_RANGE)
print(f"network created: {network.name}")
except APIException:
network = client.networks.get_by_name(name=NETWORK_PRIV)
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=PROC),
image=Image(name=SERVER_SYS),
location=loc,
volumes=[Volume(id=volume.id)],
networks=[Network(id=network.id)],
2024-02-08 03:56:29 +01:00
public_net=ServerCreatePublicNetwork(enable_ipv4=False, enable_ipv6=False),
2024-02-05 15:09:27 +01:00
user_data="""
2024-02-08 03:56:29 +01:00
#cloud-config
2024-02-05 15:09:27 +01:00
package_update: true
package_upgrade: true
2024-02-08 03:56:29 +01:00
groups:
- docker
2024-02-08 05:14:35 +01:00
2024-02-08 03:56:29 +01:00
system_info:
default_user:
groups: [ docker ]
2024-02-08 05:14:35 +01:00
2024-02-08 03:56:29 +01:00
packages:
- apt-transport-https
- ca-certificates
- curl
- gnupg
- lsb-release
- unattended-upgrades
2024-02-08 05:14:35 +01:00
2024-02-05 15:09:27 +01:00
runcmd:
2024-02-08 03:56:29 +01:00
- 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
2024-02-08 05:14:35 +01:00
- git clone https://git.wmi.amu.edu.pl/s434686/zpzc-3.git /zpzc-3
- docker compose -f /zpzc-3/db.yml up -d
- echo 'Done'
2024-02-08 03:56:29 +01:00
bootcmd:
2024-02-08 05:14:35 +01:00
- docker compose -f /zpzc-3/db.yml up -d
final_message: "The system is finally up, after $UPTIME seconds"
"""
2024-02-05 15:09:27 +01:00
)
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=PROC),
image=Image(name=SERVER_SYS),
location=loc,
networks=[Network(id=network.id)],
user_data="""
2024-02-08 03:56:29 +01:00
#cloud-config
2024-02-05 15:09:27 +01:00
package_update: true
package_upgrade: true
2024-02-08 03:56:29 +01:00
groups:
- docker
2024-02-08 05:14:35 +01:00
2024-02-08 03:56:29 +01:00
system_info:
default_user:
groups: [ docker ]
2024-02-08 05:14:35 +01:00
2024-02-08 03:56:29 +01:00
packages:
- apt-transport-https
- ca-certificates
- curl
- gnupg
- lsb-release
- unattended-upgrades
2024-02-08 05:14:35 +01:00
2024-02-05 15:09:27 +01:00
runcmd:
2024-02-08 03:56:29 +01:00
- 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
2024-02-08 05:14:35 +01:00
- git clone https://git.wmi.amu.edu.pl/s434686/zpzc-3.git /zpzc-3
- docker compose -f /zpzc-3/gitea.yml up -d
- echo 'Done'
bootcmd:
- docker compose -f /zpzc-3/gitea.yml up -d
2024-02-08 03:56:29 +01:00
final_message: "The system is finally up, after $UPTIME seconds"
2024-02-08 05:14:35 +01:00
"""
2024-02-05 15:09:27 +01:00
)
2024-02-08 03:56:29 +01:00
_gitea_server = gitea_server.server
print(f"{_gitea_server.id=} {_gitea_server.name=} {_gitea_server.status=}, {gitea_server.root_password} ")
2024-02-05 15:09:27 +01:00
except APIException:
gitea_server = client.servers.get_by_name(name=DB_SERVER_NAME)
print(f"Server already exists: {gitea_server.name}")
2024-02-08 03:56:29 +01:00
print("done")