fixing get inconsistency and adding vm_state_command
This commit is contained in:
@ -41,6 +41,9 @@ class MockSpoke(VirtualizationInterface):
|
||||
def destroy(self, email: str, id: str):
|
||||
current_app.logger.info(f"mock destroy: {id} for {email}")
|
||||
|
||||
def vm_state_command(self, email: str, id: str, command: str):
|
||||
current_app.logger.info(f"mock {command}: {id} for {email}")
|
||||
|
||||
class ShellScriptSpoke(VirtualizationInterface):
|
||||
|
||||
def validate_completed_process(self, completedProcess, email=None):
|
||||
@ -85,11 +88,20 @@ class ShellScriptSpoke(VirtualizationInterface):
|
||||
lines = completedProcess.stdout.splitlines()
|
||||
if len(lines) == 0:
|
||||
return None
|
||||
|
||||
ipaddr = lines[0].decode("utf-8")
|
||||
|
||||
result_string = lines[0].decode("utf-8")
|
||||
|
||||
fields = result_string.split(" ")
|
||||
if fields[0] != "true":
|
||||
return None
|
||||
|
||||
if len(fields) < 2:
|
||||
return VirtualMachine(id, current_app.config["SPOKE_HOST_ID"])
|
||||
|
||||
ipaddr = fields[1]
|
||||
|
||||
if not re.match(r"^([0-9]{1,3}\.){3}[0-9]{1,3}$", ipaddr):
|
||||
return None
|
||||
return VirtualMachine(id, current_app.config["SPOKE_HOST_ID"])
|
||||
|
||||
if get_ssh_host_keys:
|
||||
try:
|
||||
@ -172,4 +184,13 @@ class ShellScriptSpoke(VirtualizationInterface):
|
||||
{completedProcess.stderr}
|
||||
""")
|
||||
|
||||
def vm_state_command(self, email: str, id: str, command: str):
|
||||
validate_capsul_id(id)
|
||||
if command not in ["stop", "force-stop", "start", "restart"]:
|
||||
raise ValueError(f"command ({command}) must be one of stop, force-stop, start, or restart")
|
||||
|
||||
completedProcess = run([join(current_app.root_path, f"shell_scripts/{command}.sh"), id], capture_output=True)
|
||||
self.validate_completed_process(completedProcess, email)
|
||||
returned_string = completedProcess.stdout.decode("utf-8")
|
||||
current_app.logger.info(f"{command} vm {id} for {email} returned: {returned_string}")
|
||||
|
||||
|
Reference in New Issue
Block a user