cloud_3_3/create_servers.py
PawelDopierala edcfb81f61 Update
2024-11-26 20:48:17 +01:00

61 lines
1.7 KiB
Python

from hcloud import Client
from hcloud.images.domain import Image
from hcloud.server_types.domain import ServerType
from hcloud.locations.domain import Location
servers_no = 16
TOKEN = "V5gkzZ13coCVPKWkQbmbyGPyxDdsTjiubwVtx35jH7mix8A32JqM5CWJtqoLjtFK"
client = Client(
token=TOKEN
)
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)
cloud_init=r'''#cloud-config
packages:
- fail2ban
- ufw
- git
- python3
- python3-pip
package_update: true
package_upgrade: true
runcmd:
- git clone https://git.wmi.amu.edu.pl/s495719/cloud_3_3
- cd cloud_3_3
- chmod +x computeC
- pip install -r requirements.txt
- python3 main.py
'''
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)
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")