Search in sources :

Example 1 with ZimbraMigrationCallback

use of com.zimbra.cs.ephemeral.migrate.AttributeMigration.ZimbraMigrationCallback 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;
    }
}
Also used : ZimbraMigrationCallback(com.zimbra.cs.ephemeral.migrate.AttributeMigration.ZimbraMigrationCallback) MigrationFlag(com.zimbra.cs.ephemeral.migrate.AttributeMigration.MigrationFlag) GnuParser(org.apache.commons.cli.GnuParser) MigrationCallback(com.zimbra.cs.ephemeral.migrate.AttributeMigration.MigrationCallback) DryRunMigrationCallback(com.zimbra.cs.ephemeral.migrate.AttributeMigration.DryRunMigrationCallback) ZimbraMigrationCallback(com.zimbra.cs.ephemeral.migrate.AttributeMigration.ZimbraMigrationCallback) EntrySource(com.zimbra.cs.ephemeral.migrate.AttributeMigration.EntrySource) SomeAccountsSource(com.zimbra.cs.ephemeral.migrate.AttributeMigration.SomeAccountsSource) CommandLine(org.apache.commons.cli.CommandLine) ServiceException(com.zimbra.common.service.ServiceException) AllAccountsSource(com.zimbra.cs.ephemeral.migrate.AttributeMigration.AllAccountsSource) DryRunMigrationCallback(com.zimbra.cs.ephemeral.migrate.AttributeMigration.DryRunMigrationCallback) CommandLineParser(org.apache.commons.cli.CommandLineParser) EphemeralStore(com.zimbra.cs.ephemeral.EphemeralStore)

Aggregations

ServiceException (com.zimbra.common.service.ServiceException)1 EphemeralStore (com.zimbra.cs.ephemeral.EphemeralStore)1 AllAccountsSource (com.zimbra.cs.ephemeral.migrate.AttributeMigration.AllAccountsSource)1 DryRunMigrationCallback (com.zimbra.cs.ephemeral.migrate.AttributeMigration.DryRunMigrationCallback)1 EntrySource (com.zimbra.cs.ephemeral.migrate.AttributeMigration.EntrySource)1 MigrationCallback (com.zimbra.cs.ephemeral.migrate.AttributeMigration.MigrationCallback)1 MigrationFlag (com.zimbra.cs.ephemeral.migrate.AttributeMigration.MigrationFlag)1 SomeAccountsSource (com.zimbra.cs.ephemeral.migrate.AttributeMigration.SomeAccountsSource)1 ZimbraMigrationCallback (com.zimbra.cs.ephemeral.migrate.AttributeMigration.ZimbraMigrationCallback)1 CommandLine (org.apache.commons.cli.CommandLine)1 CommandLineParser (org.apache.commons.cli.CommandLineParser)1 GnuParser (org.apache.commons.cli.GnuParser)1