Command to run the upgrade script
authorFrederic Massart <fred@moodle.com>
Wed, 8 Aug 2012 05:37:14 +0000 (13:37 +0800)
committerFrederic Massart <fred@moodle.com>
Wed, 8 Aug 2012 05:40:39 +0000 (13:40 +0800)
config-dist.json
lib/git.py
lib/moodle.py
lib/workplace.py
moodle-upgrade.py [new file with mode: 0755]

index 8492d46..598ca9d 100644 (file)
@@ -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"
     },
 
index bbd85c1..e10d90a 100644 (file)
@@ -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)
index 7f772a8..45ee062 100644 (file)
@@ -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
index 48fd29e..cc76f6b 100644 (file)
@@ -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 (executable)
index 0000000..8f7ae66
--- /dev/null
@@ -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.')