use of com.zimbra.cs.ephemeral.migrate.AttributeMigration.DryRunMigrationCallback in project zm-mailbox by Zimbra.
the class AttributeMigrationUtil method main.
public static void main(String[] args) throws Exception {
CliUtil.toolSetup();
CommandLineParser parser = new GnuParser();
CommandLine cl = parser.parse(OPTIONS, args);
List<String> attrsToMigrate = cl.getArgList();
boolean flagChange = cl.hasOption('s') || cl.hasOption('u');
if (cl.hasOption("h") || (cl.hasOption('s') && cl.hasOption('u'))) {
usage();
return;
}
if (attrsToMigrate.isEmpty()) {
attrsToMigrate.addAll(AttributeManager.getInstance().getEphemeralAttributeNames());
}
if (cl.hasOption('d')) {
ZimbraLog.ephemeral.setLevel(Level.debug);
}
boolean dryRun = cl.hasOption('r');
if (dryRun && cl.hasOption('n')) {
ZimbraLog.ephemeral.error("cannot specify --num-threads with --dry-run option");
return;
}
if (flagChange && (dryRun || cl.hasOption('n') || cl.hasOption('a') || cl.hasOption('k'))) {
ZimbraLog.ephemeral.error("cannot specify --set-flag or --unset-flag with -r, -n, -a, or -k options");
return;
}
//a null numThreads value causes the migration process to run synchronously
Integer numThreads = null;
if (!dryRun) {
//dry runs are always synchronous
try {
numThreads = Integer.valueOf(cl.getOptionValue('n', "1"));
if (numThreads < 1) {
ZimbraLog.ephemeral.error("invalid num-threads value: '%s'", numThreads);
return;
}
} catch (NumberFormatException e) {
ZimbraLog.ephemeral.error("invalid num-threads value: '%s'", cl.getOptionValue('n'));
return;
}
}
MigrationCallback callback;
String url = Provisioning.getInstance().getConfig().getEphemeralBackendURL();
if (!dryRun) {
String backendName = null;
if (url != null) {
String[] tokens = url.split(":");
if (tokens != null && tokens.length > 0) {
backendName = tokens[0];
if (backendName.equalsIgnoreCase("ldap")) {
ZimbraLog.ephemeral.info("ephemeral backend is LDAP; migration is not needed");
return;
}
}
} else {
Zimbra.halt("no ephemeral backend specified");
return;
}
initEphemeralBackendExtension(backendName);
try {
callback = new ZimbraMigrationCallback();
} catch (ServiceException e) {
Zimbra.halt(String.format("unable to connect to ephemeral backend at %s; migration cannot proceed", url), e);
return;
}
} else {
callback = new DryRunMigrationCallback();
}
if (flagChange) {
//EphemeralStore containing the flag
EphemeralStore store = new ZimbraMigrationCallback().getStore();
MigrationFlag flag = AttributeMigration.getMigrationFlag(store);
if (cl.hasOption('s')) {
//setting flag
if (flag.isSet()) {
ZimbraLog.ephemeral.info("migration flag is already set on %s", store.getClass().getSimpleName());
} else {
ZimbraLog.ephemeral.info("setting the migration flag on %s", store.getClass().getSimpleName());
flag.set();
AttributeMigration.clearConfigCacheOnAllServers(true);
}
} else {
//unsetting flag
if (!flag.isSet()) {
ZimbraLog.ephemeral.info("migration flag is not set on %s", store.getClass().getSimpleName());
} else {
ZimbraLog.ephemeral.info("unsetting the migration flag on %s", store.getClass().getSimpleName());
flag.unset();
AttributeMigration.clearConfigCacheOnAllServers(true);
}
}
return;
}
AttributeMigration migration = new AttributeMigration(attrsToMigrate, numThreads);
migration.setCallback(callback);
EntrySource source;
if (cl.hasOption('a')) {
String[] acctValues = cl.getOptionValue('a').split(",");
source = new SomeAccountsSource(acctValues);
} else {
source = new AllAccountsSource();
}
migration.setSource(source);
if (dryRun || cl.hasOption('k')) {
migration.setDeleteOriginal(false);
}
try {
migration.migrateAllAccounts();
} catch (ServiceException e) {
Zimbra.halt(String.format("error encountered during migration to ephemeral backend at %s; migration cannot proceed", url), e);
return;
}
}
Aggregations