Search in sources :

Example 51 with Build

use of io.fabric8.openshift.api.model.Build 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)

Example 52 with Build

use of io.fabric8.openshift.api.model.Build in project fabric8 by jboss-fuse.

the class CloudContainerInstallationTask method install.

public CreateJCloudsContainerMetadata install() {
    LoginCredentials credentials = nodeMetadata.getCredentials();
    // For some cloud providers return do not allow shell access to root, so the user needs to be overrided.
    if (!Strings.isNullOrEmpty(options.getUser()) && credentials != null) {
        credentials = credentials.toBuilder().user(options.getUser()).build();
    } else {
        credentials = nodeMetadata.getCredentials();
    }
    String id = nodeMetadata.getId();
    Set<String> publicAddresses = nodeMetadata.getPublicAddresses();
    // Make a copy of the addresses, because we don't want to return back a guice implementation of Set.
    Set<String> copyOfPublicAddresses = new HashSet<String>();
    for (String publicAddress : publicAddresses) {
        copyOfPublicAddresses.add(publicAddress);
    }
    CreateJCloudsContainerMetadata jCloudsContainerMetadata = new CreateJCloudsContainerMetadata();
    jCloudsContainerMetadata.setCreateOptions(options);
    jCloudsContainerMetadata.setNodeId(nodeMetadata.getId());
    jCloudsContainerMetadata.setContainerName(containerName);
    jCloudsContainerMetadata.setPublicAddresses(copyOfPublicAddresses);
    jCloudsContainerMetadata.setHostname(nodeMetadata.getHostname());
    if (credentials != null) {
        jCloudsContainerMetadata.setIdentity(credentials.identity);
        jCloudsContainerMetadata.setCredential(credentials.credential);
    }
    String publicAddress = "";
    Properties addresses = new Properties();
    if (publicAddresses != null && !publicAddresses.isEmpty()) {
        publicAddress = publicAddresses.iterator().next();
        addresses.put(ZkDefs.PUBLIC_IP, publicAddress);
    }
    options.getSystemProperties().put(ContainerProviderUtils.ADDRESSES_PROPERTY_KEY, addresses);
    options.getMetadataMap().put(containerName, jCloudsContainerMetadata);
    // Setup firwall for node
    try {
        FirewallManager firewallManager = firewallManagerFactory.getFirewallManager(computeService);
        if (firewallManager.isSupported()) {
            listener.onStateChange("Configuring firewall.");
            String source = getOriginatingIp();
            Rule httpRule = Rule.create().source("0.0.0.0/0").destination(nodeMetadata).port(8181);
            firewallManager.addRules(httpRule);
            if (source != null) {
                Rule jmxRule = Rule.create().source(source).destination(nodeMetadata).ports(44444, 1099);
                Rule sshRule = Rule.create().source(source).destination(nodeMetadata).port(8101);
                Rule zookeeperRule = Rule.create().source(source).destination(nodeMetadata).port(2181);
                firewallManager.addRules(jmxRule, sshRule, zookeeperRule);
            }
            // where firewall configuration is shared among nodes of the same groups, e.g. EC2.
            if (!Strings.isNullOrEmpty(publicAddress)) {
                Rule zookeeperFromTargetRule = Rule.create().source(publicAddress + "/32").destination(nodeMetadata).port(2181);
                firewallManager.addRule(zookeeperFromTargetRule);
            }
        } else {
            listener.onStateChange(String.format("Skipping firewall configuration. Not supported for provider %s", options.getProviderName()));
        }
    } catch (FirewallNotSupportedOnProviderException e) {
        LOGGER.warn("Firewall manager not supported. Firewall will have to be manually configured.");
    } catch (IOException e) {
        LOGGER.warn("Could not lookup originating ip. Firewall will have to be manually configured.", e);
    } catch (Throwable t) {
        LOGGER.warn("Failed to setup firewall", t);
    }
    try {
        String script = buildInstallAndStartScript(containerName, options);
        listener.onStateChange(String.format("Installing fabric agent on container %s. It may take a while...", containerName));
        ExecResponse response = null;
        String uploadPath = "/tmp/fabric8-karaf-" + FabricConstants.FABRIC_VERSION + ".zip";
        URL distributionURL = options.getProxyUri().resolve("io/fabric8/fabric8-karaf/" + FabricConstants.FABRIC_VERSION + "/fabric8-karaf-" + FabricConstants.FABRIC_VERSION + ".zip").toURL();
        try {
            if (options.doUploadDistribution()) {
                uploadToNode(computeService.getContext(), nodeMetadata, credentials, distributionURL, uploadPath);
            }
            if (credentials != null) {
                response = computeService.runScriptOnNode(id, script, templateOptions.overrideLoginCredentials(credentials).runAsRoot(false));
            } else {
                response = computeService.runScriptOnNode(id, script, templateOptions);
            }
        } catch (AuthorizationException ex) {
            throw new Exception("Failed to connect to the container via ssh.");
        } catch (SshException ex) {
            throw new Exception("Failed to connect to the container via ssh.");
        }
        if (response != null && response.getOutput() != null) {
            if (response.getOutput().contains(ContainerProviderUtils.FAILURE_PREFIX)) {
                jCloudsContainerMetadata.setFailure(new Exception(ContainerProviderUtils.parseScriptFailure(response.getOutput())));
            }
            String overridenResolverValue = ContainerProviderUtils.parseResolverOverride(response.getOutput());
            if (overridenResolverValue != null) {
                jCloudsContainerMetadata.setOverridenResolver(overridenResolverValue);
                listener.onStateChange("Overriding resolver to " + overridenResolverValue + ".");
            }
        } else {
            jCloudsContainerMetadata.setFailure(new Exception("No response received for fabric install script."));
        }
    } catch (Throwable t) {
        jCloudsContainerMetadata.setFailure(t);
    }
    // Cleanup addresses.
    options.getSystemProperties().clear();
    return jCloudsContainerMetadata;
}
Also used : FirewallManager(io.fabric8.service.jclouds.firewall.FirewallManager) ExecResponse(org.jclouds.compute.domain.ExecResponse) AuthorizationException(org.jclouds.rest.AuthorizationException) FirewallNotSupportedOnProviderException(io.fabric8.service.jclouds.firewall.FirewallNotSupportedOnProviderException) IOException(java.io.IOException) SshException(org.jclouds.ssh.SshException) Properties(java.util.Properties) URL(java.net.URL) AuthorizationException(org.jclouds.rest.AuthorizationException) FirewallNotSupportedOnProviderException(io.fabric8.service.jclouds.firewall.FirewallNotSupportedOnProviderException) IOException(java.io.IOException) SshException(org.jclouds.ssh.SshException) LoginCredentials(org.jclouds.domain.LoginCredentials) Rule(io.fabric8.service.jclouds.firewall.Rule) HashSet(java.util.HashSet)

Example 53 with Build

use of io.fabric8.openshift.api.model.Build in project fabric8 by jboss-fuse.

the class CloudContainerInstallationTask method uploadToNode.

private void uploadToNode(ComputeServiceContext context, NodeMetadata node, LoginCredentials credentials, URL url, String path) {
    Utils utils = context.utils();
    SshClient ssh = credentials != null ? utils.sshForNode().apply(NodeMetadataBuilder.fromNodeMetadata(nodeMetadata).credentials(credentials).build()) : utils.sshForNode().apply(node);
    try (InputStream is = url.openStream()) {
        ssh.connect();
        File distro = Files.createTempFile("/tmp");
        Files.copy(is, new FileOutputStream(distro));
        ssh.put(path, Payloads.newFilePayload(distro));
        distro.delete();
    } catch (IOException e) {
        LOGGER.warn("Failed to upload. Will attempt downloading distribution via maven.");
    } finally {
        if (ssh != null) {
            ssh.disconnect();
        }
    }
}
Also used : SshClient(org.jclouds.ssh.SshClient) Utils(org.jclouds.compute.Utils) ContainerProviderUtils(io.fabric8.internal.ContainerProviderUtils) InputStream(java.io.InputStream) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) File(java.io.File)

Example 54 with Build

use of io.fabric8.openshift.api.model.Build in project fabric8 by jboss-fuse.

the class Subsystem method build.

@SuppressWarnings("InfiniteLoopStatement")
public void build(Collection<Feature> features) throws Exception {
    for (Subsystem child : children) {
        child.build(features);
    }
    if (feature != null) {
        for (Dependency dep : feature.getDependencies()) {
            Subsystem ss = this;
            while (!ss.isAcceptDependencies()) {
                ss = ss.getParent();
            }
            ss.requireFeature(dep.getName(), dep.getVersion(), !dep.isDependency());
        }
    }
    List<Requirement> processed = new ArrayList<>();
    while (true) {
        List<Requirement> requirements = getRequirements(IDENTITY_NAMESPACE);
        requirements.addAll(dependentFeatures);
        requirements.removeAll(processed);
        if (requirements.isEmpty()) {
            break;
        }
        for (Requirement requirement : requirements) {
            String name = (String) requirement.getAttributes().get(IDENTITY_NAMESPACE);
            String type = (String) requirement.getAttributes().get(CAPABILITY_TYPE_ATTRIBUTE);
            VersionRange range = (VersionRange) requirement.getAttributes().get(CAPABILITY_VERSION_ATTRIBUTE);
            if (TYPE_FEATURE.equals(type)) {
                for (Feature feature : features) {
                    if (feature.getName().equals(name) && (range == null || range.contains(VersionTable.getVersion(feature.getVersion())))) {
                        if (feature != this.feature) {
                            String ssName = this.name + "#" + (feature.hasVersion() ? feature.getName() + "-" + feature.getVersion() : feature.getName());
                            Subsystem fs = getChild(ssName);
                            if (fs == null) {
                                fs = new Subsystem(ssName, feature, this);
                                fs.build(features);
                                installable.add(fs);
                                children.add(fs);
                            }
                        }
                    }
                }
            }
            processed.add(requirement);
        }
    }
}
Also used : Requirement(org.osgi.resource.Requirement) ResourceUtils.toFeatureRequirement(io.fabric8.agent.resolver.ResourceUtils.toFeatureRequirement) ResourceUtils.addIdentityRequirement(io.fabric8.agent.resolver.ResourceUtils.addIdentityRequirement) ArrayList(java.util.ArrayList) VersionRange(org.apache.felix.utils.version.VersionRange) Dependency(io.fabric8.agent.model.Dependency) Feature(io.fabric8.agent.model.Feature)

Example 55 with Build

use of io.fabric8.openshift.api.model.Build in project fabric8 by jboss-fuse.

the class SubsystemResolver method prepare.

public void prepare(Collection<Feature> allFeatures, Map<String, Set<String>> requirements, Map<String, Set<BundleRevision>> system) throws Exception {
    // Build subsystems on the fly
    for (Map.Entry<String, Set<String>> entry : requirements.entrySet()) {
        String[] parts = entry.getKey().split("/");
        if (root == null) {
            root = new Subsystem(parts[0]);
        } else if (!root.getName().equals(parts[0])) {
            throw new IllegalArgumentException("Can not use multiple roots: " + root.getName() + ", " + parts[0]);
        }
        Subsystem ss = root;
        for (int i = 1; i < parts.length; i++) {
            ss = getOrCreateChild(ss, parts[i]);
        }
        for (String requirement : entry.getValue()) {
            ss.require(requirement);
        }
    }
    if (root == null) {
        return;
    }
    // Pre-resolve
    root.build(allFeatures);
    // Add system resources
    BundleRevision sysBundleRev = null;
    boolean hasEeCap = false;
    for (Map.Entry<String, Set<BundleRevision>> entry : system.entrySet()) {
        Subsystem ss = null;
        String[] parts = entry.getKey().split("/");
        String path = parts[0];
        if (path.equals(root.getName())) {
            ss = root;
        }
        for (int i = 1; ss != null && i < parts.length; i++) {
            path += "/" + parts[i];
            ss = ss.getChild(path);
        }
        if (ss != null) {
            ResourceImpl dummy = new ResourceImpl("dummy", "dummy", Version.emptyVersion);
            for (BundleRevision res : entry.getValue()) {
                // We need to explicitely provide service capabilities for bundles
                // We use both actual services and services declared from the headers
                // TODO: use actual services
                Map<String, String> headers = new DictionaryAsMap<>(res.getBundle().getHeaders());
                Resource tmp = ResourceBuilder.build(res.getBundle().getLocation(), headers);
                for (Capability cap : tmp.getCapabilities(ServiceNamespace.SERVICE_NAMESPACE)) {
                    dummy.addCapability(new CapabilityImpl(dummy, cap.getNamespace(), cap.getDirectives(), cap.getAttributes()));
                }
                ss.addSystemResource(res);
                for (Capability cap : res.getCapabilities(null)) {
                    hasEeCap |= cap.getNamespace().equals(EXECUTION_ENVIRONMENT_NAMESPACE);
                }
                if (res.getBundle().getBundleId() == 0) {
                    sysBundleRev = res;
                }
            }
            ss.addSystemResource(dummy);
        }
    }
    // Under Equinox, the osgi.ee capabilities are not provided by the system bundle
    if (!hasEeCap && sysBundleRev != null) {
        String provideCaps = sysBundleRev.getBundle().getHeaders().get(PROVIDE_CAPABILITY);
        environmentResource = new ResourceImpl("environment", "karaf.environment", Version.emptyVersion);
        environmentResource.addCapabilities(ResourceBuilder.parseCapability(environmentResource, provideCaps));
        root.addSystemResource(environmentResource);
    }
}
Also used : CapabilitySet(io.fabric8.agent.resolver.CapabilitySet) Set(java.util.Set) Capability(org.osgi.resource.Capability) DictionaryAsMap(org.apache.felix.utils.collections.DictionaryAsMap) Resource(org.osgi.resource.Resource) ResourceImpl(io.fabric8.agent.resolver.ResourceImpl) CapabilityImpl(io.fabric8.agent.resolver.CapabilityImpl) BundleRevision(org.osgi.framework.wiring.BundleRevision) HashMap(java.util.HashMap) DictionaryAsMap(org.apache.felix.utils.collections.DictionaryAsMap) Map(java.util.Map)

Aggregations

Test (org.junit.Test)255 ArrayList (java.util.ArrayList)74 BuildImageConfiguration (io.fabric8.maven.docker.config.BuildImageConfiguration)69 ImageConfiguration (io.fabric8.maven.docker.config.ImageConfiguration)68 HashMap (java.util.HashMap)66 File (java.io.File)53 ConfigMap (io.fabric8.kubernetes.api.model.ConfigMap)51 IOException (java.io.IOException)50 ConfigMapBuilder (io.fabric8.kubernetes.api.model.ConfigMapBuilder)45 Pod (io.fabric8.kubernetes.api.model.Pod)37 Map (java.util.Map)35 Service (io.fabric8.kubernetes.api.model.Service)34 FabricService (io.fabric8.api.FabricService)33 ResourceConfig (io.fabric8.maven.core.config.ResourceConfig)30 Container (io.fabric8.api.Container)29 RunImageConfiguration (io.fabric8.maven.docker.config.RunImageConfiguration)28 PodBuilder (io.fabric8.kubernetes.api.model.PodBuilder)27 List (java.util.List)26 ServicePortBuilder (io.fabric8.kubernetes.api.model.ServicePortBuilder)25 OpenShiftClient (io.fabric8.openshift.client.OpenShiftClient)25