import optparse import logging import os import wizard from wizard import deploy from wizard import util from wizard import shell from wizard import cache from wizard.command import _base from wizard.command import migrate def main(argv, global_options): options, args = parse_args(argv) app = args[0] base_args = calculate_base_args(options) sh = make_shell(options) seen = make_cache(options) my_uid = os.getuid() # to see if we have root # loop stuff errors = {} uid = None for line in deploy.getInstallLines(global_options): # validate and filter the deployments try: d = deploy.Deployment.parse(line) except deploy.DeploymentParseError, deploy.NoSuchApplication: continue name = d.getApplication().name if name != app: continue if d.location in seen: continue # actual meat def make_on_pair(d): def on_success(stdout, stderr): seen.add(d.location) def on_error(e): if e.name == "wizard.command.migrate.AlreadyMigratedError": seen.add(d.location) logging.info("Skipped already migrated %s" % d.location) else: name = e.name if name not in errors: errors[name] = [] errors[name].append(d) logging.error("%s in %s" % (name, d.location)) return (on_success, on_error) on_success, on_error = make_on_pair(d) if not my_uid: uid = get_dir_uid(d.location) sh.wait() # wait for a parallel processing slot to be available sh.callAsUser(shell.wizard, "migrate", d.location, *base_args, uid=uid, on_success=on_success, on_error=on_error) sh.join() for name, deploys in errors.items(): logging.warning("%s from %d installs" % (name, len(deploys))) def parse_args(argv): usage = """usage: %prog massmigrate [ARGS] APPLICATION Mass migrates an application to the new repository format. Essentially equivalent to running '%prog migrate' on all autoinstalls for a particular application found by parallel-find, but with advanced reporting. When doing an actual run, it is recommended to use --cache to be able to resume gracefully (without it, massmigrate must stat every install to find out if it migrated it yet). This command is intended to be run as root on a server with the scripts AFS patch. You may run it in userspace for testing purposes, but then you MUST NOT run this on untrusted repositories. """ parser = _base.WizardOptionParser(usage) parser.add_option("--no-parallelize", dest="no_parallelize", action="store_true", default=False, help="Turn off parallelization") parser.add_option("--dry-run", dest="dry_run", action="store_true", default=False, help="Print commands that would be run. Implies --no-parallelize") parser.add_option("--max", dest="max", default=10, help="Maximum subprocesses to run concurrently") parser.add_option("--cache", dest="cache", default=None, help="Cache file to read/write paths of already processed installs. These will be skipped.") options, args, = parser.parse_all(argv) if len(args) > 1: parser.error("too many arguments") elif not args: parser.error("must specify application to migrate") if options.dry_run: options.no_parallelize = True return options, args def calculate_base_args(options): base_args = _base.makeBaseArgs(options, dry_run="--dry-run") if not options.debug: base_args.append("--quiet") return base_args def make_shell(options): if options.no_parallelize: sh = shell.DummyParallelShell() else: sh = shell.ParallelShell(max=int(options.max)) return sh def make_cache(options): if options.cache: seen = cache.Cache(options.cache) else: seen = cache.DummyCache() return seen