]> scripts.mit.edu Git - wizard.git/blobdiff - wizard/command/migrate.py
Make the commit not get call errors subsumed.
[wizard.git] / wizard / command / migrate.py
index 10de6d0841e130b67e79283cd5023489a2bee486..259d2e747861ae5728f061d3b2ff9923819b88ec 100644 (file)
@@ -1,55 +1,71 @@
 import os
-import itertools
 import shutil
 import logging
-import errno
-import sys
 
 from wizard import command, deploy, shell, util
 
 def main(argv, baton):
     options, args = parse_args(argv, baton)
-    dir = args[0]
-    command.chdir(dir)
+    if args:
+        dir = args[0]
+    else:
+        dir = os.getcwd()
+
+    shell.drop_priviledges(dir, options.log_file)
 
-    shell.drop_priviledges(options)
+    util.chdir(dir)
     sh = shell.Shell(options.dry_run)
 
     logging.info("Migrating %s" % dir)
     logging.debug("uid is %d" % os.getuid())
 
-    deployment = make_deployment() # uses chdir
-
-    # turn this into some kind of assert
-    if not deployment.configured:
-        raise NotConfiguredError(deployment.location)
-
-    version = deployment.app_version
-    repo    = version.application.repository(options.srv_path)
-    tag     = version.scripts_tag
-    check_if_tag_exists(sh, repo, tag, version)
+    deployment = deploy.ProductionCopy(".")
 
-    check_if_already_migrated(options)
+    # deal with old-style migration, remove this later
+    if os.path.isfile(".scripts/old-version") and not os.path.isfile(".scripts-version"):
+        os.rename(".scripts/old-version", ".scripts-version")
 
     os.unsetenv("GIT_DIR") # prevent some perverse errors
 
-    # XXX: turn this into a context
     try:
-        try:
-            os.open(".scripts-migrate-lock", os.O_CREAT | os.O_EXCL)
-        except OSError as e:
-            if e.errno == errno.EEXIST:
-                raise DirectoryLockedError
-            elif e.errno == errno.EACCES:
-                raise command.PermissionsError(dir)
+        deployment.verify()
+        raise AlreadyMigratedError(deployment.location)
+    except deploy.NotMigratedError:
+        pass
+    except (deploy.CorruptedAutoinstallError, AlreadyMigratedError):
+        if not options.force:
             raise
-        make_repository(sh, options, repo, tag)
-        check_variables(deployment, options)
-    finally:
+
+    deployment.verifyTag(options.srv_path)
+
+    if options.force_version:
+        version = deployment.application.makeVersion(options.force_version)
+    else:
+        try:
+            deployment.verifyVersion()
+            version = deployment.app_version
+        except deploy.VersionMismatchError as e:
+            # well, we'll use that then
+            version = deployment.application.makeVersion(str(e.real_version))
+    repo = version.application.repository(options.srv_path)
+    tag = version.scripts_tag
+    try:
+        sh.call("git", "--git-dir=%s" % repo, "rev-parse", tag)
+    except shell.CallError:
+        raise UnsupportedVersion(version.version)
+
+    with util.LockDirectory(".scripts-migrate-lock"):
         try:
-            os.unlink(".scripts-migrate-lock")
-        except OSError:
-            pass
+            if options.force:
+                perform_force(options)
+            make_repository(sh, options, repo, tag)
+            check_variables(deployment, options)
+        except KeyboardInterrupt:
+            # revert it; barring zany race conditions this is safe
+            if os.path.exists(".scripts"):
+                shutil.rmtree(".scripts")
+            if os.path.exists(".git"):
+                shutil.rmtree(".git")
 
 def parse_args(argv, baton):
     usage = """usage: %prog migrate [ARGS] DIR
@@ -68,64 +84,26 @@ NOT run this command as root."""
     parser.add_option("--force", "-f", dest="force", action="store_true",
             default=False, help="If .git or .scripts directory already exists,"
             "delete them and migrate")
+    parser.add_option("--force-version", dest="force_version",
+            default=None, help="If .scripts-version tells lies, explicitly specify"
+            "a version to migrate to.")
     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)
 
-def check_if_already_migrated(options):
+def perform_force(options):
     has_git = os.path.isdir(".git")
     has_scripts = os.path.isdir(".scripts")
 
-    # deal with old-style migration
-    if os.path.isfile(".scripts/old-version") and not os.path.isfile(".scripts-version"):
-        os.rename(".scripts/old-version", ".scripts-version")
-
-    if not has_git and has_scripts:
-        if not options.force:
-            raise CorruptedAutoinstallError(dir)
-    elif has_git and not has_scripts:
-        # can't force this
-        raise AlreadyVersionedError(dir)
-    elif has_git and has_scripts:
-        if not options.force:
-            raise AlreadyMigratedError(dir)
-
-    if options.force:
-        def rm_with_backup(file):
-            prefix = "%s.bak" % file
-            name = None
-            for i in itertools.count():
-                name = "%s.%d" % (prefix, i)
-                if not os.path.exists(name):
-                    break
-            logging.warning("Force removing %s directory (backup at %s)" % (file, name))
-            os.rename(file, name)
-        if has_git:
-            if not options.dry_run:
-                rm_with_backup(".git")
-        if has_scripts:
-            if not options.dry_run:
-                rm_with_backup(".scripts")
-
-def make_deployment():
-    try:
-        return deploy.Deployment(".")
-    except IOError as e:
-        if e.errno == errno.ENOENT:
-            raise NotAutoinstallError(dir)
-        else: raise e
-
-def check_if_tag_exists(sh, repo, tag, version):
-    # check if the version we're trying to convert exists. We assume
-    # a convention here, namely, app-1.2.3-scripts is what we want. If
-    # you broke the convention... shame on you.
-    try:
-        sh.call("git", "--git-dir", repo, "rev-parse", tag)
-    except shell.CallError:
-        raise NoTagError(version)
+    if has_git:
+        logging.warning("Force removing .git directory")
+        if not options.dry_run: backup = util.safe_unlink(".git")
+        logging.info(".git backed up to %s" % backup)
+    if has_scripts:
+        logging.warning("Force removing .scripts directory")
+        if not options.dry_run: backup = util.safe_unlink(".scripts")
+        logging.info(".scripts backed up to %s" % backup)
 
 def make_repository(sh, options, repo, tag):
     sh.call("git", "init") # create repository
@@ -178,76 +156,25 @@ class Error(command.Error):
     pass
 
 class AlreadyMigratedError(Error):
+    quiet = True
     def __init__(self, dir):
         self.dir = dir
     def __str__(self):
         return """
 
-ERROR: Directory already contains a .git and
-.scripts directory.  If you force this migration,
-both of these directories will be removed.
-"""
-
-class AlreadyVersionedError(Error):
-    def __init__(self, dir):
-        self.dir = dir
-    def __str__(self):
-        return """
-
-ERROR: Directory contains a .git directory, but not
-a .scripts directory.  If this is not a corrupt
-migration, this means that the user was versioning their
-install using Git.  You cannot force this case.
-"""
-
-class NotConfiguredError(Error):
-    def __init__(self, dir):
-        self.dir = dir
-    def __str__(self):
-        return """
-
-ERROR: The install was well-formed, but not configured
-(essential configuration files were not found.)
-"""
-
-class CorruptedAutoinstallError(Error):
-    def __init__(self, dir):
-        self.dir = dir
-    def __str__(self):
-        return """
-
-ERROR: Directory contains a .scripts directory,
-but not a .git directory.  If you force this migration,
-the .scripts directory will be removed.
-"""
-
-class NotAutoinstallError(Error):
-    def __init__(self, dir):
-        self.dir = dir
-    def __str__(self):
-        return """
-
-ERROR: Could not find .scripts-version file. Are you sure
-this is an autoinstalled application?
-"""
-
-class DirectoryLockedError(Error):
-    def __init__(self, dir):
-        self.dir = dir
-    def __str__(self):
-        return """
-
-ERROR: Could not acquire lock on directory.  Maybe there is
-another migration process running?
+This autoinstall is already migrated; move along, nothing to
+see here.  (If you really want to, you can force a re-migration
+with --force, but this will blow away the existing .git and
+.scripts directories (i.e. your history and Wizard configuration).)
 """
 
-class NoTagError(Error):
+class UnsupportedVersion(Error):
     def __init__(self, version):
         self.version = version
     def __str__(self):
         return """
 
-ERROR: Could not find tag v%s-scripts in repository
-for %s.  Double check and make sure
-the repository was prepared with all necessary tags!
-""" % (self.version.version, self.version.application.name)
+ERROR: This autoinstall is presently on %s, which is unsupported by
+Wizard.  Please manually upgrade it to one that is supported,
+and then retry the migration; usually the latest version is supported.
+""" % self.version