]> scripts.mit.edu Git - wizard.git/blobdiff - wizard/app/mediawiki.py
Suppress ^M characters from Git progress bars.
[wizard.git] / wizard / app / mediawiki.py
index ef7c1ea605de7c6b5f2de0bd8346cb8aac35fb6c..3fdec143f66fce2a0da23cbb42f939ad021e3cd6 100644 (file)
@@ -1,8 +1,12 @@
 import re
 import distutils.version
 import os
+import datetime
+import logging
+import shlex
+import shutil
 
-from wizard import app, deploy, install, shell, util
+from wizard import app, deploy, install, scripts, shell, util
 from wizard.app import php
 
 def make_filename_regex(var):
@@ -50,7 +54,12 @@ class Application(deploy.Application):
         match = regex.search(contents)
         if not match: return None
         return distutils.version.LooseVersion(match.group(2)[1:-1])
-    def install(self, options):
+    def checkWeb(self, d, out=None):
+        page = d.fetch("/index.php?title=Main_Page")
+        if type(out) is list:
+            out.append(page)
+        return page.find("<!-- Served") != -1
+    def install(self, version, options):
         try:
             os.unlink("LocalSettings.php")
         except OSError:
@@ -72,12 +81,62 @@ class Application(deploy.Application):
             'SysopPass': options.admin_password,
             'SysopPass2': options.admin_password,
             }
-        result = install.fetch(options, 'config/index.php', post=postdata)
+        result = install.fetch(options, '/config/index.php', post=postdata)
         if options.verbose: print result
         if result.find("Installation successful") == -1:
             raise install.Failure()
         os.rename('config/LocalSettings.php', 'LocalSettings.php')
-    def upgrade(self, options):
+    def upgrade(self, d, version, options):
         sh = shell.Shell()
-        sh.call("php", "maintenance/update.php")
+        if not os.path.isfile("AdminSettings.php"):
+            sh.call("git", "checkout", "-q", "mediawiki-" + str(version), "--", "AdminSettings.php")
+        try:
+            result = sh.eval("php", "maintenance/update.php", "--quick", log=True)
+        except shell.CallError as e:
+            raise app.UpgradeFailure("Update script returned non-zero exit code\nSTDOUT: %s\nSTDERR: %s" % (e.stdout, e.stderr))
+        results = result.rstrip().split()
+        if not results or not results[-1] == "Done.":
+            raise app.UpgradeFailure(result)
+    def backup(self, deployment, options):
+        sh = shell.Shell()
+        # XXX: duplicate code, refactor, also, race condition
+        backupdir = os.path.join(".scripts", "backups")
+        backup = str(deployment.version) + "-" + datetime.date.today().isoformat()
+        outdir = os.path.join(backupdir, backup)
+        if not os.path.exists(backupdir):
+            os.mkdir(backupdir)
+        if os.path.exists(outdir):
+            util.safe_unlink(outdir)
+        os.mkdir(outdir)
+        outfile = os.path.join(outdir, "db.sql")
+        try:
+            sh.call("mysqldump", "--compress", "-r", outfile, *get_mysql_args(deployment))
+            sh.call("gzip", "--best", outfile)
+        except shell.CallError as e:
+            shutil.rmtree(outdir)
+            raise app.BackupFailure(e.stderr)
+        return backup
+    def restore(self, deployment, backup, options):
+        sh = shell.Shell()
+        backup_dir = os.path.join(".scripts", "backups", backup)
+        if not os.path.exists(backup_dir):
+            raise app.RestoreFailure("Backup %s doesn't exist", backup)
+        sql = open(os.path.join(backup_dir, "db.sql"), 'w+')
+        sh.call("gunzip", "-c", os.path.join(backup_dir, "db.sql.gz"), stdout=sql)
+        sql.seek(0)
+        sh.call("mysql", *get_mysql_args(deployment), stdin=sql)
+        sql.close()
 
+def get_mysql_args(d):
+    # XXX: add support for getting these out of options
+    vars = d.extract()
+    if 'WIZARD_DBNAME' not in vars:
+        raise app.BackupFailure("Could not determine database name")
+    triplet = scripts.get_sql_credentials(vars)
+    args = []
+    if triplet is not None:
+        server, user, password = triplet
+        args += ["-h", server, "-u", user, "-p" + password]
+    name = shlex.split(vars['WIZARD_DBNAME'])[0]
+    args.append(name)
+    return args