diff --git a/dashboard/local_settings.ci.py b/dashboard/local_settings.ci.py index ffc5388..d171af4 100644 --- a/dashboard/local_settings.ci.py +++ b/dashboard/local_settings.ci.py @@ -76,7 +76,8 @@ HOSTEA = { "REMOTE": env.get_value("HOSTEA_INFRA_HOSTEA_REPO_REMOTE"), # SSH key that can push to the Git repository remote mentioned above "SSH_KEY": env.get_value("HOSTEA_INFRA_HOSTEA_REPO_SSH_KEY"), - } + }, + "HOSTEA_DOMAIN": "hostea.org", # domain at which Hostea VMs will be spun up }, } diff --git a/dashboard/local_settings.example.py b/dashboard/local_settings.example.py index 9736fd3..a5f9720 100644 --- a/dashboard/local_settings.example.py +++ b/dashboard/local_settings.example.py @@ -73,7 +73,8 @@ HOSTEA = { "REMOTE": "git@localhost:Hostea/enough.git", # SSH key that can push to the Git repository remote mentioned above "SSH_KEY": "/srv/hostea/deploy", - } + }, + "HOSTEA_DOMAIN": "", }, } diff --git a/dashboard/settings.py b/dashboard/settings.py index 7dada47..55eb1bb 100644 --- a/dashboard/settings.py +++ b/dashboard/settings.py @@ -189,7 +189,8 @@ HOSTEA = { "REMOTE": "git@localhost:Hostea/enough.git", # SSH key that can push to the Git repository remote mentioned above "SSH_KEY": "/srv/hostea/deploy", - } + }, + "HOSTEA_DOMAIN": "vm.hostea.org", # domain at which Hostea VMs will be spun up }, } diff --git a/infrastructure/utils.py b/infrastructure/utils.py index 049b2cf..68ef92d 100644 --- a/infrastructure/utils.py +++ b/infrastructure/utils.py @@ -84,13 +84,40 @@ class Infra: """ return self._host_vars_dir(subdomain=subdomain).joinpath("provision.yml") + @classmethod + def get_gitea_uri(cls, instance: Instance) -> str: + """ + Get an instance's Gitea URI + """ + base = settings.HOSTEA["INFRA"]["HOSTEA_DOMAIN"] + return f"https://{instance.name}.{base}" + + @classmethod + def _gen_woodpecker_hostname(cls, instance: Instance) -> str: + """ + Get Woodpecker hostname of an instance + """ + return (f"{instance.name}-ci",) + + @classmethod + def get_woodpecker_hostname(cls, instance: Instance) -> str: + """ + Get an instance's Gitea URI + """ + base = settings.HOSTEA["INFRA"]["HOSTEA_DOMAIN"] + return f"https://{cls._gen_woodpecker_hostname(instance=instance)}.{base}" + def get_flavor(self, instance: Instance): + """ + Get VM flavour/size/configuration from the fleet repository + """ subdomain = instance.name provision = self._provision_path(subdomain) with open(provision, "r", encoding="utf-8") as f: config = yaml.safe_load(f) if "openstack_flavor" in config: return config["openstack_flavor"].split("{{ ")[1].split(" }}")[0] + return None def _gitea_path(self, subdomain: str) -> Path: """ @@ -154,15 +181,19 @@ class Infra: def _pull(self): self.repo.git.pull(env=self.env, rebase="true") - def translate_size(self, instance: Instance) -> str: + @staticmethod + def translate_size(instance: Instance) -> str: + """ + Translate openstack(I think OVH-specific) sizes to enough.community + normalized sizes + """ if instance.configuration_id.name == "s1-2": return "openstack_flavor_small" - elif instance.configuration_id.name == "s1-4": + if instance.configuration_id.name == "s1-4": return "openstack_flavor_medium" - elif instance.configuration_id.name == "s1-8": + if instance.configuration_id.name == "s1-8": return "openstack_flavor_large" - else: - return instance.configuration_id.name + return instance.configuration_id.name def add_vm(self, instance: Instance) -> (str, Commit): """ @@ -187,7 +218,7 @@ class Infra: ctx = { "woodpecker_agent_secret": woodpecker_agent_secret, - "woodpecker_hostname": f"{subdomain}-ci", + "woodpecker_hostname": self._gen_woodpecker_hostname(instance=instance), "woodpecker_admins": f"{instance.owned_by.username}", "gitea_email": instance.owned_by.email, "gitea_password": gitea_password, @@ -203,14 +234,8 @@ class Infra: ) ) - # provision_template = "./templates/infrastructure/yml/provision.yml" size = self.translate_size(instance=instance) provision = self._provision_path(subdomain) - # TODO: instance config names are different the flavours expected: - # ``` - # openstack_flavor: ‘{{ openstack_flavor_medium }}’ * openstack_flavor: ‘{{ openstack_flavor_large }}’ - # ``` - # check with @dachary about this with open(provision, "w+", encoding="utf-8") as f: f.write( @@ -219,10 +244,7 @@ class Infra: ) ) - assert provision.exists() - # backup = self.repo_path.joinpath(f"inventory/{instance.name}-backup.yml") backup = self._backup_path(subdomain) - # backup_template = "./templates/infrastructure/yml/provision.yml" with open(backup, "w+", encoding="utf-8") as f: f.write( render_to_string( @@ -238,8 +260,6 @@ class Infra: ) ) - # hostscript = self.repo_path.join("inventory/hosts-scripts/{instance.name}-host.sh") - hostscript = self._hostscript_path(subdomain) with open(hostscript, "w+", encoding="utf-8") as f: f.write("\n") diff --git a/infrastructure/views.py b/infrastructure/views.py index ce5c03d..8d5c7ad 100644 --- a/infrastructure/views.py +++ b/infrastructure/views.py @@ -26,7 +26,7 @@ from accounts.decorators import confirm_access from dash.models import Instance from billing.utils import payment_fullfilled -from .utils import create_vm_if_not_exists +from .utils import create_vm_if_not_exists, Infra def default_ctx(title: str, username: str): @@ -71,6 +71,7 @@ docs or contact support!\n ctx = { "gitea_password": gitea_password, + "gitea_uri": Infra.get_gitea_uri(instance=instance), } return render(request, "infrastructure/html/create.html", ctx) return HttpResponse()