From f1aaa9ead1cf423b2608b3ed488bf680d1648ac8 Mon Sep 17 00:00:00 2001 From: bra-fsn Date: Wed, 25 Sep 2024 09:38:26 +0200 Subject: [PATCH] hcloud --- pyproject.toml | 3 ++- src/sc_runner/resources/hcloud.py | 44 +++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 src/sc_runner/resources/hcloud.py diff --git a/pyproject.toml b/pyproject.toml index b98ded5..97cdb31 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta" [project] name = "sparecores-runner" -version = "0.0.27" +version = "0.0.28" requires-python = ">= 3.9" dependencies = [ "click", @@ -12,6 +12,7 @@ dependencies = [ "pulumi-aws", "pulumi-azure-native", "pulumi-gcp", + "pulumi-hcloud", "requests", "sentry-sdk", "sparecores-crawler>=0.2.1", diff --git a/src/sc_runner/resources/hcloud.py b/src/sc_runner/resources/hcloud.py new file mode 100644 index 0000000..a75d406 --- /dev/null +++ b/src/sc_runner/resources/hcloud.py @@ -0,0 +1,44 @@ +import base64 + +from .. import DefaultOpt, JSON +from .. import data +from .base import StackName, default, defaults +from typing import Annotated +import click +import copy +import os +import pulumi +import pulumi_hcloud as hcloud + + +DEFAULTS = { + "instance_opts": ("HCLOUD_INSTANCE_OPTS", dict(labels={"created-by": "sc-runner"})), +} + +def resources_hcloud( + region: Annotated[str, DefaultOpt(["--region"], type=click.Choice(data.regions("hcloud")), help="Region"), StackName()] = os.environ.get("HCLOUD_REGION", "fsn1-dc14"), + instance: Annotated[str, DefaultOpt(["--instance"], type=click.Choice(data.servers("hcloud")), help="Instance type"), StackName()] = os.environ.get("INSTANCE_TYPE", "cx22"), + public_key: Annotated[str, DefaultOpt(["--public-key"], type=str, help="SSH public key")] = os.environ.get("SSH_PUBLIC_KEY", ""), + instance_opts: Annotated[str, DefaultOpt(["--instance-opts"], type=JSON, default=defaults(DEFAULTS, "instance_opts"), help="Pulumi hcloud.Server options")] = default(DEFAULTS, "instance_opts"), + user_data: Annotated[str | None, DefaultOpt(["--user-data"], type=str, help="Base64 encoded string with user_data script to run at boot")] = os.environ.get("USER_DATA", None), +): + # we don't want to modify the default + instance_opts = copy.deepcopy(instance_opts) + + if public_key and "ssh_keys" not in instance_opts: + ssh_key = hcloud.SshKey( + instance, + name=instance, + public_key=public_key + ) + instance_opts["ssh_keys"] = [ssh_key.id] + + hcloud.Server( + instance, + name=instance, + image="ubuntu-24.04", + server_type=instance, + datacenter=region, + user_data=user_data, + **instance_opts, + ) \ No newline at end of file