From b123bfa58283dcdcbe81093f93d1568919465e46 Mon Sep 17 00:00:00 2001 From: realaravinth Date: Wed, 29 Jun 2022 00:27:47 +0530 Subject: [PATCH] feat: grab commit ID after add_vm execution --- infrastructure/management/commands/vm.py | 4 ++-- infrastructure/tests.py | 6 ++---- infrastructure/utils.py | 27 ++++++++++++------------ infrastructure/views.py | 2 +- 4 files changed, 18 insertions(+), 21 deletions(-) diff --git a/infrastructure/management/commands/vm.py b/infrastructure/management/commands/vm.py index 2af6f91..ed44ab2 100644 --- a/infrastructure/management/commands/vm.py +++ b/infrastructure/management/commands/vm.py @@ -93,7 +93,7 @@ class Command(BaseCommand): instance = create_instance( vm_name=vm_name, configuration_name=size, user=user ) - gitea_password = create_vm_if_not_exists(instance) + (gitea_password, _commit) = create_vm_if_not_exists(instance) print("Instance created") print(f"Gitea admin password: {gitea_password}") except VmException as e: @@ -104,7 +104,7 @@ class Command(BaseCommand): name=size ) instance.save() - gitea_password = create_vm_if_not_exists(instance) + (gitea_password, _commit) = create_vm_if_not_exists(instance) print("Instance created") print(f"Gitea admin password: {gitea_password}") diff --git a/infrastructure/tests.py b/infrastructure/tests.py index 1b00899..cb52910 100644 --- a/infrastructure/tests.py +++ b/infrastructure/tests.py @@ -118,7 +118,6 @@ class InfraUtilTest(TestCase): repo.git.pull() self.assertEqual(repo.head.commit.hexsha == after_rm, True) - @override_settings(HOSTEA=custom_config(test_name="test_cmd")) def test_cmd(self): subdomain = "cmd_vm" @@ -174,12 +173,11 @@ class InfraUtilTest(TestCase): # verify new size is updated in repository self.assertEqual( - str.strip(infra.translate_size(instance=instance)) == - str.strip(infra.get_flavor(instance=instance)), + str.strip(infra.translate_size(instance=instance)) + == str.strip(infra.get_flavor(instance=instance)), True, ) - call_command("vm", "delete", subdomain) out = stdout.getvalue() diff --git a/infrastructure/utils.py b/infrastructure/utils.py index fcbae95..bbb9f0a 100644 --- a/infrastructure/utils.py +++ b/infrastructure/utils.py @@ -20,7 +20,7 @@ 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 +from git import Repo, Commit from git.exc import InvalidGitRepositoryError from dash.models import Instance @@ -28,22 +28,22 @@ from dash.models import Instance from .models import InstanceCreated -def create_vm_if_not_exists(instance: Instance) -> str: +def create_vm_if_not_exists(instance: Instance) -> (str, Commit): """ Create VM utility. Gitea password is returned """ infra = Infra() if not InstanceCreated.objects.filter(instance=instance).exists(): - gitea_password = infra.add_vm(instance=instance) + (gitea_password, commit) = infra.add_vm(instance=instance) instance = InstanceCreated.objects.create(instance=instance, created=True) instance.save() return gitea_password else: - if str.strip(infra.get_flavor(instance=instance)) != str.strip(infra.translate_size( - instance=instance - )): - gitea_password = infra.add_vm(instance=instance) - return gitea_password + if str.strip(infra.get_flavor(instance=instance)) != str.strip( + infra.translate_size(instance=instance) + ): + return infra.add_vm(instance=instance) + return None def delete_vm(instance: Instance, owner: str): @@ -140,13 +140,13 @@ class Infra: self.repo.git.add(str(self._service_path(subdomain=subdomain))) self.repo.git.add(str(self._hostscript_path(subdomain=subdomain))) - def _commit(self, action: str, subdomain: str): + def _commit(self, action: str, subdomain: str) -> Commit: """ Commit changes to a VM configuration """ self._add_files(subdomain=subdomain) - self.repo.git.commit( + return self.repo.git.commit( message=f"{action} VM {subdomain}", author="Dashboard Bot ", ) @@ -164,7 +164,7 @@ class Infra: else: return instance.configuration_id.name - def add_vm(self, instance: Instance) -> str: + def add_vm(self, instance: Instance) -> (str, Commit): """ Add new VM to infrastructure repository @@ -212,7 +212,6 @@ class Infra: # ``` # check with @dachary about this - with open(provision, "w+", encoding="utf-8") as f: f.write( render_to_string( @@ -253,9 +252,9 @@ class Infra: ), ) - self._commit(action="add", subdomain=subdomain) + commit = self._commit(action="add", subdomain=subdomain) self.repo.git.push(env=self.env) - return gitea_password + return (gitea_password, commit) def remove_vm(self, instance: Instance): """ diff --git a/infrastructure/views.py b/infrastructure/views.py index a7823de..aaeb7f7 100644 --- a/infrastructure/views.py +++ b/infrastructure/views.py @@ -48,7 +48,7 @@ def create_instance(request, instance_name: str): if not payment_fullfilled(instance=instance): return redirect(reverse("billing.invoice.generate", args=(instance_name,))) - create_vm_if_not_exists(instance=instance) + (gitea_password, commit) = create_vm_if_not_exists(instance=instance) return HttpResponse()