Search in sources :

Example 1 with CreateContainerMetadata

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

the class ContainerCreateOpenshiftAction method displayContainers.

protected void displayContainers(CreateContainerMetadata[] metadatas) {
    List<CreateContainerMetadata> success = new ArrayList<CreateContainerMetadata>();
    List<CreateContainerMetadata> failures = new ArrayList<CreateContainerMetadata>();
    for (CreateContainerMetadata metadata : metadatas) {
        (metadata.isSuccess() ? success : failures).add(metadata);
    }
    if (success.size() > 0) {
        System.out.println("The following containers have been created successfully:");
        for (CreateContainerMetadata m : success) {
            System.out.println("\t" + m.toString());
        }
    }
    if (failures.size() > 0) {
        System.out.println("The following containers have failed:");
        for (CreateContainerMetadata m : failures) {
            System.out.println("\t" + m.getContainerName() + ": " + m.getFailure().getMessage());
        }
    }
}
Also used : CreateContainerMetadata(io.fabric8.api.CreateContainerMetadata) ArrayList(java.util.ArrayList)

Example 2 with CreateContainerMetadata

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

the class MQCreateAction method doExecute.

@Override
protected Object doExecute() throws Exception {
    MQBrokerConfigDTO dto = createDTO();
    Profile profile = MQManager.createOrUpdateProfile(dto, fabricService, runtimeProperties);
    if (profile == null) {
        return null;
    }
    String profileId = profile.getId();
    System.out.println("MQ profile " + profileId + " ready");
    // assign profile to existing containers
    if (assign != null) {
        String[] assignContainers = assign.split(",");
        MQManager.assignProfileToContainers(fabricService, profile, assignContainers);
    }
    // create containers
    if (create != null) {
        String[] createContainers = create.split(",");
        List<CreateContainerBasicOptions.Builder> builderList = MQManager.createContainerBuilders(dto, fabricService, "child", profileId, dto.version(), createContainers);
        for (CreateContainerBasicOptions.Builder builder : builderList) {
            CreateContainerMetadata[] metadatas;
            try {
                if (builder instanceof CreateChildContainerOptions.Builder) {
                    CreateChildContainerOptions.Builder childBuilder = (CreateChildContainerOptions.Builder) builder;
                    builder = childBuilder.jmxUser(username).jmxPassword(password);
                }
                metadatas = fabricService.createContainers(builder.build());
                // check if there was a FabricAuthenticationException as failure then we can try again
                if (metadatas != null) {
                    for (CreateContainerMetadata meta : metadatas) {
                        if (meta.getFailure() != null && meta.getFailure() instanceof FabricAuthenticationException) {
                            throw (FabricAuthenticationException) meta.getFailure();
                        }
                    }
                }
                ShellUtils.storeFabricCredentials(session, username, password);
            } catch (FabricAuthenticationException fae) {
                // If authentication fails, prompts for credentials and try again.
                if (builder instanceof CreateChildContainerOptions.Builder) {
                    CreateChildContainerOptions.Builder childBuilder = (CreateChildContainerOptions.Builder) builder;
                    promptForJmxCredentialsIfNeeded();
                    metadatas = fabricService.createContainers(childBuilder.jmxUser(username).jmxPassword(password).build());
                    ShellUtils.storeFabricCredentials(session, username, password);
                }
            }
        }
    }
    return null;
}
Also used : FabricAuthenticationException(io.fabric8.api.FabricAuthenticationException) CreateChildContainerOptions(io.fabric8.api.CreateChildContainerOptions) CreateContainerMetadata(io.fabric8.api.CreateContainerMetadata) Profile(io.fabric8.api.Profile) MQBrokerConfigDTO(io.fabric8.api.jmx.MQBrokerConfigDTO) CreateContainerBasicOptions(io.fabric8.api.CreateContainerBasicOptions)

Example 3 with CreateContainerMetadata

use of io.fabric8.api.CreateContainerMetadata 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 CreateContainerMetadata

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

the class ZkDataStoreImpl method createContainerConfig.

@Override
public void createContainerConfig(CreateContainerMetadata metadata) {
    assertValid();
    try {
        CreateContainerOptions options = metadata.getCreateOptions();
        String containerId = metadata.getContainerName();
        // String parent = options.getParent();
        // String versionId = options.getVersion() != null ? options.getVersion() : getDefaultVersion();
        // Set<String> profileIds = options.getProfiles();
        // if (profileIds == null || profileIds.isEmpty()) {
        // profileIds = new LinkedHashSet<String>();
        // profileIds.add("default");
        // }
        // StringBuilder sb = new StringBuilder();
        // for (String profileId : profileIds) {
        // if (sb.length() > 0) {
        // sb.append(" ");
        // }
        // sb.append(profileId);
        // }
        // 
        // setData(curator.get(), ZkPath.CONFIG_CONTAINER.getPath(containerId), versionId);
        // setData(curator.get(), ZkPath.CONFIG_VERSIONS_CONTAINER.getPath(versionId, containerId), sb.toString());
        // setData(curator.get(), ZkPath.CONTAINER_PARENT.getPath(containerId), parent);
        setContainerMetadata(metadata);
        Map<String, String> configuration = metadata.getContainerConfiguration();
        for (Map.Entry<String, String> entry : configuration.entrySet()) {
            String key = entry.getKey();
            String value = entry.getValue();
            setData(curator.get(), ZkPath.CONTAINER_ENTRY.getPath(metadata.getContainerName(), key), value);
        }
        // If no resolver specified but a resolver is already present in the registry, use the registry value
        String resolver = metadata.getOverridenResolver() != null ? metadata.getOverridenResolver() : options.getResolver();
        if (resolver == null && exists(curator.get(), ZkPath.CONTAINER_RESOLVER.getPath(containerId)) != null) {
            resolver = getStringData(curator.get(), ZkPath.CONTAINER_RESOLVER.getPath(containerId));
        } else if (options.getResolver() != null) {
        // Use the resolver specified in the options and do nothing.
        } else if (exists(curator.get(), ZkPath.POLICIES.getPath(ZkDefs.RESOLVER)) != null) {
            // If there is a globlal resolver specified use it.
            resolver = getStringData(curator.get(), ZkPath.POLICIES.getPath(ZkDefs.RESOLVER));
        } else {
            // Fallback to the default resolver
            resolver = ZkDefs.DEFAULT_RESOLVER;
        }
        // Set the resolver if not already set
        setData(curator.get(), ZkPath.CONTAINER_RESOLVER.getPath(containerId), resolver);
    } catch (Exception e) {
        throw FabricException.launderThrowable(e);
    }
}
Also used : CreateContainerOptions(io.fabric8.api.CreateContainerOptions) Map(java.util.Map) FabricException(io.fabric8.api.FabricException) InvalidClassException(java.io.InvalidClassException) KeeperException(org.apache.zookeeper.KeeperException) IOException(java.io.IOException)

Example 5 with CreateContainerMetadata

use of io.fabric8.api.CreateContainerMetadata 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

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