]> scripts.mit.edu Git - wizard.git/commitdiff
Move generic functionality out of wizard.app.mediawiki.
authorEdward Z. Yang <ezyang@mit.edu>
Sat, 17 Oct 2009 03:00:09 +0000 (23:00 -0400)
committerEdward Z. Yang <ezyang@mit.edu>
Sat, 17 Oct 2009 03:00:09 +0000 (23:00 -0400)
Signed-off-by: Edward Z. Yang <ezyang@mit.edu>
TODO
wizard/app/__init__.py
wizard/app/mediawiki.py
wizard/deploy.py
wizard/resolve.py

diff --git a/TODO b/TODO
index 243d178943467b42d57191a2dcaa4cb466e096cf..26155de541671e95c9970651a5e252b8763ca9e0 100644 (file)
--- a/TODO
+++ b/TODO
@@ -5,8 +5,6 @@ TODO NOW:
 - Keep my sanity when upgrading 1000 installs
     - Custom merge algo: absolute php.ini symlinks to relative symlinks (this
       does not seem to have been a problem in practice)
-    - Custom merge algo: check if it's got extra \r's in the file,
-      and dos2unix it if it does, before performing the merge
     - Prune -7 call errors and automatically reprocess them (with a
       strike out counter of 3)--this requires better error parsing.
     - IOError should be aggregated, right now contains custom string
@@ -16,8 +14,7 @@ TODO NOW:
     - Distinguish between types of backup failures
     - Ignore empty blacklists; they should all have reasons
     - wizard upgrade should have different exit codes for merge failure
-      and blacklist error. This means augmenting error classes to have
-      exit codes in them
+      and blacklist error.
 
 - Distinguish from logging and reporting (so we can easily send mail
   to users)
@@ -55,7 +52,8 @@ TODO NOW:
       also be automated.
     - Indents in upgrade.py are getting pretty ridiculous; more breaking
       into functions is probably a good idea
-    - Move resolutions in mediawiki.py to a text file
+    - Move resolutions in mediawiki.py to a text file? (the parsing overhead
+      may not be worth it)
     - Investigate QuotaParseErrors
     - If a process is C-ced, it can result in a upgrade that has
       an updated filesystem but not updated database. Make this more
index 08212ee1b3eb273b1902c95b14933fca5e47b5f0..1fc1669461c5d0b608b3cbc046e9833354182e82 100644 (file)
@@ -19,9 +19,10 @@ import os.path
 import re
 import distutils.version
 import decorator
+import shlex
 
 import wizard
-from wizard import util
+from wizard import scripts, shell, util
 
 _application_list = [
     "mediawiki", "wordpress", "joomla", "e107", "gallery2",
@@ -59,6 +60,20 @@ class Application(object):
     #: Keys that are used in older versions of the application, but
     #: not for the most recent version.
     deprecated_keys = []
+    #: Dictionary of variable names to extractor functions.  These functions
+    #: take a :class:`wizard.deploy.Deployment` as an argument and return the value of
+    #: the variable, or ``None`` if it could not be found.
+    #: See also :func:`filename_regex_extractor`.
+    extractors = {}
+    #: Dictionary of variable names to substitution functions.  These functions
+    #: take a :class:`wizard.deploy.Deployment` as an argument and modify the deployment such
+    #: that an explicit instance of the variable is released with the generic
+    #: ``WIZARD_*`` constant.  See also :func:`filename_regex_substitution`.
+    substitutions = {}
+    #: Dictionary of file names to a list of resolutions, which are tuples of
+    #: a conflict marker string and a result list.  See :mod:`wizard.resolve`
+    #: for more information.
+    resolutions = {}
     def __init__(self, name):
         self.name = name
         self.versions = {}
@@ -93,7 +108,7 @@ class Application(object):
         for k,extractor in self.extractors.items():
             result[k] = extractor(deployment)
         return result
-    def parametrize(self, deployment):
+    def parametrize(self, deployment, ref_deployment):
         """
         Takes a generic source checkout and parametrizes it according to the
         values of ``deployment``.  This function operates on the current
@@ -102,7 +117,7 @@ class Application(object):
         :attr:`parametrized_files` and a simple search and replace on those
         files.
         """
-        variables = deployment.extract()
+        variables = ref_deployment.extract()
         for file in self.parametrized_files:
             try:
                 contents = open(file, "r").read()
@@ -117,11 +132,28 @@ class Application(object):
         """
         Resolves conflicted files in the current working directory.  Returns
         whether or not all conflicted files were resolved or not.  Fully
-        resolved files are added to the index, but no commit is made.  By
-        default this is a no-op and returns ``False``; subclasses should
-        replace this with useful behavior.
-        """
-        return False
+        resolved files are added to the index, but no commit is made.  The
+        default implementation uses :attr:`resolutions`.
+        """
+        resolved = True
+        sh = shell.Shell()
+        for status in sh.eval("git", "ls-files", "--unmerged").splitlines():
+            file = status.split()[-1]
+            if file in self.resolutions:
+                contents = open(file, "r").read()
+                for spec, result in self.resolutions[file]:
+                    old_contents = contents
+                    contents = resolve.resolve(contents, spec, result)
+                    if old_contents != contents:
+                        logging.info("Did resolution with spec:\n" + spec)
+                open(file, "w").write(contents)
+                if not resolve.is_conflict(contents):
+                    sh.call("git", "add", file)
+                else:
+                    resolved = False
+            else:
+                resolved = False
+        return resolved
     def prepareMerge(self, deployment):
         """
         Performs various edits to files in the current working directory in
@@ -160,19 +192,20 @@ class Application(object):
         deployment.  Subclasses should provide an implementation.
         """
         raise NotImplemented
-    def backup(self, deployment, options):
+    def backup(self, deployment, outdir, options):
         """
         Run for 'wizard backup' and upgrades to backup database schemas
-        and other non-versioned data in an application.  This assumes
+        and other non-versioned data in an application.  ``outdir`` is
+        the directory that backup files should be placed.  This assumes
         that the current working directory is the deployment.  Subclasses
-        should provide an implementation.
+        should provide an implementation, even if it is a no-op.
 
         .. note::
             Static user files may not need to be backed up, since in
             many applications upgrades do not modify static files.
         """
         raise NotImplemented
-    def restore(self, deployment, backup, options):
+    def restore(self, deployment, backup_dir, options):
         """
         Run for 'wizard restore' and failed upgrades to restore database
         and other non-versioned data to a backed up version.  This assumes
@@ -210,24 +243,6 @@ class Application(object):
         Subclasses should provide an implementation.
         """
         raise NotImplemented
-    @property
-    def extractors(self):
-        """
-        Dictionary of variable names to extractor functions.  These functions
-        take a :class:`wizard.deploy.Deployment` as an argument and return the value of
-        the variable, or ``None`` if it could not be found.
-        See also :func:`filename_regex_extractor`.
-        """
-        return {}
-    @property
-    def substitutions(self):
-        """
-        Dictionary of variable names to substitution functions.  These functions
-        take a :class:`wizard.deploy.Deployment` as an argument and modify the deployment such
-        that an explicit instance of the variable is released with the generic
-        WIZARD_* constant.  See also :func:`filename_regex_substitution`.
-        """
-        return {}
     @staticmethod
     def make(name):
         """Makes an application, but uses the correct subtype if available."""
@@ -444,6 +459,45 @@ def filename_regex_substitution(key, files, regex):
         return subs
     return h
 
+def backup_database(outdir, deployment):
+    """
+    Generic database backup function.  Assumes that ``WIZARD_DBNAME``
+    is extractable, and that :func:`wizard.scripts.get_sql_credentials`
+    works.
+    """
+    sh = shell.Shell()
+    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 BackupFailure(e.stderr)
+
+def restore_database(backup_dir, deployment):
+    sh = shell.Shell()
+    if not os.path.exists(backup_dir):
+        raise RestoreFailure("Backup %s doesn't exist", backup_dir.rpartition("/")[2])
+    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
+
 class Error(wizard.Error):
     """Generic error class for this module."""
     pass
index a959fa49410ce594cdc58d833d3664369dad1599..c5b6f3efd6b52ff0c6a4c73b9e0cc36e9fbdee6a 100644 (file)
@@ -24,7 +24,77 @@ seed = util.dictmap(make_filename_regex, {
         'WIZARD_SECRETKEY': ('wgSecretKey', 'wgProxyKey'),
         })
 
-resolutions = {
+class Application(app.Application):
+    parametrized_files = ['LocalSettings.php', 'php.ini']
+    deprecated_keys = set(['WIZARD_IP']) | php.deprecated_keys
+    extractors = app.make_extractors(seed)
+    extractors.update(php.extractors)
+    substitutions = app.make_substitutions(seed)
+    substitutions.update(php.substitutions)
+    @property
+    def install_handler(self):
+        handler = install.ArgHandler("mysql", "admin", "email")
+        handler.add(install.Arg("title", help="Title of your new MediaWiki install"))
+        return handler
+    def checkConfig(self, deployment):
+        return os.path.isfile(os.path.join(deployment.location, "LocalSettings.php"))
+    def detectVersion(self, deployment):
+        contents = deployment.read("includes/DefaultSettings.php")
+        regex = make_filename_regex("wgVersion")[1]
+        match = regex.search(contents)
+        if not match: return None
+        return distutils.version.LooseVersion(match.group(2)[1:-1])
+    def checkWeb(self, deployment, out=None):
+        page = deployment.fetch("/index.php?title=Main_Page")
+        if type(out) is list:
+            out.append(page)
+        return page.find("<!-- Served") != -1
+    def prepareMerge(self, deployment):
+        resolve.fix_newlines("LocalSettings.php")
+    def install(self, version, options):
+        try:
+            os.unlink("LocalSettings.php")
+        except OSError:
+            pass
+
+        os.chmod("config", 0777) # XXX: vaguely sketchy
+
+        postdata = {
+            'Sitename': options.title,
+            'EmergencyContact': options.email,
+            'LanguageCode': 'en',
+            'DBserver': options.mysql_host,
+            'DBname': options.mysql_db,
+            'DBuser': options.mysql_user,
+            'DBpassword': options.mysql_password,
+            'DBpassword2': options.mysql_password,
+            'defaultEmail': options.email,
+            'SysopName': options.admin_name,
+            'SysopPass': options.admin_password,
+            'SysopPass2': options.admin_password,
+            }
+        result = install.fetch(options, '/config/index.php', post=postdata)
+        if options.verbose or options.debug: print result
+        if result.find("Installation successful") == -1:
+            raise install.Failure()
+        os.rename('config/LocalSettings.php', 'LocalSettings.php')
+    def upgrade(self, d, version, options):
+        sh = shell.Shell()
+        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, backup_dir, options):
+        app.backup_database(backup_dir, deployment)
+    def restore(self, deployment, backup_dir, options):
+        app.restore_database(backup_dir, deployment)
+
+Application.resolutions = {
 'LocalSettings.php': [
     ("""
 <<<<<<<
@@ -119,148 +189,3 @@ $wgCacheEpoch = max( $wgCacheEpoch, gmdate( 'YmdHis', @filemtime( __FILE__ ) ) )
     ]
 }
 
-class Application(app.Application):
-    parametrized_files = ['LocalSettings.php', 'php.ini']
-    deprecated_keys = set(['WIZARD_IP']) | php.deprecated_keys
-    @property
-    def extractors(self):
-        if not self._extractors:
-            self._extractors = app.make_extractors(seed)
-            self._extractors.update(php.extractors)
-        return self._extractors
-    @property
-    def substitutions(self):
-        if not self._substitutions:
-            self._substitutions = app.make_substitutions(seed)
-            self._substitutions.update(php.substitutions)
-        return self._substitutions
-    @property
-    def install_handler(self):
-        handler = install.ArgHandler("mysql", "admin", "email")
-        handler.add(install.Arg("title", help="Title of your new MediaWiki install"))
-        return handler
-    def checkConfig(self, deployment):
-        return os.path.isfile(os.path.join(deployment.location, "LocalSettings.php"))
-    def detectVersion(self, deployment):
-        contents = deployment.read("includes/DefaultSettings.php")
-        regex = make_filename_regex("wgVersion")[1]
-        match = regex.search(contents)
-        if not match: return None
-        return distutils.version.LooseVersion(match.group(2)[1:-1])
-    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 prepareMerge(self, dir):
-        # XXX: this should be factored out
-        old_contents = open("LocalSettings.php", "r").read()
-        contents = old_contents
-        while "\r\n" in contents:
-            contents = contents.replace("\r\n", "\n")
-        contents = contents.replace("\r", "\n")
-        if contents != old_contents:
-            logging.info("Converted LocalSettings.php to UNIX file endings")
-            open("LocalSettings.php", "w").write(contents)
-    def resolveConflicts(self, dir):
-        # XXX: this is pretty generic
-        resolved = True
-        sh = shell.Shell()
-        for status in sh.eval("git", "ls-files", "--unmerged").splitlines():
-            file = status.split()[-1]
-            if file in resolutions:
-                contents = open(file, "r").read()
-                for spec, result in resolutions[file]:
-                    old_contents = contents
-                    contents = resolve.resolve(contents, spec, result)
-                    if old_contents != contents:
-                        logging.info("Did resolution with spec:\n" + spec)
-                open(file, "w").write(contents)
-                if not resolve.is_conflict(contents):
-                    sh.call("git", "add", file)
-                else:
-                    resolved = False
-            else:
-                resolved = False
-        return resolved
-    def install(self, version, options):
-        try:
-            os.unlink("LocalSettings.php")
-        except OSError:
-            pass
-
-        os.chmod("config", 0777) # XXX: vaguely sketchy
-
-        postdata = {
-            'Sitename': options.title,
-            'EmergencyContact': options.email,
-            'LanguageCode': 'en',
-            'DBserver': options.mysql_host,
-            'DBname': options.mysql_db,
-            'DBuser': options.mysql_user,
-            'DBpassword': options.mysql_password,
-            'DBpassword2': options.mysql_password,
-            'defaultEmail': options.email,
-            'SysopName': options.admin_name,
-            'SysopPass': options.admin_password,
-            'SysopPass2': options.admin_password,
-            }
-        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, d, version, options):
-        sh = shell.Shell()
-        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
index ffccf47f11726312a634eb35223abfb990f42ce2..08a00468286a86e2036e8fa9aabfac78d7cde41b 100644 (file)
@@ -11,6 +11,7 @@ import tempfile
 import logging
 import shutil
 import decorator
+import datetime
 
 import wizard
 from wizard import app, git, old_log, scripts, shell, util
@@ -298,7 +299,16 @@ class ProductionCopy(Deployment):
         """
         Performs a backup of database schemas and other non-versioned data.
         """
-        return self.application.backup(self, options)
+        backupdir = os.path.join(self.location, ".scripts", "backups")
+        backup = str(self.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)
+        self.application.backup(self, outdir, options)
+        return backup
     @chdir_to_location
     def restore(self, backup, options):
         """
@@ -307,7 +317,8 @@ class ProductionCopy(Deployment):
         does, so you probably do NOT want to call this elsewhere unless
         you know what you're doing (call 'wizard restore' instead).
         """
-        return self.application.restore(self, backup, options)
+        backup_dir = os.path.join(".scripts", "backups", backup)
+        return self.application.restore(self, backup_dir, options)
     def verifyWeb(self):
         """
         Checks if the autoinstall is viewable from the web.
index 9111203ec5d60c06fce10e2f97a29396a4dcc290..910802f8cbb3cc40be307ef2671ef3e1aee6f11a 100644 (file)
@@ -122,3 +122,21 @@ def resolve(contents, spec, result):
 def is_conflict(contents):
     # Really really simple heuristic
     return "<<<<<<<" in contents
+
+def fix_newlines(file, log=True):
+    """
+    Normalizes newlines in a file into UNIX file endings.  If
+    ``log`` is ``True`` an info log mesage is printed if
+    any normalization occurs.  Return value is ``True`` if
+    normalization occurred.
+    """
+    old_contents = open(file, "r").read()
+    contents = old_contents
+    while "\r\n" in contents:
+        contents = contents.replace("\r\n", "\n")
+    contents = contents.replace("\r", "\n")
+    if contents != old_contents:
+        logging.info("Converted %s to UNIX file endings" % file)
+        open(file, "w").write(contents)
+        return True
+    return False