updating virt_model with email and
This commit is contained in:
@ -5,6 +5,17 @@ from flask import current_app
|
||||
from time import sleep
|
||||
from os.path import join
|
||||
from subprocess import run
|
||||
from nanoid import generate
|
||||
|
||||
from capsulflask.db import get_model
|
||||
|
||||
def makeCapsulId():
|
||||
lettersAndNumbers = generate(alphabet="1234567890qwertyuiopasdfghjklzxcvbnm", size=10)
|
||||
return f"capsul-{lettersAndNumbers}"
|
||||
|
||||
def validateCapsulId(id):
|
||||
if not re.match(r"^capsul-[a-z0-9]{10}$", id):
|
||||
raise ValueError(f"vm id \"{id}\" must match \"^capsul-[a-z0-9]{{10}}$\"")
|
||||
|
||||
class VirtualMachine:
|
||||
def __init__(self, id, ipv4=None, ipv6=None):
|
||||
@ -19,21 +30,39 @@ class VirtualizationInterface:
|
||||
def listIds(self) -> list:
|
||||
pass
|
||||
|
||||
def create(self, id: str, template_file_name: str, memory: int, vcpus: int, ssh_public_keys: list) -> VirtualMachine:
|
||||
def create(self, email: str, template_file_name: str, vcpus: int, memory: int, ssh_public_keys: list) -> VirtualMachine:
|
||||
pass
|
||||
|
||||
def destroy(self, id: str):
|
||||
def destroy(self, email: str, id: str):
|
||||
pass
|
||||
|
||||
class MockVirtualization(VirtualizationInterface):
|
||||
def get(self, id):
|
||||
validateCapsulId(id)
|
||||
return VirtualMachine(id, ipv4="1.1.1.1")
|
||||
|
||||
def listIds(self) -> list:
|
||||
return get_model().allVmIds()
|
||||
|
||||
def create(self, email: str, template_file_name: str, vcpus: int, memory_mb: int, ssh_public_keys: list):
|
||||
id = makeCapsulId()
|
||||
print(f"mock create: {id} for {email}")
|
||||
sleep(5)
|
||||
return VirtualMachine(id, ipv4="1.1.1.1")
|
||||
|
||||
def destroy(self, email: str, id: str):
|
||||
print(f"mock destroy: {id} for {email}")
|
||||
|
||||
|
||||
class ShellScriptVirtualization(VirtualizationInterface):
|
||||
|
||||
def validateId(self, id):
|
||||
if not re.match(r"^capsul-[a-z0-9]{10}$", id):
|
||||
raise ValueError(f"vm id \"{id}\" must match \"^capsul-[a-z0-9]{{10}}$\"")
|
||||
def validateCompletedProcess(self, completedProcess, email=None):
|
||||
emailPart = ""
|
||||
if email != None:
|
||||
emailPart = f"for {email}"
|
||||
|
||||
def validateCompletedProcess(self, completedProcess):
|
||||
if completedProcess.returncode != 0:
|
||||
raise RuntimeError(f"""{" ".join(completedProcess.args)} failed with exit code {completedProcess.returncode}
|
||||
raise RuntimeError(f"""{" ".join(completedProcess.args)} failed {emailPart} with exit code {completedProcess.returncode}
|
||||
stdout:
|
||||
{completedProcess.stdout}
|
||||
stderr:
|
||||
@ -41,7 +70,7 @@ class ShellScriptVirtualization(VirtualizationInterface):
|
||||
""")
|
||||
|
||||
def get(self, id):
|
||||
self.validateId(id)
|
||||
validateCapsulId(id)
|
||||
completedProcess = run([join(current_app.root_path, 'shell_scripts/get.sh'), id], capture_output=True)
|
||||
self.validateCompletedProcess(completedProcess)
|
||||
lines = completedProcess.stdout.splitlines()
|
||||
@ -56,8 +85,8 @@ class ShellScriptVirtualization(VirtualizationInterface):
|
||||
self.validateCompletedProcess(completedProcess)
|
||||
return completedProcess.stdout.splitlines()
|
||||
|
||||
def create(self, id: str, template_file_name: str, vcpus: int, memory_mb: int, ssh_public_keys: list):
|
||||
self.validateId(id)
|
||||
def create(self, email: str, template_file_name: str, vcpus: int, memory_mb: int, ssh_public_keys: list):
|
||||
id = makeCapsulId()
|
||||
|
||||
if not re.match(r"^[a-zA-Z0-9_.-]$", template_file_name):
|
||||
raise ValueError(f"template_file_name \"{template_file_name}\" must match \"^[a-zA-Z0-9_.-]$\"")
|
||||
@ -83,7 +112,7 @@ class ShellScriptVirtualization(VirtualizationInterface):
|
||||
ssh_keys_string
|
||||
], capture_output=True)
|
||||
|
||||
self.validateCompletedProcess(completedProcess)
|
||||
self.validateCompletedProcess(completedProcess, email)
|
||||
lines = completedProcess.stdout.splitlines()
|
||||
|
||||
vmSettings = f"""
|
||||
@ -95,7 +124,7 @@ class ShellScriptVirtualization(VirtualizationInterface):
|
||||
"""
|
||||
|
||||
if not lines[len(lines)-1] == "success":
|
||||
raise ValueError(f"""failed to create vm with:
|
||||
raise ValueError(f"""failed to create vm for {email} with:
|
||||
{vmSettings}
|
||||
stdout:
|
||||
{completedProcess.stdout}
|
||||
@ -115,18 +144,18 @@ class ShellScriptVirtualization(VirtualizationInterface):
|
||||
if result != None:
|
||||
return result
|
||||
|
||||
raise TimeoutError(f"""timed out waiting for vm to obtain an IP address:
|
||||
raise TimeoutError(f"""timed out waiting for vm {id} ({email}) to obtain an IP address:
|
||||
{vmSettings}
|
||||
""")
|
||||
|
||||
def destroy(self, id: str):
|
||||
self.validateId(id)
|
||||
def destroy(self, email: str, id: str):
|
||||
validateCapsulId(id)
|
||||
completedProcess = run([join(current_app.root_path, 'shell_scripts/destroy.sh'), id], capture_output=True)
|
||||
self.validateCompletedProcess(completedProcess)
|
||||
self.validateCompletedProcess(completedProcess, email)
|
||||
lines = completedProcess.stdout.splitlines()
|
||||
|
||||
if not lines[len(lines)-1] == "success":
|
||||
raise ValueError(f"""failed to destroy vm "{id}":
|
||||
raise ValueError(f"""failed to destroy vm "{id}" for {email}:
|
||||
stdout:
|
||||
{completedProcess.stdout}
|
||||
stderr:
|
||||
|
Reference in New Issue
Block a user