mirror of https://git.48k.eu/ogclient
Poweroff when no VM and no jobs are running
This patch calls poweroff in virtual mode when no VM is running and no jobs are being executed. This is useful when the guest OS shutdowns so that the host OS does not continue to run.more_events
parent
7bde2a04e9
commit
6ca16dd200
|
@ -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.')
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue