From 8ef3e73b9edb0a03bf7b4aa66a398981e1455a81 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ram=C3=B3n=20M=2E=20G=C3=B3mez?= Date: Thu, 23 May 2019 13:30:23 +0200 Subject: [PATCH] #908 OGAgent for ogLive code clean up. --- oglive/Makefile | 8 +-- src/OGAgent.manifest | 5 +- .../modules/server/OpenGnSys/__init__.py | 67 +++++++++++++++++-- 3 files changed, 67 insertions(+), 13 deletions(-) diff --git a/oglive/Makefile b/oglive/Makefile index 84e0610..707bc67 100644 --- a/oglive/Makefile +++ b/oglive/Makefile @@ -22,12 +22,12 @@ install-ogagent: mkdir -p $(SBINDIR) mkdir -p $(APPSDIR) mkdir -p $(CFGDIR) - + mkdir $(LIBDIR)/img - + # Cleans up .pyc and cache folders rm -f $(PYC) $(CACHES) - + cp -r $(SOURCEDIR)/opengnsys $(LIBDIR)/opengnsys cp -r $(SOURCEDIR)/cfg $(LIBDIR)/cfg ln -fs $(LIBDIR)/cfg/ogagent.cfg $(CFGDIR) @@ -35,7 +35,7 @@ install-ogagent: cp scripts/ogagent $(BINDIR) chmod 755 $(BINDIR)/ogagent - + uninstall: rm -rf $(LIBDIR) rm -f $(BINDIR)/ogagent diff --git a/src/OGAgent.manifest b/src/OGAgent.manifest index 0e5ff97..0aecb2c 100644 --- a/src/OGAgent.manifest +++ b/src/OGAgent.manifest @@ -1,9 +1,10 @@ +<<<<<<< HEAD Description diff --git a/src/opengnsys/modules/server/OpenGnSys/__init__.py b/src/opengnsys/modules/server/OpenGnSys/__init__.py index 45f92cb..bc9909f 100644 --- a/src/opengnsys/modules/server/OpenGnSys/__init__.py +++ b/src/opengnsys/modules/server/OpenGnSys/__init__.py @@ -108,10 +108,63 @@ class OpenGnSysWorker(ServerWorker): interface = None # Bound interface for OpenGnsys REST = None # REST object logged_in = False # User session flag - locked = {} # Locked partitions - commands = [] # Running commands - random = None # Random string for secure connections - length = 32 # Random string length + browser = {} # Browser info + commands = [] # Running commands + random = None # Random string for secure connections + length = 32 # Random string length + access_token = refresh_token = None # Server authorization tokens + grant_type = 'http://opengnsys.es/grants/og_client' + + def _launch_browser(self, url): + """ + Launchs the Browser with specified URL + :param url: URL to show + """ + logger.debug('Launching browser with URL: {}'.format(url)) + # Trying to kill an old browser + try: + os.kill(self.browser['process'].pid, signal.SIGKILL) + except OSError: + logger.warn('Cannot kill the old browser process') + except KeyError: + # There is no previous browser + pass + self.browser['url'] = url + self.browser['process'] = subprocess.Popen(['browser', '-qws', url]) + + def _task_command(self, route, code, op_id, send_config=False): + """ + 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') + # Execute the code + (stat, out, err) = operations.exec_command(code) + # Remove command from the list + for c in self.commands: + if c.getName() == op_id: + self.commands.remove(c) + # Remove the REST API prefix, if needed + if route.startswith(self.REST.endpoint): + route = route[len(self.REST.endpoint):] + # Send back exit status and outputs (base64-encoded) + self.REST.sendMessage(route, {'mac': self.interface.mac, 'ip': self.interface.ip, 'trace': op_id, + 'status': stat, 'output': out.encode('utf8').encode('base64'), + 'error': err.encode('utf8').encode('base64')}) + # Show latest menu, if OGAgent runs on ogLive + if os_type == 'oglive': + # Send configuration data, if needed + if send_config: + self.REST.sendMessage('clients/configs', {'mac': self.interface.mac, 'ip': self.interface.ip, + 'config': operations.get_configuration()}) + self._launch_browser(menu_url) def onActivation(self): """ @@ -265,7 +318,7 @@ class OpenGnSysWorker(ServerWorker): :param server: :return: JSON object {"status": "status_code", "loggedin": boolean} """ - res = {'loggedin': self.loggedin} + res = {'loggedin': self.logged_in} try: res['status'] = operations.os_type.lower() except KeyError: @@ -340,7 +393,7 @@ class OpenGnSysWorker(ServerWorker): Closes user session """ logger.debug('Received logoff operation') - # Sending log off message to OGAgent client + # Send log off message to OGAgent client self.sendClientMessage('logoff', {}) return {'op': 'sent to client'} @@ -350,7 +403,7 @@ class OpenGnSysWorker(ServerWorker): Shows a message popup on the user's session """ logger.debug('Received message operation') - # Sending popup message to OGAgent client + # Send popup message to OGAgent client self.sendClientMessage('popup', post_params) return {'op': 'launched'}