Search in sources :

Example 51 with Container

use of io.fabric8.maven.docker.model.Container in project fabric8 by jboss-fuse.

the class ZookeeperPortService method findUsedPortByHost.

private Set<Integer> findUsedPortByHost(Container container, Lease existingLease) {
    assertValid();
    String ip = container.getIp();
    assertValidIp(container, ip);
    Set<Integer> ports = new HashSet<Integer>();
    String path = ZkPath.PORTS_IP.getPath(ip);
    Lease lease = null;
    try {
        if (existingLease != null) {
            lease = existingLease;
        } else {
            lease = interProcessLock.acquire(60, TimeUnit.SECONDS);
        }
        if (lease != null) {
            createDefault(curator.get(), path, "");
            String boundPorts = getStringData(curator.get(), path);
            if (boundPorts != null && !boundPorts.isEmpty()) {
                for (String port : boundPorts.split(" ")) {
                    try {
                        ports.add(Integer.parseInt(port.trim()));
                    } catch (NumberFormatException ex) {
                    // ignore
                    }
                }
            }
        } else {
            throw new FabricException("Could not acquire port lock");
        }
    } catch (InterruptedException ex) {
        cleanUpDirtyZKNodes(interProcessLock);
        throw FabricException.launderThrowable(ex);
    } catch (Exception ex) {
        throw FabricException.launderThrowable(ex);
    } finally {
        if (existingLease == null) {
            releaseLock(lease);
        }
    }
    return ports;
}
Also used : Lease(org.apache.curator.framework.recipes.locks.Lease) FabricException(io.fabric8.api.FabricException) FabricException(io.fabric8.api.FabricException) HashSet(java.util.HashSet)

Example 52 with Container

use of io.fabric8.maven.docker.model.Container in project fabric8 by jboss-fuse.

the class FabricCxfRegistrationHandler method registerApiEndpoint.

protected void registerApiEndpoint(Container container, ObjectName oName, String address, boolean started) {
    String actualEndpointUrl = null;
    try {
        String url;
        String id = container.getId();
        if (isFullAddress(address)) {
            url = toPublicAddress(id, address);
        } else {
            String cxfBus = getCxfServletPath(oName);
            url = "${zk:" + id + "/http}" + cxfBus + address;
        }
        actualEndpointUrl = ZooKeeperUtils.getSubstitutedData(curator.get(), url);
        // lets assume these locations are hard coded
        // may be nice to discover from JMX one day
        String apiDocPath = "/api-docs";
        String wsdlPath = "?wsdl";
        String wadlPath = "?_wadl";
        Version version = container.getVersion();
        String versionId = version != null ? version.getId() : null;
        String json = "{\"id\":" + JsonHelper.jsonEncodeString(id) + ", \"container\":" + JsonHelper.jsonEncodeString(id) + ", \"version\":" + JsonHelper.jsonEncodeString(versionId) + ", \"services\":[" + JsonHelper.jsonEncodeString(url) + "]" + ", \"objectName\":" + JsonHelper.jsonEncodeString(oName.toString()) + "";
        boolean rest = false;
        if (booleanAttribute(oName, "isWADL")) {
            rest = true;
            json += ", \"wadl\":" + JsonHelper.jsonEncodeString(wadlPath);
        }
        if (booleanAttribute(oName, "isSwagger")) {
            rest = true;
            json += ", \"apidocs\":" + JsonHelper.jsonEncodeString(apiDocPath);
        }
        if (booleanAttribute(oName, "isWSDL")) {
            json += ", \"wsdl\":" + JsonHelper.jsonEncodeString(wsdlPath);
        }
        json += "}";
        String path = getPath(container, oName, address, rest);
        LOGGER.info("Registered CXF API at " + path + " JSON: " + json);
        if (!started && !rest) {
            LOGGER.warn("Since the CXF service isn't started, this could really be a REST endpoint rather than WSDL at " + path);
        }
        registeredZkPaths.add(path);
        registeredUrls.put(oName.toString(), address);
        ZooKeeperUtils.setData(curator.get(), path, json, CreateMode.EPHEMERAL);
    } catch (Exception e) {
        LOGGER.error("Failed to register API endpoint for {}.", actualEndpointUrl, e);
    }
}
Also used : Version(io.fabric8.api.Version) URISyntaxException(java.net.URISyntaxException) BadBinaryOpValueExpException(javax.management.BadBinaryOpValueExpException) BadStringOperationException(javax.management.BadStringOperationException) MalformedObjectNameException(javax.management.MalformedObjectNameException) MalformedURLException(java.net.MalformedURLException) BadAttributeValueExpException(javax.management.BadAttributeValueExpException) IOException(java.io.IOException) InvalidApplicationException(javax.management.InvalidApplicationException)

Example 53 with Container

use of io.fabric8.maven.docker.model.Container in project fabric8 by jboss-fuse.

the class ZookeeperPortService method findUsedPortByContainer.

@Override
public Set<Integer> findUsedPortByContainer(Container container) {
    assertValid();
    Set<Integer> ports = new HashSet<Integer>();
    String path = ZkPath.PORTS_CONTAINER.getPath(container.getId());
    Lease lease = null;
    try {
        lease = interProcessLock.acquire(60, TimeUnit.SECONDS);
        if (lease != null) {
            if (exists(curator.get(), path) != null) {
                for (String pid : getChildren(curator.get(), path)) {
                    for (String key : getChildren(curator.get(), ZkPath.PORTS_CONTAINER_PID.getPath(container.getId(), pid))) {
                        String port = getStringData(curator.get(), ZkPath.PORTS_CONTAINER_PID_KEY.getPath(container.getId(), pid, key));
                        try {
                            ports.add(Integer.parseInt(port));
                        } catch (Exception ex) {
                        // ignore
                        }
                    }
                }
            }
        } else {
            throw new FabricException("Could not acquire port lock");
        }
    } catch (InterruptedException ex) {
        cleanUpDirtyZKNodes(interProcessLock);
        throw FabricException.launderThrowable(ex);
    } catch (Exception ex) {
        throw FabricException.launderThrowable(ex);
    } finally {
        releaseLock(lease);
    }
    return ports;
}
Also used : Lease(org.apache.curator.framework.recipes.locks.Lease) FabricException(io.fabric8.api.FabricException) FabricException(io.fabric8.api.FabricException) HashSet(java.util.HashSet)

Example 54 with Container

use of io.fabric8.maven.docker.model.Container in project fabric8 by jboss-fuse.

the class ChildContainerProvider method createKarafContainerController.

private ChildContainerController createKarafContainerController() {
    return new ChildContainerController() {

        @Override
        public CreateChildContainerMetadata create(final CreateChildContainerOptions options, final CreationStateListener listener) {
            final Container parent = fabricService.get().getContainer(options.getParent());
            ContainerTemplate containerTemplate = new ContainerTemplate(parent, options.getJmxUser(), options.getJmxPassword(), false);
            return containerTemplate.execute(new ContainerTemplate.AdminServiceCallback<CreateChildContainerMetadata>() {

                public CreateChildContainerMetadata doWithAdminService(AdminServiceMBean adminService) throws Exception {
                    return doCreateKaraf(adminService, options, listener, parent);
                }
            });
        }

        @Override
        public void start(final Container container) {
            getContainerTemplateForChild(container).execute(new ContainerTemplate.AdminServiceCallback<Object>() {

                public Object doWithAdminService(AdminServiceMBean adminService) throws Exception {
                    // update jvm options if they have have changed
                    CreateChildContainerMetadata metadata = (CreateChildContainerMetadata) container.getMetadata();
                    CreateChildContainerOptions createOptions = metadata.getCreateOptions();
                    String jvmOpts = createOptions.getJvmOpts();
                    TabularData instances = adminService.getInstances();
                    Collection<CompositeDataSupport> values = (Collection<CompositeDataSupport>) instances.values();
                    for (CompositeDataSupport o : values) {
                        if (container.getId().equals(o.get("Name"))) {
                            if (o.containsKey("JavaOpts")) {
                                String oldJavaOpts = (String) o.get("JavaOpts");
                                StringBuilder stringBuilder = ChildContainerProvider.buildJvmOpts(createOptions, fabricService.get());
                                String extendendJvmOpts = stringBuilder.toString();
                                if (jvmOpts != null && !extendendJvmOpts.equals(oldJavaOpts)) {
                                    adminService.changeJavaOpts(container.getId(), extendendJvmOpts);
                                }
                            }
                            break;
                        }
                    }
                    adminService.startInstance(container.getId(), null);
                    return null;
                }
            });
        }

        @Override
        public void stop(final Container container) {
            getContainerTemplateForChild(container).execute(new ContainerTemplate.AdminServiceCallback<Object>() {

                public Object doWithAdminService(AdminServiceMBean adminService) throws Exception {
                    String prevProvisionResult = container.getProvisionResult();
                    container.setProvisionResult(Container.PROVISION_STOPPING);
                    try {
                        adminService.stopInstance(container.getId());
                        container.setProvisionResult(Container.PROVISION_STOPPED);
                    } catch (Throwable t) {
                        container.setProvisionResult(prevProvisionResult);
                        LOG.error("Failed to stop container: " + container.getId(), t);
                        throw t;
                    }
                    return null;
                }
            });
        }

        @Override
        public void destroy(final Container container) {
            getContainerTemplateForChild(container).execute(new ContainerTemplate.AdminServiceCallback<Object>() {

                public Object doWithAdminService(AdminServiceMBean adminService) throws Exception {
                    adminService.destroyInstance(container.getId());
                    return null;
                }
            });
        }
    };
}
Also used : CompositeDataSupport(javax.management.openmbean.CompositeDataSupport) AdminServiceMBean(org.apache.karaf.admin.management.AdminServiceMBean) TabularData(javax.management.openmbean.TabularData) ContainerTemplate(io.fabric8.service.ContainerTemplate) Collection(java.util.Collection)

Example 55 with Container

use of io.fabric8.maven.docker.model.Container in project fabric8 by jboss-fuse.

the class FabricServiceImpl method destroyContainer.

public void destroyContainer(String containerId, boolean force) {
    assertValid();
    Container container = getContainer(containerId);
    if (container != null) {
        destroyContainer(container, force);
    }
}
Also used : Container(io.fabric8.api.Container)

Aggregations

Container (io.fabric8.api.Container)139 Test (org.junit.Test)75 FabricService (io.fabric8.api.FabricService)56 ArrayList (java.util.ArrayList)39 Container (io.fabric8.kubernetes.api.model.Container)38 IOException (java.io.IOException)38 Profile (io.fabric8.api.Profile)37 HashMap (java.util.HashMap)34 Map (java.util.Map)30 FabricException (io.fabric8.api.FabricException)27 BundleContext (org.osgi.framework.BundleContext)24 Version (io.fabric8.api.Version)23 File (java.io.File)23 HashSet (java.util.HashSet)20 LinkedList (java.util.LinkedList)17 PodSpec (io.fabric8.kubernetes.api.model.PodSpec)16 ImageConfiguration (io.fabric8.maven.docker.config.ImageConfiguration)16 CreateContainerMetadata (io.fabric8.api.CreateContainerMetadata)15 Pod (io.fabric8.kubernetes.api.model.Pod)12 List (java.util.List)12