1
0
mirror of https://github.com/dkmstr/openuds.git synced 2024-12-22 13:34:04 +03:00

Updated Script Executor thread

This commit is contained in:
Adolfo Gómez García 2014-12-05 10:54:23 +01:00
parent c7d3984289
commit e8b16a7e9d
4 changed files with 73 additions and 13 deletions

View File

@ -78,7 +78,7 @@ class UDSConfigDialog(QtGui.QDialog):
try:
cfg = self._getCfg()
api = REST.Api(
cfg['host'], cfg['masterKey'], cfg['ssl'], scrambledResponses=True)
cfg['host'], cfg['masterKey'], cfg['ssl'])
api.test()
QtGui.QMessageBox.information(
self, 'Test Passed', 'The test was executed successfully', QtGui.QMessageBox.Ok)

View File

@ -31,6 +31,11 @@
'''
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 uuid
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
import time
from udsactor.log import logger
from udsactor import utils
from udsactor.certs import createSelfSignedCert
import ssl
startTime = time.time()
# List os scripts that should be executed on logout
scriptsOnLogout = []
class HTTPServerHandler(BaseHTTPServer.BaseHTTPRequestHandler):
uuid = None
@ -147,6 +152,7 @@ class HTTPServerHandler(BaseHTTPServer.BaseHTTPRequestHandler):
return 'ok'
def post_script(self, params):
logger.debug('Received script: {}'.format(params))
if 'script' not in params:
raise Exception('Invalid script parameters')
if 'user' in params:
@ -155,15 +161,7 @@ class HTTPServerHandler(BaseHTTPServer.BaseHTTPRequestHandler):
else:
# Execute script at server space, that is, here
# as a secondary thread
script = 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 = ScriptExecutorThread(params['script'])
th.start()
return 'ok'

View 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))

View File

@ -38,6 +38,7 @@ from . import store
from . import REST
from . import ipc
from . import httpserver
from .scriptThread import ScriptExecutorThread
from .utils import exceptionToMessage
import socket
@ -293,3 +294,13 @@ class CommonService(object):
Overriden to log stop
'''
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