]> scripts.mit.edu Git - wizard.git/blobdiff - wizard/command/upgrade.py
Suppress ^M characters from Git progress bars.
[wizard.git] / wizard / command / upgrade.py
index 7a363af74fb1bca5d8f2d4ed967e5a402d0d470b..4efbda83ae3f18f87dd366e0a471cfcfab4d90f6 100644 (file)
 import optparse
 import sys
+import distutils.version
 import os
 import shutil
 import logging.handlers
 import errno
 import tempfile
+import itertools
 
-from wizard import command, deploy, shell, util
+from wizard import app, command, deploy, shell, util
 
-# XXX: WARNING EXPERIMENTAL DANGER DANGER WILL ROBINSON
+def main(argv, baton):
+    options, args = parse_args(argv, baton)
+    if args:
+        dir = args[0]
+    else:
+        dir = "."
+    shell.drop_priviledges(dir, options.log_file)
+    util.chdir(dir)
+    if os.path.exists(".scripts/blacklisted"):
+        raise BlacklistedError()
+    sh = shell.Shell()
+    util.set_git_env()
+    if options.continue_:
+        temp_wc_dir = os.getcwd()
+        user_commit, next_commit = open(".git/WIZARD_PARENTS", "r").read().split()
+        repo = open(".git/WIZARD_REPO", "r").read()
+        version = open(".git/WIZARD_UPGRADE_VERSION", "r").read()
+        util.chdir(sh.eval("git", "config", "remote.origin.url"))
+        d = deploy.Deployment(".")
+        try:
+            sh.call("git", "status")
+            raise LocalChangesError()
+        except shell.CallError:
+            pass
+    else:
+        d = deploy.Deployment(".")
+        d.verify()
+        d.verifyTag(options.srv_path)
+        d.verifyGit(options.srv_path)
+        d.verifyConfigured()
+        d.verifyVersion()
+        if not options.dry_run:
+            d.verifyWeb()
+        repo = d.application.repository(options.srv_path)
+        version = calculate_newest_version(sh, repo)
+        if version == d.app_version.scripts_tag and not options.force:
+            raise AlreadyUpgraded
+        if not options.dry_run:
+            perform_pre_commit(sh)
+        temp_wc_dir = perform_tmp_clone(sh)
+        with util.ChangeDirectory(temp_wc_dir):
+            sh.call("git", "remote", "add", "scripts", repo)
+            sh.call("git", "fetch", "-q", "scripts")
+            user_commit, next_commit = calculate_parents(sh, version)
+            # save variables so that --continue will work
+            # yeah yeah no trailing newline whatever
+            open(".git/WIZARD_REPO", "w").write(repo)
+            open(".git/WIZARD_UPGRADE_VERSION", "w").write(version)
+            open(".git/WIZARD_PARENTS", "w").write("%s\n%s" % (user_commit, next_commit))
+            perform_merge(sh, repo, d, version)
+    # variables: version, user_commit, next_commit, temp_wc_dir
+    with util.ChangeDirectory(temp_wc_dir):
+        try:
+            sh.call("git", "status")
+            sh.call("git", "commit", "-m", "throw-away commit")
+        except shell.CallError:
+            pass
+        message = make_commit_message(version)
+        new_tree = sh.eval("git", "rev-parse", "HEAD^{tree}")
+        final_commit = sh.eval("git", "commit-tree", new_tree,
+                "-p", user_commit, "-p", next_commit, input=message, log=True)
+        # a master branch may not necessarily exist if the user
+        # was manually installed to an earlier version
+        try:
+            sh.call("git", "checkout", "-q", "-b", "master", "--")
+        except shell.CallError:
+            sh.call("git", "checkout", "-q", "master", "--")
+        sh.call("git", "reset", "-q", "--hard", final_commit)
+        d.verifyVersion()
+    # Till now, all of our operations were in a tmp sandbox.
+    if options.dry_run:
+        logging.info("Dry run, bailing.  See results at %s" % temp_wc_dir)
+        return
+    # perform database backup
+    backup = d.backup(options)
+    # XXX: frob .htaccess to make site inaccessible
+    with util.IgnoreKeyboardInterrupts():
+        with util.LockDirectory(".scripts-upgrade-lock"):
+            # git merge (which performs a fast forward)
+            sh.call("git", "pull", "-q", temp_wc_dir, "master")
+            version_obj = distutils.version.LooseVersion(version.partition('-')[2])
+            try:
+                # run update script
+                d.upgrade(version_obj, options)
+                d.verifyWeb()
+            except app.UpgradeFailure:
+                logging.warning("Upgrade failed: rolling back")
+                perform_restore(d, backup)
+                raise
+            except deploy.WebVerificationError as e:
+                logging.warning("Web verification failed: rolling back")
+                logging.info("Web page that was output was:\n\n%s" % e.contents)
+                perform_restore(d, backup)
+                raise app.UpgradeVerificationFailure("Upgrade caused website to become inaccessible; site was rolled back")
+    # XXX: frob .htaccess to make site accessible
+    #       to do this, check if .htaccess changed, first.  Upgrade
+    #       process might have frobbed it.  Don't be
+    #       particularly worried if the segment disappeared
 
-# need errors for checking DAG integrity (if the user is on a completely
-# different history tree, stuff is problems)
+def perform_restore(d, backup):
+    # You don't want d.restore() because it doesn't perform
+    # the file level backup
+    shell.Shell().call("wizard", "restore", backup)
+    try:
+        d.verifyWeb()
+    except deploy.WebVerificationError:
+        logging.critical("Web verification failed after rollback")
 
-# we need an option that specifies the person making the update. Since n-b
-# doesn't actually have a way to do this, username is probably what we want.
-def main(argv, baton):
-    options, args = parse_args(argv)
-    dir = args[0]
-    command.chdir(dir)
-    if not os.path.isdir(".git"):
-        raise NotAutoinstallError()
+def make_commit_message(version):
+    message = "Upgraded autoinstall in %s to %s.\n\n%s" % (util.get_dir_owner(), version, util.get_git_footer())
     try:
-        d = deploy.Deployment(".")
-    except IOError as e:
-        if e.errno == errno.ENOENT:
-            raise NotAutoinstallError()
-        else: raise e
-    repo = d.application.repository
-    # begin the command line process
-    sh = shell.Shell(options.dry_run)
-    # setup environment
-    util.set_git_env()
-    # commit their changes
+        message += "\nUpgraded-by: " + util.get_operator_git()
+    except util.NoOperatorInfo:
+        pass
+    return message
+
+def calculate_newest_version(sh, repo):
+    # XXX: put this in Application
+    return sh.eval("git", "--git-dir="+repo, "describe", "--tags", "master")
+
+def calculate_parents(sh, version):
+    user_commit = sh.eval("git", "rev-parse", "HEAD")
+    next_commit = sh.eval("git", "rev-parse", version)
+    return user_commit, next_commit
+
+def perform_pre_commit(sh):
     message = "Pre-commit of %s locker before autoinstall upgrade.\n\n%s" % (util.get_dir_owner(), util.get_git_footer())
     try:
         message += "\nPre-commit-by: " + util.get_operator_git()
     except util.NoOperatorInfo:
         pass
     try:
-        sh.call("git", "commit", "-a", "-m", "Pre-upgrade commit.")
+        sh.call("git", "commit", "-a", "-m", message)
     except shell.CallError:
         logging.info("No changes detected")
-        pass
-    # perform fetch to update repository state
-    sh.call("git", "fetch", repo)
-    # clone their website to a temporary directory
-    temp_dir = tempfile.mkdtemp()
+
+def perform_tmp_clone(sh):
+    temp_dir = tempfile.mkdtemp(prefix="wizard")
     temp_wc_dir = os.path.join(temp_dir, "repo")
     logging.info("Using temporary directory: " + temp_wc_dir)
-    sh.call("git", "clone", "--shared", ".", temp_wc_dir)
-    with util.ChangeDirectory(temp_wc_dir):
-        # reconfigure the repository path
-        sh.call("git", "remote", "add", "scripts", repo)
-        sh.call("git", "fetch", "scripts")
-        # perform the merge
-        version, _ = sh.call(["git", "--git-dir="+repo, "describe", "--tags", "master"])
-        try:
-            message = "Upgraded autoinstall in %s to %s.\n\n%s" % (util.get_dir_owner(), version, util.get_git_footer())
+    sh.call("git", "clone", "-q", "--shared", ".", temp_wc_dir)
+    return temp_wc_dir
+
+def perform_merge(sh, repo, d, version):
+    # naive merge algorithm:
+    # sh.call("git", "merge", "-m", message, "scripts/master")
+    # crazy merge algorithm:
+    def make_virtual_commit(tag, parents = []):
+        """WARNING: Changes state of Git repository"""
+        sh.call("git", "checkout", "-q", tag, "--")
+        d.parametrize(".")
+        for file in d.application.parametrized_files:
             try:
-                message += "\nUpgraded-by: " + util.get_operator_git()
-            except util.NoOperatorInfo:
+                sh.call("git", "add", "--", file)
+            except shell.CallError:
                 pass
-            sh.call("git", "merge", "-m", message, "scripts/master")
-        except shell.CallError:
-            raise MergeFailed
-    # XXX: frob .htaccess to make site inaccessible
-    # git merge (which performs a fast forward)
-    #   - merge could fail (race)
-    sh.call("git", "pull", temp_wc_dir, "master")
-    # run update script
-    sh.call(".scripts/update")
-    # XXX: frob .htaccess to make site accessible
-    # XXX:  - check if .htaccess changed, first.  Upgrade
-    #       process might have frobbed it.  Don't be
-    #       particularly worried if the segment dissappeared
+        virtual_tree = sh.eval("git", "write-tree", log=True)
+        parent_args = itertools.chain(*(["-p", p] for p in parents))
+        virtual_commit = sh.eval("git", "commit-tree", virtual_tree,
+                *parent_args, input="", log=True)
+        sh.call("git", "reset", "--hard")
+        return virtual_commit
+    user_tree = sh.eval("git", "rev-parse", "HEAD^{tree}")
+    base_virtual_commit = make_virtual_commit(d.app_version.scripts_tag)
+    next_virtual_commit = make_virtual_commit(version, [base_virtual_commit])
+    user_virtual_commit = sh.eval("git", "commit-tree", user_tree,
+            "-p", base_virtual_commit, input="", log=True)
+    sh.call("git", "checkout", user_virtual_commit, "--")
+    try:
+        sh.call("git", "merge", next_virtual_commit)
+    except shell.CallError:
+        print os.getcwd()
+        logging.info("Conflict info:\n", sh.eval("git", "diff"))
+        raise MergeFailed
 
-def parse_args(argv):
-    usage = """usage: %prog upgrade [ARGS] DIR
+def parse_args(argv, baton):
+    usage = """usage: %prog upgrade [ARGS] [DIR]
 
 Upgrades an autoinstall to the latest version.  This involves
 updating files and running .scripts/update.
@@ -86,25 +195,51 @@ WARNING: This is still experimental."""
     parser = command.WizardOptionParser(usage)
     parser.add_option("--dry-run", dest="dry_run", action="store_true",
             default=False, help="Prints would would be run without changing anything")
+    # notice trailing underscore
+    parser.add_option("--continue", dest="continue_", action="store_true",
+            default=False, help="Continues an upgrade that has had its merge manually "
+            "resolved using the current working directory as the resolved copy.")
+    parser.add_option("--force", dest="force", action="store_true",
+            default=False, help="Force running upgrade even if it's already at latest version.")
+    baton.push(parser, "srv_path")
     options, args = parser.parse_all(argv)
     if len(args) > 1:
         parser.error("too many arguments")
-    elif not args:
-        parser.error("must specify directory")
     return options, args
 
 class Error(command.Error):
     """Base exception for all exceptions raised by upgrade"""
     pass
 
-class NotAutoinstallError(Error):
+class AlreadyUpgraded(Error):
     def __str__(self):
         return """
 
-ERROR: Could not find .git file. Are you sure
-this is an autoinstalled application? Did you remember
-to migrate it?
-"""
+ERROR: This autoinstall is already at the latest version."""
 
 class MergeFailed(Error):
-    pass
+    def __str__(self):
+        return """
+
+ERROR: Merge failed.  Resolve the merge by cd'ing to the
+temporary directory, finding conflicted files with `git status`,
+resolving the files, adding them using `git add` and then
+running `wizard upgrade --continue`."""
+
+class LocalChangesError(Error):
+    def __str__(self):
+        return """
+
+ERROR: Local changes occurred in the install while the merge was
+being processed so that a pull would not result in a fast-forward.
+The best way to resolve this is probably to attempt an upgrade again,
+with git rerere to remember merge resolutions (XXX: not sure if
+this actually works)."""
+
+class BlacklistedError(Error):
+    def __str__(self):
+        return """
+
+ERROR: This autoinstall was manually blacklisted against errors;
+if the user has not been notified of this, please send them
+mail."""