- sh.call("git", "checkout", "-b", "master", "--")
- except shell.CallError:
- sh.call("git", "checkout", "master", "--")
- sh.call("git", "reset", "--hard", final_commit)
- # 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
- # XXX: frob .htaccess to make site inaccessible
- # XXX: need locking
- # git merge (which performs a fast forward)
- # - merge could fail (race); that's /really/ dangerous.
- sh.call("git", "pull", temp_wc_dir, "master")
- # run update script
- d.application.upgrade(options)
- # 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
-
-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:
- 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", message)
- except shell.CallError:
- logging.info("No changes detected")
-
-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)
- 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", tag, "--")
- d.parametrize(".")
- for file in d.application.parametrized_files:
+ 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)
+ # a master branch may not necessarily exist if the user
+ # was manually installed to an earlier version