Finish create_servers.py

This commit is contained in:
PawelDopierala 2024-11-26 17:41:54 +01:00
parent b2ee7e3590
commit 8b0322cd22

View File

@ -3,6 +3,8 @@ from hcloud.images.domain import Image
from hcloud.server_types.domain import ServerType
from hcloud.locations.domain import Location
servers_no = 2
TOKEN = "V5gkzZ13coCVPKWkQbmbyGPyxDdsTjiubwVtx35jH7mix8A32JqM5CWJtqoLjtFK"
client = Client(
@ -13,12 +15,17 @@ PREFIX = "PD"
YOUR_LOCAL_SSH_PUBKEY = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCrv0JDOSvGT+UUZBVW/pJyP4LO2Myof+ZQtYkko1fPVEK0w+QnQmnWCCsbtHuYM1SnlhGxCHG0iMhAHpWO5vIF119Ia5aV2q/weQy67isO3puHgtEFGXBabp+qIJUHh9pfR+2mVYl5cze1SrqfFj/B4axoPDjRo10mnQfwXxIrqPpA98XOkea0SUky1S9nSSt6V+OSaInvy0xSigoAp1L8CJRTyclAbub0EiS8S8rEi0/5Wa77TgRmiVfyXE5ZG4VmcOgpvt0FHMCMkTC6VaC4vPBmamMho5zceRYn1/yPyHofFv6NSfHqDfCBO64e6K0XlP7lMegfh5mtzzaz2PPP"
ssh_key = client.ssh_keys.create(name=f"{PREFIX}-pzc-ssh-key", public_key=YOUR_LOCAL_SSH_PUBKEY)
print(f"Klucz {ssh_key.data_model.name} został dodany: {ssh_key.data_model.public_key}")
cloud_init=r'''#cloud-config
packages:
- fail2ban
- ufw
- git
- python3
- python3-pip
package_update: true
package_upgrade: true
runcmd:
- sudo apt update
- sudo apt install -y git python3 python3-pip
- git clone https://git.wmi.amu.edu.pl/s495719/cloud_3_3
- cd cloud_3_3
- chmod +x computeC
@ -26,17 +33,28 @@ runcmd:
- python3 main.py
'''
db_server = client.servers.create(
name=f"{PREFIX}-db",
server_type=ServerType("cpx11"),
image=Image(name="ubuntu-22.04"),
ssh_keys=[ssh_key],
location=Location("hel1"),
user_data=cloud_init,
)
servers = []
for i in range(servers_no):
server = client.servers.create(
name=f"{PREFIX}-{i}-3",
server_type=ServerType("cpx11"),
image=Image(name="ubuntu-22.04"),
location=Location("hel1"),
user_data=cloud_init,
)
servers.append(server)
db_server.action.wait_until_finished()
print(f"Tworzenie serwera db: {db_server.action.complete}")
db_server = client.servers.get_by_name(f"{PREFIX}-db")
print(f"Serwer: {db_server.data_model.name}\n\tpubliczne IP: {db_server.data_model.public_net.ipv4.ip}")
for i in range(servers_no):
servers[i].action.wait_until_finished()
ip_list = []
for i in range(servers_no):
server = client.servers.get_by_name(f"{PREFIX}-{i}-3")
ip_list.append(server.data_model.public_net.ipv4.ip)
with open('my_list.txt', 'w') as file:
for item in ip_list:
file.write(item + '\n')
print("Finished")