aetherscale

[unmaintained] code for a cloud provider tutorial
Log | Files | Refs | README | LICENSE

commit 28c32b9a1e483048159a9b9015b68abf0132de8c
parent 24abab6a259c0804928e5132cb6956e2198892ef
Author: Stefan Koch <programming@stefan-koch.name>
Date:   Sat,  5 Dec 2020 17:51:03 +0100

allow to stop an instance

Diffstat:
Maetherscale/computing.py | 16++++++++++++----
Maetherscale/execution.py | 28++++++++++++++++++++++++----
2 files changed, 36 insertions(+), 8 deletions(-)

diff --git a/aetherscale/computing.py b/aetherscale/computing.py @@ -73,6 +73,10 @@ def get_process_for_vm(vm_id: str) -> Optional[psutil.Process]: return None +def systemd_unit_name_for_vm(vm_id: str) -> str: + return f'aetherscale-vm-{vm_id}.service' + + @dataclass class QemuStartupConfig: vm_id: str @@ -128,9 +132,13 @@ def callback(ch, method, properties, body): print('VM ID not specified', file=sys.stderr) return - process = get_process_for_vm(vm_id) - if process: - process.kill() + unit_name = systemd_unit_name_for_vm(vm_id) + is_running = execution.systemctl_is_running(unit_name) + + if is_running: + execution.disable_systemd_unit(unit_name) + execution.stop_systemd_unit(unit_name) + response = { 'status': 'killed', 'vm-id': vm_id, @@ -165,7 +173,7 @@ def callback(ch, method, properties, body): hda_image=user_image, mac_addr=mac_addr, vde_folder=Path(VDE_FOLDER)) - unit_name = f'aetherscale-vm-{vm_id}.service' + unit_name = systemd_unit_name_for_vm(vm_id) create_qemu_systemd_unit(unit_name, qemu_config) execution.start_systemd_unit(unit_name) diff --git a/aetherscale/execution.py b/aetherscale/execution.py @@ -16,32 +16,52 @@ def run_command_chain(commands: List[List[str]]) -> bool: return True +def systemd_unit_path(unit_name: str) -> Path: + systemd_unit_dir = Path().home() / '.config/systemd/user' + return systemd_unit_dir / unit_name + + def copy_systemd_unit(unit_file: Path, unit_name: str): if '.' not in unit_name: raise ValueError('Unit name must contain the suffix, e.g. .service') - systemd_unit_dir = Path().home() / '.config/systemd/user' - systemd_unit_dir.mkdir(parents=True, exist_ok=True) - target_unit_file = systemd_unit_dir / unit_name + target_unit_path = systemd_unit_path(unit_name) + target_unit_path.parent.mkdir(parents=True, exist_ok=True) - shutil.copyfile(unit_file, target_unit_file) + shutil.copyfile(unit_file, target_unit_path) # Reload system subprocess.run(['systemctl', '--user', 'daemon-reload']) +def delete_systemd_unit(unit_name: str): + systemd_unit_path(unit_name).unlink(missing_ok=True) + + def start_systemd_unit(unit_name: str) -> bool: return run_command_chain([ ['systemctl', '--user', 'start', unit_name], ]) +def stop_systemd_unit(unit_name: str) -> bool: + return run_command_chain([ + ['systemctl', '--user', 'stop', unit_name], + ]) + + def enable_systemd_unit(unit_name: str) -> bool: return run_command_chain([ ['systemctl', '--user', 'enable', unit_name], ]) +def disable_systemd_unit(unit_name: str) -> bool: + return run_command_chain([ + ['systemctl', '--user', 'disable', unit_name], + ]) + + def systemctl_is_running(unit_name: str) -> bool: result = subprocess.run([ 'systemctl', '--user', 'is-active', '--quiet', unit_name])