12 from wizard import deploy, util, scripts, shell, sset, command
14 def main(argv, baton):
15 options, args = parse_args(argv, baton)
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, merge_log = command.open_logs(options.log_dir, ('warnings', 'errors', 'merge'))
25 merge_fails = [0] # otherwise I get a UnboundLocalError later on when I increment
26 deploys = deploy.parse_install_lines(app, options.versions_path, user=options.user)
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:
32 if is_root and not command.security_check_homedir(d.location):
34 # XXX: we may be able to punt based on detected versions from d
35 logging.info("Processing %s" % d.location)
36 child_args = list(base_args)
37 # calculate the log file, if a log dir was specified
39 log_file = command.calculate_log_name(options.log_dir, i, d.location)
40 child_args.append("--log-file=" + log_file)
42 def make_on_pair(d, i):
43 # we need to make another stack frame so that d and i get specific bindings.
44 def on_success(stdout, stderr):
46 warnings_log.write("%s\n" % d.location)
47 logging.warning("Warnings [%04d] %s:\n%s" % (i, d.location, stderr))
50 if e.name == "wizard.command.upgrade.AlreadyUpgraded" or \
51 e.name == "AlreadyUpgraded":
53 logging.info("Skipped already upgraded %s" % d.location)
54 elif e.name == "MergeFailed":
56 logging.warning("Merge failed: resolve at [%s], source at [%s]" % (e.stdout.rstrip(), d.location))
57 merge_log.write("%s\n" % d.location)
61 if name not in errors: errors[name] = []
62 errors[name].append(d)
63 if name == "WebVerificationError":
65 host, path = scripts.get_web_host_and_path(d.location)
66 url = "http://%s%s" % (host, path)
69 # This should actually be a warning, but
70 # it's a really common error
71 logging.info("Could not verify application at %s" % url)
73 logging.error("%s in [%04d] %s" % (name, i, d.location))
74 errors_log.write("%s\n" % d.location)
75 return (on_success, on_error)
76 on_success, on_error = make_on_pair(d, i)
77 sh.call("wizard", "upgrade", d.location, *child_args,
78 on_success=on_success, on_error=on_error)
80 for name, deploys in errors.items():
81 logging.warning("%s from %d installs" % (name, len(deploys)))
83 logging.warning("%d out of %d installs (%.1f%%) had merge failure" % (merge_fails[0], i, float(merge_fails[0])/i*100))
85 def parse_args(argv, baton):
86 usage = """usage: %prog mass-upgrade [ARGS] APPLICATION
88 Mass upgrades an application to the latest scripts version.
89 Essentially equivalent to running '%prog upgrade' on all
90 autoinstalls for a particular application found by parallel-find,
91 but with advanced reporting.
93 This command is intended to be run as root on a server with
94 the scripts AFS patch."""
95 parser = command.WizardOptionParser(usage)
96 baton.push(parser, "log_dir")
97 baton.push(parser, "seen")
98 baton.push(parser, "no_parallelize")
99 baton.push(parser, "dry_run")
100 baton.push(parser, "max_processes")
101 baton.push(parser ,"limit")
102 baton.push(parser, "versions_path")
103 baton.push(parser, "srv_path")
104 baton.push(parser, "user")
105 parser.add_option("--force", dest="force", action="store_true",
106 default=False, help="Force running upgrade even if it's already at latest version.")
107 options, args, = parser.parse_all(argv)
109 parser.error("too many arguments")
111 parser.error("must specify application to upgrade")
114 def calculate_base_args(options):
115 return command.makeBaseArgs(options, dry_run="--dry-run", srv_path="--srv-path", force="--force")