forked from shaba/openuds
Updated Script Executor thread
This commit is contained in:
parent
c7d3984289
commit
e8b16a7e9d
@ -78,7 +78,7 @@ class UDSConfigDialog(QtGui.QDialog):
|
|||||||
try:
|
try:
|
||||||
cfg = self._getCfg()
|
cfg = self._getCfg()
|
||||||
api = REST.Api(
|
api = REST.Api(
|
||||||
cfg['host'], cfg['masterKey'], cfg['ssl'], scrambledResponses=True)
|
cfg['host'], cfg['masterKey'], cfg['ssl'])
|
||||||
api.test()
|
api.test()
|
||||||
QtGui.QMessageBox.information(
|
QtGui.QMessageBox.information(
|
||||||
self, 'Test Passed', 'The test was executed successfully', QtGui.QMessageBox.Ok)
|
self, 'Test Passed', 'The test was executed successfully', QtGui.QMessageBox.Ok)
|
||||||
|
@ -31,6 +31,11 @@
|
|||||||
'''
|
'''
|
||||||
from __future__ import unicode_literals
|
from __future__ import unicode_literals
|
||||||
|
|
||||||
|
from udsactor.log import logger
|
||||||
|
from udsactor import utils
|
||||||
|
from udsactor.certs import createSelfSignedCert
|
||||||
|
from udsactor.scriptThread import ScriptExecutorThread
|
||||||
|
|
||||||
import threading
|
import threading
|
||||||
import uuid
|
import uuid
|
||||||
import json
|
import json
|
||||||
@ -39,13 +44,13 @@ from six.moves import socketserver # @UnresolvedImport, pylint: disable=import-
|
|||||||
from six.moves import BaseHTTPServer # @UnresolvedImport, pylint: disable=import-error
|
from six.moves import BaseHTTPServer # @UnresolvedImport, pylint: disable=import-error
|
||||||
import time
|
import time
|
||||||
|
|
||||||
from udsactor.log import logger
|
|
||||||
from udsactor import utils
|
|
||||||
from udsactor.certs import createSelfSignedCert
|
|
||||||
import ssl
|
import ssl
|
||||||
|
|
||||||
startTime = time.time()
|
startTime = time.time()
|
||||||
|
|
||||||
|
# List os scripts that should be executed on logout
|
||||||
|
scriptsOnLogout = []
|
||||||
|
|
||||||
|
|
||||||
class HTTPServerHandler(BaseHTTPServer.BaseHTTPRequestHandler):
|
class HTTPServerHandler(BaseHTTPServer.BaseHTTPRequestHandler):
|
||||||
uuid = None
|
uuid = None
|
||||||
@ -147,6 +152,7 @@ class HTTPServerHandler(BaseHTTPServer.BaseHTTPRequestHandler):
|
|||||||
return 'ok'
|
return 'ok'
|
||||||
|
|
||||||
def post_script(self, params):
|
def post_script(self, params):
|
||||||
|
logger.debug('Received script: {}'.format(params))
|
||||||
if 'script' not in params:
|
if 'script' not in params:
|
||||||
raise Exception('Invalid script parameters')
|
raise Exception('Invalid script parameters')
|
||||||
if 'user' in params:
|
if 'user' in params:
|
||||||
@ -155,15 +161,7 @@ class HTTPServerHandler(BaseHTTPServer.BaseHTTPRequestHandler):
|
|||||||
else:
|
else:
|
||||||
# Execute script at server space, that is, here
|
# Execute script at server space, that is, here
|
||||||
# as a secondary thread
|
# as a secondary thread
|
||||||
script = params['script']
|
th = ScriptExecutorThread(params['script'])
|
||||||
|
|
||||||
def executor():
|
|
||||||
logger.debug('Executing script: {}'.format(script))
|
|
||||||
try:
|
|
||||||
six.exec_(script, None, None)
|
|
||||||
except Exception as e:
|
|
||||||
logger.error('Error executing script: {}'.format(e))
|
|
||||||
th = threading.Thread(target=executor)
|
|
||||||
th.start()
|
th.start()
|
||||||
return 'ok'
|
return 'ok'
|
||||||
|
|
||||||
|
51
actors/src/udsactor/scriptThread.py
Normal file
51
actors/src/udsactor/scriptThread.py
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
#
|
||||||
|
# Copyright (c) 201 Virtual Cable S.L.
|
||||||
|
# All rights reserved.
|
||||||
|
#
|
||||||
|
# Redistribution and use in source and binary forms, with or without modification,
|
||||||
|
# are permitted provided that the following conditions are met:
|
||||||
|
#
|
||||||
|
# * Redistributions of source code must retain the above copyright notice,
|
||||||
|
# this list of conditions and the following disclaimer.
|
||||||
|
# * Redistributions in binary form must reproduce the above copyright notice,
|
||||||
|
# this list of conditions and the following disclaimer in the documentation
|
||||||
|
# and/or other materials provided with the distribution.
|
||||||
|
# * Neither the name of Virtual Cable S.L. nor the names of its contributors
|
||||||
|
# may be used to endorse or promote products derived from this software
|
||||||
|
# without specific prior written permission.
|
||||||
|
#
|
||||||
|
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||||
|
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||||
|
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||||
|
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||||
|
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||||
|
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||||
|
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||||
|
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||||
|
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||||
|
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
|
||||||
|
'''
|
||||||
|
@author: Adolfo Gómez, dkmaster at dkmon dot com
|
||||||
|
'''
|
||||||
|
|
||||||
|
# pylint: disable-msg=E1101,W0703
|
||||||
|
|
||||||
|
from udsactor.log import logger
|
||||||
|
|
||||||
|
import threading
|
||||||
|
import six
|
||||||
|
|
||||||
|
|
||||||
|
class ScriptExecutorThread(threading.Thread):
|
||||||
|
def __init__(self, script):
|
||||||
|
super(ScriptExecutorThread, self).__init__()
|
||||||
|
self.script = script
|
||||||
|
|
||||||
|
def run(self):
|
||||||
|
try:
|
||||||
|
logger.debug('Executing script: {}'.format(self.script))
|
||||||
|
six.exec_(self.script, globals(), None)
|
||||||
|
except Exception as e:
|
||||||
|
logger.error('Error executing script: {}'.format(e))
|
@ -38,6 +38,7 @@ from . import store
|
|||||||
from . import REST
|
from . import REST
|
||||||
from . import ipc
|
from . import ipc
|
||||||
from . import httpserver
|
from . import httpserver
|
||||||
|
from .scriptThread import ScriptExecutorThread
|
||||||
from .utils import exceptionToMessage
|
from .utils import exceptionToMessage
|
||||||
|
|
||||||
import socket
|
import socket
|
||||||
@ -293,3 +294,13 @@ class CommonService(object):
|
|||||||
Overriden to log stop
|
Overriden to log stop
|
||||||
'''
|
'''
|
||||||
logger.info('Service is being stopped')
|
logger.info('Service is being stopped')
|
||||||
|
|
||||||
|
def onLogout(self):
|
||||||
|
scripts = httpserver.scriptsOnLogout[:] # Copy scripts and remove them
|
||||||
|
httpserver.scriptsOnLogout[:] = []
|
||||||
|
|
||||||
|
for s in scripts:
|
||||||
|
th = ScriptExecutorThread(s)
|
||||||
|
th.start()
|
||||||
|
|
||||||
|
# Right now, do not wait for thread end, just return
|
||||||
|
Loading…
Reference in New Issue
Block a user