diff --git a/ctl b/ctl index 6f209f6..4cb3d14 100755 --- a/ctl +++ b/ctl @@ -73,7 +73,8 @@ if [ "$1" == "restart" ]; then "$0" start exit $? else - exit 1 + "$0" start + exit $? fi fi if [ "$1" == "open" ]; then diff --git a/oml/api.py b/oml/api.py index 7810942..53e0fd9 100644 --- a/oml/api.py +++ b/oml/api.py @@ -10,6 +10,8 @@ import ox from oxtornado import actions +import settings + import item.api import user.api @@ -76,3 +78,34 @@ def autocompleteFolder(data): 'items': ox.sorted_strings(folders) } actions.register(autocompleteFolder, cache=False) + +def getVersion(data): + ''' + check if new version is available + ''' + response = { + 'current': settings.MINOR_VERSION, + 'upgrade': False, + } + if not os.path.exists(os.path.join(settings.updates_path, 'release.json')): + return response + if not os.path.exists(os.path.join(settings.config_path, 'release.json')): + return response + with open(os.path.join(settings.updates_path, 'release.json')) as fd: + release = json.load(fd) + current = settings.release['modules']['openmedialibrary']['version'] + response['current'] = current + new = release['modules']['openmedialibrary']['version'] + response['new'] = new + response['update'] = current < new + return response +actions.register(getVersion, cache=False) + +def restart(data): + ''' + restart (and upgrade if upgrades are available) + ''' + #subprocess.Popen(['./ctl', 'restart'], preexec_fn=os.setpgrp, close_fds=True) + subprocess.Popen(['./ctl', 'restart'], close_fds=True) + return {} +actions.register(restart, cache=False) diff --git a/oml/server.py b/oml/server.py index eee6187..6c72380 100644 --- a/oml/server.py +++ b/oml/server.py @@ -4,6 +4,7 @@ from __future__ import division, print_function import os import sys +import signal from tornado.httpserver import HTTPServer from tornado.ioloop import IOLoop @@ -94,14 +95,20 @@ def run(): host = settings.server['address'] url = 'http://%s:%s/' % (host, settings.server['port']) print('open browser at %s' % url) + + def shutdown(): + if state.downloads: + state.downloads.join() + if state.tasks: + state.tasks.join() + if state.nodes: + state.nodes.join() + http_server.stop() + + signal.signal(signal.SIGTERM, shutdown) + try: state.main.start() except: print('shutting down...') - - if state.downloads: - state.downloads.join() - if state.tasks: - state.tasks.join() - if state.nodes: - state.nodes.join() + shutdown() diff --git a/oml/update.py b/oml/update.py index d8e30d6..3e2f970 100644 --- a/oml/update.py +++ b/oml/update.py @@ -56,6 +56,7 @@ def check(): old = settings.release['modules']['openmedialibrary']['version'] new = release['modules']['openmedialibrary']['version'] return verify(release) and old < new + return False def download(): if not os.path.exists(os.path.join(settings.config_path, 'release.json')): diff --git a/static/js/appDialog.js b/static/js/appDialog.js index c472124..8fe4fdb 100644 --- a/static/js/appDialog.js +++ b/static/js/appDialog.js @@ -14,7 +14,8 @@ oml.ui.appDialog = function() { $panel = Ox.TabPanel({ content: function(id) { - var $logo = Ox.Element(), + var $content = Ox.Element(), + $logo = Ox.Element(), $text = Ox.Element() .addClass('OxTextPage'), title = Ox.getObjectById( @@ -33,6 +34,47 @@ oml.ui.appDialog = function() { height: '192px' }) .appendTo($logo); + if (id == 'update') { + $content.html('

' + title + '

'); + oml.api.getVersion(function(response) { + if (response.data.update) { + Ox.Element() + .css({ + paddingTop: '4px', + paddingBottom: '16px' + }) + .html('A new version of Open Media Library is available') + .appendTo($content); + Ox.Button({ + id: 'update', + title: Ox._('Install Now') + }).bindEvent({ + click: function() { + this.options({ + disabled: true, + title: 'Installing...' + }); + oml.api.restart(function(response) { + if (response.status.code == 200) { + setTimeout(reload, 500); + } + }); + } + }).appendTo($content); + } else if (response.data.current == 'git') { + Ox.Element() + .html('To update a development version, run ./ctl update') + .appendTo($content); + } else { + Ox.Element() + .html('No updates available') + .appendTo($content); + } + }); + } else { + $content.html('

' + title + '

' + + '

The lazy brown fox jumped over the lazy black fox, but otherwise not really much happened here since you last checked.'); + } $('

') .css({ position: 'absolute', @@ -41,10 +83,7 @@ oml.ui.appDialog = function() { top: '16px', overflowY: 'auto' }) - .html( - '

' + title + '

' - + '

The lazy brown fox jumped over the lazy black fox, but otherwise not really much happened here since you last checked.' - ) + .append($content) .appendTo($text); return Ox.SplitPanel({ elements: [ @@ -103,6 +142,20 @@ oml.ui.appDialog = function() { $panel.selectTab(section); }; + function reload() { + var ws = new WebSocket('ws:' + document.location.host + '/ws'); + ws.onopen = function(event) { + document.location.href = document.location.protocol + '//' + document.location.host; + }; + ws.onerror = function(event) { + ws.close(); + }; + ws.onclose = function(event) { + console.log('waiting...'); + setTimeout(reload, 500); + }; + } + return that; -}; \ No newline at end of file +};