starting to build console controller and views
This commit is contained in:
@ -5,15 +5,10 @@ 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):
|
||||
def validate_capsul_id(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}}$\"")
|
||||
|
||||
@ -27,10 +22,10 @@ class VirtualizationInterface:
|
||||
def get(self, id: str) -> VirtualMachine:
|
||||
pass
|
||||
|
||||
def listIds(self) -> list:
|
||||
def list_ids(self) -> list:
|
||||
pass
|
||||
|
||||
def create(self, email: str, template_file_name: str, vcpus: int, memory: int, ssh_public_keys: list) -> VirtualMachine:
|
||||
def create(self, email: str, id: str, template_image_file_name: str, vcpus: int, memory: int, ssh_public_keys: list) -> VirtualMachine:
|
||||
pass
|
||||
|
||||
def destroy(self, email: str, id: str):
|
||||
@ -38,14 +33,14 @@ class VirtualizationInterface:
|
||||
|
||||
class MockVirtualization(VirtualizationInterface):
|
||||
def get(self, id):
|
||||
validateCapsulId(id)
|
||||
validate_capsul_id(id)
|
||||
return VirtualMachine(id, ipv4="1.1.1.1")
|
||||
|
||||
def listIds(self) -> list:
|
||||
return get_model().allVmIds()
|
||||
def list_ids(self) -> list:
|
||||
return get_model().all_vm_ids()
|
||||
|
||||
def create(self, email: str, template_file_name: str, vcpus: int, memory_mb: int, ssh_public_keys: list):
|
||||
id = makeCapsulId()
|
||||
def create(self, email: str, id: str, template_image_file_name: str, vcpus: int, memory_mb: int, ssh_public_keys: list):
|
||||
validate_capsul_id(id)
|
||||
print(f"mock create: {id} for {email}")
|
||||
sleep(5)
|
||||
return VirtualMachine(id, ipv4="1.1.1.1")
|
||||
@ -56,7 +51,7 @@ class MockVirtualization(VirtualizationInterface):
|
||||
|
||||
class ShellScriptVirtualization(VirtualizationInterface):
|
||||
|
||||
def validateCompletedProcess(self, completedProcess, email=None):
|
||||
def validate_completed_process(self, completedProcess, email=None):
|
||||
emailPart = ""
|
||||
if email != None:
|
||||
emailPart = f"for {email}"
|
||||
@ -70,9 +65,9 @@ class ShellScriptVirtualization(VirtualizationInterface):
|
||||
""")
|
||||
|
||||
def get(self, id):
|
||||
validateCapsulId(id)
|
||||
validate_capsul_id(id)
|
||||
completedProcess = run([join(current_app.root_path, 'shell_scripts/get.sh'), id], capture_output=True)
|
||||
self.validateCompletedProcess(completedProcess)
|
||||
self.validate_completed_process(completedProcess)
|
||||
lines = completedProcess.stdout.splitlines()
|
||||
|
||||
if not re.match(r"^([0-9]{1,3}\.){3}[0-9]{1,3}$", lines[0]):
|
||||
@ -80,16 +75,16 @@ class ShellScriptVirtualization(VirtualizationInterface):
|
||||
|
||||
return VirtualMachine(id, ipv4=lines[0])
|
||||
|
||||
def listIds(self) -> list:
|
||||
completedProcess = run([join(current_app.root_path, 'shell_scripts/listIds.sh')], capture_output=True)
|
||||
self.validateCompletedProcess(completedProcess)
|
||||
def list_ids(self) -> list:
|
||||
completedProcess = run([join(current_app.root_path, 'shell_scripts/list_ids.sh')], capture_output=True)
|
||||
self.validate_completed_process(completedProcess)
|
||||
return completedProcess.stdout.splitlines()
|
||||
|
||||
def create(self, email: str, template_file_name: str, vcpus: int, memory_mb: int, ssh_public_keys: list):
|
||||
id = makeCapsulId()
|
||||
def create(self, email: str, id: str, template_image_file_name: str, vcpus: int, memory_mb: int, ssh_public_keys: list):
|
||||
validate_capsul_id(id)
|
||||
|
||||
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_.-]$\"")
|
||||
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_.-]$\"")
|
||||
|
||||
for ssh_public_key in ssh_public_keys:
|
||||
if not re.match(r"^(ssh|ecdsa)-[0-9A-Za-z+/_=@ -]+$", ssh_public_key):
|
||||
@ -106,18 +101,18 @@ class ShellScriptVirtualization(VirtualizationInterface):
|
||||
completedProcess = run([
|
||||
join(current_app.root_path, 'shell_scripts/create.sh'),
|
||||
id,
|
||||
template_file_name,
|
||||
template_image_file_name,
|
||||
str(vcpus),
|
||||
str(memory_mb),
|
||||
ssh_keys_string
|
||||
], capture_output=True)
|
||||
|
||||
self.validateCompletedProcess(completedProcess, email)
|
||||
self.validate_completed_process(completedProcess, email)
|
||||
lines = completedProcess.stdout.splitlines()
|
||||
|
||||
vmSettings = f"""
|
||||
id={id}
|
||||
template_file_name={template_file_name}
|
||||
template_image_file_name={template_image_file_name}
|
||||
vcpus={str(vcpus)}
|
||||
memory={str(memory_mb)}
|
||||
ssh_public_keys={ssh_keys_string}
|
||||
@ -149,9 +144,9 @@ class ShellScriptVirtualization(VirtualizationInterface):
|
||||
""")
|
||||
|
||||
def destroy(self, email: str, id: str):
|
||||
validateCapsulId(id)
|
||||
validate_capsul_id(id)
|
||||
completedProcess = run([join(current_app.root_path, 'shell_scripts/destroy.sh'), id], capture_output=True)
|
||||
self.validateCompletedProcess(completedProcess, email)
|
||||
self.validate_completed_process(completedProcess, email)
|
||||
lines = completedProcess.stdout.splitlines()
|
||||
|
||||
if not lines[len(lines)-1] == "success":
|
||||
|
Reference in New Issue
Block a user