5 import logging.handlers
10 from wizard import command, deploy, shell, util
12 def main(argv, baton):
13 options, args = parse_args(argv, baton)
19 temp_wc_dir = os.getcwd()
20 user_commit, next_commit = open(".git/WIZARD_PARENTS", "r").read().split()
21 repo = open(".git/WIZARD_REPO", "r").read()
22 version = open(".git/WIZARD_UPGRADE_VERSION", "r").read()
23 util.chdir(sh.eval("git", "config", "remote.origin.url"))
24 d = deploy.Deployment(".")
26 d = deploy.Deployment(".")
28 d.verifyTag(options.srv_path)
29 d.verifyGit(options.srv_path)
31 repo = d.application.repository(options.srv_path)
32 version = calculate_newest_version(sh, repo)
33 if version == d.app_version.scripts_tag:
35 if not options.dry_run:
36 perform_pre_commit(sh)
37 temp_wc_dir = perform_tmp_clone(sh)
38 with util.ChangeDirectory(temp_wc_dir):
40 # we delayed performing the pre upgrade commit
41 # until we're in the temporary directory
42 perform_pre_commit(sh)
43 sh.call("git", "remote", "add", "scripts", repo)
44 sh.call("git", "fetch", "scripts")
45 user_commit, next_commit = calculate_parents(sh, version)
46 # save variables so that --continue will work
47 # yeah yeah no trailing newline whatever
48 open(".git/WIZARD_REPO", "w").write(repo)
49 open(".git/WIZARD_UPGRADE_VERSION", "w").write(version)
50 open(".git/WIZARD_PARENTS", "w").write("%s\n%s" % (user_commit, next_commit))
51 perform_merge(sh, repo, d, version)
52 # variables: version, user_commit, next_commit, temp_wc_dir
53 with util.ChangeDirectory(temp_wc_dir):
54 message = make_commit_message(version)
55 new_tree = sh.eval("git", "rev-parse", "HEAD^{tree}")
56 final_commit = sh.eval("git", "commit-tree", new_tree,
57 "-p", user_commit, "-p", next_commit, input=message, log=True)
58 # a master branch may not necessarily exist if the user
59 # was manually installed to an earlier version
61 sh.call("git", "checkout", "-b", "master", "--")
62 except shell.CallError:
63 sh.call("git", "checkout", "master", "--")
64 sh.call("git", "reset", "--hard", final_commit)
65 # Till now, all of our operations were in a tmp sandbox.
67 logging.info("Dry run, bailing. See results at %s" % temp_wc_dir)
69 # XXX: frob .htaccess to make site inaccessible
71 # git merge (which performs a fast forward)
72 # - merge could fail (race); that's /really/ dangerous.
73 sh.call("git", "pull", temp_wc_dir, "master")
75 d.application.upgrade(options)
76 # XXX: frob .htaccess to make site accessible
77 # XXX: - check if .htaccess changed, first. Upgrade
78 # process might have frobbed it. Don't be
79 # particularly worried if the segment dissappeared
81 def make_commit_message(version):
82 message = "Upgraded autoinstall in %s to %s.\n\n%s" % (util.get_dir_owner(), version, util.get_git_footer())
84 message += "\nUpgraded-by: " + util.get_operator_git()
85 except util.NoOperatorInfo:
89 def calculate_newest_version(sh, repo):
90 # XXX: put this in Application
91 return sh.eval("git", "--git-dir="+repo, "describe", "--tags", "master")
93 def calculate_parents(sh, version):
94 user_commit = sh.eval("git", "rev-parse", "HEAD")
95 next_commit = sh.eval("git", "rev-parse", version)
96 return user_commit, next_commit
98 def perform_pre_commit(sh):
99 message = "Pre-commit of %s locker before autoinstall upgrade.\n\n%s" % (util.get_dir_owner(), util.get_git_footer())
101 message += "\nPre-commit-by: " + util.get_operator_git()
102 except util.NoOperatorInfo:
105 sh.call("git", "commit", "-a", "-m", message)
106 except shell.CallError:
107 logging.info("No changes detected")
109 def perform_tmp_clone(sh):
110 temp_dir = tempfile.mkdtemp(prefix="wizard")
111 temp_wc_dir = os.path.join(temp_dir, "repo")
112 logging.info("Using temporary directory: " + temp_wc_dir)
113 sh.call("git", "clone", "--shared", ".", temp_wc_dir)
116 def perform_merge(sh, repo, d, version):
117 # naive merge algorithm:
118 # sh.call("git", "merge", "-m", message, "scripts/master")
119 # crazy merge algorithm:
120 def make_virtual_commit(tag, parents = []):
121 """WARNING: Changes state of Git repository"""
122 sh.call("git", "checkout", tag, "--")
124 for file in d.application.parametrized_files:
126 sh.call("git", "add", "--", file)
127 except shell.CallError:
129 virtual_tree = sh.eval("git", "write-tree", log=True)
130 parent_args = itertools.chain(*(["-p", p] for p in parents))
131 virtual_commit = sh.eval("git", "commit-tree", virtual_tree,
132 *parent_args, input="", log=True)
133 sh.call("git", "reset", "--hard")
134 return virtual_commit
135 user_tree = sh.eval("git", "rev-parse", "HEAD^{tree}")
136 base_virtual_commit = make_virtual_commit(d.app_version.scripts_tag)
137 next_virtual_commit = make_virtual_commit(version, [base_virtual_commit])
138 user_virtual_commit = sh.eval("git", "commit-tree", user_tree,
139 "-p", base_virtual_commit, input="", log=True)
140 sh.call("git", "checkout", user_virtual_commit, "--")
142 sh.call("git", "merge", next_virtual_commit)
143 except shell.CallError:
147 def parse_args(argv, baton):
148 usage = """usage: %prog upgrade [ARGS] [DIR]
150 Upgrades an autoinstall to the latest version. This involves
151 updating files and running .scripts/update.
153 WARNING: This is still experimental."""
154 parser = command.WizardOptionParser(usage)
155 parser.add_option("--dry-run", dest="dry_run", action="store_true",
156 default=False, help="Prints would would be run without changing anything")
157 # notice trailing underscore
158 parser.add_option("--continue", dest="continue_", action="store_true",
159 default=False, help="Continues an upgrade that has had its merge manually "
160 "resolved using the current working directory as the resolved copy.")
161 baton.push(parser, "srv_path")
162 options, args = parser.parse_all(argv)
164 parser.error("too many arguments")
167 class Error(command.Error):
168 """Base exception for all exceptions raised by upgrade"""
171 class AlreadyUpgraded(Error):
175 ERROR: This autoinstall is already at the latest version."""
177 class MergeFailed(Error):
181 ERROR: Merge failed. Resolve the merge by cd'ing to the
182 temporary directory, finding conflicted files with `git status`,
183 resolving the files, adding them using `git add`, and then
184 committing your changes with `git commit` (your log message
185 will be ignored), and then running `wizard upgrade --continue`."""