X-Git-Url: https://scripts.mit.edu/gitweb/wizard.git/blobdiff_plain/1c8e88f729ba87e6a8224f33b1b704a9baaddd36..aef4c25109a195042df0ab8cb979692c6077d36f:/wizard/command/upgrade.py diff --git a/wizard/command/upgrade.py b/wizard/command/upgrade.py index 91530c3..0850994 100644 --- a/wizard/command/upgrade.py +++ b/wizard/command/upgrade.py @@ -9,6 +9,9 @@ import itertools import time import errno +# XXX: read().strip() is an accident waiting to happen if anyone ever +# makes a directory with a trailing or leading space. Unlikely, but yeah. + from wizard import app, command, deploy, merge, shell, user, util buffer = 1024 * 1024 * 30 # 30 MiB we will always leave available @@ -86,6 +89,9 @@ class Upgrade(object): logging.info("Upgrading %s" % os.getcwd()) self.preflight() self.merge() + # Note invariant: we expect you to be in the production + # directory at this point, even if you --continue'd from + # the temporary directory! self.postflight() # Till now, all of our operations were in a tmp sandbox. if self.options.dry_run: @@ -108,37 +114,44 @@ class Upgrade(object): self.resumeChdir() self.resumeState() self.resumeLogging() - util.chdir(shell.eval("git", "config", "remote.origin.url")) self.resumeProd() def resumeChdir(self): """ - If we called ``--continue`` inside a production copy, check if - :file:`.wizard/pending` exists and change to that directory if so. + If we called ``--continue`` inside a working copy (the usual + situation), check if :file:`.wizard/pending` exists and change + to that directory if so. """ - # XXX: Can't use deploy; maybe should stringify constants? - if os.path.exists(".wizard/pending"): - newdir = open(".wizard/pending").read().strip() - logging.warning("Detected production copy; changing directory to %s", newdir) - os.chdir(newdir) - def resumeState(self): self.temp_wc_dir = os.getcwd() - self.wc = deploy.WorkingCopy(".") - try: - self.user_commit, self.next_commit = open(".git/WIZARD_PARENTS", "r").read().split() - self.version = open(".git/WIZARD_UPGRADE_VERSION", "r").read() - except IOError as e: - if e.errno == errno.ENOENT: - raise CannotResumeError() - else: - raise + didChdir = command.chdir_to_production() + self.prod = deploy.ProductionCopy(".") + pending_dir = open(self.prod.pending_file).read().strip() + if not didChdir: + logging.warning("Continued from a production copy; using working copy at %s", pending_dir) + self.temp_wc_dir = pending_dir + elif self.temp_wc_dir != pending_dir: + # prefer the original working copy, but warn that someone + # else someone a started an upgrade in the meantime (XXX: + # actually, that someone else should bug out, not clobber) + logging.warning("Someone else appears to have started an upgrade at %s", pending_dir) + def resumeState(self): + with util.ChangeDirectory(self.temp_wc_dir): + self.wc = deploy.WorkingCopy(".") + try: + self.user_commit, self.next_commit = open(".git/WIZARD_PARENTS", "r").read().split() + self.version = open(".git/WIZARD_UPGRADE_VERSION", "r").read() + except IOError as e: + if e.errno == errno.ENOENT: + raise CannotResumeError() + else: + raise def resumeLogging(self): - options = self.options - if not options.log_file and os.path.exists(".git/WIZARD_LOG_FILE"): - options.log_file = open(".git/WIZARD_LOG_FILE", "r").read() - command.setup_file_logger(options.log_file, options.debug) + with util.ChangeDirectory(self.temp_wc_dir): + options = self.options + if not options.log_file and os.path.exists(".git/WIZARD_LOG_FILE"): + options.log_file = open(".git/WIZARD_LOG_FILE", "r").read() + command.setup_file_logger(options.log_file, options.debug) def resumeProd(self): """Restore :attr:`prod` attribute, and check if the production copy has drifted.""" - self.prod = deploy.ProductionCopy(".") try: # simulate the action of `git status`, based on cmd_status()'s call to # refresh_cache() in builtin-commit.c @@ -169,7 +182,16 @@ class Upgrade(object): self.prod.verify() self.prod.verifyDatabase() self.prod.verifyTag(options.srv_path) - self.prod.verifyGit(options.srv_path) + try: + self.prod.verifyGit(options.srv_path) + except deploy.InconsistentWizardTagError: + shell.call("git", "fetch") + shell.call("git", "fetch", "--tags") + shell.call("wizard", "remaster") + self.prod.verifyGit(options.srv_path) + except deploy.HeadNotDescendantError: + shell.call("wizard", "remaster") + self.prod.verifyGit(options.srv_path) if not options.skip_verification: self.prod.verifyConfigured() try: @@ -194,8 +216,7 @@ class Upgrade(object): break else: raise VersionRematchFailed - if not options.skip_verification: - self.prod.verifyWeb() + self.verifyWeb() self.preflightAlreadyUpgraded() self.preflightQuota() def preflightBlacklist(self): @@ -278,7 +299,7 @@ class Upgrade(object): 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", "--shared", ".", 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 @@ -316,6 +337,14 @@ class Upgrade(object): 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) @@ -338,6 +367,10 @@ class Upgrade(object): 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: @@ -415,22 +448,19 @@ class Upgrade(object): try: # run update script self.prod.upgrade(version_obj, self.options) - self.prod.verifyWeb() + self.verifyWeb() + try: + os.unlink(self.prod.pending_file) + except OSError as e: + if e.errno != errno.ENOENT: + raise except app.UpgradeFailure: - if self.options.disable_rollback: - logging.warning("Upgrade failed, rollback is disabled") - logging.warning("You can manually rollback with: wizard restore top") - else: - logging.warning("Upgrade failed: rolling back") - self.upgradeRollback(backup) + logging.warning("Upgrade failed: rolling back") + self.upgradeRollback(backup) raise except deploy.WebVerificationError as e: - if self.options.disable_rollback: - logging.warning("Web verification failed, rollback is disabled") - logging.warning("You can manually rollback with: wizard restore top") - else: - logging.warning("Web verification failed: rolling back") - self.upgradeRollback(backup) + logging.warning("Web verification failed: rolling back") + self.upgradeRollback(backup) raise app.UpgradeVerificationFailure() # XXX: frob .htaccess to make site accessible # to do this, check if .htaccess changed, first. Upgrade @@ -442,12 +472,17 @@ class Upgrade(object): if not self.options.disable_rollback: shell.call("wizard", "restore", backup) try: - self.prod.verifyWeb() + self.verifyWeb() except deploy.WebVerificationError: logging.critical("Web verification failed after rollback") else: logging.warning("Rollback was disabled; you can rollback with `wizard restore %s`", backup) + def verifyWeb(self): + if not self.options.skip_verification: + self.prod.verifyWeb() + + # utility functions def mv_shm_to_tmp(curdir, use_shm): @@ -480,10 +515,8 @@ of the conflicted working tree to stdout, separated by a space.""" "resolved using the current working directory as the resolved copy.") parser.add_option("--force", dest="force", action="store_true", default=False, help="Force running upgrade even if it's already at latest version.") - parser.add_option("--skip-verification", dest="skip-verification", action="store_true", + parser.add_option("--skip-verification", dest="skip_verification", action="store_true", default=False, help="Skip running configuration and web verification checks.") - parser.add_option("--disable-rollback", dest="disable-rollback", action="store_true", - default=False, help="Don't automatically rollback the upgrade if the result fails to verify.") parser.add_option("--non-interactive", dest="non_interactive", action="store_true", default=False, help="Don't drop to shell in event of conflict.") parser.add_option("--rr-cache", dest="rr_cache", metavar="PATH", @@ -496,6 +529,8 @@ of the conflicted working tree to stdout, separated by a space.""" options, args = parser.parse_all(argv) if len(args) > 1: parser.error("too many arguments") + if options.skip_verification: + logging.warning("Verification is disabled; Wizard may break your application and will not tell you about it") return options, args class Error(command.Error): @@ -557,7 +592,10 @@ class BlacklistedError(Error): ERROR: This autoinstall was manually blacklisted against errors; if the user has not been notified of this, please send them -mail. +mail. If you know that this application is blacklisted and +would like to attempt an upgrade anyway, run: + + wizard blacklist --delete The reason was: %s""" % self.reason @@ -588,3 +626,23 @@ class UnknownVersionError(Error): ERROR: The version you are attempting to upgrade from (%s) is unknown to the repository Wizard is using.""" % str(self.version) + +class UpgradeInProgressError(Error): + #: Location of pending upgrade + location = None + #: Time of pending upgrade + time = None + def __init__(self, location, time): + self.location = location + self.time = time + def __str__(self): + return """ + +ERROR: There is already an upgrade in progress at + + %s + +which was last started at %s. + +To ignore and start another upgrade anyway, remove the file +.wizard/pending and try again.""" % (self.location, time.ctime(self.time))