feat: vm delete management command
ci/woodpecker/push/woodpecker Pipeline was successful
Details
ci/woodpecker/push/woodpecker Pipeline was successful
Details
parent
0606c4ade0
commit
927c2a7703
|
@ -36,6 +36,9 @@ class VmException(Exception):
|
|||
self.error = str(code)
|
||||
self.code = code
|
||||
|
||||
def __str__(self):
|
||||
return self.error
|
||||
|
||||
|
||||
def create_instance(vm_name: str, configuration_name: str, user: User) -> Instance:
|
||||
"""
|
||||
|
|
|
@ -12,6 +12,7 @@
|
|||
#
|
||||
# You should have received a copy of the GNU Affero General Public License
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
from enum import Enum, unique
|
||||
|
||||
from django.core.management.base import BaseCommand
|
||||
from django.core.exceptions import ValidationError
|
||||
|
@ -26,6 +27,15 @@ from dash.utils import create_instance
|
|||
from infrastructure.utils import create_vm_if_not_exists
|
||||
|
||||
|
||||
@unique
|
||||
class Actions(Enum):
|
||||
CREATE = "create"
|
||||
DELETE = "delete"
|
||||
|
||||
def __str__(self) -> str:
|
||||
return self.name
|
||||
|
||||
|
||||
class Command(BaseCommand):
|
||||
help = "Get user ID from username"
|
||||
action_key = "action"
|
||||
|
@ -36,7 +46,7 @@ class Command(BaseCommand):
|
|||
def add_arguments(self, parser):
|
||||
parser.add_argument(
|
||||
self.action_key,
|
||||
type=str,
|
||||
type=Actions,
|
||||
help="VM action: create/delete",
|
||||
)
|
||||
|
||||
|
@ -73,31 +83,39 @@ class Command(BaseCommand):
|
|||
self.stdout.write(self.style.WARN("flavour no match"))
|
||||
size = flavor
|
||||
user = get_user_model().objects.get(username=owner)
|
||||
instance = create_instance(vm_name=vm_name, configuration_name=size, user=user)
|
||||
create_instance
|
||||
create_vm_if_not_exists(instance)
|
||||
print("Instance created")
|
||||
try:
|
||||
instance = create_instance(
|
||||
vm_name=vm_name, configuration_name=size, user=user
|
||||
)
|
||||
create_vm_if_not_exists(instance)
|
||||
print("Instance created")
|
||||
except Exception as e:
|
||||
self.stderr.write(self.style.ERROR(f"error: {str(e)}"))
|
||||
|
||||
def delete_vm(self, *args, **options):
|
||||
create_vm
|
||||
from infrastructure.utils import delete_vm
|
||||
|
||||
vm_name = options[self.vm_name_key]
|
||||
instance = Instance.objects.get(name=vm_name)
|
||||
delete_vm(instance=instance, owner=instance.owned_by.username)
|
||||
|
||||
def handle(self, *args, **options):
|
||||
for i in [self.action_key, self.vm_name_key]:
|
||||
if i not in options:
|
||||
self.stdout.write(self.style.ERROR(f"Please provide {i}"))
|
||||
return
|
||||
if options[self.action_key] == "create":
|
||||
if options[self.action_key] == Actions.CREATE:
|
||||
for i in [self.flavor_key, self.owner_key]:
|
||||
if i not in options:
|
||||
self.stdout.write(self.style.ERROR(f"Please provide {i}"))
|
||||
return
|
||||
|
||||
self.create_vm(*args, **options)
|
||||
elif options[self.action_key] == "delete":
|
||||
elif options[self.action_key] == Actions.DELETE:
|
||||
self.delete_vm(*args, **options)
|
||||
|
||||
else:
|
||||
self.stdout.write(
|
||||
self.style.ERROR("Unknown action: {options[self.action_key]}")
|
||||
self.style.ERROR(f"Unknown action: {options[self.action_key]}")
|
||||
)
|
||||
return
|
||||
|
|
|
@ -140,3 +140,10 @@ class InfraUtilTest(TestCase):
|
|||
self.assertEqual(instance_created.instance, instance)
|
||||
|
||||
self.assertEqual(instance_created.created, True)
|
||||
|
||||
call_command("vm", "delete", vm_name)
|
||||
out = stdout.getvalue()
|
||||
|
||||
self.assertEqual(Instance.objects.filter(name=vm_name).exists(), False)
|
||||
host_vars_dir = infra._host_vars_dir(subdomain)
|
||||
self.assertEqual(host_vars_dir.exists(), False)
|
||||
|
|
|
@ -37,6 +37,14 @@ def create_vm_if_not_exists(instance: Instance):
|
|||
instance.save()
|
||||
|
||||
|
||||
def delete_vm(instance: Instance, owner: str):
|
||||
infra = Infra()
|
||||
infra.remove_vm(instance=instance)
|
||||
if InstanceCreated.objects.filter(instance=instance).exists():
|
||||
InstanceCreated.objects.get(instance=instance).delete()
|
||||
instance.delete()
|
||||
|
||||
|
||||
class Infra:
|
||||
"""
|
||||
Utility function to manage infrastructure repository
|
||||
|
|
Loading…
Reference in New Issue