cloud/IasS/ex4/vscode/cloud-init.ipynb

142 lines
4.1 KiB
Plaintext
Raw Normal View History

2022-12-30 14:12:01 +01:00
{
"cells": [
{
"cell_type": "code",
2023-01-22 18:59:39 +01:00
"execution_count": 7,
2022-12-30 14:12:01 +01:00
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Key s444498-pzc-ssh-key-2 already in use: ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIAj15oTbXA0KGBw5V/76KXDgJZpLNrxUO/rTA0u0eRl1 wirus@wirusowylapek\n"
]
}
],
"source": [
"from hcloud import Client\n",
"client = Client(\n",
" token=\"KccUEiddxtzGoLWSNC3V8tylq7MYHCjdnShtgasQ8jSbHqCjGoaa6Rq7yoz4uS23\"\n",
")\n",
"PREFIX = \"s444498\"\n",
"YOUR_LOCAL_SSH_PUBKEY = \"ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIAj15oTbXA0KGBw5V/76KXDgJZpLNrxUO/rTA0u0eRl1 wirus@wirusowylapek\"\n",
"try:\n",
" ssh_key = client.ssh_keys.create(name=f\"{PREFIX}-pzc-ssh-key-2\", public_key=YOUR_LOCAL_SSH_PUBKEY)\n",
" print(f\"Key {ssh_key.data_model.name} created: {ssh_key.data_model.public_key}\")\n",
"except:\n",
" ssh_key = client.ssh_keys.get_by_name(f\"{PREFIX}-pzc-ssh-key-2\")\n",
" print(f\"Key {ssh_key.data_model.name} already in use: {ssh_key.data_model.public_key}\")"
]
},
{
"cell_type": "code",
2023-01-22 18:59:39 +01:00
"execution_count": 8,
2022-12-30 14:12:01 +01:00
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Network in use: s444498-pzc-test-vnet (10.10.10.0/24)\n"
]
}
],
"source": [
"from hcloud.networks.domain import NetworkSubnet\n",
"\n",
"try:\n",
" vnet = client.networks.create(\n",
" name=f\"{PREFIX}-pzc-test-vnet\", \n",
" ip_range=\"10.10.10.0/24\", \n",
" subnets=[\n",
" NetworkSubnet(ip_range=\"10.10.10.0/24\", network_zone=\"eu-central\", type=\"cloud\")\n",
" ]\n",
" )\n",
" print(f\"Created network: {vnet.data_model.name} ({vnet.data_model.ip_range})\")\n",
"except:\n",
" vnet = client.networks.get_by_name(\n",
" f\"{PREFIX}-pzc-test-vnet\", \n",
" )\n",
" print(f\"Network in use: {vnet.data_model.name} ({vnet.data_model.ip_range})\")"
]
},
{
"cell_type": "code",
2023-01-22 18:59:39 +01:00
"execution_count": 9,
2022-12-30 14:12:01 +01:00
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
2023-01-22 18:59:39 +01:00
"Creating vscode server: True\n"
2022-12-30 14:12:01 +01:00
]
}
],
"source": [
"from hcloud.locations.domain import Location\n",
"from hcloud.images.domain import Image\n",
"from hcloud.server_types.domain import ServerType\n",
"\n",
2022-12-30 15:58:47 +01:00
"cloud_init_vscode=r'''#cloud-config\n",
2022-12-30 14:12:01 +01:00
"packages:\n",
" - apt-transport-https\n",
" - ca-certificates\n",
" - curl\n",
" - gnupg-agent\n",
" - software-properties-common\n",
"write_files:\n",
2022-12-30 15:58:47 +01:00
" - content: |\n",
2023-01-22 18:59:39 +01:00
" curl -fsSL https://code-server.dev/install.sh > /root/install.sh\n",
" path: /root/install.sh\n",
" owner: root:root\n",
" permissions: '755'\n",
2022-12-30 14:12:01 +01:00
"runcmd:\n",
2023-01-22 18:59:39 +01:00
" - bash install.sh\n",
2022-12-30 15:58:47 +01:00
" - code-server --bind-addr 0.0.0.0:8080\n",
2022-12-30 14:12:01 +01:00
" '''\n",
"\n",
2022-12-30 15:58:47 +01:00
"vscode_server = client.servers.create(\n",
" name=f\"{PREFIX}-vscode\", \n",
2022-12-30 14:12:01 +01:00
" server_type=ServerType(\"cx11\"), \n",
" image=Image(name=\"ubuntu-20.04\"), \n",
" ssh_keys=[ssh_key], \n",
" networks=[vnet], \n",
" location=Location(\"hel1\"), \n",
2022-12-30 15:58:47 +01:00
" user_data=cloud_init_vscode\n",
2022-12-30 14:12:01 +01:00
")\n",
"\n",
2022-12-30 15:58:47 +01:00
"vscode_server.action.wait_until_finished()\n",
2023-01-22 18:59:39 +01:00
"print(f\"Creating vscode server: {vscode_server.action.complete}\")"
2022-12-30 14:12:01 +01:00
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3.10.8 64-bit",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.8"
},
"orig_nbformat": 4,
"vscode": {
"interpreter": {
"hash": "767d51c1340bd893661ea55ea3124f6de3c7a262a8b4abca0554b478b1e2ff90"
}
}
},
"nbformat": 4,
"nbformat_minor": 2
}