Exemplo n.º 1
0
  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());
    if (settings.getRmiServerPort() > 0)
      props.put(
          new Integer(instance.getRmiServerPort()).toString(),
          new Integer(settings.getRmiServerPort()).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;
  }
Exemplo n.º 2
0
  public synchronized Instance createInstance(String name, InstanceSettings settings)
      throws Exception {
    if (instances.get(name) != null) {
      throw new IllegalArgumentException("Instance '" + name + "' already exists");
    }
    String loc = settings.getLocation() != null ? settings.getLocation() : name;
    File karafBase = new File(loc);
    if (!karafBase.isAbsolute()) {
      karafBase = new File(storageLocation, loc);
    }
    int sshPort = settings.getSshPort();
    if (sshPort <= 0) {
      sshPort = ++defaultSshPortStart;
    }
    int rmiRegistryPort = settings.getRmiRegistryPort();
    if (rmiRegistryPort <= 0) {
      rmiRegistryPort = ++defaultRmiRegistryPortStart;
    }
    int rmiServerPort = settings.getRmiServerPort();
    if (rmiServerPort <= 0) {
      rmiServerPort = ++defaultRmiServerPortStart;
    }
    println(
        Ansi.ansi()
            .a("Creating new instance on SSH port ")
            .a(sshPort)
            .a(" and RMI registry port ")
            .a(rmiRegistryPort)
            .a(" / RMI server port ")
            .a(rmiServerPort)
            .a(" at: ")
            .a(Ansi.Attribute.INTENSITY_BOLD)
            .a(karafBase)
            .a(Ansi.Attribute.RESET)
            .toString());

    mkdir(karafBase, "bin");
    mkdir(karafBase, "etc");
    mkdir(karafBase, "system");
    mkdir(karafBase, "deploy");
    mkdir(karafBase, "data");

    copyResourceToDir(karafBase, "etc/config.properties", true);
    copyResourceToDir(karafBase, "etc/jre.properties", true);
    copyResourceToDir(karafBase, "etc/custom.properties", true);
    copyResourceToDir(karafBase, "etc/java.util.logging.properties", true);
    copyResourceToDir(karafBase, "etc/org.apache.felix.fileinstall-deploy.cfg", true);
    copyResourceToDir(karafBase, "etc/org.apache.karaf.log.cfg", true);
    copyResourceToDir(karafBase, FEATURES_CFG, true);
    copyResourceToDir(karafBase, "etc/org.ops4j.pax.logging.cfg", true);
    copyResourceToDir(karafBase, "etc/org.ops4j.pax.url.mvn.cfg", true);
    copyResourceToDir(karafBase, "etc/startup.properties", true);
    copyResourceToDir(karafBase, "etc/users.properties", true);

    HashMap<String, String> props = new HashMap<String, String>();
    props.put("${SUBST-KARAF-NAME}", name);
    props.put("${SUBST-KARAF-HOME}", System.getProperty("karaf.home"));
    props.put("${SUBST-KARAF-BASE}", karafBase.getPath());
    props.put("${SUBST-SSH-PORT}", Integer.toString(sshPort));
    props.put("${SUBST-RMI-REGISTRY-PORT}", Integer.toString(rmiRegistryPort));
    props.put("${SUBST-RMI-SERVER-PORT}", Integer.toString(rmiServerPort));
    copyFilteredResourceToDir(karafBase, "etc/system.properties", props);
    copyFilteredResourceToDir(karafBase, "etc/org.apache.karaf.shell.cfg", props);
    copyFilteredResourceToDir(karafBase, "etc/org.apache.karaf.management.cfg", props);
    // If we use batch files, use batch files, else use bash scripts (even on cygwin)
    boolean windows = System.getProperty("os.name").startsWith("Win");
    boolean cygwin = windows && new File(System.getProperty("karaf.home"), "bin/admin").exists();
    if (windows && !cygwin) {
      copyFilteredResourceToDir(karafBase, "bin/karaf.bat", props);
      copyFilteredResourceToDir(karafBase, "bin/start.bat", props);
      copyFilteredResourceToDir(karafBase, "bin/stop.bat", props);
    } else {
      copyFilteredResourceToDir(karafBase, "bin/karaf", props);
      copyFilteredResourceToDir(karafBase, "bin/start", props);
      copyFilteredResourceToDir(karafBase, "bin/stop", props);
      if (!cygwin) {
        chmod(new File(karafBase, "bin/karaf"), "a+x");
        chmod(new File(karafBase, "bin/start"), "a+x");
        chmod(new File(karafBase, "bin/stop"), "a+x");
      }
    }

    handleFeatures(new File(karafBase, FEATURES_CFG), settings);

    String javaOpts = settings.getJavaOpts();
    if (javaOpts == null || javaOpts.length() == 0) {
      javaOpts = "-server -Xmx512M -Dcom.sun.management.jmxremote";
    }
    Instance instance = new InstanceImpl(this, name, karafBase.toString(), settings.getJavaOpts());
    instances.put(name, instance);
    saveState();
    return instance;
  }