Delete a VM, mark it as destroyable

This commit is contained in:
Julien Duponchelle committed 2015-01-23 11:28:58 +01:00
1 parent 28308b10bc
commit f97c2b2cbe
6 files changed
+105 -11

No files matched your search

+17 -3
View File
@@ -149,14 +149,13 @@ class BaseManager:
self._vms[vm.uuid] = vm
return vm
# FIXME: should be named close_vm and we should have a
# delete_vm when a user deletes a VM (including files in workdir)
@asyncio.coroutine
def delete_vm(self, uuid):
def close_vm(self, uuid):
"""
Delete a VM
:param uuid: VM UUID
:returns: VM instance
"""
vm = self.get_vm(uuid)
@@ -164,7 +163,22 @@ class BaseManager:
yield from vm.close()
else:
vm.close()
return vm
@asyncio.coroutine
def delete_vm(self, uuid):
"""
Delete a VM. VM working directory will be destroy when
we receive a commit.
:param uuid: VM UUID
:returns: VM instance
"""
vm = yield from self.close_vm(uuid)
vm.project.mark_vm_for_destruction(vm)
del self._vms[vm.uuid]
return vm
@staticmethod
def _has_privileged_access(executable):
+1 -1
View File
@@ -96,7 +96,7 @@ class BaseVM:
Return VM working directory
"""
return self._project.vm_working_directory(self.manager.module_name.lower(), self._uuid)
return self._project.vm_working_directory(self)
def create(self):
"""
+21 -4
View File
@@ -18,6 +18,7 @@
import aiohttp
import os
import tempfile
import shutil
from uuid import UUID, uuid4
@@ -45,6 +46,7 @@ class Project:
if location is None:
self._location = tempfile.mkdtemp()
self._vms_to_destroy = set()
self._path = os.path.join(self._location, self._uuid)
try:
os.makedirs(os.path.join(self._path, "vms"), exist_ok=True)
@@ -66,25 +68,40 @@ class Project:
return self._path
def vm_working_directory(self, module, vm_uuid):
def vm_working_directory(self, vm):
"""
Return a working directory for a specific VM.
If the directory doesn't exist, the directory is created.
:param module: The module name (vpcs, dynamips...)
:param vm_uuid: VM UUID
:param vm: An instance of VM
:returns: A string with a VM working directory
"""
workdir = os.path.join(self._path, module, vm_uuid)
workdir = os.path.join(self._path, vm.manager.module_name.lower(), vm.uuid)
try:
os.makedirs(workdir, exist_ok=True)
except OSError as e:
raise aiohttp.web.HTTPInternalServerError(text="Could not create VM working directory: {}".format(e))
return workdir
def mark_vm_for_destruction(self, vm):
"""
:param vm: An instance of VM
"""
self._vms_to_destroy.add(vm)
def __json__(self):
return {
"uuid": self._uuid,
"location": self._location
}
def commit(self):
"""Write project changes on disk"""
while self._vms_to_destroy:
vm = self._vms_to_destroy.pop()
directory = self.vm_working_directory(vm)
if os.path.exists(directory):
shutil.rmtree(directory)