169 lines
5.0 KiB
Python
169 lines
5.0 KiB
Python
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 time import sleep
|
|
|
|
|
|
YOUR_API_TOKEN = "qOpoQdAg5VxZVpNONWbLbJO3xgGqSvkjm4kQV9H1RlnDhL2rI2P8CbII3hl2nyYb"
|
|
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)],
|
|
user_data="""
|
|
#cloud-config
|
|
users:
|
|
- name: holu
|
|
groups: users, admin
|
|
sudo: ALL=(ALL) NOPASSWD:ALL
|
|
shell: /bin/bash
|
|
packages:
|
|
- fail2ban
|
|
- ufw
|
|
package_update: true
|
|
package_upgrade: true
|
|
runcmd:
|
|
- curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker.gpg
|
|
- chmod a+r /usr/share/keyrings/docker.gpg
|
|
- echo \
|
|
"deb [arch="$(dpkg --print-architecture)" signed-by=/usr/share/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \
|
|
$(. /etc/os-release && echo "${UBUNTU_CODENAME:-$VERSION_CODENAME}") stable" | \
|
|
sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
|
|
- aptitude update
|
|
- aptitude install docker-ce docker-ce-cli containerd.io
|
|
- groupadd docker
|
|
- usermod -aG docker $USER
|
|
- newgrp docker
|
|
- curl -L "https://github.com/docker/compose/releases/latest/download/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
|
|
- chmod +x /usr/local/bin/docker-compose
|
|
- git clone https://git.wmi.amu.edu.pl/s434686/zpzc-3
|
|
- cd zpzc-3
|
|
- systemctl enable docker
|
|
- docker-compose -f db.yml up -d
|
|
""",
|
|
)
|
|
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="""
|
|
#cloud-config
|
|
users:
|
|
- name: holu
|
|
groups: users, admin
|
|
sudo: ALL=(ALL) NOPASSWD:ALL
|
|
shell: /bin/bash
|
|
packages:
|
|
- fail2ban
|
|
- ufw
|
|
package_update: true
|
|
package_upgrade: true
|
|
runcmd:
|
|
- curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker.gpg
|
|
- chmod a+r /usr/share/keyrings/docker.gpg
|
|
- echo \
|
|
"deb [arch="$(dpkg --print-architecture)" signed-by=/usr/share/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \
|
|
$(. /etc/os-release && echo "${UBUNTU_CODENAME:-$VERSION_CODENAME}") stable" | \
|
|
sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
|
|
- aptitude update
|
|
- aptitude install docker-ce docker-ce-cli containerd.io
|
|
- groupadd docker
|
|
- usermod -aG docker $USER
|
|
- newgrp docker
|
|
- curl -L "https://github.com/docker/compose/releases/latest/download/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
|
|
- chmod +x /usr/local/bin/docker-compose
|
|
- git clone https://git.wmi.amu.edu.pl/s434686/zpzc-3
|
|
- cd zpzc-3
|
|
- systemctl enable docker
|
|
- docker-compose -f gitea.yml up -d
|
|
""",
|
|
)
|
|
gitea_server = response.server
|
|
print(f"{gitea_server.id=} {gitea_server.name=} {gitea_server.status=}")
|
|
except APIException:
|
|
gitea_server = client.servers.get_by_name(name=DB_SERVER_NAME)
|
|
print(f"Server already exists: {gitea_server.name}")
|
|
|
|
|
|
sleep(300)
|
|
|
|
|
|
print("Turn off Public IP")
|
|
db_server.power_off()
|
|
|
|
sleep(15)
|
|
|
|
db_server.public_net.primary_ipv6.delete()
|
|
db_server.public_net.primary_ipv4.delete()
|
|
|
|
sleep(10)
|
|
|
|
db_server.power_on()
|