From 13dd99a39b35b6363e628ad9d5c2c2222635b230 Mon Sep 17 00:00:00 2001 From: Frederic Massart Date: Wed, 8 Aug 2012 13:37:14 +0800 Subject: [PATCH] Command to run the upgrade script --- config-dist.json | 5 +++-- lib/git.py | 8 ++++++++ lib/moodle.py | 26 +++++++++++++++++++++++++- lib/workplace.py | 32 +++++++++++++++++++++++++++++++- moodle-upgrade.py | 41 +++++++++++++++++++++++++++++++++++++++++ 5 files changed, 108 insertions(+), 4 deletions(-) create mode 100755 moodle-upgrade.py diff --git a/config-dist.json b/config-dist.json index 8492d46..598ca9d 100644 --- a/config-dist.json +++ b/config-dist.json @@ -7,8 +7,9 @@ }, "remotes": { - "stable": "git://github.com/moodle/moodle.git", - "integration": "git://github.com/moodle/integration.git", + // In any case, the integration branch MUST end with integration.git + "stable": "git://git.moodle.org/moodle.git", + "integration": "git://git.moodle.org/integration.git", "mine": "git@github.com/FMCorz/moodle.git" }, diff --git a/lib/git.py b/lib/git.py index bbd85c1..e10d90a 100644 --- a/lib/git.py +++ b/lib/git.py @@ -66,6 +66,14 @@ class Git(object): cmd = 'fetch %s %s' % (remote, ref) return self.execute(cmd) + def getConfig(self, name): + cmd = 'config --get %s' % name + result = self.execute(cmd) + if result[0] == 0: + return result[1].strip() + else: + return None + def hasBranch(self, branch, remote = ''): if remote != '': cmd = 'show-ref --verify --quiet "refs/remotes/%s/%s"' % (remote, branch) diff --git a/lib/moodle.py b/lib/moodle.py index 7f772a8..45ee062 100644 --- a/lib/moodle.py +++ b/lib/moodle.py @@ -192,7 +192,6 @@ class Moodle(object): try: self.addConfig('sessioncookiepath', '/%s/' % self.identifier) except Exception as e: - print e debug('Could not append $CFG->sessioncookiepath to config.php') self.reload() @@ -217,6 +216,17 @@ class Moodle(object): return True + def isIntegration(self): + """Returns whether an instance is an integration one or not""" + remote = self.git().getConfig('remote.origin.url') + if remote != None and remote.endswith('integration.git'): + return True + return False + + def isStable(self): + """Assume an instance is stable if not integration""" + return not self.isIntegration() + def _load(self): """Loads the information""" if not self.isInstance(self.path): @@ -261,6 +271,9 @@ class Moodle(object): else: self.version['stablebranch'] = 'MOODLE_%s_STABLE' % self.version['branch'] + # Integration or stable? + self.version['integration'] = self.isIntegration() + f.close() else: # Should never happen @@ -291,3 +304,14 @@ class Moodle(object): """Reloads the information""" self._loaded = False return self._load() + + def update(self): + """Update the instance from the remote""" + pass + + def upgrade(self): + """Calls the upgrade script""" + cli = '/admin/cli/upgrade.php' + args = '--non-interactive --allow-unstable' + result = self.cli(cli, args, stdout = None, stderr = None) + return result[0] == 0 diff --git a/lib/workplace.py b/lib/workplace.py index 48fd29e..cc76f6b 100644 --- a/lib/workplace.py +++ b/lib/workplace.py @@ -192,7 +192,7 @@ class Workplace(object): return True - def list(self): + def list(self, integration = None, stable = None): """Return the list of Moodle instances""" dirs = os.listdir(self.path) names = [] @@ -200,6 +200,10 @@ class Workplace(object): if d == '.' or d == '..': continue if not os.path.isdir(os.path.join(self.path, d)): continue if not self.isMoodle(d): continue + if integration != None or stable != None: + M = self.get(d) + if integration == False and M.isIntegration(): continue + if stable == False and M.isStable(): continue names.append(d) return names @@ -225,6 +229,32 @@ class Workplace(object): return False + def resolveMultiple(self, names = []): + """Return multiple instances""" + if type(names) != list: + if type(names) == str: + names = list(names) + else: + raise Exception('Unexpected variable type') + + # Nothing has been passed, we use resolve() + if len(names) < 1: + M = self.resolve() + if M: + return [ M ] + else: + return [ ] + + # Try to resolve each instance + result = [] + for name in names: + M = self.resolve(name = name) + if M: + result.append(M) + else: + debug('Could not find instance called %s' % name) + return result + def updateCachedClones(self, integration = True, stable = True): """Update the cached clone of the repositories""" pass diff --git a/moodle-upgrade.py b/moodle-upgrade.py new file mode 100755 index 0000000..8f7ae66 --- /dev/null +++ b/moodle-upgrade.py @@ -0,0 +1,41 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +import sys +import argparse +from lib import config, workplace, moodle, tools +from lib.tools import debug + +C = config.Conf().get +Wp = workplace.Workplace() + +# Arguments +parser = argparse.ArgumentParser(description='Runs the Moodle upgrade script') +parser.add_argument('-a', '--all', action='store_true', help='runs the script on every instances', dest='all') +parser.add_argument('-i', '--integration', action='store_true', help='runs the script on the integration instances', dest='integration') +parser.add_argument('-s', '--stable', action='store_true', help='runs the script on the stable instances', dest='stable') +parser.add_argument('-u', '--update', action='store_true', help='update the instance before running the upgrade script', dest='update') +parser.add_argument('names', metavar='names', default=None, nargs='*', help='name of the instances') +args = parser.parse_args() + +names = args.names +if args.all: + names = Wp.list() +elif args.integration or args.stable: + names = Wp.list(integration = args.integration, stable = args.stable) + +Mlist = Wp.resolveMultiple(names) +if len(Mlist) < 1: + debug('No instances to work on. Exiting...') + sys.exit(1) + +for M in Mlist: + if args.update: + debug('Updating %s...' % M.get('identifier')) + M.update() + debug('Upgrading %s...' % M.get('identifier')) + if M.upgrade(): + debug('Error during the upgrade of %s' % M.get('identifier')) + debug('') + +debug('Done.') -- 2.11.0