Пример #1
0
  public void destroyContainer(Container container, boolean force) {
    assertValid();
    String containerId = container.getId();
    LOGGER.info("Destroying container {}", containerId);
    boolean destroyed = false;
    try {
      ContainerProvider provider = getProvider(container, true);
      if (provider != null) {
        try {
          provider.stop(container);
        } catch (Exception ex) {
          // Ignore error while stopping and try to destroy.
        }
        provider.destroy(container);
        destroyed = true;
      }

    } finally {
      try {
        if (destroyed || force) {
          portService.get().unregisterPort(container);
          getDataStore().deleteContainer(container.getId());
        }
      } catch (Exception e) {
        LOGGER.warn(
            "Failed to cleanup container {} entries due to: {}. This will be ignored.",
            containerId,
            e.getMessage());
      }
    }
  }
Пример #2
0
 protected String getDockerContainerId(Container container) {
   CreateDockerContainerMetadata containerMetadata = getContainerMetadata(container);
   if (containerMetadata != null) {
     return containerMetadata.getId();
   }
   return container.getId();
 }
Пример #3
0
 public T container(final Container container) {
   this.container = container;
   if (container != null) {
     id(container.getId());
   }
   return (T) this;
 }
 @Override
 public ChildContainerController getControllerForContainer(Container container) {
   Installation installation = getProcessManager().getInstallation(container.getId());
   ChildContainerController answer = null;
   if (installation != null) {
     answer = createProcessManagerController();
   }
   return answer;
 }
 private int getCompletedExchangesCount(final Container c) {
   String response =
       new AnsiString(
               executeCommand(
                   "fabric:container-connect -u admin -p admin "
                       + c.getId()
                       + " camel:route-info fabric-client | grep \"Exchanges Completed\""))
           .getPlain()
           .toString();
   System.out.println(response);
   return Integer.parseInt(response.replaceAll("[^0-9]", ""));
 }
Пример #6
0
 @Override
 public String profileWebAppURL(String webAppId, String profileId, String versionId) {
   if (versionId == null || versionId.length() == 0) {
     Version version = getDefaultVersion();
     if (version != null) {
       versionId = version.getId();
     }
   }
   List<Container> containers =
       Containers.containersForProfile(getContainers(), profileId, versionId);
   for (Container container : containers) {
     String url = containerWebAppURL(webAppId, container.getId());
     if (url != null && url.length() > 0) {
       return url;
     }
   }
   return null;
 }
Пример #7
0
 private ContainerProvider getProvider(Container container, boolean returnNull) {
   CreateContainerMetadata metadata = container.getMetadata();
   String type = metadata != null ? metadata.getCreateOptions().getProviderType() : null;
   if (type == null) {
     if (returnNull) {
       return null;
     }
     throw new UnsupportedOperationException(
         "Container " + container.getId() + " has not been created using Fabric");
   }
   ContainerProvider provider = getProvider(type);
   if (provider == null) {
     if (returnNull) {
       return null;
     }
     throw new UnsupportedOperationException("Container provider " + type + " not supported");
   }
   return provider;
 }
Пример #8
0
  @Test
  public void testRegistryEntries() throws Exception {
    int startingPort = 9090;
    System.err.println(executeCommand("fabric:create -n root"));
    CuratorFramework curator = getCurator();
    // Wait for zookeeper service to become available.
    System.err.println(
        executeCommand("fabric:profile-create --parents feature-camel fabric-camel"));
    System.err.println(
        executeCommand("fabric:profile-create --parents fabric-camel fabric-camel-server"));
    System.err.println(
        executeCommand("fabric:profile-create --parents fabric-camel fabric-camel-client"));
    System.err.println(
        executeCommand(
            "fabric:profile-edit --repositories mvn:org.fusesource.examples.fabric-camel-cluster/features/"
                + System.getProperty("fabric.version")
                + "/xml/features fabric-camel"));

    System.err.println(
        executeCommand("fabric:profile-edit --features camel-server fabric-camel-server"));
    executeCommand("fabric:profile-edit --features camel-client fabric-camel-client");

    Set<Container> containers =
        ContainerBuilder.create(3)
            .withName("fabric-camel")
            .withProfiles("fabric-camel")
            .assertProvisioningResult()
            .build();

    // We will use the first container as a client and the rest as servers.
    Container camelClientContainer = containers.iterator().next();
    containers.remove(camelClientContainer);
    Set<Container> camelServerContainers = new LinkedHashSet<Container>(containers);

    int index = 1;
    for (Container c : camelServerContainers) {
      setData(curator, ZkPath.CONTAINER_PROVISION_RESULT.getPath(c.getId()), "changing");
      executeCommand(
          "fabric:container-connect -u admin -p admin "
              + camelClientContainer.getId()
              + " log:set DEBUG");
      System.err.println(
          executeCommand(
              "fabric:profile-create --parents fabric-camel-server fabric-camel-server-" + index));
      System.err.println(
          executeCommand(
              "fabric:profile-edit --pid io.fabric8.examples.camel.loadbalancing.server/portNumber="
                  + (startingPort++)
                  + " fabric-camel-server-"
                  + index));
      System.err.println(executeCommand("fabric:profile-display fabric-camel-server-" + index));
      System.err.println(
          executeCommand(
              "fabric:container-change-profile "
                  + c.getId()
                  + " fabric-camel-server-"
                  + (index++)));
    }

    Provision.provisioningSuccess(camelServerContainers, PROVISION_TIMEOUT);
    setData(
        curator,
        ZkPath.CONTAINER_PROVISION_RESULT.getPath(camelClientContainer.getId()),
        "changing");
    System.err.println(
        executeCommand(
            "fabric:container-change-profile "
                + camelClientContainer.getId()
                + " fabric-camel-client"));
    Provision.provisioningSuccess(
        Arrays.asList(new Container[] {camelClientContainer}), PROVISION_TIMEOUT);

    System.err.println(executeCommand("fabric:container-list"));
    System.err.println(executeCommand("fabric:profile-display --overlay fabric-camel-server"));

    // Check that the entries have been properly propagated.
    Assert.assertNotNull(exists(curator, "/fabric/registry/camel/endpoints/"));
    Assert.assertEquals(1, getChildren(curator, "/fabric/registry/camel/endpoints/").size());
    Thread.sleep(5000);
    System.err.println(
        executeCommand(
            "fabric:container-connect -u admin -p admin "
                + camelClientContainer.getId()
                + " camel:route-list"));
    String response =
        new AnsiString(
                executeCommand(
                    "fabric:container-connect -u admin -p admin "
                        + camelClientContainer.getId()
                        + " camel:route-info fabric-client | grep Failed"))
            .getPlain()
            .toString();
    System.err.println(response);
    int failed = Integer.parseInt(response.replaceAll("[^0-9]", ""));
    Assert.assertEquals("Failed exchanges found on client", 0, failed);

    // We want to kill all but one server, so we take out the first and keep it to the end.
    Container lastActiveServerContainer = camelServerContainers.iterator().next();
    camelServerContainers.remove(lastActiveServerContainer);
    for (Container c : camelServerContainers) {
      c.destroy();
      Thread.sleep(25000);
      response =
          new AnsiString(
                  executeCommand(
                      "fabric:container-connect -u admin -p admin "
                          + camelClientContainer.getId()
                          + " camel:route-info fabric-client | grep Failed"))
              .getPlain()
              .toString();
      System.err.println(response);
      failed = Integer.parseInt(response.replaceAll("[^0-9]", ""));
      Assert.assertEquals(
          "Failed exchanges found after container:" + c.getId() + " shut down", 0, failed);
    }
  }
  @Test
  public void testRegistryEntries() throws Exception {
    System.out.println(executeCommand("fabric:create -n root"));
    System.out.println(executeCommand("fabric:profile-list"));

    ServiceProxy<FabricService> fabricProxy =
        ServiceProxy.createServiceProxy(bundleContext, FabricService.class);
    try {
      FabricService fabricService = fabricProxy.getService();
      CuratorFramework curator = fabricService.adapt(CuratorFramework.class);

      Set<ContainerProxy> containers =
          ContainerBuilder.create(fabricProxy, 3)
              .withName("fabric-camel")
              .withProfiles("feature-camel")
              .assertProvisioningResult()
              .build();
      try {
        // We will use the first container as a client and the rest as servers.
        LinkedList<Container> containerList = new LinkedList<Container>(containers);
        Container client = containerList.removeLast();

        LinkedList<Container> servers = new LinkedList<Container>(containerList);

        for (Container c : servers) {
          Profile p = c.getVersion().getProfile("example-camel-cluster.server");
          c.setProfiles(new Profile[] {p});
        }

        Provision.provisioningSuccess(servers, PROVISION_TIMEOUT);

        Profile p = client.getVersion().getProfile("example-camel-cluster.client");
        client.setProfiles(new Profile[] {p});

        Provision.provisioningSuccess(Arrays.asList(new Container[] {client}), PROVISION_TIMEOUT);

        System.out.println(executeCommand("fabric:container-list"));
        System.out.println(executeCommand("fabric:profile-display --overlay fabric-camel-server"));

        // Check that the entries have been properly propagated.
        Assert.assertNotNull(exists(curator, "/fabric/registry/camel/endpoints"));
        Assert.assertEquals(1, getChildren(curator, "/fabric/registry/camel/endpoints").size());

        Assert.assertTrue(
            Provision.waitForCondition(
                Arrays.asList(client),
                new ContainerCondition() {
                  @Override
                  public Boolean checkConditionOnContainer(final Container c) {
                    return getCompletedExchangesCount(c) > 0;
                  }
                },
                60000L));

        // We want to kill all but one server, so we take out the first and keep it to the end.
        Container lastActiveServerContainer = servers.removeLast();

        for (Container c : servers) {
          try {
            c.destroy(true);
          } catch (Exception ex) {
            // ignore.
          }
          // Get the number of exchanges completed before we kill the server.
          final int completed = getCompletedExchangesCount(client);

          // Ensure that we still have messages flowing
          Assert.assertTrue(
              Provision.waitForCondition(
                  Arrays.asList(client),
                  new ContainerCondition() {
                    @Override
                    public Boolean checkConditionOnContainer(final Container c) {
                      return getCompletedExchangesCount(c) > completed + 3;
                    }
                  },
                  60000L));
        }
        System.out.println(
            new AnsiString(
                    executeCommand(
                        "fabric:container-connect -u admin -p admin "
                            + client.getId()
                            + " camel:route-info fabric-client"))
                .getPlain()
                .toString());
      } finally {
        ContainerBuilder.destroy(containers);
      }
    } finally {
      fabricProxy.close();
    }
  }
Пример #10
0
 public void stopContainer(Container container, boolean force) {
   assertValid();
   LOGGER.info("Stopping container {}", container.getId());
   ContainerProvider provider = getProvider(container);
   provider.stop(container);
 }