Search in sources :

Example 51 with Config

use of io.fabric8.docker.client.Config in project fabric8 by jboss-fuse.

the class JsonRuleBaseReader method parseJson.

/**
 * Will try to parse the {@link InputStream} which is expected to be in the following
 * JSON format:
 * <pre>
 * { "rulebase" : [
 *    { "rule": "/foo/{path}", "to": "https://foo.com/cheese/{path}"},
 *    { "rule": "/customers/{id}/address/{addressId}", "to": "http://another.com/addresses/{addressId}/customer/{id}"}
 *  ]
 * }
 * </pre>
 *
 * <strong>Note that the passed-in {@link InputStream} will be closed by this method</strong>. This
 * is a little unusual as normally the closing is the responsibility of the party that created the
 * InputStream, but in this case we decided handling this is more user friendly.
 *
 * @param in the {@link InputStream} stream to read.
 * @return {@code Map} where the key maps to the 'rule' in the JSON, and the value maps to 'to'.
 */
public static Map<String, HttpProxyRule> parseJson(InputStream in) {
    chechNotNull(in);
    HashMap<String, HttpProxyRule> map = new HashMap<String, HttpProxyRule>();
    try {
        JsonNode config = OM.readTree(in);
        JsonNode globalCookiePath = config.get("cookiePath");
        JsonNode globalDomain = config.get("cookieDomain");
        for (JsonNode entry : getRuleBase(config)) {
            String rule = entry.get("rule").asText();
            map.put(rule, new HttpProxyRule(rule).to(entry.get("to").asText()).setCookiePath(getGlobal(entry, globalCookiePath, "cookiePath")).setCookieDomain(getGlobal(entry, globalDomain, "cookieDomain")));
        }
        return map;
    } catch (IOException e) {
        throw new RuntimeException(e.getMessage(), e);
    } finally {
        safeClose(in);
    }
}
Also used : HashMap(java.util.HashMap) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException) HttpProxyRule(io.fabric8.gateway.model.HttpProxyRule)

Example 52 with Config

use of io.fabric8.docker.client.Config in project fabric8 by jboss-fuse.

the class KarafContainerRegistration method configurationEvent.

/**
 * Receives notification of a Configuration that has changed.
 *
 * @param event The <code>ConfigurationEvent</code>.
 */
@Override
public void configurationEvent(ConfigurationEvent event) {
    if (isValid()) {
        try {
            Container current = new ImmutableContainerBuilder().id(runtimeIdentity).ip(ip).build();
            RuntimeProperties sysprops = runtimeProperties.get();
            String runtimeIdentity = sysprops.getRuntimeIdentity();
            if (event.getPid().equals(SSH_PID) && event.getType() == ConfigurationEvent.CM_UPDATED) {
                Configuration config = configAdmin.get().getConfiguration(SSH_PID, null);
                int sshPort = Integer.parseInt((String) config.getProperties().get(SSH_BINDING_PORT_KEY));
                int sshConnectionPort = getSshConnectionPort(current, sshPort);
                String sshUrl = getSshUrl(runtimeIdentity, sshConnectionPort);
                setData(curator.get(), CONTAINER_SSH.getPath(runtimeIdentity), sshUrl);
                if (portService.get().lookupPort(current, SSH_PID, SSH_BINDING_PORT_KEY) != sshPort) {
                    portService.get().unregisterPort(current, SSH_PID);
                    portService.get().registerPort(current, SSH_PID, SSH_BINDING_PORT_KEY, sshPort);
                }
            }
            if (event.getPid().equals(HTTP_PID) && event.getType() == ConfigurationEvent.CM_UPDATED) {
                Configuration config = configAdmin.get().getConfiguration(HTTP_PID, null);
                boolean httpEnabled = isHttpEnabled();
                boolean httpsEnabled = isHttpsEnabled();
                String protocol = httpsEnabled && !httpEnabled ? "https" : "http";
                int httpConnectionPort = -1;
                if (httpEnabled) {
                    int httpPort = Integer.parseInt((String) config.getProperties().get(HTTP_BINDING_PORT_KEY));
                    httpConnectionPort = getHttpConnectionPort(current, httpPort);
                    if (portService.get().lookupPort(current, HTTP_PID, HTTP_BINDING_PORT_KEY) != httpPort) {
                        portService.get().unregisterPort(current, HTTP_PID, HTTP_BINDING_PORT_KEY);
                        portService.get().registerPort(current, HTTP_PID, HTTP_BINDING_PORT_KEY, httpPort);
                    }
                }
                if (httpsEnabled) {
                    int httpsPort = Integer.parseInt((String) config.getProperties().get(HTTPS_BINDING_PORT_KEY));
                    if (httpConnectionPort == -1) {
                        httpConnectionPort = getHttpsConnectionPort(current, httpsPort);
                    }
                    if (portService.get().lookupPort(current, HTTP_PID, HTTPS_BINDING_PORT_KEY) != httpsPort) {
                        portService.get().unregisterPort(current, HTTP_PID, HTTPS_BINDING_PORT_KEY);
                        portService.get().registerPort(current, HTTP_PID, HTTPS_BINDING_PORT_KEY, httpsPort);
                    }
                }
                String httpUrl = getHttpUrl(protocol, runtimeIdentity, httpConnectionPort);
                setData(curator.get(), CONTAINER_HTTP.getPath(runtimeIdentity), httpUrl);
            }
            if (event.getPid().equals(MANAGEMENT_PID) && event.getType() == ConfigurationEvent.CM_UPDATED) {
                Configuration config = configAdmin.get().getConfiguration(MANAGEMENT_PID, null);
                int rmiServerPort = Integer.parseInt((String) config.getProperties().get(RMI_SERVER_BINDING_PORT_KEY));
                int rmiServerConnectionPort = getRmiServerConnectionPort(current, rmiServerPort);
                int rmiRegistryPort = Integer.parseInt((String) config.getProperties().get(RMI_REGISTRY_BINDING_PORT_KEY));
                int rmiRegistryConnectionPort = getRmiRegistryConnectionPort(current, rmiRegistryPort);
                String jmxUrl = getJmxUrl(runtimeIdentity, rmiServerConnectionPort, rmiRegistryConnectionPort);
                setData(curator.get(), CONTAINER_JMX.getPath(runtimeIdentity), jmxUrl);
                // Whenever the JMX URL changes we need to make sure that the java.rmi.server.hostname points to a valid address.
                System.setProperty(SystemProperties.JAVA_RMI_SERVER_HOSTNAME, current.getIp());
                if (portService.get().lookupPort(current, MANAGEMENT_PID, RMI_REGISTRY_BINDING_PORT_KEY) != rmiRegistryPort || portService.get().lookupPort(current, MANAGEMENT_PID, RMI_SERVER_BINDING_PORT_KEY) != rmiServerPort) {
                    portService.get().unregisterPort(current, MANAGEMENT_PID);
                    portService.get().registerPort(current, MANAGEMENT_PID, RMI_SERVER_BINDING_PORT_KEY, rmiServerPort);
                    portService.get().registerPort(current, MANAGEMENT_PID, RMI_REGISTRY_BINDING_PORT_KEY, rmiRegistryPort);
                }
            }
        } catch (Exception ex) {
            LOGGER.error("Cannot reconfigure container", ex);
        }
    }
}
Also used : Container(io.fabric8.api.Container) BootstrapConfiguration(io.fabric8.zookeeper.bootstrap.BootstrapConfiguration) Configuration(org.osgi.service.cm.Configuration) ImmutableContainerBuilder(io.fabric8.internal.ImmutableContainerBuilder) RuntimeProperties(io.fabric8.api.RuntimeProperties) KeeperException(org.apache.zookeeper.KeeperException) IOException(java.io.IOException)

Example 53 with Config

use of io.fabric8.docker.client.Config in project fabric8 by jboss-fuse.

the class KarafContainerRegistration method getOrAllocatePortForKey.

/**
 * Returns a port number for the use in the specified pid and key.
 * If the port is already registered it is directly returned. Else the {@link ConfigurationAdmin} or a default value is used.
 * In the later case, the port will be checked against the already registered ports and will be increased, till it doesn't match the used ports.
 */
private int getOrAllocatePortForKey(Container container, String pid, String key, int defaultValue, PortService.Lock lock) throws IOException, KeeperException, InterruptedException {
    Configuration config = configAdmin.get().getConfiguration(pid, null);
    Set<Integer> unavailable = portService.get().findUsedPortByHost(container, lock);
    int port = portService.get().lookupPort(container, pid, key);
    if (port > 0) {
        return port;
    } else if (config.getProperties() != null && config.getProperties().get(key) != null) {
        try {
            port = Integer.parseInt((String) config.getProperties().get(key));
        } catch (NumberFormatException ex) {
            port = defaultValue;
        }
    } else {
        port = defaultValue;
    }
    while (unavailable.contains(port)) {
        port++;
    }
    return port;
}
Also used : BootstrapConfiguration(io.fabric8.zookeeper.bootstrap.BootstrapConfiguration) Configuration(org.osgi.service.cm.Configuration)

Example 54 with Config

use of io.fabric8.docker.client.Config in project watchdog by isdream.

the class OpenshiftListenerTest method main.

public static void main(String[] args) {
    // Configure config = new Parser().parse("config/podWatcher.yaml");
    Configure config = null;
    try {
        config = new Parser().parse("config/podWatcher.yaml");
        OpenShiftClient client = getClient("https", "master.example.com", "8443", "NaD95GPF8rIpbphyCyep--pJQH7t3pzBBV9aXQE77O4");
        Listener listener = new OpenShiftListener();
        listener.start(client, config);
        Thread.sleep(600000);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : OpenShiftListener(com.github.isdream.cwatcher.listeners.OpenShiftListener) OpenShiftListener(com.github.isdream.cwatcher.listeners.OpenShiftListener) DefaultOpenShiftClient(io.fabric8.openshift.client.DefaultOpenShiftClient) OpenShiftClient(io.fabric8.openshift.client.OpenShiftClient) FileNotFoundException(java.io.FileNotFoundException) FileNotFoundException(java.io.FileNotFoundException)

Example 55 with Config

use of io.fabric8.docker.client.Config in project fabric8-maven-plugin by fabric8io.

the class DockerBuildServiceTest method testSuccessfulBuild.

@Test
public void testSuccessfulBuild() throws Exception {
    new Expectations() {

        {
            hub.getBuildService();
            result = buildService;
        }
    };
    final BuildService.BuildContext context = new BuildService.BuildContext.Builder().build();
    final io.fabric8.maven.core.service.BuildService.BuildServiceConfig config = new io.fabric8.maven.core.service.BuildService.BuildServiceConfig.Builder().dockerBuildContext(context).build();
    final String imageName = "image-name";
    final ImageConfiguration image = new ImageConfiguration.Builder().name(imageName).buildConfig(new BuildImageConfiguration.Builder().from("from").build()).build();
    DockerBuildService service = new DockerBuildService(hub, config);
    service.build(image);
    new FullVerificationsInOrder() {

        {
            buildService.buildImage(image, context);
            buildService.tagImage(imageName, image);
        }
    };
}
Also used : Expectations(mockit.Expectations) BuildService(io.fabric8.maven.docker.service.BuildService) FullVerificationsInOrder(mockit.FullVerificationsInOrder) BuildImageConfiguration(io.fabric8.maven.docker.config.BuildImageConfiguration) ImageConfiguration(io.fabric8.maven.docker.config.ImageConfiguration) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)106 BuildImageConfiguration (io.fabric8.maven.docker.config.BuildImageConfiguration)37 HashMap (java.util.HashMap)34 IOException (java.io.IOException)32 ResourceConfig (io.fabric8.maven.core.config.ResourceConfig)28 File (java.io.File)26 ConfigMap (io.fabric8.kubernetes.api.model.ConfigMap)24 Map (java.util.Map)24 ProcessorConfig (io.fabric8.maven.core.config.ProcessorConfig)23 ImageConfiguration (io.fabric8.maven.docker.config.ImageConfiguration)21 Expectations (mockit.Expectations)20 DefaultKubernetesClient (io.fabric8.kubernetes.client.DefaultKubernetesClient)17 ArrayList (java.util.ArrayList)17 VolumeConfig (io.fabric8.maven.core.config.VolumeConfig)15 AbstractConfigHandlerTest (io.fabric8.maven.docker.config.handler.AbstractConfigHandlerTest)15 ConfigMapBuilder (io.fabric8.kubernetes.api.model.ConfigMapBuilder)14 AuthConfig (io.fabric8.maven.docker.access.AuthConfig)13 DeploymentConfig (io.fabric8.openshift.api.model.DeploymentConfig)12 Before (org.junit.Before)12 KubernetesClient (io.fabric8.kubernetes.client.KubernetesClient)11