From 582ab7873b33b5ed2e0d7fd334ff7bda1567ab09 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ram=C3=B3n=20M=2E=20G=C3=B3mez?= Date: Mon, 30 Sep 2019 13:27:04 +0200 Subject: [PATCH] #930: OGAgent for ogLive can launch the Browser. --- .../modules/server/OpenGnSys/__init__.py | 161 +++++++++++------- 1 file changed, 100 insertions(+), 61 deletions(-) diff --git a/src/opengnsys/modules/server/OpenGnSys/__init__.py b/src/opengnsys/modules/server/OpenGnSys/__init__.py index 82ba7dd..cbfe122 100644 --- a/src/opengnsys/modules/server/OpenGnSys/__init__.py +++ b/src/opengnsys/modules/server/OpenGnSys/__init__.py @@ -41,7 +41,6 @@ import urllib from opengnsys import REST from opengnsys import operations from opengnsys.log import logger -from opengnsys.scriptThread import ScriptExecutorThread from opengnsys.workers import ServerWorker from six.moves.urllib import parse @@ -167,6 +166,46 @@ class OpenGnSysWorker(ServerWorker): '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) + def onActivation(self): """ Sends OGAgent activation notification to OpenGnsys server @@ -222,21 +261,11 @@ class OpenGnSysWorker(ServerWorker): logger.debug('Successful connection after {} tries'.format(t)) elif t == 100: raise Exception('Initialization error: Cannot connect to remote server') - # Delete marking files - for f in ['ogboot.me', 'ogboot.firstboot', 'ogboot.secondboot']: - try: - os.remove(os.sep + f) - except OSError: - pass - # Copy file "HostsFile.FirstOctetOfIPAddress" to "HostsFile", if it exists - # (used in "exam mode" from the University of Seville) - hosts_file = os.path.join(operations.get_etc_path(), 'hosts') - new_hosts_file = hosts_file + '.' + self.interface.ip.split('.')[0] - if os.path.isfile(new_hosts_file): - shutil.copyfile(new_hosts_file, hosts_file) - ### Separate in a different function to launch browser while catching disk configuration - # Create HTML file (TEMPORARY) - message = """ + # Completing OGAgent initialization process + os_type = operations.os_type.lower() + if os_type == 'oglive': + ### Following code may be separated in a different function to launch browser while catching disk configuration + message = """