Search in sources :

Example 21 with CreateContainerMetadata

use of io.fabric8.api.CreateContainerMetadata in project fabric8 by jboss-fuse.

the class FabricManager method changeCreateOptionsField.

@Override
public void changeCreateOptionsField(String containerId, String field, Object value) {
    CreateContainerMetadata<? extends CreateContainerOptions> metadata = getContainerMetaData(containerId);
    if (metadata == null) {
        return;
    }
    CreateContainerOptions options = metadata.getCreateOptions();
    if (options == null) {
        return;
    }
    ObjectMapper mapper = getObjectMapper();
    JsonNode optionsJson = mapper.convertValue(options, JsonNode.class);
    JsonNode valueJson = mapper.convertValue(value, JsonNode.class);
    ((ObjectNode) optionsJson).put(field, valueJson);
    Object builder = null;
    try {
        builder = options.getClass().getMethod("builder").invoke(null);
    } catch (Exception e) {
        LOG.warn("Failed to get builder when setting " + field + " on container " + containerId, e);
        throw new RuntimeException("Failed to get builder when setting " + field + " on container " + containerId, e);
    }
    builder = mapper.convertValue(optionsJson, builder.getClass());
    CreateContainerOptions newOptions = null;
    try {
        newOptions = (CreateContainerOptions) builder.getClass().getMethod("build").invoke(builder);
    } catch (Exception e) {
        LOG.warn("Failed to build CreatecontainerOptions when setting " + field + " on container " + containerId, e);
        throw new RuntimeException("Failed to build CreatecontainerOptions when setting " + field + " on container " + containerId, e);
    }
    metadata.setCreateOptions(newOptions);
    if (LOG.isDebugEnabled()) {
        LOG.debug("Create container metadata: " + metadata);
    }
    fabricService.adapt(DataStore.class).setContainerMetadata(metadata);
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) DataStore(io.fabric8.api.DataStore) CreateContainerOptions(io.fabric8.api.CreateContainerOptions) JsonNode(com.fasterxml.jackson.databind.JsonNode) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) MalformedObjectNameException(javax.management.MalformedObjectNameException) FabricException(io.fabric8.api.FabricException) MalformedURLException(java.net.MalformedURLException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) IOException(java.io.IOException)

Example 22 with CreateContainerMetadata

use of io.fabric8.api.CreateContainerMetadata in project fabric8 by jboss-fuse.

the class JcloudsContainerProvider method start.

@Override
public void start(Container container) {
    assertValid();
    CreateContainerMetadata metadata = container.getMetadata();
    if (!(metadata instanceof CreateJCloudsContainerMetadata)) {
        throw new IllegalStateException("Container doesn't have valid create container metadata type");
    } else {
        CreateJCloudsContainerMetadata jCloudsContainerMetadata = (CreateJCloudsContainerMetadata) metadata;
        CreateJCloudsContainerOptions options = jCloudsContainerMetadata.getCreateOptions();
        ComputeService computeService = getOrCreateComputeService(options);
        try {
            String nodeId = jCloudsContainerMetadata.getNodeId();
            Optional<RunScriptOptions> runScriptOptions = ToRunScriptOptions.withComputeService(computeService).apply(jCloudsContainerMetadata);
            String script = buildStartScript(container.getId(), options);
            ExecResponse response;
            if (runScriptOptions.isPresent()) {
                response = computeService.runScriptOnNode(nodeId, script, runScriptOptions.get());
            } else {
                response = computeService.runScriptOnNode(nodeId, script);
            }
            if (response == null) {
                jCloudsContainerMetadata.setFailure(new Exception("No response received for fabric install script."));
            } else if (response.getOutput() != null && response.getOutput().contains(ContainerProviderUtils.FAILURE_PREFIX)) {
                jCloudsContainerMetadata.setFailure(new Exception(ContainerProviderUtils.parseScriptFailure(response.getOutput())));
            }
        } catch (Throwable t) {
            jCloudsContainerMetadata.setFailure(t);
        }
    }
}
Also used : RunScriptOptions(org.jclouds.compute.options.RunScriptOptions) ToRunScriptOptions(io.fabric8.service.jclouds.functions.ToRunScriptOptions) ExecResponse(org.jclouds.compute.domain.ExecResponse) CreateContainerMetadata(io.fabric8.api.CreateContainerMetadata) ComputeService(org.jclouds.compute.ComputeService) URISyntaxException(java.net.URISyntaxException) RunNodesException(org.jclouds.compute.RunNodesException) MalformedURLException(java.net.MalformedURLException)

Example 23 with CreateContainerMetadata

use of io.fabric8.api.CreateContainerMetadata in project fabric8 by jboss-fuse.

the class ContainerCreateCloud method displayContainers.

protected void displayContainers(CreateContainerMetadata[] metadatas) {
    if (isEnsembleServer) {
        System.out.println(String.format(ENSEMBLE_SERVER_DISPLAY_FORMAT, ENSEMBLE_SERVER_OUTPUT_HEADERS));
    } else {
        System.out.println(String.format(DISPLAY_FORMAT, OUTPUT_HEADERS));
    }
    if (metadatas != null && metadatas.length > 0) {
        for (CreateContainerMetadata ccm : metadatas) {
            String status = "success";
            if (ccm.getFailure() != null) {
                status = ccm.getFailure().getMessage();
            }
            String containerName = ccm.getContainerName() != null ? ccm.getContainerName() : "";
            String nodeId = "";
            Set<String> publicAddresses = null;
            if (ccm instanceof CreateJCloudsContainerMetadata) {
                CreateJCloudsContainerMetadata metadata = (CreateJCloudsContainerMetadata) ccm;
                nodeId = metadata.getNodeId() != null ? metadata.getNodeId() : "";
                publicAddresses = metadata.getPublicAddresses();
            }
            if (isEnsembleServer) {
                System.out.println(String.format(ENSEMBLE_SERVER_DISPLAY_FORMAT, nodeId, containerName, ccm.getCreateOptions().getZookeeperPassword(), publicAddresses, status));
            } else {
                System.out.println(String.format(DISPLAY_FORMAT, nodeId, containerName, publicAddresses, status));
            }
        }
    }
}
Also used : CreateJCloudsContainerMetadata(io.fabric8.service.jclouds.CreateJCloudsContainerMetadata) CreateContainerMetadata(io.fabric8.api.CreateContainerMetadata)

Example 24 with CreateContainerMetadata

use of io.fabric8.api.CreateContainerMetadata in project fabric8 by jboss-fuse.

the class CreateContainerTask method call.

@Override
public Set<ContainerProxy> call() throws Exception {
    Set<ContainerProxy> containers = new HashSet<ContainerProxy>();
    FabricService fabricService = fabricServiceProxy.getService();
    CreateContainerMetadata[] allMetadata = fabricService.createContainers(options);
    if (allMetadata != null && allMetadata.length > 0) {
        for (CreateContainerMetadata metadata : allMetadata) {
            Container container = metadata.getContainer();
            containers.add(ContainerProxy.wrap(container, fabricServiceProxy));
            if (!metadata.isSuccess()) {
                throw new FabricException("Failed to create container.", metadata.getFailure());
            }
        }
    }
    return containers;
}
Also used : Container(io.fabric8.api.Container) FabricService(io.fabric8.api.FabricService) CreateContainerMetadata(io.fabric8.api.CreateContainerMetadata) FabricException(io.fabric8.api.FabricException) HashSet(java.util.HashSet)

Example 25 with CreateContainerMetadata

use of io.fabric8.api.CreateContainerMetadata in project fabric8 by jboss-fuse.

the class FabricTestSupport method createChildContainer.

private Container createChildContainer(FabricService fabricService, String name, String parent, String profileName, String jvmOpts) throws Exception {
    Thread.sleep(DEFAULT_WAIT);
    Container parentContainer = fabricService.getContainer(parent);
    Assert.assertNotNull(parentContainer);
    CreateChildContainerOptions.Builder builder = CreateChildContainerOptions.builder().name(name).parent(parent).zookeeperPassword(fabricService.getZookeeperPassword()).jmxUser("admin").jmxPassword("admin");
    if (jvmOpts != null) {
        builder.jvmOpts(jvmOpts);
    } else {
        builder.jvmOpts("-Xms512m -Xmx1024m -Djava.net.preferIPv4Stack=true -Dpatching.disabled=true -Djava.security.egd=file:/dev/./urandom");
    }
    CreateContainerMetadata[] metadata = fabricService.createContainers(builder.build());
    if (metadata.length > 0) {
        if (metadata[0].getFailure() != null) {
            throw new Exception("Error creating child container:" + name, metadata[0].getFailure());
        }
        Container container = metadata[0].getContainer();
        Version version = fabricService.getRequiredDefaultVersion();
        Profile profile = version.getRequiredProfile(profileName);
        Assert.assertNotNull("Expected to find profile with name:" + profileName, profile);
        container.setProfiles(new Profile[] { profile });
        Provision.containersStatus(Arrays.asList(container), "success", PROVISION_TIMEOUT);
        return container;
    }
    throw new Exception("Could container not created");
}
Also used : Container(io.fabric8.api.Container) Version(io.fabric8.api.Version) CreateChildContainerOptions(io.fabric8.api.CreateChildContainerOptions) CreateContainerMetadata(io.fabric8.api.CreateContainerMetadata) Profile(io.fabric8.api.Profile)

Aggregations

CreateContainerMetadata (io.fabric8.api.CreateContainerMetadata)22 FabricException (io.fabric8.api.FabricException)10 CreateContainerOptions (io.fabric8.api.CreateContainerOptions)6 Container (io.fabric8.api.Container)5 IOException (java.io.IOException)5 ArrayList (java.util.ArrayList)5 ContainerProvider (io.fabric8.api.ContainerProvider)4 MalformedURLException (java.net.MalformedURLException)4 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)3 Session (com.jcraft.jsch.Session)3 CreateContainerBasicOptions (io.fabric8.api.CreateContainerBasicOptions)3 FabricService (io.fabric8.api.FabricService)3 HashSet (java.util.HashSet)3 Map (java.util.Map)3 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)2 CreateChildContainerOptions (io.fabric8.api.CreateChildContainerOptions)2 Profile (io.fabric8.api.Profile)2 ToRunScriptOptions (io.fabric8.service.jclouds.functions.ToRunScriptOptions)2 InvalidClassException (java.io.InvalidClassException)2 URISyntaxException (java.net.URISyntaxException)2