feat: grab commit ID after add_vm execution
ci/woodpecker/push/woodpecker Pipeline was successful Details

wip-hostea-domain
Aravinth Manivannan 2022-06-29 00:27:47 +05:30
parent 5ec87c83ec
commit b123bfa582
Signed by: realaravinth
GPG Key ID: AD9F0F08E855ED88
4 changed files with 18 additions and 21 deletions

View File

@ -93,7 +93,7 @@ class Command(BaseCommand):
instance = create_instance( instance = create_instance(
vm_name=vm_name, configuration_name=size, user=user 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("Instance created")
print(f"Gitea admin password: {gitea_password}") print(f"Gitea admin password: {gitea_password}")
except VmException as e: except VmException as e:
@ -104,7 +104,7 @@ class Command(BaseCommand):
name=size name=size
) )
instance.save() instance.save()
gitea_password = create_vm_if_not_exists(instance) (gitea_password, _commit) = create_vm_if_not_exists(instance)
print("Instance created") print("Instance created")
print(f"Gitea admin password: {gitea_password}") print(f"Gitea admin password: {gitea_password}")

View File

@ -118,7 +118,6 @@ class InfraUtilTest(TestCase):
repo.git.pull() repo.git.pull()
self.assertEqual(repo.head.commit.hexsha == after_rm, True) self.assertEqual(repo.head.commit.hexsha == after_rm, True)
@override_settings(HOSTEA=custom_config(test_name="test_cmd")) @override_settings(HOSTEA=custom_config(test_name="test_cmd"))
def test_cmd(self): def test_cmd(self):
subdomain = "cmd_vm" subdomain = "cmd_vm"
@ -174,12 +173,11 @@ class InfraUtilTest(TestCase):
# verify new size is updated in repository # verify new size is updated in repository
self.assertEqual( self.assertEqual(
str.strip(infra.translate_size(instance=instance)) == str.strip(infra.translate_size(instance=instance))
str.strip(infra.get_flavor(instance=instance)), == str.strip(infra.get_flavor(instance=instance)),
True, True,
) )
call_command("vm", "delete", subdomain) call_command("vm", "delete", subdomain)
out = stdout.getvalue() out = stdout.getvalue()

View File

@ -20,7 +20,7 @@ from pathlib import Path
from django.utils.crypto import get_random_string from django.utils.crypto import get_random_string
from django.template.loader import render_to_string from django.template.loader import render_to_string
from django.conf import settings from django.conf import settings
from git import Repo from git import Repo, Commit
from git.exc import InvalidGitRepositoryError from git.exc import InvalidGitRepositoryError
from dash.models import Instance from dash.models import Instance
@ -28,22 +28,22 @@ from dash.models import Instance
from .models import InstanceCreated 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 Create VM utility. Gitea password is returned
""" """
infra = Infra() infra = Infra()
if not InstanceCreated.objects.filter(instance=instance).exists(): 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 = InstanceCreated.objects.create(instance=instance, created=True)
instance.save() instance.save()
return gitea_password return gitea_password
else: else:
if str.strip(infra.get_flavor(instance=instance)) != str.strip(infra.translate_size( if str.strip(infra.get_flavor(instance=instance)) != str.strip(
instance=instance infra.translate_size(instance=instance)
)): ):
gitea_password = infra.add_vm(instance=instance) return infra.add_vm(instance=instance)
return gitea_password return None
def delete_vm(instance: Instance, owner: str): 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._service_path(subdomain=subdomain)))
self.repo.git.add(str(self._hostscript_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 Commit changes to a VM configuration
""" """
self._add_files(subdomain=subdomain) self._add_files(subdomain=subdomain)
self.repo.git.commit( return self.repo.git.commit(
message=f"{action} VM {subdomain}", message=f"{action} VM {subdomain}",
author="Dashboard Bot <bot@dashboard.hostea.org>", author="Dashboard Bot <bot@dashboard.hostea.org>",
) )
@ -164,7 +164,7 @@ class Infra:
else: else:
return instance.configuration_id.name 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 Add new VM to infrastructure repository
@ -212,7 +212,6 @@ class Infra:
# ``` # ```
# check with @dachary about this # check with @dachary about this
with open(provision, "w+", encoding="utf-8") as f: with open(provision, "w+", encoding="utf-8") as f:
f.write( f.write(
render_to_string( 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) self.repo.git.push(env=self.env)
return gitea_password return (gitea_password, commit)
def remove_vm(self, instance: Instance): def remove_vm(self, instance: Instance):
""" """

View File

@ -48,7 +48,7 @@ def create_instance(request, instance_name: str):
if not payment_fullfilled(instance=instance): if not payment_fullfilled(instance=instance):
return redirect(reverse("billing.invoice.generate", args=(instance_name,))) 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() return HttpResponse()