From beb4b29c49f280f8e0dc3e5e803722fab3d708f4 Mon Sep 17 00:00:00 2001 From: realaravinth Date: Sat, 25 Jun 2022 18:02:03 +0530 Subject: [PATCH] feat: pass template configuration, map VM sizes, generate secrets return gitea passwd, git pull before writing and push after add/rm --- infrastructure/utils.py | 62 +++++++++++++++++++++++++++++++++-------- 1 file changed, 51 insertions(+), 11 deletions(-) diff --git a/infrastructure/utils.py b/infrastructure/utils.py index 11612d6..69a6ed5 100644 --- a/infrastructure/utils.py +++ b/infrastructure/utils.py @@ -16,6 +16,7 @@ import os import shutil from pathlib import Path +from django.utils.crypto import get_random_string from django.template.loader import render_to_string from django.conf import settings from git import Repo @@ -31,6 +32,7 @@ class Infra: def __init__(self): conf = settings.HOSTEA["INFRA"]["HOSTEA_REPO"] + self.hostea_domain = settings.HOSTEA["INFRA"]["HOSTEA_DOMAIN"] self.repo_path = Path(conf["PATH"]) if not self.repo_path.exists(): os.makedirs(self.repo_path) @@ -92,9 +94,9 @@ class Infra: """ Add all relevant files of a VM """ - self.repo.index.add(self._host_vars_dir(subdomain=subdomain)) - self.repo.index.add(self._backup_path(subdomain=subdomain)) - self.repo.index.add(self._hostscript_path(subdomain=subdomain)) + self.repo.git.add(str(self._host_vars_dir(subdomain=subdomain))) + self.repo.git.add(str(self._backup_path(subdomain=subdomain))) + self.repo.git.add(str(self._hostscript_path(subdomain=subdomain))) def _commit(self, action: str, subdomain: str): """ @@ -103,13 +105,18 @@ class Infra: self._add_files(subdomain=subdomain) self.repo.git.commit( - f"{action} VM {subdomain}", author="bot@dashboard.hostea.org" + message=f"{action} VM {subdomain}", + author="Dashboard Bot ", ) - def add_vm(self, instance: Instance): + def add_vm(self, instance: Instance) -> str: """ Add new VM to infrastructure repository + + The gitea user password is returned """ + self.repo.git.pull() + subdomain = instance.name host_vars_dir = self._host_vars_dir(subdomain) @@ -120,9 +127,26 @@ class Infra: if not hostscript_path.exists(): os.makedirs(hostscript_path) + woodpecker_agent_secret = get_random_string(64) + gitea_password = get_random_string(20) + + ctx = { + "woodpecker_agent_secret": woodpecker_agent_secret, + "woodpecker_hostname": f"{subdomain}-ci", + "woodpecker_admins": f"{instance.owned_by.username}", + "gitea_email": instance.owned_by.email, + "gitea_password": gitea_password, + "subdomain": subdomain, + } + gitea = self._gitea_path(subdomain) - gitea_template = "./templates/infrastructure/yml/gitea.yml" - shutil.copy(gitea_template, gitea) + with open(gitea, "w", encoding="utf-8") as f: + f.write( + render_to_string( + "infrastructure/yml/gitea.yml", + context=ctx, + ) + ) # provision_template = "./templates/infrastructure/yml/provision.yml" provision = self._provision_path(subdomain) @@ -131,11 +155,21 @@ class Infra: # openstack_flavor: ‘{{ openstack_flavor_medium }}’ * openstack_flavor: ‘{{ openstack_flavor_large }}’ # ``` # check with @dachary about this + + size = None + if instance.configuration_id.name == "s1-2": + size = "openstack_flavor_small" + elif instance.configuration_id.name == "s1-4": + size = "openstack_flavor_medium" + elif instance.configuration_id.name == "s1-8": + size = "openstack_flavor_large" + else: + size = instance.configuration_id.name + with open(provision, "w", encoding="utf-8") as f: f.write( render_to_string( - "infrastructure/yml/provision.yml", - context={"vm_size": instance.instance_id.name}, + "infrastructure/yml/provision.yml", context={"vm_size": size} ) ) @@ -154,16 +188,20 @@ class Infra: self.write_hostscript( subdomain=subdomain, content=render_to_string( - "infrastructure/sh/create.sh", context={"subdomain": subdomain} + "infrastructure/sh/hostscripts/create.sh", + context={"subdomain": subdomain, "hostea_domain": self.hostea_domain}, ), ) self._commit(action="add", subdomain=subdomain) + self.repo.git.push() + return gitea_password def remove_vm(self, instance: Instance): """ Remove a VM from infrastructure repository """ + self.repo.git.pull() subdomain = instance.name host_vars_dir = self._host_vars_dir(subdomain) @@ -175,7 +213,9 @@ class Infra: self.write_hostscript( subdomain=subdomain, content=render_to_string( - "infrastructure/sh/rm.sh", context={"subdomain": subdomain} + "infrastructure/sh/hostscripts/create.sh", + context={"subdomain": subdomain, "hostea_domain": self.hostea_domain}, ), ) self._commit(action="rm", subdomain=subdomain) + self.repo.git.push()