install updates on startup, check for updates

This commit is contained in:
j 2014-08-07 11:46:23 +02:00
parent 792afe0824
commit 537eba0f0f
6 changed files with 89 additions and 41 deletions

7
ctl
View File

@ -44,6 +44,9 @@ if [ "$1" == "start" ]; then
echo openmedialibrary already running echo openmedialibrary already running
exit 1 exit 1
fi fi
if [ ! -d "$BASE/$NAME/.git" ]; then
python2 oml install_update
fi
python2 oml server $PID python2 oml server $PID
rm -f $PID rm -f $PID
exit $? exit $?
@ -97,7 +100,7 @@ if [ "$1" == "update" ]; then
echo Update $NAME.. echo Update $NAME..
cd "$BASE/$NAME" cd "$BASE/$NAME"
git pull git pull
find . -name '*.pyc' -exec rm "{}" \; find . -name "*.pyc" -exec rm "{}" \;
"$0" setup "$0" setup
"$0" update_static > /dev/null "$0" update_static > /dev/null
NEW=`"$0" version` NEW=`"$0" version`
@ -105,7 +108,7 @@ if [ "$1" == "update" ]; then
else else
python2 oml update python2 oml update
fi fi
exit exit $?
fi fi
if [ "$1" == "python" ]; then if [ "$1" == "python" ]; then
cd "$BASE/$NAME" cd "$BASE/$NAME"

View File

@ -32,6 +32,7 @@ manager.add_command('db', MigrateCommand)
manager.add_command('release', commands.Release) manager.add_command('release', commands.Release)
manager.add_command('debug', commands.Debug) manager.add_command('debug', commands.Debug)
manager.add_command('update', commands.Update) manager.add_command('update', commands.Update)
manager.add_command('install_update', commands.InstallUpdate)
manager.add_command('start', commands.Start) manager.add_command('start', commands.Start)
manager.add_command('stop', commands.Stop) manager.add_command('stop', commands.Stop)
manager.add_command('setup', commands.Setup) manager.add_command('setup', commands.Setup)

View File

@ -5,6 +5,7 @@ from __future__ import division
import subprocess import subprocess
from os.path import join, exists, dirname from os.path import join, exists, dirname
import os import os
import sys
from flask.ext.script import Command, Option from flask.ext.script import Command, Option
@ -65,16 +66,24 @@ class Stop(Command):
def run(self): def run(self):
pass pass
class InstallUpdate(Command):
"""
Update to latest development version
"""
def run(self):
import update
if not update.install():
print "UPDATE FAILED"
sys.exit(1)
class Update(Command): class Update(Command):
""" """
Update to latest development version Update to latest development version
""" """
def run(self): def run(self):
import update import update
if update.update(): if not (update.download() and update.install()):
print "OK" print "UPDATE FAILED"
else:
print "FAILED"
class PostUpdate(Command): class PostUpdate(Command):
""" """

View File

@ -7,6 +7,8 @@ import time
import logging import logging
import state import state
import settings
import update
logger = logging.getLogger('oml.downloads') logger = logging.getLogger('oml.downloads')
@ -19,8 +21,15 @@ class Downloads(Thread):
self.daemon = True self.daemon = True
self.start() self.start()
def download_updates(self):
now = int(time.mktime(time.gmtime()))
if now > settings.server.get('last_update_check', 0) + 24*60*60:
settings.server['last_update_check'] = now
update.download()
def download_next(self): def download_next(self):
import item.models import item.models
self.download_updates()
for t in item.models.Transfer.query.filter( for t in item.models.Transfer.query.filter(
item.models.Transfer.added!=None, item.models.Transfer.added!=None,
item.models.Transfer.progress<1).order_by(item.models.Transfer.added): item.models.Transfer.progress<1).order_by(item.models.Transfer.added):

View File

@ -14,15 +14,15 @@ updates_path = os.path.normpath(os.path.join(base_dir, '..', 'updates'))
oml_config_path = os.path.join(base_dir, 'config.json') oml_config_path = os.path.join(base_dir, 'config.json')
config_dir = os.path.normpath(os.path.join(base_dir, '..', 'config')) config_path = os.path.normpath(os.path.join(base_dir, '..', 'config'))
if not os.path.exists(config_dir): if not os.path.exists(config_path):
os.makedirs(config_dir) os.makedirs(config_path)
db_path = os.path.join(config_dir, 'data.db') db_path = os.path.join(config_path, 'data.db')
icons_db_path = os.path.join(config_dir, 'icons.db') icons_db_path = os.path.join(config_path, 'icons.db')
key_path = os.path.join(config_dir, 'node.key') key_path = os.path.join(config_path, 'node.key')
ssl_cert_path = os.path.join(config_dir, 'node.ssl.crt') ssl_cert_path = os.path.join(config_path, 'node.ssl.crt')
ssl_key_path = os.path.join(config_dir, 'node.ssl.key') ssl_key_path = os.path.join(config_path, 'node.ssl.key')
db = SQLAlchemy() db = SQLAlchemy()
@ -32,10 +32,10 @@ if os.path.exists(oml_config_path):
else: else:
config = {} config = {}
preferences = pdict(os.path.join(config_dir, 'preferences.json'), config['user']['preferences']) preferences = pdict(os.path.join(config_path, 'preferences.json'), config['user']['preferences'])
ui = pdict(os.path.join(config_dir, 'ui.json'), config['user']['ui']) ui = pdict(os.path.join(config_path, 'ui.json'), config['user']['ui'])
server = pdict(os.path.join(config_dir, 'server.json')) server = pdict(os.path.join(config_path, 'server.json'))
server_defaults = { server_defaults = {
'port': 9842, 'port': 9842,
'address': '::1', 'address': '::1',
@ -51,7 +51,7 @@ for key in server_defaults:
if key not in server: if key not in server:
server[key] = server_defaults[key] server[key] = server_defaults[key]
release = pdict(os.path.join(config_dir, 'release.json')) release = pdict(os.path.join(config_path, 'release.json'))
if os.path.exists(key_path): if os.path.exists(key_path):
with open(key_path) as fd: with open(key_path) as fd:

View File

@ -32,8 +32,8 @@ def verify(release):
return False return False
return True return True
def download(url, filename): def download_module(url, filename):
print 'download', filename print 'download', os.path.basename(filename)
dirname = os.path.dirname(filename) dirname = os.path.dirname(filename)
if dirname and not os.path.exists(dirname): if dirname and not os.path.exists(dirname):
os.makedirs(dirname) os.makedirs(dirname)
@ -44,7 +44,7 @@ def download(url, filename):
f.write(data) f.write(data)
data = u.read(4096) data = u.read(4096)
def new_version(): def check():
if settings.release: if settings.release:
r = requests.get(RELEASE_URL) r = requests.get(RELEASE_URL)
release_data = r.content release_data = r.content
@ -53,7 +53,9 @@ def new_version():
new = release['modules']['openmedialibrary']['version'] new = release['modules']['openmedialibrary']['version']
return verify(release) and old < new return verify(release) and old < new
def update(): def download():
if not os.path.exists(os.path.join(settings.config_path, 'release.json')):
return True
r = requests.get(RELEASE_URL) r = requests.get(RELEASE_URL)
release_data = r.content release_data = r.content
release = json.loads(release_data) release = json.loads(release_data)
@ -61,36 +63,60 @@ def update():
new = release['modules']['openmedialibrary']['version'] new = release['modules']['openmedialibrary']['version']
if verify(release) and old < new: if verify(release) and old < new:
ox.makedirs(settings.updates_path) ox.makedirs(settings.updates_path)
os.chdir(os.path.dirname(settings.base_dir))
current_files = {'release.json'}
for module in release['modules']:
if release['modules'][module]['version'] > settings.release['modules'][module]['version']:
module_tar = os.path.join(settings.updates_path, release['modules'][module]['name'])
url = RELEASE_URL.replace('release.json', release['modules'][module]['name'])
if not os.path.exists(module_tar):
download_module(url, module_tar)
if ox.sha1sum(module_tar) != release['modules'][module]['sha1']:
os.unlink(module_tar)
return False
current_files.add(os.path.basename(module_tar))
with open(os.path.join(settings.updates_path, 'release.json'), 'w') as fd: with open(os.path.join(settings.updates_path, 'release.json'), 'w') as fd:
fd.write(release_data) fd.write(release_data)
for f in set(os.walk(settings.updates_path).next()[2])-current_files:
os.unlink(os.join(settings.updates_path, f))
return True
return True
def install():
if not os.path.exists(os.path.join(settings.updates_path, 'release.json')):
return True
if not os.path.exists(os.path.join(settings.config_path, 'release.json')):
return True
with open(os.path.join(settings.updates_path, 'release.json')) as fd:
release = json.load(fd)
old = settings.release['modules']['openmedialibrary']['version']
new = release['modules']['openmedialibrary']['version']
if verify(release) and old < new:
os.chdir(os.path.dirname(settings.base_dir)) os.chdir(os.path.dirname(settings.base_dir))
for module in release['modules']: for module in release['modules']:
if release['modules'][module]['version'] > settings.release['modules'][module]['version']: if release['modules'][module]['version'] > settings.release['modules'][module]['version']:
package_tar = os.path.join(settings.updates_path, release['modules'][module]['name']) module_tar = os.path.join(settings.updates_path, release['modules'][module]['name'])
url = RELEASE_URL.replace('release.json', release['modules'][module]['name']) if os.path.exists(module_tar) and ox.sha1sum(module_tar) == release['modules'][module]['sha1']:
download(url, package_tar) #tar fails if old platform is moved before extract
if ox.sha1sum(package_tar) == release['modules'][module]['sha1']: new = '%s_new' % module
ox.makedirs('new') ox.makedirs(new)
os.chdir('new') os.chdir(new)
tar = tarfile.open(package_tar) tar = tarfile.open(module_tar)
tar.extractall() tar.extractall()
tar.close() tar.close()
os.chdir(os.path.dirname(settings.base_dir)) os.chdir(os.path.dirname(settings.base_dir))
shutil.move(module, '%s_old' % module) shutil.move(module, '%s_old' % module)
shutil.move(os.path.join('new', module), module) shutil.move(os.path.join(new, module), module)
shutil.rmtree('%s_old' % module) shutil.rmtree('%s_old' % module)
shutil.rmtree('new') shutil.rmtree(new)
else: else:
return False return False
os.unlink(package_tar) shutil.copy(os.path.join(settings.updates_path, 'release.json'), os.path.join(settings.config_path, 'release.json'))
with open(os.path.join(settings.config_dir, 'release.json'), 'w') as fd: for cmd in [
fd.write(release_data) ['./ctl', 'stop'],
cmd = ['./ctl', 'stop'] ['./ctl', 'setup'],
subprocess.call(cmd) ['./ctl', 'postupdate', '-o', old, '-n', new]
cmd = ['./ctl', 'setup'] ]:
subprocess.call(cmd) subprocess.call(cmd)
cmd = ['./ctl', 'postupdate', '-o', old, '-n', new]
subprocess.call(cmd)
cmd = ['./ctl', 'start']
return True return True
return True return True