2020-05-10 23:59:30 +00:00
|
|
|
import subprocess
|
|
|
|
import re
|
2021-01-30 07:39:48 +00:00
|
|
|
import sys
|
|
|
|
import json
|
2020-05-10 23:59:30 +00:00
|
|
|
|
|
|
|
from flask import current_app
|
|
|
|
from time import sleep
|
|
|
|
from os.path import join
|
|
|
|
from subprocess import run
|
2020-05-11 01:23:00 +00:00
|
|
|
|
|
|
|
from capsulflask.db import get_model
|
2021-01-30 07:39:48 +00:00
|
|
|
from capsulflask.shared import my_exec_info_message, VirtualMachine
|
2020-05-11 01:23:00 +00:00
|
|
|
|
2020-05-11 06:47:14 +00:00
|
|
|
def validate_capsul_id(id):
|
2020-05-13 18:56:43 +00:00
|
|
|
if not re.match(r"^(cvm|capsul)-[a-z0-9]{10}$", id):
|
2020-05-11 01:23:00 +00:00
|
|
|
raise ValueError(f"vm id \"{id}\" must match \"^capsul-[a-z0-9]{{10}}$\"")
|
2020-05-10 23:59:30 +00:00
|
|
|
|
|
|
|
class VirtualizationInterface:
|
2020-05-13 18:56:43 +00:00
|
|
|
def capacity_avaliable(self, additional_ram_bytes: int) -> bool:
|
|
|
|
pass
|
|
|
|
|
2021-01-30 07:39:48 +00:00
|
|
|
def get(self, id: str, get_ssh_host_keys: bool) -> VirtualMachine:
|
2020-05-10 23:59:30 +00:00
|
|
|
pass
|
|
|
|
|
2020-05-11 06:47:14 +00:00
|
|
|
def list_ids(self) -> list:
|
2020-05-10 23:59:30 +00:00
|
|
|
pass
|
|
|
|
|
2021-01-30 07:39:48 +00:00
|
|
|
def create(self, email: str, id: str, template_image_file_name: str, vcpus: int, memory: int, ssh_authorized_keys: list):
|
2020-05-10 23:59:30 +00:00
|
|
|
pass
|
|
|
|
|
2020-05-11 01:23:00 +00:00
|
|
|
def destroy(self, email: str, id: str):
|
2020-05-10 23:59:30 +00:00
|
|
|
pass
|
|
|
|
|
2020-05-11 01:23:00 +00:00
|
|
|
class MockVirtualization(VirtualizationInterface):
|
2020-05-13 18:56:43 +00:00
|
|
|
def capacity_avaliable(self, additional_ram_bytes):
|
|
|
|
return True
|
|
|
|
|
2021-01-30 07:39:48 +00:00
|
|
|
def get(self, id, get_ssh_host_keys):
|
2020-05-11 06:47:14 +00:00
|
|
|
validate_capsul_id(id)
|
2021-01-30 07:39:48 +00:00
|
|
|
|
|
|
|
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, ipv4="1.1.1.1", ssh_host_keys=ssh_host_keys)
|
|
|
|
|
2020-05-11 01:23:00 +00:00
|
|
|
return VirtualMachine(id, ipv4="1.1.1.1")
|
|
|
|
|
2020-05-11 06:47:14 +00:00
|
|
|
def list_ids(self) -> list:
|
2020-05-22 02:40:41 +00:00
|
|
|
return get_model().all_non_deleted_vm_ids()
|
2020-05-11 01:23:00 +00:00
|
|
|
|
2021-01-30 07:39:48 +00:00
|
|
|
def create(self, email: str, id: str, template_image_file_name: str, vcpus: int, memory_mb: int, ssh_authorized_keys: list):
|
2020-05-11 06:47:14 +00:00
|
|
|
validate_capsul_id(id)
|
2020-05-16 04:19:01 +00:00
|
|
|
current_app.logger.info(f"mock create: {id} for {email}")
|
2020-05-11 20:13:20 +00:00
|
|
|
sleep(1)
|
2020-05-11 01:23:00 +00:00
|
|
|
|
|
|
|
def destroy(self, email: str, id: str):
|
2020-05-16 04:19:01 +00:00
|
|
|
current_app.logger.info(f"mock destroy: {id} for {email}")
|
2020-05-11 01:23:00 +00:00
|
|
|
|
|
|
|
|
2020-05-10 23:59:30 +00:00
|
|
|
class ShellScriptVirtualization(VirtualizationInterface):
|
|
|
|
|
2020-05-11 06:47:14 +00:00
|
|
|
def validate_completed_process(self, completedProcess, email=None):
|
2020-05-11 01:23:00 +00:00
|
|
|
emailPart = ""
|
|
|
|
if email != None:
|
|
|
|
emailPart = f"for {email}"
|
2020-05-10 23:59:30 +00:00
|
|
|
|
|
|
|
if completedProcess.returncode != 0:
|
2020-05-11 01:23:00 +00:00
|
|
|
raise RuntimeError(f"""{" ".join(completedProcess.args)} failed {emailPart} with exit code {completedProcess.returncode}
|
2020-05-10 23:59:30 +00:00
|
|
|
stdout:
|
|
|
|
{completedProcess.stdout}
|
|
|
|
stderr:
|
|
|
|
{completedProcess.stderr}
|
|
|
|
""")
|
|
|
|
|
2020-05-13 18:56:43 +00:00
|
|
|
def capacity_avaliable(self, additional_ram_bytes):
|
2020-05-16 20:49:49 +00:00
|
|
|
my_args=[join(current_app.root_path, 'shell_scripts/capacity-avaliable.sh'), str(additional_ram_bytes)]
|
2020-05-13 18:56:43 +00:00
|
|
|
completedProcess = run(my_args, capture_output=True)
|
|
|
|
|
|
|
|
if completedProcess.returncode != 0:
|
2020-05-16 04:19:01 +00:00
|
|
|
current_app.logger.error(f"""
|
2020-05-13 18:56:43 +00:00
|
|
|
capacity-avaliable.sh exited {completedProcess.returncode} with
|
|
|
|
stdout:
|
|
|
|
{completedProcess.stdout}
|
|
|
|
stderr:
|
|
|
|
{completedProcess.stderr}
|
|
|
|
""")
|
|
|
|
return False
|
|
|
|
|
|
|
|
lines = completedProcess.stdout.splitlines()
|
2020-05-16 20:51:38 +00:00
|
|
|
output = lines[len(lines)-1]
|
|
|
|
if not output == b"yes":
|
|
|
|
current_app.logger.error(f"capacity-avaliable.sh exited 0 and returned {output} but did not return \"yes\" ")
|
2020-05-13 18:56:43 +00:00
|
|
|
return False
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
2021-01-30 07:39:48 +00:00
|
|
|
def get(self, id, get_ssh_host_keys):
|
2020-05-11 06:47:14 +00:00
|
|
|
validate_capsul_id(id)
|
2020-05-10 23:59:30 +00:00
|
|
|
completedProcess = run([join(current_app.root_path, 'shell_scripts/get.sh'), id], capture_output=True)
|
2020-05-11 06:47:14 +00:00
|
|
|
self.validate_completed_process(completedProcess)
|
2021-01-30 07:39:48 +00:00
|
|
|
ipaddr_lines = completedProcess.stdout.splitlines()
|
|
|
|
if len(ipaddr_lines) == 0:
|
2021-01-29 06:26:21 +00:00
|
|
|
return None
|
|
|
|
|
2021-01-30 07:39:48 +00:00
|
|
|
ipaddr = ipaddr_lines[0].decode("utf-8")
|
2020-05-10 23:59:30 +00:00
|
|
|
|
2020-05-16 21:45:21 +00:00
|
|
|
if not re.match(r"^([0-9]{1,3}\.){3}[0-9]{1,3}$", ipaddr):
|
2020-05-10 23:59:30 +00:00
|
|
|
return None
|
|
|
|
|
2021-01-30 07:39:48 +00:00
|
|
|
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)
|
|
|
|
return VirtualMachine(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())}"""
|
|
|
|
)
|
|
|
|
|
2020-05-16 21:45:21 +00:00
|
|
|
return VirtualMachine(id, ipv4=ipaddr)
|
2020-05-10 23:59:30 +00:00
|
|
|
|
2020-05-11 06:47:14 +00:00
|
|
|
def list_ids(self) -> list:
|
2020-05-13 18:56:43 +00:00
|
|
|
completedProcess = run([join(current_app.root_path, 'shell_scripts/list-ids.sh')], capture_output=True)
|
2020-05-11 06:47:14 +00:00
|
|
|
self.validate_completed_process(completedProcess)
|
2020-05-22 03:45:00 +00:00
|
|
|
return list(map(lambda x: x.decode("utf-8"), completedProcess.stdout.splitlines() ))
|
2020-05-10 23:59:30 +00:00
|
|
|
|
2021-01-30 07:39:48 +00:00
|
|
|
def create(self, email: str, id: str, template_image_file_name: str, vcpus: int, memory_mb: int, ssh_authorized_keys: list):
|
2020-05-11 06:47:14 +00:00
|
|
|
validate_capsul_id(id)
|
2020-05-10 23:59:30 +00:00
|
|
|
|
2020-10-30 02:25:45 +00:00
|
|
|
if not re.match(r"^[a-zA-Z0-9/_.-]+$", template_image_file_name):
|
|
|
|
raise ValueError(f"template_image_file_name \"{template_image_file_name}\" must match \"^[a-zA-Z0-9/_.-]+$\"")
|
2020-05-10 23:59:30 +00:00
|
|
|
|
2021-01-30 07:39:48 +00:00
|
|
|
for ssh_public_key in ssh_authorized_keys:
|
2020-05-11 21:24:37 +00:00
|
|
|
if not re.match(r"^(ssh|ecdsa)-[0-9A-Za-z+/_=@. -]+$", ssh_public_key):
|
|
|
|
raise ValueError(f"ssh_public_key \"{ssh_public_key}\" must match \"^(ssh|ecdsa)-[0-9A-Za-z+/_=@. -]+$\"")
|
2020-05-10 23:59:30 +00:00
|
|
|
|
|
|
|
if vcpus < 1 or vcpus > 8:
|
|
|
|
raise ValueError(f"vcpus \"{vcpus}\" must match 1 <= vcpus <= 8")
|
|
|
|
|
|
|
|
if memory_mb < 512 or memory_mb > 16384:
|
|
|
|
raise ValueError(f"memory_mb \"{memory_mb}\" must match 512 <= memory_mb <= 16384")
|
|
|
|
|
2021-01-30 07:39:48 +00:00
|
|
|
ssh_keys_string = "\n".join(ssh_authorized_keys)
|
2020-05-10 23:59:30 +00:00
|
|
|
|
|
|
|
completedProcess = run([
|
|
|
|
join(current_app.root_path, 'shell_scripts/create.sh'),
|
|
|
|
id,
|
2020-05-11 06:47:14 +00:00
|
|
|
template_image_file_name,
|
2020-05-10 23:59:30 +00:00
|
|
|
str(vcpus),
|
|
|
|
str(memory_mb),
|
|
|
|
ssh_keys_string
|
|
|
|
], capture_output=True)
|
|
|
|
|
2020-05-11 06:47:14 +00:00
|
|
|
self.validate_completed_process(completedProcess, email)
|
2020-05-10 23:59:30 +00:00
|
|
|
lines = completedProcess.stdout.splitlines()
|
2020-05-16 21:55:18 +00:00
|
|
|
status = lines[len(lines)-1].decode("utf-8")
|
2020-05-10 23:59:30 +00:00
|
|
|
|
|
|
|
vmSettings = f"""
|
|
|
|
id={id}
|
2020-05-11 06:47:14 +00:00
|
|
|
template_image_file_name={template_image_file_name}
|
2020-05-10 23:59:30 +00:00
|
|
|
vcpus={str(vcpus)}
|
|
|
|
memory={str(memory_mb)}
|
2021-01-30 07:39:48 +00:00
|
|
|
ssh_authorized_keys={ssh_keys_string}
|
2020-05-10 23:59:30 +00:00
|
|
|
"""
|
|
|
|
|
2020-05-16 21:55:18 +00:00
|
|
|
if not status == "success":
|
2020-05-11 01:23:00 +00:00
|
|
|
raise ValueError(f"""failed to create vm for {email} with:
|
2020-05-10 23:59:30 +00:00
|
|
|
{vmSettings}
|
|
|
|
stdout:
|
|
|
|
{completedProcess.stdout}
|
|
|
|
stderr:
|
|
|
|
{completedProcess.stderr}
|
|
|
|
""")
|
|
|
|
|
2020-05-11 01:23:00 +00:00
|
|
|
def destroy(self, email: str, id: str):
|
2020-05-11 06:47:14 +00:00
|
|
|
validate_capsul_id(id)
|
2020-05-10 23:59:30 +00:00
|
|
|
completedProcess = run([join(current_app.root_path, 'shell_scripts/destroy.sh'), id], capture_output=True)
|
2020-05-11 06:47:14 +00:00
|
|
|
self.validate_completed_process(completedProcess, email)
|
2020-05-10 23:59:30 +00:00
|
|
|
lines = completedProcess.stdout.splitlines()
|
2020-05-16 21:55:18 +00:00
|
|
|
status = lines[len(lines)-1].decode("utf-8")
|
2020-05-10 23:59:30 +00:00
|
|
|
|
2020-05-16 21:55:18 +00:00
|
|
|
if not status == "success":
|
2020-05-11 01:23:00 +00:00
|
|
|
raise ValueError(f"""failed to destroy vm "{id}" for {email}:
|
2020-05-10 23:59:30 +00:00
|
|
|
stdout:
|
|
|
|
{completedProcess.stdout}
|
|
|
|
stderr:
|
|
|
|
{completedProcess.stderr}
|
|
|
|
""")
|