Dodanie 'cloud-init.py'

This commit is contained in:
Kornelia Girejko 2023-01-08 23:17:02 +01:00
parent 77dfa1334f
commit 84dc798fe1
1 changed files with 59 additions and 0 deletions

59
cloud-init.py Normal file
View File

@ -0,0 +1,59 @@
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
from hcloud import Client
client = Client(
token="KccUEiddxtzGoLWSNC3V8tylq7MYHCjdnShtgasQ8jSbHqCjGoaa6Rq7yoz4uS23"
)
PREFIX = "478815"
YOUR_LOCAL_SSH_PUBKEY = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAICZpOz3Rrdlg0daWESWGqSvMNSCrlROYU/Yy5/f+cPbh comment"
vnet_name = f"{PREFIX}-network"
vnet = client.networks.get_by_name(vnet_name) or None
if not vnet:
vnet = client.networks.create(
name=vnet_name,
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})")
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 {ssh_key.data_model.name} został dodany: {ssh_key.data_model.public_key}")
cloud_init_vsc=r'''
write_files:
- content: |
curl -fsSL https://code-server.dev/install.sh > /root/install.sh
path: /root/install.sh
owner: root:root
permissions: '755'
runcmd:
- bash /root/install.sh
- code-server --bind-addr 0.0.0.0:8080
'''
vsc_server = client.servers.create(
name=f"{PREFIX}-vscode",
server_type=ServerType("cx11"),
image=Image(name="ubuntu-20.04"),
ssh_keys=[ssh_key],
networks=[vnet],
location=Location("hel1"),
user_data=cloud_init_vsc)
vsc_server.action.wait_until_finished()
print(f"Tworzenie serwera vscode: {vsc_server.action.complete}")
vsc_server = client.servers.get_by_name(f"{PREFIX}-vscode")
print(f"Serwer: {vsc_server.data_model.name}\n\tpubliczne IP: {vsc_server.data_model.public_net.ipv4.ip}\n\tprywatne IP: {vsc_server.data_model.private_net[0].ip}")
print(f"http://{vsc_server.data_model.public_net.ipv4.ip}:8080")