]> scripts.mit.edu Git - wizard.git/blob - wizard/command/mass_migrate.py
Implement MediaWiki scaffolding for auto conflict resolution; untested.
[wizard.git] / wizard / command / mass_migrate.py
1 import optparse
2 import logging
3 import os
4 import os.path
5 import pwd
6 import hashlib
7 import errno
8 import time
9 import itertools
10
11 import wizard
12 from wizard import deploy, util, shell, sset, command
13
14 def main(argv, baton):
15     options, args = parse_args(argv, baton)
16     app = args[0]
17     base_args = calculate_base_args(options)
18     sh = shell.ParallelShell.make(options.no_parallelize, options.max_processes)
19     seen = sset.make(options.seen)
20     is_root = not os.getuid()
21     warnings_log, errors_log = command.open_logs(options.log_dir)
22     # loop stuff
23     errors = {}
24     i = 0
25     deploys = deploy.parse_install_lines(app, options.versions_path)
26     requested_deploys = itertools.islice(deploys, options.limit)
27     for i, d in enumerate(requested_deploys, 1):
28         # check if we want to punt due to --limit
29         if d.location in seen:
30             continue
31         if is_root and not command.security_check_homedir(d):
32             continue
33         logging.info("Processing %s" % d.location)
34         child_args = list(base_args)
35         # calculate the log file, if a log dir was specified
36         if options.log_dir:
37             log_file = command.calculate_log_name(options.log_dir, i, d.location)
38             child_args.append("--log-file=" + log_file)
39         # actual meat
40         def make_on_pair(d, i):
41             # we need to make another stack frame so that d and i get specific bindings.
42             def on_success(stdout, stderr):
43                 if stderr:
44                     warnings_log.write("%s\n" % d.location)
45                     logging.warning("Warnings [%04d] %s:\n%s" % (i, d.location, stderr))
46                 seen.add(d.location)
47             def on_error(e):
48                 if e.name == "wizard.command.migrate.AlreadyMigratedError" or \
49                    e.name == "AlreadyMigratedError":
50                     seen.add(d.location)
51                     logging.info("Skipped already migrated %s" % d.location)
52                 else:
53                     name = e.name
54                     if name not in errors: errors[name] = []
55                     errors[name].append(d)
56                     logging.error("%s in [%04d] %s" % (name, i, d.location))
57                     errors_log.write("%s\n" % d.location)
58             return (on_success, on_error)
59         on_success, on_error = make_on_pair(d, i)
60         sh.call("wizard", "migrate", d.location, *child_args,
61                       on_success=on_success, on_error=on_error)
62     sh.join()
63     for name, deploys in errors.items():
64         logging.warning("%s from %d installs" % (name, len(deploys)))
65
66 def parse_args(argv, baton):
67     usage = """usage: %prog mass-migrate [ARGS] APPLICATION
68
69 Mass migrates an application to the new repository format.
70 Essentially equivalent to running '%prog migrate' on all
71 autoinstalls for a particular application found by parallel-find,
72 but with advanced reporting.
73
74 This command is intended to be run as root on a server with
75 the scripts AFS patch."""
76     parser = command.WizardOptionParser(usage)
77     baton.push(parser, "log_dir")
78     baton.push(parser, "seen")
79     baton.push(parser, "no_parallelize")
80     baton.push(parser, "dry_run")
81     baton.push(parser, "max_processes")
82     parser.add_option("--force", dest="force", action="store_true",
83             default=False, help="Force migrations to occur even if .scripts or .git exists.")
84     baton.push(parser ,"limit")
85     baton.push(parser, "versions_path")
86     baton.push(parser, "srv_path")
87     options, args, = parser.parse_all(argv)
88     if len(args) > 1:
89         parser.error("too many arguments")
90     elif not args:
91         parser.error("must specify application to migrate")
92     return options, args
93
94 def calculate_base_args(options):
95     return command.makeBaseArgs(options, dry_run="--dry-run", srv_path="--srv-path",
96             force="--force")
97