forked from Hostea/dashboard
feat: grab commit ID after add_vm execution
parent
5ec87c83ec
commit
b123bfa582
|
@ -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}")
|
||||
|
||||
|
|
|
@ -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()
|
||||
|
||||
|
|
|
@ -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 <bot@dashboard.hostea.org>",
|
||||
)
|
||||
|
@ -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):
|
||||
"""
|
||||
|
|
|
@ -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()
|
||||
|
||||
|
|
Loading…
Reference in New Issue