Update 'hertzner.py'

This commit is contained in:
Patryk Gałka 2024-02-08 03:56:29 +01:00
parent 6cf02cae77
commit 415abc7c8b

View File

@ -4,6 +4,8 @@ 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 time import sleep
@ -67,37 +69,44 @@ try:
location=loc,
volumes=[Volume(id=volume.id)],
networks=[Network(id=network.id)],
public_net=ServerCreatePublicNetwork(enable_ipv4=False, enable_ipv6=False),
user_data="""
#cloud-config
users:
- name: holu
groups: users, admin
sudo: ALL=(ALL) NOPASSWD:ALL
shell: /bin/bash
packages:
- fail2ban
- ufw
#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:
- 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
- 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
- docker-compose -f db.yml up -d
""",
- systemctl start docker
- git clone https://git.wmi.amu.edu.pl/s434686/zpzc-3
final_message: "The system is finally up, after $UPTIME seconds"
bootcmd:
- cd zpzc-3
- docker compose -f db.yml up -d
"""
)
db_server = response.server
print(f"{db_server.id=} {db_server.name=} {db_server.status=}")
@ -114,55 +123,49 @@ try:
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
#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:
- 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
- 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/s434686/zpzc-3
- cd zpzc-3
- systemctl enable docker
- docker-compose -f gitea.yml up -d
- docker compose -f db.yml up -d
final_message: "The system is finally up, after $UPTIME seconds"
bootcmd:
- cd zpzc-3
- docker compose -f db.yml up -d
""",
)
gitea_server = response.server
print(f"{gitea_server.id=} {gitea_server.name=} {gitea_server.status=}")
_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}")
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()
print("done")