import optparse import sys import os import shutil import logging.handlers import errno import tempfile from wizard import deploy from wizard import shell from wizard import util from wizard.command import _base # 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) # we need an option that specifies the person making the update. Since n-b # doesn't actually have a way to do this, username is probably what we want. def main(argv, global_options): options, args = parse_args(argv) dir = args[0] _base.chdir(dir) if not os.path.isdir(".git"): raise NotAutoinstallError() try: d = deploy.Deployment.fromDir(".") except IOError as e: if e.errno == errno.ENOENT: raise NotAutoinstallError() else: raise e repo = d.getApplication().getRepository() # begin the command line process sh = shell.Shell(options.dry_run) # setup environment util.set_git_env() # commit their changes 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", "Pre-upgrade commit.") except shell.CallError: logging.info("No changes detected") pass # perform fetch to update repository state sh.call("git", "fetch", repo) # clone their website to a temporary directory temp_dir = tempfile.mkdtemp() 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): # 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"]) try: 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 sh.call("git", "merge", "-m", message, "scripts/master") except shell.CallError: raise MergeFailed # 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 parse_args(argv): usage = """usage: %prog upgrade [ARGS] DIR Upgrades an autoinstall ot the latest version. This involves updating files and running .scripts/update. WARNING: This is still experimental.""" parser = _base.WizardOptionParser(usage) parser.add_option("--dry-run", dest="dry_run", action="store_true", default=False, help="Prints would would be run without changing anything") 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(_base.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): pass