1
0
mirror of https://github.com/dkmstr/openuds.git synced 2025-03-20 06:50:23 +03:00

* Set logging to INFO by default

* Added messages to Actor User Space
This commit is contained in:
Adolfo Gómez García 2014-12-09 12:03:50 +01:00
parent bc6d500898
commit 722c8b2b61
7 changed files with 237 additions and 24 deletions

View File

@ -37,12 +37,22 @@ from PyQt4 import QtGui
from PyQt4 import QtCore
import pickle
import time
import signal
from udsactor import ipc
from udsactor import utils
from udsactor.log import logger
from udsactor.service import IPC_PORT
from udsactor import operations
from about_dialog_ui import Ui_UDSAboutDialog
from message_dialog_ui import Ui_UDSMessageDialog
from udsactor.scriptThread import ScriptExecutorThread
trayIcon = None
def sigTerm(sigNo, stackFrame):
if trayIcon:
trayIcon.quit()
# About dialog
@ -56,6 +66,20 @@ class UDSAboutDialog(QtGui.QDialog):
self.hide()
class UDSMessageDialog(QtGui.QDialog):
def __init__(self, parent=None):
QtGui.QDialog.__init__(self, parent)
self.ui = Ui_UDSMessageDialog()
self.ui.setupUi(self)
def displayMessage(self, message):
self.ui.message.setText(message)
self.show()
def closeDialog(self):
self.hide()
class MessagesProcessor(QtCore.QThread):
logoff = QtCore.pyqtSignal(name='logoff')
@ -66,11 +90,16 @@ class MessagesProcessor(QtCore.QThread):
def __init__(self):
super(self.__class__, self).__init__()
try:
self.ipc = ipc.ClientIPC(IPC_PORT)
self.ipc.start()
except Exception:
self.ipc = None
# Retries connection for a while
for _ in range(10):
try:
self.ipc = ipc.ClientIPC(IPC_PORT)
self.ipc.start()
break
except Exception:
logger.debug('IPC Server is not reachable')
self.ipc = None
time.sleep(2)
self.running = False
@ -83,7 +112,7 @@ class MessagesProcessor(QtCore.QThread):
return self.ipc is not None
def requestInformation(self):
if self.ipc:
if self.ipc is not None:
info = self.ipc.requestInformation()
logger.debug('Request information: {}'.format(info))
@ -146,6 +175,7 @@ class UDSSystemTray(QtGui.QSystemTrayIcon):
self.maxIdleTime = None
self.timer = QtCore.QTimer()
self.timer.timeout.connect(self.checkIdle)
self.showIdleWarn = True
if self.ipc.isAlive() is False:
raise Exception('no connection to service, exiting')
@ -162,6 +192,7 @@ class UDSSystemTray(QtGui.QSystemTrayIcon):
self.ipc.requestInformation()
self.aboutDlg = UDSAboutDialog()
self.msgDlg = UDSMessageDialog()
self.counter = 0
@ -172,19 +203,35 @@ class UDSSystemTray(QtGui.QSystemTrayIcon):
self.ipc.sendLogin(operations.getCurrentUser())
def checkIdle(self):
if self.maxIdleTime is None: # No idle checl
return
idleTime = operations.getIdleDuration()
remainingTime = self.maxIdleTime - idleTime
if remainingTime > 300: # Reset show Warning dialog if we have more than 5 minutes left
self.showIdleWarn = True
logger.debug('User has been idle for: {}'.format(idleTime))
if self.maxIdleTime is not None and idleTime > self.maxIdleTime:
if remainingTime <= 0:
logger.info('User has been idle for too long, notifying Broker that service can be reclaimed')
self.quit()
def displayMessage(self, message):
self.counter += 1
print(message.toUtf8(), '--', self.counter)
if self.showIdleWarn is True and remainingTime < 120: # With two minutes, show a warning message
self.showIdleWarn = False
self.msgDlg.displayMessage("You have been idle for too long. The session will end if you don't resume operations")
logger.debug('Here')
def executeScript(self, message):
self.counter += 1
print(message.toUtf8(), '--', self.counter)
def displayMessage(self, message):
logger.debug('Displaying message')
self.msgDlg.displayMessage("You have been idle for too long. The session will end if you don't resume operations")
QtGui.QMessageBox.information(None, "UDS Actor", message)
def executeScript(self, script):
logger.debug('Executing script')
th = ScriptExecutorThread(script)
th.start()
def logoff(self):
self.counter += 1
@ -207,14 +254,19 @@ class UDSSystemTray(QtGui.QSystemTrayIcon):
self.aboutDlg.exec_()
def quit(self):
logger.debug('Quit invoked')
if self.stopped is True:
return
self.stopped = True
# If we close Client, send Logoff to Broker
self.ipc.sendLogout(operations.getCurrentUser())
self.timer.stop()
self.ipc.stop()
operations.loggoff()
try:
# If we close Client, send Logoff to Broker
self.ipc.sendLogout(operations.getCurrentUser())
self.timer.stop()
self.ipc.stop()
operations.loggoff()
except Exception:
# May we have lost connection with server, simply exit in that case
pass
self.app.quit()
if __name__ == '__main__':
@ -224,6 +276,9 @@ if __name__ == '__main__':
# QtGui.QMessageBox.critical(None, "Systray", "I couldn't detect any system tray on this system.")
sys.exit(1)
# This is important so our app won't close on message windows
QtGui.QApplication.setQuitOnLastWindowClosed(False)
try:
trayIcon = UDSSystemTray(app)
except Exception:
@ -232,6 +287,9 @@ if __name__ == '__main__':
trayIcon.show()
# Catch kill and logout user :)
signal.signal(signal.SIGTERM, sigTerm)
res = app.exec_()
logger.debug('Exiting')

View File

@ -19,6 +19,9 @@
<property name="windowTitle">
<string>About UDS Actor</string>
</property>
<property name="locale">
<locale language="English" country="UnitedStates"/>
</property>
<property name="modal">
<bool>true</bool>
</property>

View File

@ -2,7 +2,7 @@
# Form implementation generated from reading ui file 'about-dialog.ui'
#
# Created: Fri Nov 21 10:28:41 2014
# Created: Tue Dec 09 10:54:30 2014
# by: PyQt4 UI code generator 4.11.2
#
# WARNING! All changes made in this file will be lost!
@ -31,6 +31,7 @@ class Ui_UDSAboutDialog(object):
font.setFamily(_fromUtf8("Verdana"))
font.setPointSize(9)
UDSAboutDialog.setFont(font)
UDSAboutDialog.setLocale(QtCore.QLocale(QtCore.QLocale.English, QtCore.QLocale.UnitedStates))
UDSAboutDialog.setModal(True)
self.vboxlayout = QtGui.QVBoxLayout(UDSAboutDialog)
self.vboxlayout.setSpacing(9)

View File

@ -0,0 +1,89 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>UDSMessageDialog</class>
<widget class="QDialog" name="UDSMessageDialog">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>339</width>
<height>188</height>
</rect>
</property>
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Expanding">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="font">
<font>
<family>Verdana</family>
<pointsize>10</pointsize>
</font>
</property>
<property name="windowTitle">
<string>UDS Actor</string>
</property>
<widget class="QWidget" name="verticalLayoutWidget">
<property name="geometry">
<rect>
<x>10</x>
<y>10</y>
<width>321</width>
<height>171</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QTextBrowser" name="message"/>
</item>
<item>
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeType">
<enum>QSizePolicy::Fixed</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>15</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QDialogButtonBox" name="buttonBox">
<property name="standardButtons">
<set>QDialogButtonBox::Ok</set>
</property>
</widget>
</item>
</layout>
</widget>
</widget>
<resources/>
<connections>
<connection>
<sender>buttonBox</sender>
<signal>clicked(QAbstractButton*)</signal>
<receiver>UDSMessageDialog</receiver>
<slot>closeDialog()</slot>
<hints>
<hint type="sourcelabel">
<x>203</x>
<y>161</y>
</hint>
<hint type="destinationlabel">
<x>337</x>
<y>125</y>
</hint>
</hints>
</connection>
</connections>
<slots>
<slot>closeDialog()</slot>
</slots>
</ui>

View File

@ -0,0 +1,61 @@
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'message-dialog.ui'
#
# Created: Tue Dec 9 11:27:47 2014
# by: PyQt4 UI code generator 4.11.2
#
# WARNING! All changes made in this file will be lost!
from PyQt4 import QtCore, QtGui
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s
try:
_encoding = QtGui.QApplication.UnicodeUTF8
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig)
class Ui_UDSMessageDialog(object):
def setupUi(self, UDSMessageDialog):
UDSMessageDialog.setObjectName(_fromUtf8("UDSMessageDialog"))
UDSMessageDialog.resize(339, 188)
sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Expanding)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(UDSMessageDialog.sizePolicy().hasHeightForWidth())
UDSMessageDialog.setSizePolicy(sizePolicy)
font = QtGui.QFont()
font.setFamily(_fromUtf8("Verdana"))
font.setPointSize(10)
UDSMessageDialog.setFont(font)
self.verticalLayoutWidget = QtGui.QWidget(UDSMessageDialog)
self.verticalLayoutWidget.setGeometry(QtCore.QRect(10, 10, 321, 171))
self.verticalLayoutWidget.setObjectName(_fromUtf8("verticalLayoutWidget"))
self.verticalLayout = QtGui.QVBoxLayout(self.verticalLayoutWidget)
self.verticalLayout.setMargin(0)
self.verticalLayout.setObjectName(_fromUtf8("verticalLayout"))
self.message = QtGui.QTextBrowser(self.verticalLayoutWidget)
self.message.setObjectName(_fromUtf8("message"))
self.verticalLayout.addWidget(self.message)
spacerItem = QtGui.QSpacerItem(20, 15, QtGui.QSizePolicy.Minimum, QtGui.QSizePolicy.Fixed)
self.verticalLayout.addItem(spacerItem)
self.buttonBox = QtGui.QDialogButtonBox(self.verticalLayoutWidget)
self.buttonBox.setStandardButtons(QtGui.QDialogButtonBox.Ok)
self.buttonBox.setObjectName(_fromUtf8("buttonBox"))
self.verticalLayout.addWidget(self.buttonBox)
self.retranslateUi(UDSMessageDialog)
QtCore.QObject.connect(self.buttonBox, QtCore.SIGNAL(_fromUtf8("clicked(QAbstractButton*)")), UDSMessageDialog.closeDialog)
QtCore.QMetaObject.connectSlotsByName(UDSMessageDialog)
def retranslateUi(self, UDSMessageDialog):
UDSMessageDialog.setWindowTitle(_translate("UDSMessageDialog", "UDS Actor", None))

View File

@ -149,15 +149,16 @@ def reboot(flags=0):
def loggoff():
'''
Right now shutdowns the machine...
Right now restarts the machine...
'''
# Workaround for dummy thread
if six.PY3 is False:
import threading
threading._DummyThread._Thread__stop = lambda x: 42
subprocess.call(['/sbin/shutdown', 'now', '-h'])
subprocess.call(['/usr/bin/systemctl', 'poweroff', '-i'])
subprocess.call(['/usr/bin/pkill', '-u', os.environ['USER']])
# subprocess.call(['/sbin/shutdown', 'now', '-r'])
# subprocess.call(['/usr/bin/systemctl', 'reboot', '-i'])
def renameComputer(newName):
@ -235,4 +236,4 @@ def getCurrentUser():
'''
Returns current logged in user
'''
return os.environ['USER']
return os.environ['USER']

View File

@ -45,7 +45,7 @@ OTHER, DEBUG, INFO, WARN, ERROR, FATAL = (10000 * (x + 1) for x in six.moves.xra
class Logger(object):
def __init__(self):
self.logLevel = OTHER
self.logLevel = INFO
self.logger = LocalLogger()
self.remoteLogger = None