From 23041d5a8b1e98eebf6134bc398f0044625438ff Mon Sep 17 00:00:00 2001 From: Calin Crisan Date: Wed, 2 Jul 2014 13:28:56 +0200 Subject: [PATCH] added rw remounting of root partition before update --- src/handlers.py | 1 + src/server.py | 2 +- src/update.py | 35 ++++++++++++++++++++++++++--------- static/js/main.js | 26 ++++++++++++++++++++++---- 4 files changed, 50 insertions(+), 14 deletions(-) diff --git a/src/handlers.py b/src/handlers.py index f9e5f01..a317f6a 100644 --- a/src/handlers.py +++ b/src/handlers.py @@ -310,6 +310,7 @@ class ConfigHandler(BaseHandler): if reboot[0]: if settings.ENABLE_REBOOT: def call_reboot(): + logging.info('rebooting') os.system('reboot') ioloop = IOLoop.instance() diff --git a/src/server.py b/src/server.py index 181b45e..57153f2 100644 --- a/src/server.py +++ b/src/server.py @@ -51,7 +51,7 @@ application = Application( (r'^/update/?$', handlers.UpdateHandler), (r'^.*$', handlers.NotFoundHandler), ], - debug=True, # enables autoreload + debug=not settings.ENABLE_REBOOT, # enables autoreload when reboot is disabled log_function=log_request, static_path=settings.STATIC_PATH, static_url_prefix=settings.STATIC_URL diff --git a/src/update.py b/src/update.py index 21d8c75..e053635 100644 --- a/src/update.py +++ b/src/update.py @@ -15,14 +15,18 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . +import datetime import json import logging import os.path import shutil +import subprocess import tempfile import time import urllib2 +from tornado.ioloop import IOLoop + import settings @@ -141,19 +145,25 @@ def cleanup(path): logging.error('could cleanup update directory: %(msg)s' % {'msg': unicode(e)}) -def is_updatable(): - # the parent directory of the project directory - # needs to be writable in order for the updating to be possible - - parent = os.path.dirname(settings.PROJECT_PATH) - - return os.access(parent, os.W_OK) - - def perform_update(version): logging.info('updating to version %(version)s...' % {'version': version}) try: + # make sure the partition where motionEye resides is writable + if settings.ENABLE_REBOOT: + try: + df_lines = subprocess.check_output('df %s' % settings.PROJECT_PATH, shell=True).split('\n') + last_line = [l for l in df_lines if l.strip()][-1] + mount_point = last_line.split()[-1] + + os.system('mount -o remount,rw %s' % mount_point) + + except Exception as e: + logging.error('failed to remount root partition rw: %s' % e, exc_info=True) + + if not os.access(settings.PROJECT_PATH, os.W_OK): + raise Exception('path "%s" is not writable' % settings.PROJECT_PATH) + # download the archive archive = download(version) temp_path = os.path.dirname(archive) @@ -188,6 +198,13 @@ def perform_update(version): logging.info('updating done') + if settings.ENABLE_REBOOT: + def call_reboot(): + logging.info('rebooting') + os.system('reboot') + + IOLoop.instance().add_timeout(datetime.timedelta(seconds=2), call_reboot) + return True except Exception as e: diff --git a/static/js/main.js b/static/js/main.js index e9bd2ba..dc4fd38 100644 --- a/static/js/main.js +++ b/static/js/main.js @@ -1039,11 +1039,29 @@ function doUpdate() { showModalDialog(''); ajax('POST', '/update/?version=' + data.update_version, null, function (result) { if (result) { - setTimeout(function () { - runAlertDialog('motionEye was successfully updated!', function () { - window.location.reload(true); + var count = 0; + function checkServer() { + $.ajax({ + type: 'GET', + url: '/config/0/get/', + success: function () { + runAlertDialog('motionEye was successfully updated!', function () { + window.location.reload(true); + }); + }, + error: function () { + if (count < 25) { + count += 1; + setTimeout(checkServer, 2000); + } + else { + window.location.reload(true); + } + } }); - }, 10000); + } + + setTimeout(checkServer, 15000); } else { runAlertDialog('Update failed!', function () { -- 2.39.5