summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/ogRest.py15
-rw-r--r--src/virtual/ogOperations.py26
2 files changed, 40 insertions, 1 deletions
diff --git a/src/ogRest.py b/src/ogRest.py
index fed998e..abe05c1 100644
--- a/src/ogRest.py
+++ b/src/ogRest.py
@@ -19,7 +19,7 @@ import signal
from src.restRequest import *
from src.linux.ogOperations import OgLinuxOperations
-from src.virtual.ogOperations import OgVirtualOperations
+from src.virtual.ogOperations import OgVirtualOperations, OgVM
class ThreadState(Enum):
IDLE = 0
@@ -97,6 +97,17 @@ class ogThread():
ogRest.state = ThreadState.IDLE
+ def check_vm_state_loop(ogRest):
+ POLLING_WAIT_TIME = 3
+ while True:
+ state = ogRest.operations.check_vm_state()
+ installed_os = ogRest.operations.get_installed_os()
+ if state == OgVM.State.STOPPED and \
+ ogRest.state == ThreadState.IDLE and \
+ len(installed_os) > 0:
+ ogRest.operations.poweroff_host()
+ time.sleep(POLLING_WAIT_TIME)
+
def poweroff(ogRest):
time.sleep(2)
ogRest.operations.poweroff()
@@ -242,6 +253,8 @@ class ogRest():
self.operations = OgLinuxOperations()
elif self.mode == 'virtual':
self.operations = OgVirtualOperations()
+ threading.Thread(target=ogThread.check_vm_state_loop,
+ args=(self,)).start()
else:
raise ValueError('Mode not supported.')
diff --git a/src/virtual/ogOperations.py b/src/virtual/ogOperations.py
index b13aeb4..1b4a871 100644
--- a/src/virtual/ogOperations.py
+++ b/src/virtual/ogOperations.py
@@ -19,12 +19,17 @@ import pathlib
import re
import math
import sys
+import enum
class OgVM:
DEFAULT_CPU = 'host'
DEFAULT_VGA = 'std'
DEFAULT_QMP_PORT = 4444
+ class State(enum.Enum):
+ STOPPED = 0
+ RUNNING = 1
+
def __init__(self,
partition_path,
memory=None,
@@ -134,6 +139,7 @@ class OgVirtualOperations:
self.OG_PATH = os.path.dirname(os.path.realpath(sys.argv[0]))
self.OG_IMAGES_PATH = f'{self.OG_PATH}/images'
self.OG_PARTITIONS_PATH = f'{self.OG_PATH}/partitions'
+ self.OG_PARTITIONS_CFG_PATH = f'{self.OG_PATH}/partitions.json'
if not os.path.exists(self.OG_IMAGES_PATH):
os.mkdir(self.OG_IMAGES_PATH, mode=0o755)
@@ -168,6 +174,26 @@ class OgVirtualOperations:
except:
pass
+ def check_vm_state(self):
+ try:
+ qmp = OgQMP(self.IP, self.VIRTUAL_PORT)
+ qmp.disconnect()
+ return OgVM.State.RUNNING
+ except:
+ return OgVM.State.STOPPED
+
+ def get_installed_os(self):
+ installed_os = {}
+ try:
+ with open(self.OG_PARTITIONS_CFG_PATH, 'r') as f:
+ cfg = json.loads(f.read())
+ for part in cfg['partition_setup']:
+ if len(part['os']) > 0:
+ installed_os[part['os']] = (part['disk'], part['partition'])
+ except:
+ pass
+ return installed_os
+
def execCMD(self, request, ogRest):
# TODO Implement.
raise NotImplementedError