From 071ea87c30d87eb108991ea5d395f690576698b3 Mon Sep 17 00:00:00 2001 From: Adam Dangoor Date: Mon, 8 Apr 2019 16:07:52 +0100 Subject: [PATCH] Ignore aborted VMs [skip ci] --- CHANGELOG.rst | 2 + .../dcos_vagrant/commands/_common.py | 43 ++++++++++++------- 2 files changed, 30 insertions(+), 15 deletions(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index ba19d3ddb..196f81ef0 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -7,6 +7,8 @@ Next ---- * Allow multiple ``--node`` options on ``run`` commands to run a command on multiple nodes. +* ``minidcos vagrant list`` will now not show clusters with aborted VMs. + A VM is aborted for example if the host is shut down. 2019.04.02.1 ------------ diff --git a/src/dcos_e2e_cli/dcos_vagrant/commands/_common.py b/src/dcos_e2e_cli/dcos_vagrant/commands/_common.py index 042376a4f..302907dcd 100644 --- a/src/dcos_e2e_cli/dcos_vagrant/commands/_common.py +++ b/src/dcos_e2e_cli/dcos_vagrant/commands/_common.py @@ -37,6 +37,26 @@ def _description_from_vm_name(vm_name: str) -> str: return str(description) +@functools.lru_cache() +def _running_vm_names() -> Set[str]: + """ + Return the names of all running VMs. + """ + # We do not show e.g. aborted VMs. + # For example, a VM is aborted when the host is rebooted. + # This is problematic as we cannot assume that the workspace directory, + # which might be in /tmp/ is still there. + ls_output = bytes(vertigo_py.ls(option='runningvms')) # type: ignore + lines = ls_output.decode().strip().split('\n') + lines = [line for line in lines if line] + vm_names = set([]) + for line in lines: + vm_name_in_quotes, _ = line.split(' ') + vm_name = vm_name_in_quotes[1:-1] + vm_names.add(vm_name) + return vm_names + + @functools.lru_cache() def _ip_from_vm_name(vm_name: str) -> Optional[IPv4Address]: """ @@ -64,13 +84,11 @@ def existing_cluster_ids() -> Set[str]: """ Return the IDs of existing clusters. """ - ls_output = vertigo_py.ls(option='vms') # type: ignore - lines = ls_output.decode().strip().split('\n') - lines = [line for line in lines if line] + vm_names = _running_vm_names() cluster_ids = set() - for line in lines: - vm_name_in_quotes, _ = line.split(' ') - vm_name = vm_name_in_quotes[1:-1] + for vm_name in vm_names: + # A VM is in a cluster if it has a description and that description is + # valid JSON and has a known key. description = _description_from_vm_name(vm_name=vm_name) try: data = json.loads(s=description) @@ -165,13 +183,9 @@ def _vm_names(self) -> Set[str]: """ Return VirtualBox and Vagrant names of VMs in this cluster. """ - ls_output = vertigo_py.ls(option='vms') # type: ignore - lines = ls_output.decode().strip().split('\n') - lines = [line for line in lines if line] - vm_names = set() - for line in lines: - vm_name_in_quotes, _ = line.split(' ') - vm_name = vm_name_in_quotes[1:-1] + running_vm_names = _running_vm_names() + vm_names = set([]) + for vm_name in running_vm_names: description = _description_from_vm_name(vm_name=vm_name) try: data = json.loads(s=description) @@ -278,8 +292,7 @@ def vagrant_client(self) -> Any: # We ignore files such as .DS_Store files. [vagrant_root] = [ - item for item in vagrant_root_parent.iterdir() - if item.is_dir() + item for item in vagrant_root_parent.iterdir() if item.is_dir() ] # We import Vagrant here instead of at the top of the file because, if