summaryrefslogtreecommitdiffstats
path: root/src/virtual/poweroffd.py
diff options
context:
space:
mode:
authorJose M. Guisado <jguisado@soleta.eu>2021-09-01 13:13:39 +0200
committerOpenGnSys Support Team <soporte-og@soleta.eu>2021-09-01 13:29:58 +0200
commit0c00f64669bfbdb6bde8e5cb7cfc205dec284e50 (patch)
tree3439f9c75282c0fa4e8cfe4ac88bc0fdd422b292 /src/virtual/poweroffd.py
parent082079ad78979efc163f785811dfd56deb078059 (diff)
#1059 virtual: replace qmp polling for event listening
Polling for a qmp port availability is undesirable, as QEMU only handles one connection to the qmp port at a time, ogClient may interfere with cloneer-manager. Check vm thread now connects to a separate qmp tcp socket, listening for a shutdown guest event. When ogClient is run just after ogVDI installation (before guest installation) it will try to connect until it's possible, ie: after an iso is specified and a qemu vm is started that exposes the appropiate qmp tcp port.
Diffstat (limited to 'src/virtual/poweroffd.py')
-rw-r--r--src/virtual/poweroffd.py53
1 files changed, 53 insertions, 0 deletions
diff --git a/src/virtual/poweroffd.py b/src/virtual/poweroffd.py
new file mode 100644
index 0000000..63d08e1
--- /dev/null
+++ b/src/virtual/poweroffd.py
@@ -0,0 +1,53 @@
+#
+# Copyright (C) 2021 Soleta Networks <info@soleta.eu>
+#
+# This program is free software: you can redistribute it and/or modify it under
+# the terms of the GNU Affero General Public License as published by the
+# Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+
+
+from src.virtual.qmp import QEMUMonitorProtocol
+from src.virtual.qmp import QMPCapabilitiesError, QMPConnectError
+
+QMP_DEFAULT_PORT = 4445
+QMP_DEFAULT_HOST = "127.0.0.1"
+
+def is_shutdown_event(qmp_ev):
+ """
+ """
+ return qmp_ev.get('event') == 'SHUTDOWN'
+
+
+def init(host=QMP_DEFAULT_HOST, port=QMP_DEFAULT_PORT):
+ """
+ """
+ qmpconn = QEMUMonitorProtocol((host, port))
+ try:
+ qmpconn.connect()
+ except ConnectionRefusedError:
+ print("Critical err: Connection refused")
+ return None
+ except QMPCapabilitiesError as e:
+ print("Error negotiating capabilities")
+ return None
+ return qmpconn
+
+
+def run(qmpconn):
+ """
+ """
+ while(True):
+ try:
+ qmp_ev = qmpconn.pull_event(wait=True)
+ except QMPConnectError as e:
+ print("Error trying to pull an event")
+ ret = -1
+ break
+ if is_shutdown_event(qmp_ev):
+ print("Detected guest shutdown, let's go")
+ ret = 0
+ break
+
+ qmpconn.close()
+ return ret