#930: Remove duplicate code and fix some typos.
parent
d8ba7b2bf0
commit
a648e3fbbe
|
@ -33,7 +33,9 @@ from __future__ import unicode_literals
|
||||||
import os
|
import os
|
||||||
import random
|
import random
|
||||||
import shutil
|
import shutil
|
||||||
|
import signal
|
||||||
import string
|
import string
|
||||||
|
import subprocess
|
||||||
import threading
|
import threading
|
||||||
import time
|
import time
|
||||||
import urllib
|
import urllib
|
||||||
|
@ -45,12 +47,12 @@ from opengnsys.workers import ServerWorker
|
||||||
from six.moves.urllib import parse
|
from six.moves.urllib import parse
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# Check authorization header decorator
|
# Check authorization header decorator
|
||||||
def check_secret(fnc):
|
def check_secret(fnc):
|
||||||
"""
|
"""
|
||||||
Decorator to check for received secret key and raise exception if it isn't valid.
|
Decorator to check for received secret key and raise exception if it isn't valid.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def wrapper(*args, **kwargs):
|
def wrapper(*args, **kwargs):
|
||||||
try:
|
try:
|
||||||
this, path, get_params, post_params, server = args # @UnusedVariable
|
this, path, get_params, post_params, server = args # @UnusedVariable
|
||||||
|
@ -73,6 +75,7 @@ def catch_background_error(fnc):
|
||||||
fnc(*args, **kwargs)
|
fnc(*args, **kwargs)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
this.REST.sendMessage('error?id={}'.format(kwargs.get('requestId', 'error')), {'error': '{}'.format(e)})
|
this.REST.sendMessage('error?id={}'.format(kwargs.get('requestId', 'error')), {'error': '{}'.format(e)})
|
||||||
|
|
||||||
return wrapper
|
return wrapper
|
||||||
|
|
||||||
|
|
||||||
|
@ -160,48 +163,8 @@ class OpenGnSysWorker(ServerWorker):
|
||||||
if os_type == 'oglive':
|
if os_type == 'oglive':
|
||||||
# Send configuration data, if needed
|
# Send configuration data, if needed
|
||||||
if send_config:
|
if send_config:
|
||||||
self.REST.sendMessage('clients/configs', {'mac': self.interface.mac, 'ip': self.interface.ip,
|
self.REST.sendMessage('client/configs', {'mac': self.interface.mac, 'ip': self.interface.ip,
|
||||||
'config': operations.get_configuration()})
|
'config': operations.get_configuration()})
|
||||||
self._launch_browser(menu_url)
|
|
||||||
|
|
||||||
def _launch_browser(self, url):
|
|
||||||
"""
|
|
||||||
Launchs the Browser with specified URL
|
|
||||||
:param url: URL to show
|
|
||||||
"""
|
|
||||||
logger.debug('Launching browser with URL: {}'.format(url))
|
|
||||||
if hasattr(self.browser, 'process'):
|
|
||||||
self.browser['process'].kill()
|
|
||||||
self.browser['url'] = url
|
|
||||||
self.browser['process'] = subprocess.Popen(['browser', '-qws', url])
|
|
||||||
|
|
||||||
def _task_command(self, route, code, op_id):
|
|
||||||
"""
|
|
||||||
Task to execute a command and return results to a server URI
|
|
||||||
:param route: server callback REST route to return results
|
|
||||||
:param code: code to execute
|
|
||||||
:param op_id: operation id.
|
|
||||||
"""
|
|
||||||
menu_url = ''
|
|
||||||
# Show execution tacking log, if OGAgent runs on ogLive
|
|
||||||
os_type = operations.os_type.lower()
|
|
||||||
if os_type == 'oglive':
|
|
||||||
menu_url = self.browser['url']
|
|
||||||
self._launch_browser('http://localhost/cgi-bin/httpd-log.sh')
|
|
||||||
# Executing command
|
|
||||||
(stat, out, err) = operations.exec_command(code)
|
|
||||||
# Removing command from the list
|
|
||||||
for c in self.commands:
|
|
||||||
if c.getName() == op_id:
|
|
||||||
self.commands.remove(c)
|
|
||||||
# Removing the REST API prefix, if needed
|
|
||||||
if route.startswith(self.REST.endpoint):
|
|
||||||
route = route[len(self.REST.endpoint):]
|
|
||||||
# Sending results
|
|
||||||
self.REST.sendMessage(route, {'mac': self.interface.mac, 'ip': self.interface.ip, 'trace': op_id,
|
|
||||||
'status': stat, 'output': out, 'error': err})
|
|
||||||
# Show latest menu, if OGAgent runs on ogLive
|
|
||||||
if os_type == 'oglive':
|
|
||||||
self._launch_browser(menu_url)
|
self._launch_browser(menu_url)
|
||||||
|
|
||||||
def onActivation(self):
|
def onActivation(self):
|
||||||
|
@ -262,20 +225,20 @@ class OpenGnSysWorker(ServerWorker):
|
||||||
# Completing OGAgent initialization process
|
# Completing OGAgent initialization process
|
||||||
os_type = operations.os_type.lower()
|
os_type = operations.os_type.lower()
|
||||||
if os_type == 'oglive':
|
if os_type == 'oglive':
|
||||||
### Following code may be separated in a different function to launch browser while catching disk configuration
|
# # Following code may be separated in a different function to launch browser while get disk configuration
|
||||||
message = """
|
message = """
|
||||||
<html>
|
<html>
|
||||||
<head></head>
|
<head></head>
|
||||||
<style>
|
<style>
|
||||||
#barra { width: 20px; height: 10px; position: relative; background: darkslategrey; }
|
#bar { width: 20px; height: 10px; position: relative; background: darkslategrey; }
|
||||||
</style>
|
</style>
|
||||||
<body>
|
<body>
|
||||||
<h1 style="margin: 5em 0 0 5em; font-size: 250%; color: darkslategrey;">
|
<h1 style="margin: 5em 0 0 5em; font-size: 250%; color: darkslategrey;">
|
||||||
<span id="opengnsys"><span style="font-weight: lighter;">Open</span>Gnsys 3</div>
|
<span id="opengnsys"><span style="font-weight: lighter;">Open</span>Gnsys 3</div>
|
||||||
<div id="barra"></span>
|
<div id="bar"></span>
|
||||||
</h1>
|
</h1>
|
||||||
<script>
|
<script>
|
||||||
var elem = document.getElementById("barra");
|
var elem = document.getElementById("bar");
|
||||||
var max = document.getElementById("opengnsys").offsetWidth;
|
var max = document.getElementById("opengnsys").offsetWidth;
|
||||||
var pos = 0;
|
var pos = 0;
|
||||||
var inc = true;
|
var inc = true;
|
||||||
|
@ -405,6 +368,7 @@ class OpenGnSysWorker(ServerWorker):
|
||||||
# Rebooting thread
|
# Rebooting thread
|
||||||
def rebt():
|
def rebt():
|
||||||
operations.reboot()
|
operations.reboot()
|
||||||
|
|
||||||
threading.Thread(target=rebt).start()
|
threading.Thread(target=rebt).start()
|
||||||
return {'op': 'launched'}
|
return {'op': 'launched'}
|
||||||
|
|
||||||
|
@ -424,6 +388,7 @@ class OpenGnSysWorker(ServerWorker):
|
||||||
def pwoff():
|
def pwoff():
|
||||||
time.sleep(2)
|
time.sleep(2)
|
||||||
operations.poweroff()
|
operations.poweroff()
|
||||||
|
|
||||||
threading.Thread(target=pwoff).start()
|
threading.Thread(target=pwoff).start()
|
||||||
return {'op': 'launched'}
|
return {'op': 'launched'}
|
||||||
|
|
||||||
|
@ -433,12 +398,14 @@ class OpenGnSysWorker(ServerWorker):
|
||||||
Processes an script execution (script should be encoded in base64)
|
Processes an script execution (script should be encoded in base64)
|
||||||
:param path:
|
:param path:
|
||||||
:param get_params:
|
:param get_params:
|
||||||
:param post_params: JSON object {"script": "commands"}
|
:param post_params: object with format:
|
||||||
:param server: authorization header
|
id: operation id.
|
||||||
:return: JSON object {"op": "launched"}
|
script: command code
|
||||||
|
redirect_url: callback REST route
|
||||||
|
:param server: headers data
|
||||||
|
:rtype: JSON object with launching status
|
||||||
"""
|
"""
|
||||||
logger.debug('Processing script request')
|
logger.debug('Processing script operation with params: {}'.format(post_params))
|
||||||
|
|
||||||
# Processing data
|
# Processing data
|
||||||
try:
|
try:
|
||||||
script = urllib.unquote(post_params.get('script').decode('base64')).decode('utf8')
|
script = urllib.unquote(post_params.get('script').decode('base64')).decode('utf8')
|
||||||
|
@ -495,8 +462,8 @@ class OpenGnSysWorker(ServerWorker):
|
||||||
:return: object
|
:return: object
|
||||||
"""
|
"""
|
||||||
serial_no = '' # Serial number
|
serial_no = '' # Serial number
|
||||||
storage = [] # Storage configuration
|
storage = [] # Storage configuration
|
||||||
warnings = 0 # Number of warnings
|
warnings = 0 # Number of warnings
|
||||||
logger.debug('Received getconfig operation')
|
logger.debug('Received getconfig operation')
|
||||||
# Processing data
|
# Processing data
|
||||||
for row in operations.get_configuration().split(';'):
|
for row in operations.get_configuration().split(';'):
|
||||||
|
@ -511,7 +478,7 @@ class OpenGnSysWorker(ServerWorker):
|
||||||
elif len(cols) == 7:
|
elif len(cols) == 7:
|
||||||
disk, part_no, part_type, fs, op_sys, size, usage = cols
|
disk, part_no, part_type, fs, op_sys, size, usage = cols
|
||||||
try:
|
try:
|
||||||
if int(npart) == 0:
|
if int(part_no) == 0:
|
||||||
# Disk information
|
# Disk information
|
||||||
storage.append({'disk': int(disk), 'parttable': int(part_type), 'size': int(size)})
|
storage.append({'disk': int(disk), 'parttable': int(part_type), 'size': int(size)})
|
||||||
else:
|
else:
|
||||||
|
@ -529,38 +496,6 @@ class OpenGnSysWorker(ServerWorker):
|
||||||
# Returning configuration data and count of warnings
|
# Returning configuration data and count of warnings
|
||||||
return {'serial': serial_no, 'storage': storage, 'warnings': warnings}
|
return {'serial': serial_no, 'storage': storage, 'warnings': warnings}
|
||||||
|
|
||||||
@check_secret
|
|
||||||
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: operation id.
|
|
||||||
script: command code
|
|
||||||
redirect_url: callback REST route
|
|
||||||
:param server: headers data
|
|
||||||
:rtype: object with launching status
|
|
||||||
"""
|
|
||||||
logger.debug('Received command operation with params: {}'.format(post_params))
|
|
||||||
# Processing data
|
|
||||||
try:
|
|
||||||
script = post_params.get('script')
|
|
||||||
op_id = post_params.get('id')
|
|
||||||
route = post_params.get('redirect_url')
|
|
||||||
# Checking if the thread id. exists
|
|
||||||
for c in self.commands:
|
|
||||||
if c.getName() == str(op_id):
|
|
||||||
raise Exception('Task id. already exists: {}'.format(op_id))
|
|
||||||
# Launching a new thread
|
|
||||||
thr = threading.Thread(name=op_id, target=self.task_command, args=(script, route, op_id))
|
|
||||||
thr.start()
|
|
||||||
self.commands.append(thr)
|
|
||||||
except Exception as e:
|
|
||||||
logger.error('Got exception {}'.format(e))
|
|
||||||
return {'error': e}
|
|
||||||
return {'op': 'launched'}
|
|
||||||
|
|
||||||
@check_secret
|
@check_secret
|
||||||
def process_execinfo(self, path, get_params, post_params, server):
|
def process_execinfo(self, path, get_params, post_params, server):
|
||||||
"""
|
"""
|
||||||
|
@ -597,6 +532,7 @@ class OpenGnSysWorker(ServerWorker):
|
||||||
:param get_params:
|
:param get_params:
|
||||||
:param post_params:
|
:param post_params:
|
||||||
:param server:
|
:param server:
|
||||||
|
:return: array of component data objects
|
||||||
"""
|
"""
|
||||||
data = []
|
data = []
|
||||||
logger.debug('Received hardware operation')
|
logger.debug('Received hardware operation')
|
||||||
|
|
Loading…
Reference in New Issue