Search in sources :

Example 1 with ContainerProvider

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

the class FabricServiceImpl method stopContainer.

public void stopContainer(Container container, boolean force) {
    assertValid();
    LOGGER.info("Stopping container {}", container.getId());
    ContainerProvider provider = getProvider(container);
    try {
        provider.stop(container);
    } catch (RuntimeException ex) {
        // if its already stopped then ignore the exception
        boolean stopped = "Instance already stopped".equals(ex.getMessage());
        if (!stopped) {
            throw ex;
        }
    }
}
Also used : ContainerProvider(io.fabric8.api.ContainerProvider)

Example 2 with ContainerProvider

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

the class FabricServiceImpl method createContainerAutoScaler.

@Override
public ContainerAutoScaler createContainerAutoScaler(FabricRequirements requirements, ProfileRequirements profileRequirements) {
    Collection<ContainerProvider> providerCollection = getProviders().values();
    for (ContainerProvider containerProvider : providerCollection) {
        // lets pick the highest weighted autoscaler (e.g. to prefer openshift to docker to child
        SortedMap<Integer, ContainerAutoScaler> sortedAutoScalers = new TreeMap<Integer, ContainerAutoScaler>();
        if (containerProvider instanceof ContainerAutoScalerFactory) {
            ContainerAutoScalerFactory provider = (ContainerAutoScalerFactory) containerProvider;
            ContainerAutoScaler autoScaler = provider.createAutoScaler(requirements, profileRequirements);
            if (autoScaler != null) {
                int weight = autoScaler.getWeight();
                sortedAutoScalers.put(weight, autoScaler);
            }
        }
        if (!sortedAutoScalers.isEmpty()) {
            Integer key = sortedAutoScalers.lastKey();
            if (key != null) {
                return sortedAutoScalers.get(key);
            }
        }
    }
    return null;
}
Also used : ContainerProvider(io.fabric8.api.ContainerProvider) ContainerAutoScalerFactory(io.fabric8.api.ContainerAutoScalerFactory) ContainerAutoScaler(io.fabric8.api.ContainerAutoScaler) TreeMap(java.util.TreeMap)

Example 3 with ContainerProvider

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

the class FabricServiceImpl method getProvider.

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;
}
Also used : ContainerProvider(io.fabric8.api.ContainerProvider) CreateContainerMetadata(io.fabric8.api.CreateContainerMetadata)

Example 4 with ContainerProvider

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

the class FabricServiceImpl method getValidProviders.

@Override
public Map<String, ContainerProvider> getValidProviders() {
    assertValid();
    Map<String, ContainerProvider> validProviders = new HashMap<String, ContainerProvider>();
    for (ContainerProvider cp : getProviders().values()) {
        if (cp.isValidProvider()) {
            validProviders.put(cp.getScheme(), cp);
        }
    }
    return Collections.unmodifiableMap(validProviders);
}
Also used : ContainerProvider(io.fabric8.api.ContainerProvider) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap)

Example 5 with ContainerProvider

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

the class FabricManager method createContainers.

@Override
public Map<String, String> createContainers(Map<String, Object> options) {
    if (LOG.isDebugEnabled()) {
        LOG.debug("Creating containers from JSON data: " + options);
    }
    String providerType = (String) options.get("providerType");
    if (providerType == null) {
        throw new RuntimeException("No providerType provided");
    }
    CreateContainerBasicOptions.Builder builder = null;
    ContainerProvider provider = fabricService.getValidProviders().get(providerType);
    if (provider == null) {
        throw new RuntimeException("Can't find valid provider of type: " + providerType);
    }
    Class<?> clazz = provider.getOptionsType();
    try {
        builder = (CreateContainerBasicOptions.Builder) clazz.getMethod("builder").invoke(null);
    } catch (Exception e) {
        LOG.warn("Failed to find builder type", e);
    }
    if (builder == null) {
        throw new RuntimeException("Unknown provider type : " + providerType);
    }
    ObjectMapper mapper = getObjectMapper();
    builder = mapper.convertValue(options, builder.getClass());
    builder.zookeeperPassword(fabricService.getZookeeperPassword());
    builder.zookeeperUrl(fabricService.getZookeeperUrl());
    Object profileObject = options.get("profiles");
    if (profileObject != null) {
        List profiles = mapper.convertValue(profileObject, List.class);
        builder.profiles(profiles);
    }
    if (builder.getProxyUri() == null) {
        builder.proxyUri(fabricService.getMavenRepoURI());
    }
    CreateContainerOptions build = builder.build();
    if (LOG.isDebugEnabled()) {
        LOG.debug("Created container options: " + build + " with profiles " + build.getProfiles());
    }
    CreateContainerMetadata<?>[] metadatas = fabricService.createContainers(build);
    Map<String, String> rc = new LinkedHashMap<String, String>();
    for (CreateContainerMetadata<?> metadata : metadatas) {
        if (!metadata.isSuccess()) {
            LOG.error("Failed to create container {}: ", metadata.getContainerName(), metadata.getFailure());
            rc.put(metadata.getContainerName(), metadata.getFailure().getMessage());
        }
    }
    return rc;
}
Also used : ContainerProvider(io.fabric8.api.ContainerProvider) CreateContainerMetadata(io.fabric8.api.CreateContainerMetadata) CreateContainerOptions(io.fabric8.api.CreateContainerOptions) MalformedObjectNameException(javax.management.MalformedObjectNameException) FabricException(io.fabric8.api.FabricException) MalformedURLException(java.net.MalformedURLException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) IOException(java.io.IOException) LinkedHashMap(java.util.LinkedHashMap) List(java.util.List) ArrayList(java.util.ArrayList) CreateContainerBasicOptions(io.fabric8.api.CreateContainerBasicOptions) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Aggregations

ContainerProvider (io.fabric8.api.ContainerProvider)10 CreateContainerMetadata (io.fabric8.api.CreateContainerMetadata)4 CreateContainerBasicOptions (io.fabric8.api.CreateContainerBasicOptions)3 FabricException (io.fabric8.api.FabricException)3 IOException (java.io.IOException)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 CreateContainerOptions (io.fabric8.api.CreateContainerOptions)2 ProfileDependencyException (io.fabric8.api.ProfileDependencyException)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 TreeMap (java.util.TreeMap)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 EncryptionOperationNotPossibleException (org.jasypt.exceptions.EncryptionOperationNotPossibleException)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 Container (io.fabric8.api.Container)1 ContainerAutoScaler (io.fabric8.api.ContainerAutoScaler)1 ContainerAutoScalerFactory (io.fabric8.api.ContainerAutoScalerFactory)1 CreateContainerBasicMetadata (io.fabric8.api.CreateContainerBasicMetadata)1 CreationStateListener (io.fabric8.api.CreationStateListener)1 FabricService (io.fabric8.api.FabricService)1