Method check to perform some verifications on the environment
authorFrederic Massart <fred@moodle.com>
Fri, 12 Oct 2012 14:09:05 +0000 (22:09 +0800)
committerFrederic Massart <fred@moodle.com>
Fri, 12 Oct 2012 14:09:05 +0000 (22:09 +0800)
README.md
lib/config.py
lib/moodle.py
moodle-check.py [new file with mode: 0755]

index df02dda..e134dfd 100644 (file)
--- a/README.md
+++ b/README.md
@@ -105,6 +105,10 @@ Restore the second backup of the instance stable_master
 
     moodle backup --restore stable_master_02
 
+### - check
+
+Perform some checks on the environment to identify possible problems
+
 ### - create
 
 Create a new instance of Moodle. It will be named according to your config file.
index c4f901f..3da0e84 100644 (file)
@@ -31,8 +31,10 @@ class Conf(object):
        path = None
        configfile = None
 
-       def __init__(self, path = None):
+       def __init__(self, path, filename = None):
                self.path = path
+               if filename != None:
+                       self.filename = filename
                self.configfile = os.path.join(self.path, self.filename)
                self.load()
 
index 870796c..3a2476b 100644 (file)
@@ -308,7 +308,10 @@ class Moodle(object):
 
     def isIntegration(self):
         """Returns whether an instance is an integration one or not"""
-        remote = self.git().getConfig('remote.%s.url' % C.get('upstreamRemote'))
+        r = C.get('upstreamRemote') or 'upstream'
+        if not self.git().getRemote(r):
+            r = 'origin'
+        remote = self.git().getConfig('remote.%s.url' % r)
         if remote != None and remote.endswith('integration.git'):
             return True
         return False
diff --git a/moodle-check.py b/moodle-check.py
new file mode 100755 (executable)
index 0000000..e113df6
--- /dev/null
@@ -0,0 +1,115 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+"""
+Moodle Development Kit
+
+Copyright (c) 2012 Frédéric Massart - FMCorz.net
+
+This program is free software: you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation, either version 3 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+http://github.com/FMCorz/mdk
+"""
+
+import sys
+import os
+import argparse
+from lib import workplace
+from lib.tools import debug
+from lib.config import C, Conf
+
+Wp = workplace.Workplace()
+
+# Arguments
+parser = argparse.ArgumentParser(description='Perform several checks on your current installation')
+# parser.add_argument('-f', '--fix', dest='fix', action='store_true', help='Fix the problems where possible')
+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('names', metavar='names', default=None, nargs='*', help='name of the instances')
+args = parser.parse_args()
+
+# Check configuration file
+debug('[Config file]')
+distSettings = Conf(os.path.dirname(__file__), 'config-dist.json').get()
+allSettings = C.get()
+
+errors = []
+def dictCompare(orig, mine, path = ''):
+    for k in orig:
+        currentpath = path + '.' + k
+        currentpath = currentpath.strip('.')
+        if k not in mine:
+            errors.append(currentpath)
+        elif type(orig[k]) in (dict, list):
+            dictCompare(orig[k], mine[k], currentpath)
+dictCompare(distSettings, allSettings)
+
+if len(errors) > 0:
+    for err in errors:
+        debug(u'Missing setting %s' % err)
+else:
+    debug('All good!')
+debug('')
+
+# Checking instances
+names = args.names
+if not names:
+    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 check.')
+else:
+    upstream = C.get('upstreamRemote')
+    myremote = C.get('myRemote')
+    myremoteurl = C.get('remotes.mine')
+
+    if C.get('useCacheAsRemote'):
+        remoteInt = os.path.abspath(os.path.realpath(os.path.join(C.get('dirs.moodle'), 'integration.git')))
+        remoteSta = os.path.abspath(os.path.realpath(os.path.join(C.get('dirs.moodle'), 'moodle.git')))
+    else:
+        remoteInt = C.get('remotes.integration')
+        remoteSta = C.get('remotes.stable')
+
+    for M in Mlist:
+        errors = []
+        isIntegration = M.isIntegration()
+
+        # Checking remotes
+        if upstream:
+            thisupstream = M.git().getRemote(upstream)
+            if not thisupstream:
+                errors.append('Missing remote %s' % upstream)
+            elif isIntegration and thisupstream != remoteInt:
+                errors.append('Remote %s is not %s' % (upstream, remoteInt))
+            elif not isIntegration and thisupstream != remoteSta:
+                errors.append('Remote %s is not %s' % (upstream, remoteSta))
+
+        if myremote:
+            thismyremote = M.git().getRemote(myremote)
+            if not thismyremote:
+                errors.append('Missing remote %s' % myremote)
+            elif thismyremote != myremoteurl:
+                errors.append('Remote %s is not %s' % (myremote, myremoteurl))
+
+        if len(errors) > 0:
+            debug('[%s]' % M.get('identifier'))
+            for err in errors:
+                debug(u'  %s' % err)
+
+# Done.
+debug('')
+debug('Done.')