From 9fd926d690aef5ac1b3085a1d26a4192cf945adc Mon Sep 17 00:00:00 2001 From: Stavros Sachtouris <saxtouri@admin.grnet.gr> Date: Tue, 21 Apr 2015 15:30:49 +0300 Subject: [PATCH] Emancipate GUI when retrieving endpoints This patch is related to the endpoint URLs needed to make various UI operations i.e., login and vising the pithos page on a browser. The helper used to provide the GUI with all URLs necessary, while now they are retrieved by the GUI itself, with the tokenless request "POST <auth_url endpoint>/tokens". --- agkyra/agkyra/gui.py | 14 ++++++-------- agkyra/agkyra/gui/menu.html | 17 ++++++++++++----- agkyra/agkyra/gui/protocol.js | 7 +++---- agkyra/agkyra/gui/settings.html | 19 +++++++------------ agkyra/agkyra/gui/settings.js | 30 ++++++++++++++++++++++++++++++ 5 files changed, 58 insertions(+), 29 deletions(-) diff --git a/agkyra/agkyra/gui.py b/agkyra/agkyra/gui.py index b622ab2..25c6804 100644 --- a/agkyra/agkyra/gui.py +++ b/agkyra/agkyra/gui.py @@ -20,11 +20,10 @@ LOG = logging.getLogger(__name__) class GUI(WebSocketBaseClient): """Launch the GUI when the helper server is ready""" - def __init__(self, addr, gui_exec_path, gui_id): + def __init__(self, addr, gui_id): """Initialize the GUI Launcher""" super(GUI, self).__init__(addr) self.addr = addr - self.gui_exec_path = gui_exec_path self.gui_id = gui_id self.start = self.connect @@ -39,11 +38,10 @@ class GUI(WebSocketBaseClient): with NamedTemporaryFile(delete=False) as fp: json.dump(dict(gui_id=self.gui_id, address=self.addr), fp) # subprocess.call blocks the execution - LOG.debug('RUN: %s %s' % (self.gui_exec_path, fp.name)) + LOG.debug('RUN: %s' % (fp.name)) subprocess.call([ - '/home/saxtouri/node-webkit-v0.11.6-linux-x64/nw', - # self.gui_exec_path, - abspath('gui/gui.nw'), + abspath('agkyra/nwjs/nw'), + abspath('agkyra/gui.nw'), fp.name, '--data-path', abspath('~/.agkyra')]) LOG.debug('GUI process closed, remove temp file') @@ -85,11 +83,11 @@ class HelperServer(object): t.join() -def run(gui_exec_path): +def run(): """Prepare helper and GUI and run them in the proper order""" server = HelperServer() addr = 'ws://localhost:%s' % server.port - gui = GUI(addr, gui_exec_path, server.gui_id) + gui = GUI(addr, server.gui_id) LOG.info('Start helper server') server.start() diff --git a/agkyra/agkyra/gui/menu.html b/agkyra/agkyra/gui/menu.html index bcba22b..194aeb3 100644 --- a/agkyra/agkyra/gui/menu.html +++ b/agkyra/agkyra/gui/menu.html @@ -4,6 +4,7 @@ <body> <script src="protocol.js"></script> <script src="settings.js"></script> + <script src="static/js/jquery.js"></script> <script type="text/javascript"> // Setup GUI @@ -69,9 +70,8 @@ var pithos_page_menu = new gui.MenuItem({ icon: 'images/pithos.png', enabled: false, click: function () { - var pithos_ui = globals['settings']['pithos_ui']; - console.log('Visit ' + pithos_ui); - gui.Shell.openExternal(pithos_ui); + console.log('Visit ' + get_pithos_ui()); + gui.Shell.openExternal(get_pithos_ui()); } }); menu.append(pithos_page_menu); @@ -123,6 +123,7 @@ menu.append(new gui.MenuItem({ // Update progress var client_ready = false; window.setInterval(function() { + var menu_modified = false; if (!client_ready) { if (globals.authenticated) { settings_menu.enabled = true; @@ -132,11 +133,18 @@ window.setInterval(function() { if (client_ready) { if (!pithos_page_menu.enabled) { - // GET pithos page with tokenless operations + if ((get_pithos_ui() == null) && (globals.settings.url)) { + refresh_endpoints(globals.settings.url); + } + if (get_pithos_ui() != null) { + pithos_page_menu.enabled = true; + tray.menu = menu; + } } if (!local_folder_menu.enabled) { if (globals.settings.directory) { local_folder_menu.enabled = true; + tray.menu = menu; } } } @@ -144,7 +152,6 @@ window.setInterval(function() { var status = globals['status']; var new_progress = progress_item.label; var new_pause = pause_item.label; - var menu_modified = false; if (status['paused'] !== null) { switch(pause_item.label) { case pause_syncing: if (status['paused']) { diff --git a/agkyra/agkyra/gui/protocol.js b/agkyra/agkyra/gui/protocol.js index 87b56e8..58bce87 100644 --- a/agkyra/agkyra/gui/protocol.js +++ b/agkyra/agkyra/gui/protocol.js @@ -16,11 +16,10 @@ var globals = { 'url': null, 'container': null, 'directory': null, - 'pithos_ui': null, 'exclude': null }, 'status': {"progress": null, "paused": null}, - 'authenticated': false + 'authenticated': false, } // Protocol: requests ::: responses @@ -66,7 +65,7 @@ socket.onopen = function() { } socket.onmessage = function(e) { var r = JSON.parse(e.data) - //console.log('RECV: ' + r['action']) + console.log('RECV: ' + r['action']) switch(r['action']) { case 'post gui_id': if (r['ACCEPTED'] === 202) { @@ -87,7 +86,7 @@ socket.onmessage = function(e) { } break; case 'get settings': - console.log(r); + // console.log(r); globals['settings'] = r; break; case 'put settings': diff --git a/agkyra/agkyra/gui/settings.html b/agkyra/agkyra/gui/settings.html index 5975734..9233fac 100644 --- a/agkyra/agkyra/gui/settings.html +++ b/agkyra/agkyra/gui/settings.html @@ -23,8 +23,7 @@ $('#exclude').val( fs.readFileSync(exclude, encoding='utf-8')); } catch (err) {console.log(err);} - var pithos_ui = get_setting('pithos_ui'); - if (pithos_ui) { + if (get_pithos_ui()) { $('#get_creds').show(); } else { console.log('No pithos view, remove credential button'); @@ -48,14 +47,15 @@ } var gui = require('nw.gui'); - function extract_cookie(url) { + function get_credentials() { var cookie_name = '_pithos2_a'; - var logout_url = 'https://accounts.okeanos.grnet.gr/ui/logout' - var w = gui.Window.open(logout_url, { + var account_ui = get_account_ui() + var w = gui.Window.open(account_ui + '/logout', { focus: false, width: 20, height: 20 }); w.close(); - var w = gui.Window.open(logout_url, { + var next = get_pithos_ui(); + var w = gui.Window.open(account_ui + '/login?next=' + next, { focus: true, width: 520, height: 920 }); w.cookies.onChanged.addListener(function(info) { @@ -66,11 +66,6 @@ } }); } - - function get_credentials() { - var pithos_ui = get_setting('pithos_ui'); - extract_cookie(pithos_ui); - } </script> </head> <body> @@ -90,7 +85,7 @@ </div> <div class="small-9 columns"> <input type="text" id="cloud-url" placeholder="Authentication URL" - onchange="set_setting('url', $(this).val())"> + onchange="set_setting('url', $(this).val()); refresh_endpoints($(this).val())"> <small class="error" style="visibility: hidden">Invalid entry</small> </div> </div> diff --git a/agkyra/agkyra/gui/settings.js b/agkyra/agkyra/gui/settings.js index 0df6d76..a3d1e91 100644 --- a/agkyra/agkyra/gui/settings.js +++ b/agkyra/agkyra/gui/settings.js @@ -17,3 +17,33 @@ function get_setting(key) { function set_setting(key, val) { global.settings[key] = val; } + +function refresh_endpoints(identity_url) { + $.post(identity_url + '/tokens', function(data) { + var endpoints = data.access.serviceCatalog + global.pithos_ui = null; + global.account_ui = null; + $.each(endpoints, function(i, endpoint) { + switch(endpoint.type) { + case 'object-store': try { + global.pithos_ui = endpoint['endpoints'][0]['SNF:uiURL']; + } catch(err) { console.log('Failed to get pithos_ui ' + err); } + break; + case 'account': try { + global.account_ui = endpoint['endpoints'][0]['SNF:uiURL']; + } catch(err) { console.log('Failed to get account_ui ' + err); } + break; + } + }); + }); +} + +function get_pithos_ui() { + if (global.pithos_ui) {return global.pithos_ui;} + else {return null;} +} + +function get_account_ui() { + if (global.account_ui) {return global.account_ui;} + else {return null;} +} -- GitLab