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 2011/02/08 12:18:33 UTC

svn commit: r1068334 - in /karaf/trunk: admin/command/src/main/java/org/apache/karaf/admin/command/ admin/command/src/main/resources/OSGI-INF/blueprint/ admin/core/src/main/java/org/apache/karaf/admin/ admin/core/src/main/java/org/apache/karaf/admin/in...

Author: jbonofre
Date: Tue Feb  8 11:18:33 2011
New Revision: 1068334

URL: http://svn.apache.org/viewvc?rev=1068334&view=rev
Log:
[KARAF-216] Add admin:clone command.

Added:
    karaf/trunk/admin/command/src/main/java/org/apache/karaf/admin/command/CloneCommand.java
Modified:
    karaf/trunk/admin/command/src/main/resources/OSGI-INF/blueprint/admin-command.xml
    karaf/trunk/admin/core/src/main/java/org/apache/karaf/admin/AdminService.java
    karaf/trunk/admin/core/src/main/java/org/apache/karaf/admin/internal/AdminServiceImpl.java
    karaf/trunk/manual/src/main/webapp/users-guide/child-instances.conf

Added: karaf/trunk/admin/command/src/main/java/org/apache/karaf/admin/command/CloneCommand.java
URL: http://svn.apache.org/viewvc/karaf/trunk/admin/command/src/main/java/org/apache/karaf/admin/command/CloneCommand.java?rev=1068334&view=auto
==============================================================================
--- karaf/trunk/admin/command/src/main/java/org/apache/karaf/admin/command/CloneCommand.java (added)
+++ karaf/trunk/admin/command/src/main/java/org/apache/karaf/admin/command/CloneCommand.java Tue Feb  8 11:18:33 2011
@@ -0,0 +1,57 @@
+/*
+ * 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.admin.command;
+
+import org.apache.felix.gogo.commands.Argument;
+import org.apache.felix.gogo.commands.Command;
+import org.apache.felix.gogo.commands.Option;
+import org.apache.karaf.admin.InstanceSettings;
+import org.apache.karaf.admin.command.AdminCommandSupport;
+
+/**
+ * <p>
+ * Clone an existing Karaf instance.
+ * </p>
+ */
+@Command(scope = "admin", name = "clone", description = "Clones an existing container instance.")
+public class CloneCommand extends AdminCommandSupport {
+
+    @Option(name = "-s", aliases = {"--ssh-port"}, description = "Port number for remote secure shell connection", required = false, multiValued = false)
+    int sshPort = 0;
+
+    @Option(name = "-r", aliases = {"--rmi-port", "--rmi-registry-port"}, description = "Port number for RMI registry connection", required = false, multiValued = false)
+    int rmiPort = 0;
+
+    @Option(name = "-l", aliases = {"--location"}, description = "Location of the clone container instance in the file system", required = false, multiValued = false)
+    String location;
+
+    @Option(name = "-o", aliases = {"--java-opts"}, description = "JVM options to use when launching the clone instance", required = false, multiValued = false)
+    String javaOpts;
+
+    @Argument(index = 0, name = "name", description = "The name of the source container instance", required = true, multiValued = false)
+    String name;
+
+    @Argument(index = 1, name = "cloneName", description = "The name of the clone container instance", required = true, multiValued = false)
+    String cloneName;
+
+    protected Object doExecute() throws Exception {
+        InstanceSettings settings = new InstanceSettings(sshPort, rmiPort, location, javaOpts, null, null);
+        getAdminService().cloneInstance(name, cloneName, settings);
+        return null;
+    }
+
+}

Modified: karaf/trunk/admin/command/src/main/resources/OSGI-INF/blueprint/admin-command.xml
URL: http://svn.apache.org/viewvc/karaf/trunk/admin/command/src/main/resources/OSGI-INF/blueprint/admin-command.xml?rev=1068334&r1=1068333&r2=1068334&view=diff
==============================================================================
--- karaf/trunk/admin/command/src/main/resources/OSGI-INF/blueprint/admin-command.xml (original)
+++ karaf/trunk/admin/command/src/main/resources/OSGI-INF/blueprint/admin-command.xml Tue Feb  8 11:18:33 2011
@@ -76,6 +76,15 @@
                 <null />
             </completers>
         </command>
+        <command name="admin/clone">
+            <action class="org.apache.karaf.admin.command.CloneCommand">
+                <property name="adminService" ref="adminService" />
+            </action>
+            <completers>
+                <ref component-id="instanceCompleter" />
+                <null />
+            </completers>
+        </command>
         <command name="admin/change-ssh-port">
             <action class="org.apache.karaf.admin.command.ChangeSshPortCommand">
                 <property name="adminService" ref="adminService" />

Modified: karaf/trunk/admin/core/src/main/java/org/apache/karaf/admin/AdminService.java
URL: http://svn.apache.org/viewvc/karaf/trunk/admin/core/src/main/java/org/apache/karaf/admin/AdminService.java?rev=1068334&r1=1068333&r2=1068334&view=diff
==============================================================================
--- karaf/trunk/admin/core/src/main/java/org/apache/karaf/admin/AdminService.java (original)
+++ karaf/trunk/admin/core/src/main/java/org/apache/karaf/admin/AdminService.java Tue Feb  8 11:18:33 2011
@@ -22,6 +22,8 @@ public interface AdminService {
 
     void renameInstance(String name, String newName) throws Exception;
 
+    Instance cloneInstance(String name, String cloneName, InstanceSettings settings) throws Exception;
+
     Instance[] getInstances();
 
     Instance getInstance(String name);    

Modified: karaf/trunk/admin/core/src/main/java/org/apache/karaf/admin/internal/AdminServiceImpl.java
URL: http://svn.apache.org/viewvc/karaf/trunk/admin/core/src/main/java/org/apache/karaf/admin/internal/AdminServiceImpl.java?rev=1068334&r1=1068333&r2=1068334&view=diff
==============================================================================
--- karaf/trunk/admin/core/src/main/java/org/apache/karaf/admin/internal/AdminServiceImpl.java (original)
+++ karaf/trunk/admin/core/src/main/java/org/apache/karaf/admin/internal/AdminServiceImpl.java Tue Feb  8 11:18:33 2011
@@ -37,6 +37,7 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 public class AdminServiceImpl implements AdminService {
+
     public static final String STORAGE_FILE = "instance.properties";
     public static final String BACKUP_EXTENSION = ".bak";
     private static final String FEATURES_CFG = "etc/org.apache.karaf.features.cfg";
@@ -291,6 +292,63 @@ public class AdminServiceImpl implements
         saveState();
     }
 
+    public synchronized Instance cloneInstance(String name, String cloneName, InstanceSettings settings) throws Exception {
+        if (instances.get(cloneName) != null) {
+            throw new IllegalArgumentException("Instance " + cloneName + " already exists");
+        }
+        Instance instance = instances.get(name);
+        if (instance == null) {
+            throw new IllegalArgumentException("Instance " + name + " not found");
+        }
+        if (instance.isRoot()) {
+            throw new IllegalArgumentException("You can't clone the root instance");
+        }
+        if (instance.getPid() != 0) {
+            throw new IllegalStateException("Instance not stopped");
+        }
+
+        println(Ansi.ansi().a("Cloning instance ").a(Ansi.Attribute.INTENSITY_BOLD).a(name).a(Ansi.Attribute.RESET).a(" into ").a(Ansi.Attribute.INTENSITY_BOLD).a(cloneName).toString());
+        // define the clone instance location
+        String cloneLocationPath = settings.getLocation() != null ? settings.getLocation() : name;
+        File cloneLocation = new File(cloneLocationPath);
+        if (!cloneLocation.isAbsolute()) {
+            cloneLocation = new File(storageLocation, cloneLocationPath);
+        }
+        // copy instance directory
+        String locationPath = instance.getLocation();
+        File location = new File(locationPath);
+        copy(location, cloneLocation);
+        // create the properties map including the instance name, location, ssh and rmi port numbers
+        HashMap<String, String> props = new HashMap<String, String>();
+        props.put(name, cloneName);
+        props.put(locationPath, cloneLocationPath);
+        if (settings.getSshPort() > 0)
+            props.put(new Integer(instance.getSshPort()).toString(), new Integer(settings.getSshPort()).toString());
+        if (settings.getRmiRegistryPort() > 0)
+            props.put(new Integer(instance.getRmiRegistryPort()).toString(), new Integer(settings.getRmiRegistryPort()).toString());
+        // filtering clone files
+        filterResource(cloneLocation, "etc/customer.properties", props);
+        filterResource(cloneLocation, "etc/org.apache.karaf.management.cfg", props);
+        filterResource(cloneLocation, "etc/org.apache.karaf.shell.cfg", props);
+        filterResource(cloneLocation, "etc/org.ops4j.pax.logging.cfg", props);
+        filterResource(cloneLocation, "etc/system.properties", props);
+        filterResource(cloneLocation, "bin/karaf", props);
+        filterResource(cloneLocation, "bin/start", props);
+        filterResource(cloneLocation, "bin/stop", props);
+        filterResource(cloneLocation, "bin/karaf.bat", props);
+        filterResource(cloneLocation, "bin/start.bat", props);
+        filterResource(cloneLocation, "bin/stop.bat", props);
+        // create and add the clone instance in the registry
+        String javaOpts = settings.getJavaOpts();
+        if (javaOpts == null || javaOpts.length() == 0) {
+            javaOpts = "-server -Xmx512M -Dcom.sun.management.jmxremote";
+        }
+        Instance cloneInstance = new InstanceImpl(this, name, cloneLocation.toString(), settings.getJavaOpts());
+        instances.put(name, instance);
+        saveState();
+        return cloneInstance;
+    }
+
     synchronized void saveState() throws IOException {
         Properties storage = new Properties();
         Instance[] data = getInstances();
@@ -445,4 +503,27 @@ public class AdminServiceImpl implements
         return status;
     }
 
+    private void copy(File source, File destination) throws Exception {
+        if (source.isDirectory()) {
+            if (!destination.exists()) {
+                destination.mkdirs();
+            }
+            String[] children = source.list();
+            for (String child : children) {
+                copy(new File(source, child), new File(destination, child));
+            }
+        } else {
+            InputStream in = new FileInputStream(source);
+            OutputStream out = new FileOutputStream(destination);
+            byte[] buffer = new byte[1024];
+            int length;
+            while ((length = in.read(buffer)) > 0) {
+                out.write(buffer, 0, length);
+            }
+            in.close();
+            out.close();
+        }
+
+    }
+
 }

Modified: karaf/trunk/manual/src/main/webapp/users-guide/child-instances.conf
URL: http://svn.apache.org/viewvc/karaf/trunk/manual/src/main/webapp/users-guide/child-instances.conf?rev=1068334&r1=1068333&r2=1068334&view=diff
==============================================================================
--- karaf/trunk/manual/src/main/webapp/users-guide/child-instances.conf (original)
+++ karaf/trunk/manual/src/main/webapp/users-guide/child-instances.conf Tue Feb  8 11:18:33 2011
@@ -100,6 +100,24 @@ admin:destroy instance
 {code}
 Note that only stopped instances can be destroyed.
 
+h2. Renaming a child instance
+
+You can easily change the name of an existing instance using the [{{admin:rename}}|/commands/admin-rename] command:
+{code}
+admin:rename instance newName
+{code}
+Note that only stopped instances can be renamed.
+
+h2. Cloning a child instance
+
+You can easily clone (copy) an existing instance using the [{{admin:clone}}|/commands/admin-clone] command:
+{code}
+admin:clone instance cloneName
+{code}
+Note that only stopped instances can be cloned.
+
+All instance files will be copied and the name and port numbers are changed in the cloned instance.
+
 h2. Using the admin script
 
 You can also manage the local instances of Karaf.  The {{admin}} script in the {{[karaf-install-dir]/bin}} directory provides the same commands as the {{admin}} console commands, apart from [{{admin:connect}}|/commands/admin-connect].