Search in sources :

Example 1 with AllAccountsSource

use of com.zimbra.cs.ephemeral.migrate.AttributeMigration.AllAccountsSource 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);
    boolean dryRun = cl.hasOption('r');
    boolean useNumThreads = cl.hasOption('n');
    boolean debug = cl.hasOption('d');
    boolean help = cl.hasOption('h');
    boolean useAccount = cl.hasOption('a');
    boolean showStatus = cl.hasOption('s');
    boolean clear = cl.hasOption('c');
    List<String> clArgs = cl.getArgList();
    if (clArgs.isEmpty() && !help && !clear && !showStatus) {
        ZimbraLog.ephemeral.error("must specify URL of destination ephemeral store");
        return;
    }
    if (help || (clear && showStatus)) {
        usage();
        return;
    }
    if (debug) {
        ZimbraLog.ephemeral.setLevel(Level.debug);
    }
    if (dryRun && useNumThreads) {
        ZimbraLog.ephemeral.error("cannot specify --num-threads with --dry-run option");
        return;
    }
    if (clear && (dryRun || useNumThreads || useAccount || showStatus || !clArgs.isEmpty())) {
        ZimbraLog.ephemeral.error("cannot specify --clear with arguments or other options");
        return;
    }
    if (showStatus && (dryRun || useNumThreads || useAccount || clear || !clArgs.isEmpty())) {
        ZimbraLog.ephemeral.error("cannot specify --status with arguments or other 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;
        }
    }
    if (showStatus) {
        showMigrationInfo();
        return;
    } else if (clear) {
        clearMigrationInfo();
        return;
    }
    String destURL = clArgs.get(0);
    List<String> attrsToMigrate;
    MigrationCallback callback;
    if (clArgs.size() > 1) {
        attrsToMigrate = clArgs.subList(1, clArgs.size());
    } else {
        attrsToMigrate = new ArrayList<String>(AttributeManager.getInstance().getEphemeralAttributeNames());
    }
    if (!dryRun) {
        String backendName = null;
        String[] tokens = destURL.split(":");
        if (tokens != null && tokens.length > 0) {
            backendName = tokens[0];
            if (backendName.equalsIgnoreCase("ldap")) {
                ZimbraLog.ephemeral.info("migrating to LDAP is not supported");
                return;
            }
        }
        if (Provisioning.getInstance().getConfig().getEphemeralBackendURL().equalsIgnoreCase(destURL)) {
            ZimbraLog.ephemeral.info("destination URL cannot be the same as the current ephemeral backend URL");
            return;
        }
        ExtensionUtil.initEphemeralBackendExtension(backendName);
        try {
            callback = new ZimbraMigrationCallback(destURL);
        } catch (ServiceException e) {
            Zimbra.halt(String.format("unable to connect to ephemeral backend at %s; migration cannot proceed", destURL), e);
            return;
        }
    } else {
        callback = new DryRunMigrationCallback();
        MigrationInfo.setFactory(InMemoryMigrationInfo.Factory.class);
    }
    try {
        AttributeMigration migration = new AttributeMigration(destURL, attrsToMigrate, numThreads);
        AttributeMigration.setCallback(callback);
        EntrySource source;
        if (useAccount) {
            String[] acctValues = cl.getOptionValue('a').split(",");
            source = new SomeAccountsSource(acctValues);
        } else {
            source = new AllAccountsSource();
        }
        migration.setSource(source);
        migration.migrateAllAccounts();
    } catch (InvalidAttributeException e) {
        Zimbra.halt(String.format("Migration can't proceed: %s", e.getMessage()));
        return;
    } catch (ServiceException e) {
        Zimbra.halt(String.format("error encountered during migration to ephemeral backend at %s; migration cannot proceed", destURL), e);
        return;
    }
}
Also used : ZimbraMigrationCallback(com.zimbra.cs.ephemeral.migrate.AttributeMigration.ZimbraMigrationCallback) 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)

Aggregations

ServiceException (com.zimbra.common.service.ServiceException)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 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