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 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'))
30 deploys = deploy.parse_install_lines(app, options.versions_path, user=options.user)
31 requested_deploys = itertools.islice(deploys, options.limit)
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:
40 if is_root and not command.security_check_homedir(d.location):
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
49 log_file = command.calculate_log_name(options.log_dir, i)
50 child_args.append("--log-file=" + log_file)
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):
56 report.lookup.write("[%04d] %s\n" % (i, d.location))
57 logging.warning("[%04d] Warnings at [%s]:\n%s" % (i, d.location, stderr))
60 if e.name == "AlreadyUpgraded":
62 logging.info("[%04d] Skipped already upgraded %s" % (i, d.location))
63 elif e.name == "MergeFailed":
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))
71 if name == "WebVerificationError":
73 host, path = scripts.get_web_host_and_path(d.location)
74 url = "http://%s%s" % (host, path)
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))
83 if name not in errors: errors[name] = []
84 errors[name].append(d)
85 msg = "[%04d] %s in %s" % (i, name, d.location)
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)
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))
99 printPercent("merge conflicts", fails['merge'], i)
101 printPercent("web verification failure", fails['verify'], i)
103 def parse_args(argv, baton):
104 usage = """usage: %prog mass-upgrade [ARGS] APPLICATION
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.
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)
129 parser.error("too many arguments")
131 parser.error("must specify application to upgrade")
134 def calculate_base_args(options):
135 return command.makeBaseArgs(options, dry_run="--dry-run", srv_path="--srv-path", force="--force")