diff --git a/code-server/a.py b/code-server/a.py new file mode 100755 index 0000000..3777eba --- /dev/null +++ b/code-server/a.py @@ -0,0 +1,122 @@ +from hcloud import Client +from hcloud.locations.domain import Location +from hcloud.images.domain import Image +from hcloud.server_types.domain import ServerType +from hcloud.networks.domain import NetworkSubnet + +import sys + +if(len(sys.argv) > 1): + hetz_token = sys.argv[1] +else: + print("Hetzner token missing!") + +if(len(sys.argv) > 2): + ssh_public_key = " ".join([str(e) for e in sys.argv[2:5]]) +else: + print("SSH public key missing!") + +if(len(sys.argv) > 5): + index = sys.argv[5] +else: + index = "478874" + +client = Client( + token=hetz_token +) + +ssh_key_name = index+"-ssh-key" +network_name = index+"-subnet" +volume_name = index+"-vol" +db_name = index+"-db" +server_name = index+"-server" + +try: + ssh_key = client.ssh_keys.create(name=ssh_key_name, public_key=ssh_public_key) +except: + ssh_key = client.ssh_keys.get_by_name(name=ssh_key_name) +print(f"SSH key {ssh_key.data_model.name} added: {ssh_key.data_model.public_key}") + + +try: + vnet = client.networks.create( + name=network_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"Created network: {vnet.data_model.name} ({vnet.data_model.ip_range})") +except: + vnet = client.networks.get_by_name( + network_name, + ) + print(f"Network in use: {vnet.data_model.name} ({vnet.data_model.ip_range})") + + + +cloud_init_server=r"""#cloud-config +packages: + - apt-transport-https + - ca-certificates + - curl + - gnupg-agent + - software-properties-common +write_files: + - path: /root/docker-compose.yml + content: | + version: "2.1" + services: + code-server: + image: lscr.io/linuxserver/code-server:latest + container_name: code-server + environment: + - PUID=1000 + - PGID=1000 + - TZ=Europe/London + - SUDO_PASSWORD=123 + volumes: + - /path/to/appdata/config:/config + ports: + - 8443:8443 + restart: unless-stopped +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 + - apt-get install -y python3 python3-pip + - 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 +""" + +volume = client.volumes.create( + name=volume_name, + size=10, + format="ext4", + location=Location("hel1") +) + +if(volume.action.complete): + print(f"Created volume: {volume.volume.name}") + +code_server = client.servers.create( + name=server_name, + server_type=ServerType("cx11"), + image=Image(name="ubuntu-22.04"), + ssh_keys=[ssh_key], + networks=[vnet], + volumes= [volume.volume], + automount=True, + location=Location("hel1"), + user_data=cloud_init_server +) + +code_server.action.wait_until_finished() +if(code_server.action.complete): + print(f"Created code server: {code_server.server.name}") + +print(f"Code server up -> http://{code_server.server.data_model.public_net.ipv4.ip}:8443") \ No newline at end of file diff --git a/code-server/b.py b/code-server/b.py new file mode 100644 index 0000000..f75eff0 --- /dev/null +++ b/code-server/b.py @@ -0,0 +1,45 @@ +#!/usr/bin/env python3 +from hcloud import Client +from hcloud.images.domain import Image +from hcloud.networks.domain import NetworkSubnet +from hcloud.locations.domain import Location +from hcloud.server_types.domain import ServerType +import sys + +if(len(sys.argv) > 1): + index = sys.argv[1] +else: + index = "478874" + +client = Client( + token="KccUEiddxtzGoLWSNC3V8tylq7MYHCjdnShtgasQ8jSbHqCjGoaa6Rq7yoz4uS23" +) + +servers = client.servers.get_all() +print(f"Deleting servers") +for s in servers: + if s.data_model.name.startswith(index): + action = client.servers.delete(s) + action.wait_until_finished() + print(f"\tDeleting servers {s.data_model.name} ({s.data_model.public_net.ipv4.ip}): {action.data_model.status}") + +ssh_keys = client.ssh_keys.get_all() +print(f"Deleting SSH keys") +for s in ssh_keys: + if s.data_model.name.startswith(index): + action = client.ssh_keys.delete(s) + print(f"\tDeleting keys {s.name}: {action}") + +vnets = client.networks.get_all() +print(f"Deleting sub-networks") +for s in vnets: + if s.data_model.name.startswith(index): + action = client.networks.delete(s) + print(f"\tDeleting networks {s.name}: {action}") + +volumes = client.volumes.get_all() +print(f"Deleting volumes") +for v in volumes: + if v.data_model.name.startswith(index): + action = client.volumes.delete(v) + print(f"\tDeleting volumes {v.name}: {action}") diff --git a/code-server/clean.sh b/code-server/clean.sh new file mode 100755 index 0000000..fc6d275 --- /dev/null +++ b/code-server/clean.sh @@ -0,0 +1,3 @@ +#!/bin/bash +echo "Clearing leftovers..." +python3 b.py \ No newline at end of file diff --git a/code-server/deploy.sh b/code-server/deploy.sh new file mode 100755 index 0000000..a4640bd --- /dev/null +++ b/code-server/deploy.sh @@ -0,0 +1,8 @@ +#!/bin/bash +echo "Creating virtual enviroment ..." +python3 -m venv hetz_gitea_venv +echo "Installing dependencies ..." +pip install -q hcloud +echo "Initiating gitea cloud deploy ..." +chmod +x a.py +python3 a.py $(cat ../token_file.txt) $(find ~ -name *.pub | tail -n 1 | xargs cat) "478874" \ No newline at end of file diff --git a/code-server/readme.md b/code-server/readme.md new file mode 100644 index 0000000..e69de29