jkevan commented on code in PR #458:
URL: https://github.com/apache/unomi/pull/458#discussion_r916768879
##########
tools/shell-commands/src/main/java/org/apache/unomi/shell/migration/actions/Migrate.java:
##########
@@ -16,120 +16,183 @@
*/
package org.apache.unomi.shell.migration.actions;
+import groovy.lang.GroovyClassLoader;
+import groovy.lang.GroovyShell;
+import groovy.util.GroovyScriptEngine;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.karaf.shell.api.action.Action;
import org.apache.karaf.shell.api.action.Argument;
import org.apache.karaf.shell.api.action.Command;
import org.apache.karaf.shell.api.action.lifecycle.Reference;
import org.apache.karaf.shell.api.action.lifecycle.Service;
import org.apache.karaf.shell.api.console.Session;
-import org.apache.unomi.shell.migration.Migration;
+import org.apache.unomi.shell.migration.MigrateScript;
import org.apache.unomi.shell.migration.utils.ConsoleUtils;
import org.apache.unomi.shell.migration.utils.HttpUtils;
-import org.osgi.framework.BundleContext;
-import org.osgi.framework.InvalidSyntaxException;
-import org.osgi.framework.ServiceReference;
-import org.osgi.framework.Version;
-
+import org.osgi.framework.*;
+import org.osgi.framework.wiring.BundleWiring;
+
+import java.io.IOException;
+import java.net.URL;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
import java.util.*;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
-@Command(scope = "unomi", name = "migrate", description = "This will Migrate
your date in ES to be compliant with current version")
+@Command(scope = "unomi", name = "migrate", description = "This will Migrate
your data in ES to be compliant with current version")
@Service
public class Migrate implements Action {
+ public static final String CONFIG_ES_ADDRESS = "esAddress";
+ public static final String CONFIG_TRUST_ALL_CERTIFICATES =
"httpClient.trustAllCertificates";
@Reference
Session session;
@Reference
BundleContext bundleContext;
- @Argument(name = "fromVersionWithoutSuffix", description = "Origin version
without suffix/qualifier (e.g: 1.2.0)", multiValued = false, valueToShowInHelp
= "1.2.0")
- private String fromVersionWithoutSuffix;
+ @Argument(index = 0, name = "originVersion", description = "Origin version
without suffix/qualifier (e.g: 1.2.0)", valueToShowInHelp = "1.2.0")
+ private String originVersion;
public Object execute() throws Exception {
- if (fromVersionWithoutSuffix == null) {
- listMigrations();
+ // Load migration scrips
+ Set<MigrateScript> scripts = loadOSGIScripts();
+ scripts.addAll(loadFileSystemScripts());
+
+ if (originVersion == null) {
+ displayMigrations(scripts);
+ ConsoleUtils.printMessage(session, "Select your migration starting
point by specifying the current version (e.g. 1.2.0) or the last script that
was already run (e.g. 1.2.1)");
return null;
}
- String confirmation =
ConsoleUtils.askUserWithAuthorizedAnswer(session,"[WARNING] You are about to
execute a migration, this a very sensitive operation, are you sure? (yes/no):
", Arrays.asList("yes", "no"));
- if (confirmation.equalsIgnoreCase("no")) {
- System.out.println("Migration process aborted");
+ // Check that there is some migration scripts available from given
version
+ Version fromVersion = new Version(originVersion);
+ scripts = filterScriptsFromVersion(scripts, fromVersion);
+ if (scripts.size() == 0) {
+ ConsoleUtils.printMessage(session, "No migration scripts available
found starting from version: " + originVersion);
return null;
+ } else {
+ ConsoleUtils.printMessage(session, "The following migration
scripts starting from version: " + originVersion + " will be executed.");
+ displayMigrations(scripts);
}
- System.out.println("Starting migration process from version: " +
fromVersionWithoutSuffix);
-
- Version fromVersion = new Version(fromVersionWithoutSuffix);
- Version currentVersion = getCurrentVersionWithoutQualifier();
- System.out.println("current version: " + currentVersion.toString());
- if (currentVersion.compareTo(fromVersion) <= 0) {
- System.out.println("From version is same or superior than current
version, nothing to migrate.");
+ // Check for user approval before migrate
+ if (ConsoleUtils.askUserWithAuthorizedAnswer(session,
+ "[WARNING] You are about to execute a migration, this a very
sensitive operation, are you sure? (yes/no): ",
+ Arrays.asList("yes", "no")).equalsIgnoreCase("no")) {
+ ConsoleUtils.printMessage(session, "Migration process aborted");
return null;
}
- CloseableHttpClient httpClient = null;
- try {
- httpClient = HttpUtils.initHttpClient(session);
-
- String esAddress = ConsoleUtils.askUserWithDefaultAnswer(session,
"Enter ElasticSearch 7 TARGET address (default = http://localhost:9200): ",
"http://localhost:9200");
-
- for (Migration migration : getMigrations()) {
- if (fromVersion.compareTo(migration.getToVersion()) < 0) {
- String migrateConfirmation =
ConsoleUtils.askUserWithAuthorizedAnswer(session,"Starting migration to version
" + migration.getToVersion() + ", do you want to proceed? (yes/no): ",
Arrays.asList("yes", "no"));
- if (migrateConfirmation.equalsIgnoreCase("no")) {
- System.out.println("Migration process aborted");
- break;
- }
- migration.execute(session, httpClient, esAddress,
bundleContext);
- System.out.println("Migration to version " +
migration.getToVersion() + " done successfully");
+ // Build conf
+ Map<String, Object> migrationConfig = new HashMap<>();
+ migrationConfig.put(CONFIG_ES_ADDRESS,
ConsoleUtils.askUserWithDefaultAnswer(session, "Enter ElasticSearch 7 TARGET
address (default = http://localhost:9200): ", "http://localhost:9200"));
Review Comment:
Should be a dedicated story/task but yes it need to be done.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]