UNOMI-752 : ensure target indices and alias does not exist before per… (#591)

* UNOMI-752 : ensure target indices and alias does not exist before performing migration

* UNOMI-752 : ensure target indices and alias does not exist before performing migration

* UNOMI-752 : change step oder to have the migration rerun working properly
diff --git a/tools/shell-commands/src/main/java/org/apache/unomi/shell/migration/MigrationException.java b/tools/shell-commands/src/main/java/org/apache/unomi/shell/migration/MigrationException.java
new file mode 100644
index 0000000..2fdf7c2
--- /dev/null
+++ b/tools/shell-commands/src/main/java/org/apache/unomi/shell/migration/MigrationException.java
@@ -0,0 +1,26 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.unomi.shell.migration;
+
+/**
+ * Simple exception to handle errors in migration
+ */
+public class MigrationException extends RuntimeException {
+    public MigrationException(String message) {
+        super(message);
+    }
+}
diff --git a/tools/shell-commands/src/main/java/org/apache/unomi/shell/migration/service/MigrationContext.java b/tools/shell-commands/src/main/java/org/apache/unomi/shell/migration/service/MigrationContext.java
index 21835c3..7f64b59 100644
--- a/tools/shell-commands/src/main/java/org/apache/unomi/shell/migration/service/MigrationContext.java
+++ b/tools/shell-commands/src/main/java/org/apache/unomi/shell/migration/service/MigrationContext.java
@@ -199,6 +199,19 @@
     }
 
     /**
+     * Same as above without stacktrace
+     * @param msg the message to print out with a newline
+     */
+    public void printException(String msg) {
+        if (session == null) {
+            logger.error(msg);
+        } else {
+            PrintStream writer = session.getConsole();
+            writer.println(msg);
+        }
+    }
+
+    /**
      * Get config for property name, in case the property doesn't exist on file system config file
      * Best effort will be made to prompt question in karaf shell to get the needed information
      *
diff --git a/tools/shell-commands/src/main/java/org/apache/unomi/shell/migration/service/MigrationServiceImpl.java b/tools/shell-commands/src/main/java/org/apache/unomi/shell/migration/service/MigrationServiceImpl.java
index 9722bb3..f0159d9 100644
--- a/tools/shell-commands/src/main/java/org/apache/unomi/shell/migration/service/MigrationServiceImpl.java
+++ b/tools/shell-commands/src/main/java/org/apache/unomi/shell/migration/service/MigrationServiceImpl.java
@@ -26,6 +26,7 @@
 import org.apache.http.impl.client.BasicCredentialsProvider;
 import org.apache.http.impl.client.CloseableHttpClient;
 import org.apache.karaf.shell.api.console.Session;
+import org.apache.unomi.shell.migration.MigrationException;
 import org.apache.unomi.shell.migration.MigrationService;
 import org.apache.unomi.shell.migration.utils.HttpUtils;
 import org.osgi.framework.Bundle;
@@ -128,6 +129,9 @@
                 context.printMessage("Starting execution of: " + migrateScript);
                 try {
                     migrateScript.getCompiledScript().run();
+                } catch (MigrationException e) {
+                    context.printException("Error executing: " + migrateScript);
+                    throw e;
                 } catch (Exception e) {
                     context.printException("Error executing: " + migrateScript, e);
                     throw e;
diff --git a/tools/shell-commands/src/main/resources/META-INF/cxs/migration/migrate-2.2.0-00-migrationEnvCheck.groovy b/tools/shell-commands/src/main/resources/META-INF/cxs/migration/migrate-2.2.0-00-migrationEnvCheck.groovy
new file mode 100644
index 0000000..bf28355
--- /dev/null
+++ b/tools/shell-commands/src/main/resources/META-INF/cxs/migration/migrate-2.2.0-00-migrationEnvCheck.groovy
@@ -0,0 +1,38 @@
+import org.apache.unomi.shell.migration.MigrationException
+import org.apache.unomi.shell.migration.service.MigrationContext
+import org.apache.unomi.shell.migration.utils.MigrationUtils
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+MigrationContext context = migrationContext
+String esAddress = context.getConfigString("esAddress")
+String indexPrefix = context.getConfigString("indexPrefix")
+def mewIndicesAndAliases = ["systemitems", "session-000001", "event-000001", "session", "event"]
+
+// Check env is ready for migration
+context.performMigrationStep("2.2.0-check-env-ready-for-migrate-event-session", () -> {
+    def currentIndex = ""
+    if (mewIndicesAndAliases.any{index -> {
+        currentIndex = index
+        return MigrationUtils.indexExists(context.getHttpClient(), esAddress, "${indexPrefix}-${currentIndex}")
+    }}) {
+        throw new MigrationException("Migration creates new index/aliases. Index or alias ${indexPrefix}-${currentIndex}  already exists, please remove it then restart migration")
+    }
+})
+
+
diff --git a/tools/shell-commands/src/main/resources/META-INF/cxs/migration/migrate-2.2.0-05-indicesReduction.groovy b/tools/shell-commands/src/main/resources/META-INF/cxs/migration/migrate-2.2.0-05-indicesReduction.groovy
index 631f0bc..6d2f9cf 100644
--- a/tools/shell-commands/src/main/resources/META-INF/cxs/migration/migrate-2.2.0-05-indicesReduction.groovy
+++ b/tools/shell-commands/src/main/resources/META-INF/cxs/migration/migrate-2.2.0-05-indicesReduction.groovy
@@ -42,7 +42,6 @@
         exportconfig: [reduceTo: "systemitems", renameId: true],
         rulestats: [reduceTo: "systemitems", renameId: true],
         groovyaction: [reduceTo: "systemitems", renameId: true],
-
         persona: [reduceTo: "profile", renameId: false]
 ]
 
diff --git a/tools/shell-commands/src/main/resources/META-INF/cxs/migration/migrate-2.2.0-00-rolloverAndMigrateEventSession.groovy b/tools/shell-commands/src/main/resources/META-INF/cxs/migration/migrate-2.2.0-10-rolloverAndMigrateEventSession.groovy
similarity index 100%
rename from tools/shell-commands/src/main/resources/META-INF/cxs/migration/migrate-2.2.0-00-rolloverAndMigrateEventSession.groovy
rename to tools/shell-commands/src/main/resources/META-INF/cxs/migration/migrate-2.2.0-10-rolloverAndMigrateEventSession.groovy