#750: Using more descriptive status; new route {{{POST /command}}} to launch command/script in a callback thread that returns all data to a server route.
parent
9e81e9020c
commit
2af37100e0
|
@ -218,23 +218,14 @@ class OpenGnSysWorker(ServerWorker):
|
||||||
:param server:
|
:param server:
|
||||||
:return: JSON object {"status": "status_code", "loggedin": boolean}
|
:return: JSON object {"status": "status_code", "loggedin": boolean}
|
||||||
"""
|
"""
|
||||||
res = {'status': '', 'loggedin': self.logged_in}
|
res = {'loggedin': self.loggedin}
|
||||||
if platform.system() == 'Linux': # GNU/Linux
|
try:
|
||||||
# Check if it's OpenGnsys Client.
|
res['status'] = operations.os_type.lower()
|
||||||
if os.path.exists('/scripts/oginit'):
|
except KeyError:
|
||||||
# Check if OpenGnsys Client is busy.
|
res['status'] = ''
|
||||||
if self.locked:
|
# Check if OpenGnsys Client is busy
|
||||||
res['status'] = 'BSY'
|
if res['status'] == 'oglive' and self.locked:
|
||||||
else:
|
res['status'] = 'busy'
|
||||||
res['status'] = 'OPG'
|
|
||||||
else:
|
|
||||||
# Check if there is an active session.
|
|
||||||
res['status'] = 'LNX'
|
|
||||||
elif platform.system() == 'Windows': # Windows
|
|
||||||
# Check if there is an active session.
|
|
||||||
res['status'] = 'WIN'
|
|
||||||
elif platform.system() == 'Darwin': # Mac OS X ??
|
|
||||||
res['status'] = 'OSX'
|
|
||||||
return res
|
return res
|
||||||
|
|
||||||
@check_secret
|
@check_secret
|
||||||
|
@ -344,7 +335,7 @@ class OpenGnSysWorker(ServerWorker):
|
||||||
# Skip blank rows
|
# Skip blank rows
|
||||||
pass
|
pass
|
||||||
elif len(cols) == 7:
|
elif len(cols) == 7:
|
||||||
disk, npart, tpart, fs, os, size, usage = cols
|
disk, npart, tpart, fs, opsys, size, usage = cols
|
||||||
try:
|
try:
|
||||||
if int(npart) == 0:
|
if int(npart) == 0:
|
||||||
# Disk information
|
# Disk information
|
||||||
|
@ -352,7 +343,7 @@ class OpenGnSysWorker(ServerWorker):
|
||||||
else:
|
else:
|
||||||
# Partition information
|
# Partition information
|
||||||
storage.append({'disk': int(disk), 'partition': int(npart), 'parttype': tpart,
|
storage.append({'disk': int(disk), 'partition': int(npart), 'parttype': tpart,
|
||||||
'filesystem': fs, 'operatingsystem': os, 'size': int(size),
|
'filesystem': fs, 'operatingsystem': opsys, 'size': int(size),
|
||||||
'usage': int(usage)})
|
'usage': int(usage)})
|
||||||
except ValueError:
|
except ValueError:
|
||||||
logger.warn('Configuration parameter error: {}'.format(cols))
|
logger.warn('Configuration parameter error: {}'.format(cols))
|
||||||
|
@ -364,6 +355,38 @@ class OpenGnSysWorker(ServerWorker):
|
||||||
# Returning configuration data and count of warnings
|
# Returning configuration data and count of warnings
|
||||||
return {'serialno': serialno, 'storage': storage, 'warnings': warnings}
|
return {'serialno': serialno, 'storage': storage, 'warnings': warnings}
|
||||||
|
|
||||||
|
def task_command(self, code, route):
|
||||||
|
"""
|
||||||
|
Task to execute a command
|
||||||
|
:param code: Code to execute
|
||||||
|
:param route: server REST route to return results (including its parameters)
|
||||||
|
"""
|
||||||
|
(stat, out, err) = operations.exec_command(code)
|
||||||
|
self.REST.sendMessage(route, {'status': stat, 'output': out, 'error': err})
|
||||||
|
|
||||||
|
def process_command(self, path, get_params, post_params, server):
|
||||||
|
"""
|
||||||
|
Launches a thread to executing a command
|
||||||
|
:param path: ignored
|
||||||
|
:param get_params: ignored
|
||||||
|
:param post_params: object with format {"id": OperationId, "code": "Code", url: "ReturnURL"}
|
||||||
|
:param server: ignored
|
||||||
|
:rtype: object with launching status
|
||||||
|
"""
|
||||||
|
logger.debug('Recieved command operation with params: {}'.format(post_params))
|
||||||
|
self.checkSecret(server)
|
||||||
|
# Processing data
|
||||||
|
try:
|
||||||
|
code = post_params.get('code')
|
||||||
|
cmd_id = post_params.get('id')
|
||||||
|
route = '{}?id={}'.format(post_params.get('route'), cmd_id)
|
||||||
|
# Launching new thread
|
||||||
|
threading.Thread(target=self.task_command, args=(code, route)).start()
|
||||||
|
except Exception as e:
|
||||||
|
logger.error('Got exception {}'.format(e))
|
||||||
|
return {'error': e}
|
||||||
|
return {'op': 'launched'}
|
||||||
|
|
||||||
def process_hardware(self, path, get_params, post_params, server):
|
def process_hardware(self, path, get_params, post_params, server):
|
||||||
"""
|
"""
|
||||||
Returns client's hardware profile
|
Returns client's hardware profile
|
||||||
|
@ -394,4 +417,4 @@ class OpenGnSysWorker(ServerWorker):
|
||||||
:return:
|
:return:
|
||||||
"""
|
"""
|
||||||
logger.debug('Recieved software operation with params: {}'.format(post_params))
|
logger.debug('Recieved software operation with params: {}'.format(post_params))
|
||||||
return operations.get_software(post_params['disk'], post_params['part'])
|
return operations.get_software(post_params.get('disk'), post_params.get('part'))
|
||||||
|
|
|
@ -189,6 +189,21 @@ def get_configuration():
|
||||||
return cfgdata
|
return cfgdata
|
||||||
|
|
||||||
|
|
||||||
|
def exec_command(cmd):
|
||||||
|
"""
|
||||||
|
Executing a shell command
|
||||||
|
:param cmd:
|
||||||
|
:return: object with components:
|
||||||
|
output: standard output
|
||||||
|
error: error output
|
||||||
|
exit: exit code
|
||||||
|
"""
|
||||||
|
proc = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
||||||
|
(out, err) = proc.communicate()
|
||||||
|
stat = proc.returncode
|
||||||
|
return stat, out, err
|
||||||
|
|
||||||
|
|
||||||
def get_hardware():
|
def get_hardware():
|
||||||
"""
|
"""
|
||||||
Returns client's hardware list
|
Returns client's hardware list
|
||||||
|
|
|
@ -139,5 +139,5 @@ setup(
|
||||||
description='OpenGnsys Agent',
|
description='OpenGnsys Agent',
|
||||||
author='Adolfo Gomez',
|
author='Adolfo Gomez',
|
||||||
author_email='agomez@virtualcable.es',
|
author_email='agomez@virtualcable.es',
|
||||||
zipfile='OGAgent.zip',
|
zipfile='OGAgent.zip', requires=['six']
|
||||||
)
|
)
|
||||||
|
|
Loading…
Reference in New Issue