diff --git a/capsulflask/spoke_model.py b/capsulflask/spoke_model.py index 15ab6e0..2f3f1b0 100644 --- a/capsulflask/spoke_model.py +++ b/capsulflask/spoke_model.py @@ -1,5 +1,7 @@ import subprocess import re +import json +import sys from flask import current_app from time import sleep @@ -8,15 +10,24 @@ from subprocess import run from capsulflask.db import get_model -from capsulflask.shared import VirtualizationInterface, VirtualMachine, validate_capsul_id +from capsulflask.shared import VirtualizationInterface, VirtualMachine, validate_capsul_id, my_exec_info_message class MockSpoke(VirtualizationInterface): def capacity_avaliable(self, additional_ram_bytes): return True - def get(self, id): + def get(self, id, get_ssh_host_keys): validate_capsul_id(id) + + if get_ssh_host_keys: + ssh_host_keys = json.loads("""[ + {"key_type":"ED25519", "content":"ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIN8cna0zeKSKl/r8whdn/KmDWhdzuWRVV0GaKIM+eshh", "sha256":"V4X2apAF6btGAfS45gmpldknoDX0ipJ5c6DLfZR2ttQ"}, + {"key_type":"RSA", "content":"ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCvotgzgEP65JUQ8S8OoNKy1uEEPEAcFetSp7QpONe6hj4wPgyFNgVtdoWdNcU19dX3hpdse0G8OlaMUTnNVuRlbIZXuifXQ2jTtCFUA2mmJ5bF+XjGm3TXKMNGh9PN+wEPUeWd14vZL+QPUMev5LmA8cawPiU5+vVMLid93HRBj118aCJFQxLgrdP48VPfKHFRfCR6TIjg1ii3dH4acdJAvlmJ3GFB6ICT42EmBqskz2MPe0rIFxH8YohCBbAbrbWYcptHt4e48h4UdpZdYOhEdv89GrT8BF2C5cbQ5i9qVpI57bXKrj8hPZU5of48UHLSpXG8mbH0YDiOQOfKX/Mt", "sha256":"ghee6KzRnBJhND2kEUZSaouk7CD6o6z2aAc8GPkV+GQ"}, + {"key_type":"ECDSA", "content":"ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBLLgOoATz9R4aS2kk7vWoxX+lshK63t9+5BIHdzZeFE1o+shlcf0Wji8cN/L1+m3bi0uSETZDOAWMP3rHLJj9Hk=", "sha256":"aCYG1aD8cv/TjzJL0bi9jdabMGksdkfa7R8dCGm1yYs"} + ]""") + return VirtualMachine(id, current_app.config["SPOKE_HOST_ID"], ipv4="1.1.1.1", ssh_host_keys=ssh_host_keys) + return VirtualMachine(id, current_app.config["SPOKE_HOST_ID"], ipv4="1.1.1.1") def list_ids(self) -> list: @@ -67,7 +78,7 @@ class ShellScriptSpoke(VirtualizationInterface): return True - def get(self, id): + def get(self, id, get_ssh_host_keys): validate_capsul_id(id) completedProcess = run([join(current_app.root_path, 'shell_scripts/get.sh'), id], capture_output=True) self.validate_completed_process(completedProcess) @@ -77,6 +88,18 @@ class ShellScriptSpoke(VirtualizationInterface): if not re.match(r"^([0-9]{1,3}\.){3}[0-9]{1,3}$", ipaddr): return None + if get_ssh_host_keys: + try: + completedProcess2 = run([join(current_app.root_path, 'shell_scripts/ssh-keyscan.sh'), ipaddr], capture_output=True) + self.validate_completed_process(completedProcess2) + ssh_host_keys = json.loads(completedProcess2.stdout.decode("utf-8")) + return VirtualMachine(id, current_app.config["SPOKE_HOST_ID"], ipv4=ipaddr, ssh_host_keys=ssh_host_keys) + except: + current_app.logger.warning(f""" + failed to ssh-keyscan {id} at {ipaddr}: + {my_exec_info_message(sys.exc_info())}""" + ) + return VirtualMachine(id, current_app.config["SPOKE_HOST_ID"], ipv4=ipaddr) def list_ids(self) -> list: