import optparse import sys import os import shutil import logging.handlers import errno import tempfile import itertools from wizard import command, deploy, shell, util # XXX: WARNING EXPERIMENTAL DANGER DANGER WILL ROBINSON # need errors for checking DAG integrity (if the user is on a completely # different history tree, stuff is problems) def main(argv, baton): options, args = parse_args(argv, baton) dir = args[0] command.chdir(dir) if not os.path.isdir(".git"): raise NotAutoinstallError() try: d = deploy.Deployment(".") except IOError as e: if e.errno == errno.ENOENT: raise NotAutoinstallError() else: raise e repo = d.application.repository(options.srv_path) # begin the command line process sh = shell.Shell() # setup environment util.set_git_env() # commit their changes if not options.dry_run: pre_upgrade_commit(sh) # perform fetch to update repository state sh.call("git", "fetch", repo) # clone their website to a temporary directory 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): if options.dry_run: pre_upgrade_commit(sh) # 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")[0].rstrip() user_commit = sh.call("git", "rev-parse", "HEAD")[0].rstrip() next_commit = sh.call("git", "rev-parse", version)[0].rstrip() 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 try: # 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(temp_wc_dir) for file in d.application.parametrized_files: try: sh.call("git", "add", "--", file) except shell.CallError: pass virtual_tree = sh.call("git", "write-tree")[0].rstrip() parent_args = itertools.chain(*(["-p", p] for p in parents)) virtual_commit = sh.call("git", "commit-tree", virtual_tree, *parent_args, input="")[0].rstrip() sh.call("git", "reset", "--hard") return virtual_commit user_tree = sh.call("git", "rev-parse", "HEAD^{tree}")[0].rstrip() base_virtual_commit = make_virtual_commit(d.app_version.pristine_tag) next_virtual_commit = make_virtual_commit(version, [base_virtual_commit]) user_virtual_commit = sh.call("git", "commit-tree", user_tree, "-p", base_virtual_commit, input="")[0].rstrip() sh.call("git", "checkout", user_virtual_commit, "--") sh.call("git", "merge", next_virtual_commit) # XXX except shell.CallError: print temp_wc_dir raise MergeFailed # Make it possible to resume here new_tree = sh.call("git", "rev-parse", "HEAD^{tree}")[0].rstrip() final_commit = sh.call("git", "commit-tree", new_tree, "-p", user_commit, "-p", next_commit, input=message)[0].rstrip() try: 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 # 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 def pre_upgrade_commit(sh): try: 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 sh.call("git", "commit", "-a", "-m", message) except shell.CallError: logging.info("No changes detected") pass 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. 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") 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): def __str__(self): return """ ERROR: Could not find .git file. Are you sure this is an autoinstalled application? Did you remember to migrate it? """ class MergeFailed(Error): def __str__(self): return """ ERROR: Merge failed. Change directory to the temporary directory and manually resolve the merge. """