]> scripts.mit.edu Git - wizard.git/blob - wizard/command/mass_upgrade.py
0f5e0b8ab161ea6bdeaa56458b5662af969b9d3b
[wizard.git] / wizard / command / mass_upgrade.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, scripts, 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, ('lookup', 'warnings', 'errors'), options.redo, ('merge', 'verify'))
23     # loop stuff
24     errors = {}
25     i = 0
26     fails = {
27             "merge": 0,
28             "verify": 0,
29         }
30     deploys = deploy.parse_install_lines(app, options.versions_path, user=options.user)
31     requested_deploys = itertools.islice(deploys, options.limit)
32     try:
33         for i, d in enumerate(requested_deploys, 1):
34             report.lookup.write("%04d %s\n" % (i, d.location))
35             # check if we want to punt due to --limit
36             if d.location in seen:
37                 continue
38             if i in report.skip:
39                 continue
40             if is_root and not command.security_check_homedir(d.location):
41                 continue
42             # XXX: we may be able to punt based on detected versions from d, which
43             # would be faster than spinning up a new process. On the other hand,
44             # `seen` makes this mostly not a problem
45             logging.info("[%04d] Processing %s" % (i, d.location))
46             child_args = list(base_args)
47             # calculate the log file, if a log dir was specified
48             if options.log_dir:
49                 log_file = command.calculate_log_name(options.log_dir, i)
50                 child_args.append("--log-file=" + log_file)
51             # actual meat
52             def make_on_pair(d, i):
53                 # we need to make another stack frame so that d and i get specific bindings.
54                 def on_success(stdout, stderr):
55                     if stderr:
56                         report.lookup.write("[%04d] %s\n" % (i, d.location))
57                         logging.warning("[%04d] Warnings at [%s]:\n%s" % (i, d.location, stderr))
58                     seen.add(d.location)
59                 def on_error(e):
60                     if e.name == "AlreadyUpgraded":
61                         seen.add(d.location)
62                         logging.info("[%04d] Skipped already upgraded %s" % (i, d.location))
63                     elif e.name == "MergeFailed":
64                         seen.add(d.location)
65                         conflicts, _, tmpdir = e.stdout.rstrip().partition(" ")
66                         logging.warning("[%04d] Conflicts in %d files: resolve at [%s], source at [%s]" % (i, int(conflicts), tmpdir, d.location))
67                         report.merge.write("[%04d] %s %d %s\n" % (i, tmpdir, int(conflicts), d.location))
68                         fails['merge'] += 1
69                     else:
70                         name = e.name
71                         if name == "WebVerificationError":
72                             try:
73                                 host, path = scripts.get_web_host_and_path(d.location)
74                                 url = "http://%s%s" % (host, path)
75                             except ValueError:
76                                 url = d.location
77                             # This should actually be a warning, but
78                             # it's a really common error
79                             logging.info("[%04d] Could not verify application at %s" % (i, url))
80                             report.verify.write("[%04d] %s\n" % (i, url))
81                             fails['verify'] += 1
82                         else:
83                             if name not in errors: errors[name] = []
84                             errors[name].append(d)
85                             msg = "[%04d] %s in %s" % (i, name, d.location)
86                             logging.error(msg)
87                             report.errors.write(msg + "\n")
88                 return (on_success, on_error)
89             on_success, on_error = make_on_pair(d, i)
90             sh.call("wizard", "upgrade", d.location, *child_args,
91                           on_success=on_success, on_error=on_error)
92         sh.join()
93     finally:
94         for name, deploys in errors.items():
95             logging.warning("%s from %d installs" % (name, len(deploys)))
96         def printPercent(description, number, total):
97             logging.warning("%d out of %d installs (%.1f%%) had %s" % (number, total, float(number)/total*100, description))
98         if fails['merge']:
99             printPercent("merge conflicts", fails['merge'], i)
100         if fails['verify']:
101             printPercent("web verification failure", fails['verify'], i)
102
103 def parse_args(argv, baton):
104     usage = """usage: %prog mass-upgrade [ARGS] APPLICATION
105
106 Mass upgrades an application to the latest scripts version.
107 Essentially equivalent to running '%prog upgrade' on all
108 autoinstalls for a particular application found by parallel-find,
109 but with advanced reporting.
110
111 This command is intended to be run as root on a server with
112 the scripts AFS patch."""
113     parser = command.WizardOptionParser(usage)
114     baton.push(parser, "log_dir")
115     baton.push(parser, "seen")
116     baton.push(parser, "no_parallelize")
117     baton.push(parser, "dry_run")
118     baton.push(parser, "max_processes")
119     baton.push(parser ,"limit")
120     baton.push(parser, "versions_path")
121     baton.push(parser, "srv_path")
122     baton.push(parser, "user")
123     parser.add_option("--force", dest="force", action="store_true",
124             default=False, help="Force running upgrade even if it's already at latest version.")
125     parser.add_option("--redo", dest="redo", action="store_true",
126             default=False, help="Redo failed upgrades; use this if you updated Wizard's code.")
127     options, args, = parser.parse_all(argv)
128     if len(args) > 1:
129         parser.error("too many arguments")
130     elif not args:
131         parser.error("must specify application to upgrade")
132     return options, args
133
134 def calculate_base_args(options):
135     return command.makeBaseArgs(options, dry_run="--dry-run", srv_path="--srv-path", force="--force")
136