- if not options.dry_run:
- perform_pre_commit(sh)
- # If /dev/shm exists, it's a tmpfs and we can use it
- # to do a fast git merge. Don't forget to move it to
- # /tmp if it fails.
- if not options.dry_run:
- use_shm = os.path.exists("/dev/shm")
- temp_dir, temp_wc_dir = perform_tmp_clone(sh, use_shm)
- with util.ChangeDirectory(temp_wc_dir):
- wc = deploy.WorkingCopy(".")
- 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))
- open(".git/WIZARD_SIZE", "w").write(str(scripts.get_disk_usage()))
- if options.log_file:
- open(".git/WIZARD_LOG_FILE", "w").write(options.log_file)
- perform_merge(sh, repo, d, wc, version, use_shm, kib_limit and kib_limit - kib_usage or None)
- # 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)
+
+ def merge(self):
+ if not self.options.dry_run:
+ self.mergePreCommit()
+ self.mergeClone()
+ logging.debug("Temporary WC dir is %s", self.temp_wc_dir)
+ with util.ChangeDirectory(self.temp_wc_dir):
+ self.wc = deploy.WorkingCopy(".")
+ shell.call("git", "remote", "add", "wizard", self.repo)
+ shell.call("git", "fetch", "-q", "wizard")
+ self.user_commit = shell.eval("git", "rev-parse", "HEAD")
+ self.next_commit = shell.eval("git", "rev-parse", self.version)
+ self.mergeSaveState()
+ self.mergePerform()
+ def mergePreCommit(self):
+ def get_file_set(rev):
+ return set(shell.eval("git", "ls-tree", "-r", "--name-only", rev).split("\n"))
+ # add all files that are unversioned but would be replaced by the pull,
+ # and generate a new commit
+ old_files = get_file_set("HEAD")
+ new_files = get_file_set(self.version)
+ added_files = new_files - old_files
+ for f in added_files:
+ if os.path.lexists(f): # broken symbolic links count too!
+ shell.call("git", "add", f)
+ message = "Pre-commit before autoinstall upgrade.\n\n%s" % util.get_git_footer()
+ try:
+ message += "\nPre-commit-by: " + util.get_operator_git()
+ except util.NoOperatorInfo:
+ pass
+ try:
+ shell.call("git", "commit", "-a", "-m", message)
+ except shell.CallError as e:
+ if "Permission denied" in e.stderr:
+ raise util.PermissionsError
+ elif e.stderr:
+ raise
+ logging.info("No changes detected")
+ def mergeClone(self):
+ # If /dev/shm exists, it's a tmpfs and we can use it
+ # to do a fast git merge. Don't forget to move it to
+ # /tmp if it fails.
+ if not self.options.dry_run and not self.options.debug:
+ self.use_shm = os.path.exists("/dev/shm")
+ if self.use_shm:
+ dir = "/dev/shm/wizard"
+ if not os.path.exists(dir):
+ os.mkdir(dir)
+ # XXX: race
+ os.chmod(dir, 0o777)
+ else:
+ dir = None
+ self.temp_dir = tempfile.mkdtemp(prefix="wizard", dir=dir)
+ self.temp_wc_dir = os.path.join(self.temp_dir, "repo")
+ logging.info("Using temporary directory: " + self.temp_wc_dir)
+ shell.call("git", "clone", "-q", ".", self.temp_wc_dir)
+ def mergeSaveState(self):
+ """Save variables so that ``--continue`` will work."""
+ # yeah yeah no trailing newline whatever
+ open(".git/WIZARD_UPGRADE_VERSION", "w").write(self.version)
+ open(".git/WIZARD_PARENTS", "w").write("%s\n%s" % (self.user_commit, self.next_commit))
+ open(".git/WIZARD_SIZE", "w").write(str(util.disk_usage()))
+ if self.options.log_file:
+ open(".git/WIZARD_LOG_FILE", "w").write(self.options.log_file)
+ def mergePerform(self):
+ def prepare_config():
+ self.wc.prepareConfig()
+ shell.call("git", "add", ".")
+ def resolve_conflicts():
+ return self.wc.resolveConflicts()
+ shell.call("git", "config", "merge.conflictstyle", "diff3")
+ # setup rerere
+ if self.options.rr_cache is None:
+ self.options.rr_cache = os.path.join(self.prod.location, ".git", "rr-cache")
+ if not os.path.exists(self.options.rr_cache):
+ os.mkdir(self.options.rr_cache)
+ os.symlink(self.options.rr_cache, os.path.join(self.wc.location, ".git", "rr-cache"))
+ shell.call("git", "config", "rerere.enabled", "true")
+ try:
+ merge.merge(self.wc.app_version.wizard_tag, self.version,
+ prepare_config, resolve_conflicts)
+ except merge.MergeError:
+ self.mergeFail()
+ def mergeFail(self):
+ files = set()
+ for line in shell.eval("git", "ls-files", "--unmerged").splitlines():
+ files.add(line.split(None, 3)[-1])
+ conflicts = len(files)
+ # XXX: this is kind of fiddly; note that temp_dir still points at the OLD
+ # location after this code.
+ self.temp_wc_dir = mv_shm_to_tmp(os.getcwd(), self.use_shm)
+ self.wc.location = self.temp_wc_dir
+ os.chdir(self.temp_wc_dir)
+ if os.path.exists(self.prod.pending_file):
+ mtime = os.path.getmtime(self.prod.pending_file)
+ pending_location = open(self.prod.pending_file).read().strip()
+ # don't complain if .wizard/pending is a day old
+ if mtime > (time.time() - 60 * 60 * 24):
+ raise UpgradeInProgressError(pending_location, mtime)
+ else:
+ logging.warning("Probably harmless old pending upgrade at %s from %s", pending_location, time.ctime(mtime))
+ open(self.prod.pending_file, "w").write(self.temp_wc_dir)
+ if self.options.non_interactive:
+ print "%d %s" % (conflicts, self.temp_wc_dir)
+ raise MergeFailed
+ else:
+ user_shell = os.getenv("SHELL")
+ if not user_shell: user_shell = "/bin/bash"
+ # XXX: scripts specific hack, since mbash doesn't respect the current working directory
+ # When the revolution comes (i.e. $ATHENA_HOMEDIR/Scripts is your Scripts home
+ # directory) this isn't strictly necessary, but we'll probably need to support
+ # web_scripts directories ad infinitum.
+ if user_shell == "/usr/local/bin/mbash": user_shell = "/bin/bash"
+ while 1:
+ print
+ print "ERROR: The merge failed with %d conflicts in these files:" % conflicts
+ print
+ for file in sorted(files):
+ print " * %s" % file
+ print
+ print "Please resolve these conflicts (edit and then `git add`), and"
+ print "then type 'exit'. You will now be dropped into a shell whose working"
+ print "directory is %s" % self.temp_wc_dir
+ print
+ print "NOTE: If you resolve these conflicts, and then the upgrade fails for"
+ print "an unrelated reason, you can run 'wizard upgrade --continue' from this"
+ print "directory to try again."
+ try:
+ shell.call(user_shell, "-i", interactive=True)
+ except shell.CallError as e:
+ logging.warning("Shell returned non-zero exit code %d" % e.code)
+ if shell.eval("git", "ls-files", "--unmerged").strip():
+ print
+ print "WARNING: There are still unmerged files."
+ out = raw_input("Continue editing? [y/N]: ")
+ if out == "y" or out == "Y":
+ continue
+ else:
+ print "Aborting. The conflicted working copy can be found at:"
+ print
+ print " %s" % self.temp_wc_dir
+ print
+ print "and you can resume the upgrade process by running in that directory:"
+ print
+ print " wizard upgrade --continue"
+ sys.exit(1)
+ break
+
+ def postflight(self):
+ with util.ChangeDirectory(self.temp_wc_dir):
+ if shell.eval("git", "ls-files", "-u").strip():
+ raise UnmergedChangesError
+ shell.call("git", "commit", "--allow-empty", "-am", "throw-away commit")
+ self.wc.parametrize(self.prod)
+ shell.call("git", "add", ".")
+ message = self.postflightCommitMessage()
+ new_tree = shell.eval("git", "write-tree")
+ final_commit = shell.eval("git", "commit-tree", new_tree,
+ "-p", self.user_commit, "-p", self.next_commit, input=message, log=True)