172 lines
4.9 KiB
Python
172 lines
4.9 KiB
Python
import sys
|
|
from hcloud import Client
|
|
from hcloud.networks.domain import NetworkSubnet
|
|
from hcloud.locations.domain import Location
|
|
from hcloud.images.domain import Image
|
|
from hcloud.server_types.domain import ServerType
|
|
|
|
token = ""
|
|
|
|
try:
|
|
token = sys.argv[1]
|
|
except Exception as ex:
|
|
print('ERROR: Pass valid token as an argument')
|
|
|
|
|
|
client = Client(
|
|
token=token
|
|
)
|
|
|
|
ssh_key = client.ssh_keys.get_by_name(name="wojjar3@st.amu.edu.pl")
|
|
|
|
vnet = client.networks.get_by_name(name="s434704-automate-gitea-network")
|
|
|
|
if not vnet:
|
|
vnet = client.networks.create(
|
|
name="s434704-automate-gitea-network",
|
|
ip_range="10.10.10.0/24",
|
|
subnets=[
|
|
NetworkSubnet(ip_range="10.10.10.0/24", network_zone="eu-central", type="cloud")
|
|
]
|
|
)
|
|
print(f"Utworzono sieć wirtualną {vnet.data_model.name} ({vnet.data_model.ip_range})")
|
|
|
|
|
|
cloud_init_db=r'''#cloud-config
|
|
|
|
packages:
|
|
- apt-transport-https
|
|
- ca-certificates
|
|
- curl
|
|
- gnupg-agent
|
|
- software-properties-common
|
|
|
|
write_files:
|
|
- path: /root/docker-compose.yml
|
|
content: |
|
|
services:
|
|
db:
|
|
image: mysql:8
|
|
restart: always
|
|
ports:
|
|
- "10.10.10.2:3306:3306"
|
|
environment:
|
|
- MYSQL_ROOT_PASSWORD=gitea
|
|
- MYSQL_USER=gitea
|
|
- MYSQL_PASSWORD=gitea
|
|
- MYSQL_DATABASE=gitea
|
|
volumes:
|
|
- db_data:/var/lib/mysql
|
|
volumes:
|
|
db_data: {}
|
|
|
|
# instalujemy docker, docker-compose a następnie uruchamiamy naszą bazę danych
|
|
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 = client.servers.create(
|
|
name="s434704-db-gitea-auto",
|
|
server_type=ServerType("cx11"),
|
|
image=Image(name="ubuntu-20.04"),
|
|
ssh_keys=[ssh_key],
|
|
networks=[vnet],
|
|
location=Location("hel1"),
|
|
user_data=cloud_init_db,
|
|
)
|
|
|
|
db_server.action.wait_until_finished()
|
|
print(f"Utworzono serwer db: {db_server.action.complete}", f"{db_server.server.data_model.name} ({db_server.server.data_model.public_net.ipv4.ip})")
|
|
|
|
|
|
db_server = client.servers.get_by_name("s434704-db-gitea-auto")
|
|
|
|
db_private_ip = db_server.data_model.private_net[0].ip
|
|
|
|
cloud_init_gitea=rf'''#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:
|
|
server:
|
|
image: gitea/gitea:1.15.6-rootless
|
|
environment:
|
|
- GITEA__database__DB_TYPE=mysql
|
|
- GITEA__database__HOST={db_private_ip}:3306
|
|
- GITEA__database__NAME=gitea
|
|
- GITEA__database__USER=gitea
|
|
- GITEA__database__PASSWD=gitea
|
|
restart: always
|
|
volumes:
|
|
- gitea_data:/var/lib/gitea
|
|
- gitea_config:/etc/gitea
|
|
- gitea_timezone:/etc/timezone:ro
|
|
- gitea_localtime:/etc/localtime:ro
|
|
ports:
|
|
- "3000:3000"
|
|
- "222:22"
|
|
volumes:
|
|
gitea_data: {{}}
|
|
gitea_config: {{}}
|
|
gitea_timezone: {{}}
|
|
gitea_localtime: {{}}
|
|
|
|
# instalujemy docker, docker-compose a następnie uruchamiamy naszą bazę danych
|
|
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
|
|
'''
|
|
|
|
gitea_server = client.servers.create(
|
|
name="s434704-gitea-auto",
|
|
server_type=ServerType("cx11"),
|
|
image=Image(name="ubuntu-20.04"),
|
|
ssh_keys=[ssh_key],
|
|
networks=[vnet],
|
|
location=Location("hel1"),
|
|
user_data=cloud_init_gitea
|
|
)
|
|
|
|
gitea_server.action.wait_until_finished()
|
|
|
|
volume = client.volumes.get_by_name(name="s434704-gitea-volume")
|
|
|
|
if volume:
|
|
volume.delete()
|
|
|
|
volume = client.volumes.create(
|
|
size=10,
|
|
name="s434704-gitea-volume",
|
|
server=gitea_server.server
|
|
)
|
|
|
|
client.servers.power_on(gitea_server.server)
|
|
|
|
print(f"Utworzono serwer gitea: {gitea_server.action.complete}", f"{gitea_server.server.data_model.name} ({gitea_server.server.data_model.public_net.ipv4.ip})")
|
|
|
|
|