You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@karaf.apache.org by jb...@apache.org on 2022/12/20 05:34:55 UTC

[karaf] branch main updated: [KARAF-7068] Add instance package in a zip file method and use in instance:package and corresponding Instance MBean operation

This is an automated email from the ASF dual-hosted git repository.

jbonofre pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/karaf.git


The following commit(s) were added to refs/heads/main by this push:
     new f3a6fcaefa [KARAF-7068] Add instance package in a zip file method and use in instance:package and corresponding Instance MBean operation
     new f5b14039a3 This closes #1626
f3a6fcaefa is described below

commit f3a6fcaefa909feeda9a790f65ebdfa2a50f50b4
Author: Aleksy Wróblewski <al...@bbbit.io>
AuthorDate: Fri Oct 14 19:40:18 2022 +0200

    [KARAF-7068] Add instance package in a zip file method and use in instance:package and corresponding Instance MBean operation
---
 .../instance/command/InstanceCommandSupport.java   |  2 +-
 .../karaf/instance/command/PackageCommand.java     | 52 ++++++++++++++++++++++
 .../org/apache/karaf/instance/core/Instance.java   |  2 +
 .../apache/karaf/instance/core/InstancesMBean.java |  1 +
 .../karaf/instance/core/internal/InstanceImpl.java | 38 ++++++++++++++++
 .../instance/core/internal/InstancesMBeanImpl.java |  8 ++++
 .../java/org/apache/karaf/itests/InstanceTest.java | 38 ++++++++++++----
 manual/src/main/asciidoc/user-guide/instances.adoc | 17 +++++++
 8 files changed, 148 insertions(+), 10 deletions(-)

diff --git a/instance/src/main/java/org/apache/karaf/instance/command/InstanceCommandSupport.java b/instance/src/main/java/org/apache/karaf/instance/command/InstanceCommandSupport.java
index 64b9f81e44..bb941a299b 100644
--- a/instance/src/main/java/org/apache/karaf/instance/command/InstanceCommandSupport.java
+++ b/instance/src/main/java/org/apache/karaf/instance/command/InstanceCommandSupport.java
@@ -44,7 +44,7 @@ public abstract class InstanceCommandSupport implements Action {
     protected Instance getExistingInstance(String name) {
         Instance i = instanceService.getInstance(name);
         if (i == null) {
-            throw new IllegalArgumentException("Instances '" + name + "' does not exist");
+            throw new IllegalArgumentException("Instance '" + name + "' does not exist");
         }
         return i;
     }
diff --git a/instance/src/main/java/org/apache/karaf/instance/command/PackageCommand.java b/instance/src/main/java/org/apache/karaf/instance/command/PackageCommand.java
new file mode 100644
index 0000000000..9de744665e
--- /dev/null
+++ b/instance/src/main/java/org/apache/karaf/instance/command/PackageCommand.java
@@ -0,0 +1,52 @@
+/*
+ * 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.karaf.instance.command;
+
+import org.apache.karaf.instance.command.completers.InstanceCompleter;
+import org.apache.karaf.instance.core.Instance;
+import org.apache.karaf.shell.api.action.Argument;
+import org.apache.karaf.shell.api.action.Command;
+import org.apache.karaf.shell.api.action.Completion;
+import org.apache.karaf.shell.api.action.lifecycle.Service;
+
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.nio.file.FileVisitResult;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.nio.file.SimpleFileVisitor;
+import java.nio.file.attribute.BasicFileAttributes;
+import java.util.zip.ZipEntry;
+import java.util.zip.ZipOutputStream;
+
+@Command(scope = "instance", name = "package", description = "Create a .zip archive of the instance.")
+@Service
+public class PackageCommand extends InstanceCommandSupport {
+
+    @Argument(index = 0, name = "name", description = "The name of the container instance", required = true)
+    @Completion(InstanceCompleter.class)
+    private String name;
+    @Argument(index = 1, name = "destination", description = "Destination path for the archive", required = true)
+    private String destination;
+
+    protected Object doExecute() throws Exception {
+        getExistingInstance(name).packageInZip(destination);
+        return "Archive available at " + destination;
+    }
+}
diff --git a/instance/src/main/java/org/apache/karaf/instance/core/Instance.java b/instance/src/main/java/org/apache/karaf/instance/core/Instance.java
index b3fd5d491c..e7e09d9702 100644
--- a/instance/src/main/java/org/apache/karaf/instance/core/Instance.java
+++ b/instance/src/main/java/org/apache/karaf/instance/core/Instance.java
@@ -65,4 +65,6 @@ public interface Instance {
 
     void changeSshHost(String host) throws Exception;
 
+    void packageInZip(String destination) throws Exception;
+
 }
diff --git a/instance/src/main/java/org/apache/karaf/instance/core/InstancesMBean.java b/instance/src/main/java/org/apache/karaf/instance/core/InstancesMBean.java
index 1c74294511..02a3ba075b 100644
--- a/instance/src/main/java/org/apache/karaf/instance/core/InstancesMBean.java
+++ b/instance/src/main/java/org/apache/karaf/instance/core/InstancesMBean.java
@@ -47,6 +47,7 @@ public interface InstancesMBean {
     void destroyInstance(String name) throws MBeanException;
     void startInstance(String name) throws MBeanException;
     void startInstance(String name, String opts) throws MBeanException;
+    void packageInstance(String name, String destination) throws MBeanException;
     void startInstance(String name, String opts, boolean wait, boolean debug) throws MBeanException;
     void stopInstance(String name) throws MBeanException;
     void renameInstance(String originalName, String newName) throws MBeanException;
diff --git a/instance/src/main/java/org/apache/karaf/instance/core/internal/InstanceImpl.java b/instance/src/main/java/org/apache/karaf/instance/core/internal/InstanceImpl.java
index da48b805b0..26e0b24d15 100644
--- a/instance/src/main/java/org/apache/karaf/instance/core/internal/InstanceImpl.java
+++ b/instance/src/main/java/org/apache/karaf/instance/core/internal/InstanceImpl.java
@@ -18,6 +18,13 @@ package org.apache.karaf.instance.core.internal;
 
 import org.apache.karaf.instance.core.Instance;
 
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.nio.file.*;
+import java.nio.file.attribute.BasicFileAttributes;
+import java.util.zip.ZipEntry;
+import java.util.zip.ZipOutputStream;
+
 public class InstanceImpl implements Instance {
 
     private final InstanceServiceImpl service;
@@ -115,4 +122,35 @@ public class InstanceImpl implements Instance {
     public void changeSshHost(String host) throws Exception {
         service.changeInstanceSshHost(name, host);
     }
+
+    public void packageInZip(String destination) throws Exception {
+        Path sourcePath = Paths.get(getLocation());
+        Path destinationPath = Paths.get(destination).normalize();
+        try (ZipOutputStream zOut = new ZipOutputStream(new FileOutputStream(destination.toString()))) {
+            Files.walkFileTree(sourcePath, new SimpleFileVisitor<Path>() {
+                @Override
+                public FileVisitResult visitFile(Path file, BasicFileAttributes attributes) throws IOException {
+                    if (attributes.isSymbolicLink()) {
+                        return FileVisitResult.CONTINUE;
+                    }
+
+                    String entryName = sourcePath.relativize(file).toString();
+
+                    if (Paths.get(entryName).normalize().equals(destinationPath)) {
+                        // Prevent the zip from trying to zip itself when ran in the instance directory,
+                        // e.g. when executing instance:package root ./archives/root.zip
+                        return FileVisitResult.CONTINUE;
+                    }
+
+                    ZipEntry zipEntry = new ZipEntry(entryName);
+                    System.out.println(entryName);
+                    zOut.putNextEntry(zipEntry);
+                    Files.copy(file, zOut);
+                    zOut.closeEntry();
+
+                    return FileVisitResult.CONTINUE;
+                }
+            });
+        }
+    }
 }
diff --git a/instance/src/main/java/org/apache/karaf/instance/core/internal/InstancesMBeanImpl.java b/instance/src/main/java/org/apache/karaf/instance/core/internal/InstancesMBeanImpl.java
index ee0716f41b..a2c59809a6 100644
--- a/instance/src/main/java/org/apache/karaf/instance/core/internal/InstancesMBeanImpl.java
+++ b/instance/src/main/java/org/apache/karaf/instance/core/internal/InstancesMBeanImpl.java
@@ -77,6 +77,14 @@ public class InstancesMBeanImpl extends StandardMBean implements InstancesMBean
         }
     }
 
+    public void packageInstance(String name, String destination) throws MBeanException {
+        try {
+            getExistingInstance(name).packageInZip(destination);
+        } catch (Exception e) {
+            throw new MBeanException(null, e.toString());
+        }
+    }
+
     public void changeSshPort(String name, int port) throws MBeanException {
         try {
             getExistingInstance(name).changeSshPort(port);
diff --git a/itests/test/src/test/java/org/apache/karaf/itests/InstanceTest.java b/itests/test/src/test/java/org/apache/karaf/itests/InstanceTest.java
index d4c85310d2..135c2a3340 100644
--- a/itests/test/src/test/java/org/apache/karaf/itests/InstanceTest.java
+++ b/itests/test/src/test/java/org/apache/karaf/itests/InstanceTest.java
@@ -13,20 +13,26 @@
  */
 package org.apache.karaf.itests;
 
-import javax.management.MBeanServer;
-import javax.management.MBeanServerConnection;
-import javax.management.ObjectName;
-import javax.management.openmbean.TabularData;
-
 import org.junit.Assert;
 import org.junit.Test;
 import org.junit.runner.RunWith;
-
 import org.ops4j.pax.exam.junit.PaxExam;
 import org.ops4j.pax.exam.spi.reactors.ExamReactorStrategy;
 import org.ops4j.pax.exam.spi.reactors.PerClass;
 
+import javax.management.MBeanServer;
+import javax.management.MBeanServerConnection;
+import javax.management.ObjectName;
+import javax.management.openmbean.TabularData;
 import java.lang.management.ManagementFactory;
+import java.nio.file.Paths;
+import java.util.Collections;
+import java.util.List;
+import java.util.zip.ZipEntry;
+import java.util.zip.ZipFile;
+
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
 
 @RunWith(PaxExam.class)
 @ExamReactorStrategy(PerClass.class)
@@ -35,9 +41,9 @@ public class InstanceTest extends BaseTest {
     @Test
     public void createDestroyCommand() throws Exception {
         System.out.println(executeCommand("instance:create itest1"));
-        assertContains("itest1" ,executeCommand("instance:list"));
+        assertContains("itest1", executeCommand("instance:list"));
         System.out.println(executeCommand("instance:destroy itest1"));
-        assertContainsNot("itest1" ,executeCommand("instance:list"));
+        assertContainsNot("itest1", executeCommand("instance:list"));
     }
 
     @Test
@@ -85,6 +91,21 @@ public class InstanceTest extends BaseTest {
         executeCommand("instance:destroy itest666");
     }
 
+    @Test
+    public void packageCommand() throws Exception {
+        executeCommand("instance:create test");
+        executeCommand("instance:package test archive.zip");
+        String zipPath = Paths.get(System.getProperty("karaf.home"), "archive.zip").toString();
+        ZipFile zipFile = new ZipFile(zipPath);
+
+        List<? extends ZipEntry> entries = Collections.list(zipFile.entries());
+        assertFalse(entries.isEmpty());
+        assertTrue(entries.stream().anyMatch(e -> e.getName().equals("bin/karaf")));
+        assertTrue(entries.stream().anyMatch(e -> e.getName().equals("etc/system.properties")));
+
+        executeCommand("instance:destroy test");
+    }
+
     private int getInstancesNum(MBeanServerConnection connection, ObjectName name) throws Exception {
         TabularData instances = (TabularData) connection.getAttribute(name, "Instances");
         return instances.size();
@@ -121,5 +142,4 @@ public class InstanceTest extends BaseTest {
                 new String[]{"java.lang.String", "int", "int", "int", "java.lang.String", "java.lang.String", "java.lang.String", "java.lang.String"});
         mbeanServer.invoke(name, "renameInstance", new Object[]{"itest5", "new_itest5"}, new String[]{"java.lang.String", "java.lang.String"});
     }
-
 }
diff --git a/manual/src/main/asciidoc/user-guide/instances.adoc b/manual/src/main/asciidoc/user-guide/instances.adoc
index 4094c1bdb0..0eb20b3493 100644
--- a/manual/src/main/asciidoc/user-guide/instances.adoc
+++ b/manual/src/main/asciidoc/user-guide/instances.adoc
@@ -275,6 +275,23 @@ where test is the instance name.
 The `instance:destroy` deletes the instance store (the location where the instance files are stored).
 ====
 
+===== Package an instance
+You can create a .zip file to save all the files of an instance (including the root instance) by executing `instance:package` command. You must provide
+an instance name and the target path. The target path can be relative to the instance home directory
+or an absolute path in your filesystem. The command will output filenames of the compressed files (just like zip Unix command).
+----
+karaf@root()> instance:package root root.zip    
+RELEASE-NOTES.md
+lock
+NOTICE
+deploy/README
+BUILDING.md
+etc/org.apache.karaf.command.acl.config.cfg
+...
+Archive available at root.zip
+----
+
+
 ===== Rename an instance
 
 You can change the name of a stopped instance using the `instance:rename` command: