]> scripts.mit.edu Git - wizard.git/blobdiff - wizard/command/migrate.py
Use diff3 conflict style, which gives more information.
[wizard.git] / wizard / command / migrate.py
index d5ae90ef862927d1f338cb4161a2257b361f158c..7af52a8e6c1251f4bcb70db5b604791623736e64 100644 (file)
@@ -1,40 +1,65 @@
 import os
 import shutil
 import logging
 import os
 import shutil
 import logging
-import errno
-import sys
 
 from wizard import command, deploy, shell, util
 
 def main(argv, baton):
 
 from wizard import command, deploy, shell, util
 
 def main(argv, baton):
-    options, args = parse_args(argv)
-    dir = args[0]
+    options, args = parse_args(argv, baton)
+    if args:
+        dir = args[0]
+    else:
+        dir = os.getcwd()
+
+    shell.drop_priviledges(dir, options.log_file)
+
+    util.chdir(dir)
+    sh = shell.Shell(options.dry_run)
 
 
+    logging.info("Migrating %s" % dir)
     logging.debug("uid is %d" % os.getuid())
 
     logging.debug("uid is %d" % os.getuid())
 
-    command.chdir(dir)
-    check_if_already_migrated(options)
+    deployment = deploy.ProductionCopy(".")
 
 
-    deployment = make_deployment() # uses chdir
-    version = deployment.app_version
-    repo    = version.application.repository
-    tag     = version.scripts_tag
+    # 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
 
 
     os.unsetenv("GIT_DIR") # prevent some perverse errors
 
-    sh = shell.Shell(options.dry_run)
-    check_if_tag_exists(sh, repo, tag)
-    make_repository(sh, options, repo, tag)
-    make_variables(deployment, options)
+    try:
+        deployment.verify()
+        raise AlreadyMigratedError(deployment.location)
+    except deploy.NotMigratedError:
+        pass
+    except (deploy.CorruptedAutoinstallError, AlreadyMigratedError):
+        if not options.force:
+            raise
 
 
-    if not options.dry_run:
-        deployment.updateVersion()
-        os.rename(".scripts-version", ".scripts/old-version") # archive
+    deployment.verifyTag(options.srv_path)
+
+    if options.force_version:
+        version = deployment.application.makeVersion(options.force_version)
     else:
     else:
-        logging.info("# create .scripts/version containing \"%s-%s\"" % (deployment.application.name, deployment.version))
-        logging.info("mv .scripts-version .scripts/old-version")
+        deployment.verifyVersion()
+        version = deployment.app_version
+    repo = version.application.repository(options.srv_path)
+    tag = version.scripts_tag
 
 
-def parse_args(argv):
+    with util.LockDirectory(".scripts-migrate-lock"):
+        try:
+            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
 
 Migrates a directory to our Git-based autoinstall format.
     usage = """usage: %prog migrate [ARGS] DIR
 
 Migrates a directory to our Git-based autoinstall format.
@@ -45,45 +70,32 @@ This command is meant to be run as the owner of the install
 it is upgrading (see the scripts AFS kernel patch).  Do
 NOT run this command as root."""
     parser = command.WizardOptionParser(usage)
 it is upgrading (see the scripts AFS kernel patch).  Do
 NOT run this command as root."""
     parser = command.WizardOptionParser(usage)
+    baton.push(parser, "srv_path")
     parser.add_option("--dry-run", dest="dry_run", action="store_true",
             default=False, help="Prints would would be run without changing anything")
     parser.add_option("--force", "-f", dest="force", action="store_true",
     parser.add_option("--dry-run", dest="dry_run", action="store_true",
             default=False, help="Prints would would be run without changing anything")
     parser.add_option("--force", "-f", dest="force", action="store_true",
-            default=False, help="If .git or .scripts directory already exists, delete them and migrate")
+            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")
     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)
 
     return (options, args)
 
-def check_if_already_migrated(options):
-    if os.path.isdir(".git") or os.path.isdir(".scripts"):
-        if not options.force:
-            raise AlreadyMigratedError(dir)
-        else:
-            if os.path.isdir(".git"):
-                logging.warning("Force removing .git directory")
-                if not options.dry_run: shutil.rmtree(".git")
-            if os.path.isdir(".scripts"):
-                logging.warning("Force removing .scripts directory")
-                if not options.dry_run: shutil.rmtree(".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):
-    # check if the version we're trying to convert exists. We assume
-    # a convention here, namely, v1.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)
+def perform_force(options):
+    has_git = os.path.isdir(".git")
+    has_scripts = os.path.isdir(".scripts")
+
+    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
 
 def make_repository(sh, options, repo, tag):
     sh.call("git", "init") # create repository
@@ -112,6 +124,7 @@ def make_repository(sh, options, repo, tag):
     sh.call("git", "reset", tag, "--")
     # checkout the .scripts directory
     sh.call("git", "checkout", ".scripts")
     sh.call("git", "reset", tag, "--")
     # checkout the .scripts directory
     sh.call("git", "checkout", ".scripts")
+    logging.info("Diffstat:\n" + sh.eval("git", "diff", "--stat"))
     # commit user local changes
     message = "Autoinstall migration of %s locker.\n\n%s" % (util.get_dir_owner(), util.get_git_footer())
     util.set_git_env()
     # commit user local changes
     message = "Autoinstall migration of %s locker.\n\n%s" % (util.get_dir_owner(), util.get_git_footer())
     util.set_git_env()
@@ -120,66 +133,30 @@ def make_repository(sh, options, repo, tag):
     except util.NoOperatorInfo:
         pass
     sh.call("git", "commit", "--allow-empty", "-a", "-m", message)
     except util.NoOperatorInfo:
         pass
     sh.call("git", "commit", "--allow-empty", "-a", "-m", message)
-    # for verbose purposes, give us a git status and git diff
-    if options.verbose:
-        try:
-            sh.call("git", "status")
-        except shell.CallError:
-            pass
-        try:
-            sh.call("git", "diff")
-        except shell.CallError:
-            pass
 
 
-def make_variables(d, options):
-    """Make .scripts/variables which contains variables based off of
-    what was regexed out of existing configuration files."""
+def check_variables(d, options):
+    """Attempt to extract variables and complain if some are missing."""
     variables = d.extract()
     variables = d.extract()
-    if not options.dry_run: f = open(".scripts/variables", "w")
     for k,v in variables.items():
     for k,v in variables.items():
-        if v is None:
-            # once we get everything on the same version, you should
-            # actually start paying attention to these warnings
+        if v is None and k not in d.application.deprecated_keys:
             logging.warning("Variable %s not found" % k)
         else:
             logging.debug("Variable %s is %s" % (k,v))
             logging.warning("Variable %s not found" % k)
         else:
             logging.debug("Variable %s is %s" % (k,v))
-            if not options.dry_run:
-                f.write("%s %s\n" % (k,v))
-            else:
-                logging.info('# write line "%s %s" to .scripts/variables' % (k,v))
-    if not options.dry_run: f.close()
 
 class Error(command.Error):
     """Base exception for all exceptions raised by migrate"""
     pass
 
 class AlreadyMigratedError(Error):
 
 class Error(command.Error):
     """Base exception for all exceptions raised by migrate"""
     pass
 
 class AlreadyMigratedError(Error):
+    quiet = True
     def __init__(self, dir):
         self.dir = dir
     def __str__(self):
         return """
 
     def __init__(self, dir):
         self.dir = dir
     def __str__(self):
         return """
 
-ERROR: Directory already contains a .git and/or
-.scripts directory.  Did you already migrate it?
-"""
-
-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?
+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):
-    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)