160 lines
5.5 KiB
Python
160 lines
5.5 KiB
Python
from hcloud import Client
|
|
|
|
from hcloud.images.domain import Image
|
|
from hcloud.server_types.domain import ServerType
|
|
|
|
from hcloud.networks.domain import NetworkSubnet
|
|
|
|
from hcloud.locations.domain import Location
|
|
|
|
api_token = ""
|
|
|
|
with open("token.txt", "r") as file:
|
|
api_token = file.read().strip()
|
|
|
|
client = Client(token=api_token)
|
|
|
|
PREFIX = "444465"
|
|
|
|
YOUR_LOCAL_SSH_PUBKEY = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQC7JxVZ/BY1lfvAyUPysBBhq5SDs+48HEySg+aZ8dvjnnMc1kV8ENOgYpBmih1FI/Gmvkhw/8tHUH9AgGQrKQ3s0EKi7uqycDQnvstQH1y9YhuvyveXK0VR2v4MvVZXj6vD1ayqAZ2ojytmwWc4Cz+BBF+evTisGtjhy3iNmumi67le6u6tDJz1j4/KVxZPusG970pCU9UKR/rFOq3GE95L8vZeMPZ0Y5bTL16QiWWLX4gyaYnMQCij/kOxIeSfv9wnJFZ/JYUIssKOEHOaqVb3mNd50+eSGN+zJzLIy6WZNiois6KYuoBlkyJriZYYHiEKkVJX5dtraGJfF8a/M5U0zJ02kS+mwHmcTdXI2LQGLOvRFvkNsckRHwJaFNK/8L/xaYQnM8xPzVgC8QIntRJAIUmpEbInuqy0jgZ1b6K1sN2IHD6s0g8LkmgKxnr07LxyyXnp4u6sRuuex9ylMP3P3aAo5Iwg69QK+oz4pCXD+pLi4MhR01ri63Bz2QYCJp8= andrzej@andrzej-VivoBook-ASUSLaptop-X515JA-F515JA"
|
|
|
|
vnet_name = f"{PREFIX}-pzc-test-vnet"
|
|
vnet = client.networks.get_by_name(vnet_name) or None
|
|
if not vnet:
|
|
vnet = client.networks.create(
|
|
name=vnet_name,
|
|
ip_range="10.0.0.0/16",
|
|
subnets=[
|
|
NetworkSubnet(ip_range="10.0.0.0/16", network_zone="eu-central", type="cloud")
|
|
]
|
|
)
|
|
|
|
print(f"Sieć wirtualna utworzona: {vnet.data_model.name}")
|
|
|
|
ssh_name = f"{PREFIX}-ssh-key"
|
|
ssh_key = client.ssh_keys.get_by_name(ssh_name) or None
|
|
if not ssh_key:
|
|
ssh_key = client.ssh_keys.create(name=ssh_name, public_key=YOUR_LOCAL_SSH_PUBKEY)
|
|
|
|
print(f"Klucz dodany: {ssh_key.data_model.name}")
|
|
|
|
cloud_init_db=r'''
|
|
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.0.0.2:3306:3306"
|
|
environment:
|
|
MYSQL_ROOT_PASSWORD: chuj123456
|
|
MYSQL_DATABASE: gitea
|
|
MYSQL_USER: root
|
|
MYSQL_PASSWORD: chuj123456
|
|
volumes:
|
|
- db_data:/var/lib/mysql
|
|
phpmyadmin:
|
|
image: phpmyadmin
|
|
restart: always
|
|
# przekierowanie portów zostawione tylko dla przykładu, nie należy tak robić na produkcji
|
|
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
|
|
'''
|
|
|
|
db_server = client.servers.create(
|
|
name=f"{PREFIX}-db",
|
|
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"Serwer mysql tworzenie: {db_server.action.complete}")
|
|
|
|
db_server = client.servers.get_by_name(f"{PREFIX}-db")
|
|
print(f"Server: {db_server.data_model.name}\n\tprivate IP: {db_server.data_model.private_net[0].ip}")
|
|
|
|
cloud_init_gitea=r'''
|
|
packages:
|
|
- apt-transport-https
|
|
- ca-certificates
|
|
- curl
|
|
- gnupg-agent
|
|
- software-properties-common
|
|
# tworzymy plik docker-compose.yml
|
|
write_files:
|
|
- path: /root/docker-compose.yml
|
|
content: |
|
|
version: '3.9'
|
|
services:
|
|
server:
|
|
image: gitea/gitea:latest
|
|
environment:
|
|
GITEA_database_DB_TYPE: mysql
|
|
GITEA_database_HOST: 10.0.0.2:3306
|
|
GITEA_database_NAME: gitea
|
|
GITEA_database_USER: root
|
|
GITEA_database_PASSWD: chuj123456
|
|
restart: always
|
|
volumes:
|
|
- ./data:/root/gitea
|
|
- ./config:/root/gitea/config
|
|
- /etc/timezone:/etc/timezone:ro
|
|
- /etc/localtime:/etc/localtime:ro
|
|
- /mnt/volume:/data
|
|
ports:
|
|
- "3000:3000"
|
|
- "222:22"
|
|
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=f"{PREFIX}-gitea",
|
|
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()
|
|
print(f"Tworzenie serwera gitea: {gitea_server.action.complete}")
|
|
|
|
gitea_server = client.servers.get_by_name(f"{PREFIX}-gitea")
|
|
print(f"Server: {gitea_server.data_model.name}\n\tpublic IP: {gitea_server.data_model.public_net.ipv4.ip}\n\tprivate IP: {gitea_server.data_model.private_net[0].ip}")
|
|
|