New command to uninstall an instance
authorFrederic Massart <fred@moodle.com>
Wed, 9 Oct 2013 07:09:32 +0000 (15:09 +0800)
committerFrederic Massart <fred@moodle.com>
Wed, 9 Oct 2013 07:11:55 +0000 (15:11 +0800)
README.md
extra/bash_completion
lib/commands/__init__.py
lib/commands/uninstall.py [new file with mode: 0644]
lib/moodle.py

index 837755a..2a08044 100644 (file)
--- a/README.md
+++ b/README.md
@@ -323,6 +323,13 @@ Gets some information about the issue on the tracker.
     Tester              : Tim Barker (timb)
     ------------------------------------------------------------------------
 
+
+uninstall
+---------
+
+Uninstall an instance: removes config file, drops the database, delete dataroot content, ...
+
+
 update
 ------
 
index d3fe715..ebf446b 100644 (file)
@@ -61,7 +61,7 @@ function _mdk() {
     if [[ "${COMP_CWORD}" == 1 ]]; then
         # List the commands and aliases.
         # Ignoring these commands on purpose: init
-        OPTS="alias backport backup behat check config create fix info install phpunit plugin purge pull push rebase remove run update upgrade"
+        OPTS="alias backport backup behat check config create fix info install phpunit plugin purge pull push rebase remove run tracker uninstall update upgrade"
         OPTS="$OPTS $($BIN alias list 2> /dev/null | cut -d ':' -f 1)"
     else
         # List of options according to the command.
index a019137..35de075 100644 (file)
@@ -49,6 +49,7 @@ commandsList = [
     'remove',
     'run',
     'tracker',
+    'uninstall',
     'update',
     'upgrade'
 ]
diff --git a/lib/commands/uninstall.py b/lib/commands/uninstall.py
new file mode 100644 (file)
index 0000000..ec17c5b
--- /dev/null
@@ -0,0 +1,69 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+"""
+Moodle Development Kit
+
+Copyright (c) 2013 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 logging
+from lib.command import Command
+
+
+class UninstallCommand(Command):
+
+    _description = 'Uninstall a Moodle instance'
+    _arguments = [
+        (
+            ['name'],
+            {
+                'default': None,
+                'help': 'name of the instance',
+                'metavar': 'name',
+                'nargs': '?'
+            }
+        ),
+        (
+            ['-y'],
+            {
+                'action': 'store_true',
+                'dest': 'do',
+                'help': 'do not ask for confirmation'
+            }
+        )
+    ]
+
+    def run(self, args):
+
+        M = self.Wp.resolve(args.name)
+        if not M:
+            raise Exception('This is not a Moodle instance')
+        elif not M.isInstalled():
+            logging.info('This instance is not installed')
+            return
+
+        if not args.do:
+            confirm = raw_input('Are you sure? (Y/n) ')
+            if confirm != 'Y':
+                logging.info('Aborting...')
+                return
+
+        logging.info('Uninstalling %s...' % M.get('identifier'))
+        M.uninstall()
+        logging.info('Done.')
index eb8919b..dcf9821 100644 (file)
@@ -627,6 +627,31 @@ class Moodle(object):
         self.removeConfig(name)
         self.addConfig(name, value)
 
+    def uninstall(self):
+        """Uninstall the instance"""
+
+        if not self.isInstalled():
+            raise Exception('The instance is not installed')
+
+        # Delete the content in moodledata
+        dataroot = self.get('dataroot')
+        if os.path.isdir(dataroot):
+            logging.debug('Deleting dataroot content (%s)' % (dataroot))
+            shutil.rmtree(dataroot)
+            mkdir(dataroot, 0777)
+
+        # Drop the database
+        dbname = self.get('dbname')
+        if self.dbo().dbexists(dbname):
+            logging.debug('Droping database (%s)' % (dbname))
+            self.dbo().dropdb(dbname)
+
+        # Remove the config file
+        configFile = os.path.join(self.get('path'), 'config.php')
+        if os.path.isfile(configFile):
+            logging.debug('Deleting config.php')
+            os.remove(configFile)
+
     def updateTrackerGitInfo(self, branch=None, ref=None):
         """Updates the git info on the tracker issue"""