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