Repository: incubator-unomi
Updated Branches:
  refs/heads/master 4213752df -> d7d7103d4


UNOMI-150 Migration to delete old index template

Create a migration script in order to delete old Elasticsearch index
template.


Project: http://git-wip-us.apache.org/repos/asf/incubator-unomi/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-unomi/commit/f01d84fb
Tree: http://git-wip-us.apache.org/repos/asf/incubator-unomi/tree/f01d84fb
Diff: http://git-wip-us.apache.org/repos/asf/incubator-unomi/diff/f01d84fb

Branch: refs/heads/master
Commit: f01d84fb68e2a24d43f516d24f56df846c4c85ed
Parents: 29d939f
Author: Jarek Lipski <jlip...@jahia.com>
Authored: Wed Feb 7 18:57:19 2018 +0100
Committer: Jarek Lipski <jlip...@jahia.com>
Committed: Wed Feb 7 19:02:33 2018 +0100

----------------------------------------------------------------------
 .../impl/MigrationTo130IndexTemplate.java       | 74 ++++++++++++++++++++
 .../shell/migration/utils/ConsoleUtils.java     | 11 +++
 .../migration/utils/HttpRequestException.java   | 33 +++++++++
 .../unomi/shell/migration/utils/HttpUtils.java  |  2 +-
 .../resources/OSGI-INF/blueprint/blueprint.xml  |  2 +
 5 files changed, 121 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/f01d84fb/tools/shell-commands/src/main/java/org/apache/unomi/shell/migration/impl/MigrationTo130IndexTemplate.java
----------------------------------------------------------------------
diff --git 
a/tools/shell-commands/src/main/java/org/apache/unomi/shell/migration/impl/MigrationTo130IndexTemplate.java
 
b/tools/shell-commands/src/main/java/org/apache/unomi/shell/migration/impl/MigrationTo130IndexTemplate.java
new file mode 100644
index 0000000..1aad590
--- /dev/null
+++ 
b/tools/shell-commands/src/main/java/org/apache/unomi/shell/migration/impl/MigrationTo130IndexTemplate.java
@@ -0,0 +1,74 @@
+/*
+ * 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.impl;
+
+import org.apache.felix.service.command.CommandSession;
+import org.apache.http.impl.client.CloseableHttpClient;
+import org.apache.unomi.shell.migration.Migration;
+import org.apache.unomi.shell.migration.utils.ConsoleUtils;
+import org.apache.unomi.shell.migration.utils.HttpRequestException;
+import org.apache.unomi.shell.migration.utils.HttpUtils;
+import org.osgi.framework.Version;
+
+import java.io.IOException;
+
+public class MigrationTo130IndexTemplate implements Migration {
+    private CloseableHttpClient httpClient;
+    private CommandSession session;
+    private String esAddress;
+
+    @Override
+    public Version getFromVersion() {
+        return null;
+    }
+
+    @Override
+    public Version getToVersion() {
+        return new Version("1.3.0");
+    }
+
+    @Override
+    public void execute(CommandSession session, CloseableHttpClient 
httpClient, String esAddress) throws IOException {
+        try {
+            this.httpClient = httpClient;
+            this.session = session;
+            this.esAddress = esAddress;
+            deleteOldIndexTemplate();
+        } catch (IOException e) {
+            if (httpClient != null) {
+                httpClient.close();
+            }
+            throw e;
+        }
+
+    }
+
+    private void deleteOldIndexTemplate() throws IOException {
+        String oldMonthlyIndexTemplate = "context_monthlyindex";
+        try {
+            ConsoleUtils.printMessage(session,"Deleting old monthly index 
template " + oldMonthlyIndexTemplate);
+            HttpUtils.executeDeleteRequest(httpClient, esAddress + 
"/_template/" + oldMonthlyIndexTemplate, null);
+        } catch (HttpRequestException e) {
+            if (e.getCode() == 404) {
+                ConsoleUtils.printMessage(session,"Old monthly index template 
not found, skipping deletion");
+            } else {
+                throw e;
+            }
+        }
+
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/f01d84fb/tools/shell-commands/src/main/java/org/apache/unomi/shell/migration/utils/ConsoleUtils.java
----------------------------------------------------------------------
diff --git 
a/tools/shell-commands/src/main/java/org/apache/unomi/shell/migration/utils/ConsoleUtils.java
 
b/tools/shell-commands/src/main/java/org/apache/unomi/shell/migration/utils/ConsoleUtils.java
index e8fdc83..0f5cb5c 100644
--- 
a/tools/shell-commands/src/main/java/org/apache/unomi/shell/migration/utils/ConsoleUtils.java
+++ 
b/tools/shell-commands/src/main/java/org/apache/unomi/shell/migration/utils/ConsoleUtils.java
@@ -23,6 +23,7 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 import java.io.IOException;
+import java.io.PrintStream;
 import java.util.List;
 
 /**
@@ -79,4 +80,14 @@ public class ConsoleUtils {
         ConsoleReader reader = (ConsoleReader) session.get(".jline.reader");
         return reader.readLine(msg, null);
     }
+
+    /**
+     * Print a message in the console.
+     * @param session
+     * @param msg
+     */
+    public static void printMessage(CommandSession session, String msg) {
+        PrintStream writer = session.getConsole();
+        writer.println(msg);
+    }
 }

http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/f01d84fb/tools/shell-commands/src/main/java/org/apache/unomi/shell/migration/utils/HttpRequestException.java
----------------------------------------------------------------------
diff --git 
a/tools/shell-commands/src/main/java/org/apache/unomi/shell/migration/utils/HttpRequestException.java
 
b/tools/shell-commands/src/main/java/org/apache/unomi/shell/migration/utils/HttpRequestException.java
new file mode 100644
index 0000000..e1622a0
--- /dev/null
+++ 
b/tools/shell-commands/src/main/java/org/apache/unomi/shell/migration/utils/HttpRequestException.java
@@ -0,0 +1,33 @@
+/*
+ * 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.utils;
+
+import java.io.IOException;
+
+public class HttpRequestException extends IOException {
+
+    private int code;
+
+    public HttpRequestException(String message, int code) {
+        super(message);
+        this.code = code;
+    }
+
+    public int getCode() {
+        return code;
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/f01d84fb/tools/shell-commands/src/main/java/org/apache/unomi/shell/migration/utils/HttpUtils.java
----------------------------------------------------------------------
diff --git 
a/tools/shell-commands/src/main/java/org/apache/unomi/shell/migration/utils/HttpUtils.java
 
b/tools/shell-commands/src/main/java/org/apache/unomi/shell/migration/utils/HttpUtils.java
index 2f16c83..18c12f1 100644
--- 
a/tools/shell-commands/src/main/java/org/apache/unomi/shell/migration/utils/HttpUtils.java
+++ 
b/tools/shell-commands/src/main/java/org/apache/unomi/shell/migration/utils/HttpUtils.java
@@ -141,7 +141,7 @@ public class HttpUtils {
         CloseableHttpResponse response = httpClient.execute(httpRequestBase);
         final int statusCode = response.getStatusLine().getStatusCode();
         if (statusCode >= 400) {
-            throw new IOException("Couldn't execute " + httpRequestBase + " 
response: " + EntityUtils.toString(response.getEntity()));
+            throw new HttpRequestException("Couldn't execute " + 
httpRequestBase + " response: " + EntityUtils.toString(response.getEntity()), 
statusCode);
         }
 
         HttpEntity entity = response.getEntity();

http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/f01d84fb/tools/shell-commands/src/main/resources/OSGI-INF/blueprint/blueprint.xml
----------------------------------------------------------------------
diff --git 
a/tools/shell-commands/src/main/resources/OSGI-INF/blueprint/blueprint.xml 
b/tools/shell-commands/src/main/resources/OSGI-INF/blueprint/blueprint.xml
index 65f91f4..1fb63c4 100644
--- a/tools/shell-commands/src/main/resources/OSGI-INF/blueprint/blueprint.xml
+++ b/tools/shell-commands/src/main/resources/OSGI-INF/blueprint/blueprint.xml
@@ -22,6 +22,7 @@
            xsi:schemaLocation="http://www.osgi.org/xmlns/blueprint/v1.0.0 
http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd";>
 
     <bean id="migrateTo130" 
class="org.apache.unomi.shell.migration.impl.MigrationTo130"/>
+    <bean id="migrateTo130IndexTemplate" 
class="org.apache.unomi.shell.migration.impl.MigrationTo130IndexTemplate"/>
 
     <shell:command-bundle>
         <shell:command>
@@ -30,6 +31,7 @@
                     <list>
                         <!-- Migration beans must be executed in the right 
order so you must place new bean at the end of this list -->
                         <ref component-id="migrateTo130"/>
+                        <ref component-id="migrateTo130IndexTemplate"/>
                     </list>
                 </shell:property>
             </shell:action>

Reply via email to