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