2021-11-27 13:44:57 +01:00
|
|
|
from hcloud import Client
|
|
|
|
from hcloud.networks.client import BoundNetwork
|
|
|
|
from hcloud.servers.client import BoundServer
|
|
|
|
from hcloud.ssh_keys.client import BoundSSHKey
|
|
|
|
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
|
|
|
|
from constants import *
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
with open(API_KEY_FNAME) as f:
|
|
|
|
api_key = f.read()
|
|
|
|
|
|
|
|
client = Client(api_key)
|
|
|
|
|
|
|
|
ssh_key = get_ssh_key(client)
|
|
|
|
vnet = get_vnet(client)
|
|
|
|
_ = create_db_server(client, vnet, ssh_key)
|
|
|
|
_ = create_gitea_server(client, vnet, ssh_key)
|
|
|
|
|
|
|
|
|
|
|
|
def get_ssh_key(client: Client) -> BoundSSHKey:
|
|
|
|
ssh_key = client.ssh_keys.get_by_name(SSH_KEY_NAME)
|
|
|
|
if ssh_key is None:
|
|
|
|
with open(SSH_KEY_FNAME) as f:
|
|
|
|
ssh_key_pub = f.read()
|
|
|
|
ssh_key = client.ssh_keys.create(
|
|
|
|
name=SSH_KEY_NAME, public_key=ssh_key_pub)
|
|
|
|
print(
|
|
|
|
f"Klucz SSH: {ssh_key.data_model.name}, {ssh_key.data_model.public_key}")
|
2021-11-27 14:00:32 +01:00
|
|
|
return ssh_key
|
2021-11-27 13:44:57 +01:00
|
|
|
|
|
|
|
|
|
|
|
def get_vnet(client: Client) -> BoundNetwork:
|
|
|
|
vnet = client.networks.get_by_name(VNET_NAME)
|
|
|
|
if vnet is None:
|
|
|
|
vnet = client.networks.create(
|
|
|
|
name=VNET_NAME,
|
|
|
|
ip_range=IP_RANGE,
|
|
|
|
subnets=[
|
|
|
|
NetworkSubnet(ip_range=IP_RANGE,
|
|
|
|
network_zone=NETWORK_ZONE, type=NETWORK_TYPE)
|
|
|
|
]
|
|
|
|
)
|
|
|
|
print(
|
|
|
|
f"Sieć wirtualna: {vnet.data_model.name} ({vnet.data_model.ip_range})")
|
2021-11-27 14:00:32 +01:00
|
|
|
return vnet
|
2021-11-27 13:44:57 +01:00
|
|
|
|
|
|
|
|
|
|
|
def create_db_server(client: Client, vnet: BoundNetwork, ssh_key: BoundSSHKey) -> BoundServer:
|
|
|
|
db_server = client.servers.get_by_name(DB_SERVER_NAME)
|
|
|
|
if db_server is not None:
|
|
|
|
print(
|
|
|
|
f"Serwer: {db_server.data_model.name}\n\tpubliczne IP: {db_server.data_model.public_net.ipv4.ip}\n\tprywatne IP: {db_server.data_model.private_net[0].ip}")
|
|
|
|
return db_server
|
2021-11-27 14:03:30 +01:00
|
|
|
# with open(CLOUD_INIT_DB_FNAME) as f:
|
|
|
|
# cloud_init_db = f.read()
|
|
|
|
|
|
|
|
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: |
|
|
|
|
version: '3.9'
|
|
|
|
services:
|
|
|
|
db:
|
|
|
|
image: mysql:5.7
|
|
|
|
restart: always
|
|
|
|
ports:
|
|
|
|
- "10.10.10.2:3306:3306"
|
|
|
|
environment:
|
|
|
|
MYSQL_ROOT_PASSWORD: gitea
|
|
|
|
MYSQL_DATABASE: gitea
|
|
|
|
MYSQL_USER: gitea
|
|
|
|
MYSQL_PASSWORD: gitea
|
|
|
|
volumes:
|
|
|
|
- db_data:/var/lib/mysql
|
|
|
|
phpmyadmin:
|
|
|
|
image: phpmyadmin
|
|
|
|
restart: always
|
|
|
|
ports:
|
|
|
|
- "8080:80"
|
|
|
|
|
|
|
|
volumes:
|
|
|
|
db_data: {}
|
|
|
|
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
|
|
|
|
'''
|
|
|
|
|
2021-11-27 13:44:57 +01:00
|
|
|
db_server_resp = client.servers.create(
|
2021-11-27 14:07:50 +01:00
|
|
|
name='s440054_3_2_db',
|
|
|
|
server_type=ServerType('cx11'),
|
|
|
|
image=Image(name='ubuntu-20.04'),
|
2021-11-27 13:44:57 +01:00
|
|
|
ssh_keys=[ssh_key],
|
|
|
|
networks=[vnet],
|
2021-11-27 14:07:50 +01:00
|
|
|
location=Location('hel1'),
|
2021-11-27 13:44:57 +01:00
|
|
|
user_data=cloud_init_db
|
|
|
|
)
|
|
|
|
|
|
|
|
db_server_resp.action.wait_until_finished()
|
|
|
|
print(f"Tworzenie serwera db: {db_server_resp.action.complete}")
|
|
|
|
|
|
|
|
db_server = client.servers.get_by_name(DB_SERVER_NAME)
|
|
|
|
print(
|
|
|
|
f"Serwer: {db_server.data_model.name}\n\tpubliczne IP: {db_server.data_model.public_net.ipv4.ip}\n\tprywatne IP: {db_server.data_model.private_net[0].ip}")
|
|
|
|
|
|
|
|
return db_server
|
|
|
|
|
|
|
|
|
|
|
|
def create_gitea_server(client: Client, vnet: BoundNetwork, ssh_key: BoundSSHKey) -> BoundServer:
|
|
|
|
gitea_server = client.servers.get_by_name(GITEA_SERVER_NAME)
|
|
|
|
if gitea_server is not None:
|
|
|
|
print(
|
|
|
|
f"Serwer: {gitea_server.data_model.name}\n\tpubliczne IP: {gitea_server.data_model.public_net.ipv4.ip}\n\tprywatne IP: {gitea_server.data_model.private_net[0].ip}")
|
|
|
|
return gitea_server
|
|
|
|
with open(CLOUD_INIT_GITEA_FNAME) as f:
|
|
|
|
cloud_init_gitea = f.read()
|
|
|
|
gitea_server_resp = client.servers.create(
|
|
|
|
name=GITEA_SERVER_NAME,
|
|
|
|
server_type=ServerType(MACHINE_TYPE),
|
|
|
|
image=Image(name=MACHINE_OS),
|
|
|
|
ssh_keys=[ssh_key],
|
|
|
|
networks=[vnet],
|
|
|
|
location=Location(MACHINE_LOCATION),
|
|
|
|
user_data=cloud_init_gitea
|
|
|
|
)
|
|
|
|
gitea_server_resp.action.wait_until_finished()
|
|
|
|
print(f"Tworzenie serwera gitea: {gitea_server_resp.action.complete}")
|
|
|
|
gitea_server = client.servers.get_by_name(GITEA_SERVER_NAME)
|
|
|
|
print(
|
|
|
|
f"Serwer: {gitea_server.data_model.name}\n\tpubliczne IP: {gitea_server.data_model.public_net.ipv4.ip}\n\tprywatne IP: {gitea_server.data_model.private_net[0].ip}")
|
|
|
|
return gitea_server
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|