8 from wizard import deploy
9 from wizard import util
10 from wizard import shell
11 from wizard import sset
12 from wizard.command import _command
13 from wizard.command import migrate
15 def main(argv, baton):
16 options, args = parse_args(argv, baton)
18 base_args = calculate_base_args(options)
19 sh = make_shell(options)
20 seen = make_serialized_set(options)
21 my_uid = os.getuid() # to see if we have root
25 for line in deploy.getInstallLines(options.versions_path):
26 # validate and filter the deployments
28 d = deploy.Deployment.parse(line)
29 except deploy.DeploymentParseError, deploy.NoSuchApplication:
31 name = d.application.name
32 if name != app: continue
33 if d.location in seen:
35 # security check: see if the user's directory is the prefix of what
37 uid = util.get_dir_uid(d.location)
38 real = os.path.realpath(d.location)
39 if not real.startswith(pwd.getpwuid(uid).pw_dir + "/"):
40 logging.error("Security check failed, owner of deployment and owner of home directory mismatch for %s" % d.location)
44 def on_success(stdout, stderr):
47 if e.name == "wizard.command.migrate.AlreadyMigratedError":
49 logging.info("Skipped already migrated %s" % d.location)
52 if name not in errors: errors[name] = []
53 errors[name].append(d)
54 logging.error("%s in %s" % (name, d.location))
55 return (on_success, on_error)
56 on_success, on_error = make_on_pair(d)
57 sh.wait() # wait for a parallel processing slot to be available
58 sh.callAsUser(shell.wizard, "migrate", d.location, *base_args,
59 uid=uid, on_success=on_success, on_error=on_error)
61 for name, deploys in errors.items():
62 logging.warning("%s from %d installs" % (name, len(deploys)))
64 def parse_args(argv, baton):
65 usage = """usage: %prog massmigrate [ARGS] APPLICATION
67 Mass migrates an application to the new repository format.
68 Essentially equivalent to running '%prog migrate' on all
69 autoinstalls for a particular application found by parallel-find,
70 but with advanced reporting.
72 When doing an actual run, it is recommended to use --seen to
73 be able to resume gracefully (without it, massmigrate must
74 stat every install to find out if it migrated it yet).
76 This command is intended to be run as root on a server with
77 the scripts AFS patch. You may run it as an unpriviledged
78 user for testing purposes, but then you MUST NOT run this on
79 untrusted repositories."""
80 parser = _command.WizardOptionParser(usage)
81 parser.add_option("--no-parallelize", dest="no_parallelize", action="store_true",
82 default=False, help="Turn off parallelization")
83 parser.add_option("--dry-run", dest="dry_run", action="store_true",
84 default=False, help="Print commands that would be run. Implies --no-parallelize")
85 parser.add_option("--max", dest="max",
86 default=10, help="Maximum subprocesses to run concurrently")
87 parser.add_option("--seen", dest="seen",
88 default=None, help="File to read/write paths of already processed installs. These will be skipped.")
89 baton.push(parser, "versions_path")
90 options, args, = parser.parse_all(argv)
92 parser.error("too many arguments")
94 parser.error("must specify application to migrate")
96 options.no_parallelize = True
99 def calculate_base_args(options):
100 base_args = _command.makeBaseArgs(options, dry_run="--dry-run")
101 if not options.debug:
102 base_args.append("--quiet")
105 def make_shell(options):
106 if options.no_parallelize:
107 sh = shell.DummyParallelShell()
109 sh = shell.ParallelShell(max=int(options.max))
112 def make_serialized_set(options):
114 seen = sset.SerializedSet(options.seen)
116 seen = sset.DummySerializedSet()