use of io.fabric8.docker.client.Config in project fabric8 by jboss-fuse.
the class ServiceFactoryTest method testDiscoveryOnRestartCurator.
@Test
public void testDiscoveryOnRestartCurator() throws Exception {
underTest = new ActiveMQServiceFactory();
underTest.curator = curator;
Properties props = new Properties();
props.put("config", "amq.xml");
props.put("broker-name", "amq");
props.put("group", "amq");
props.put("connectors", "openwire");
props.put("openwire-port", "0");
props.put("container.ip", "localhost");
underTest.updated("b", props);
final AtomicReference<CuratorFramework> curatorFrameworkAtomicReference = new AtomicReference<>(curator);
OsgiFabricDiscoveryAgent osgiFabricDiscoveryAgent = new OsgiFabricDiscoveryAgent(new BundleContext() {
@Override
public String getProperty(String s) {
return null;
}
@Override
public Bundle getBundle() {
return null;
}
@Override
public Bundle installBundle(String s, InputStream inputStream) throws BundleException {
return null;
}
@Override
public Bundle installBundle(String s) throws BundleException {
return null;
}
@Override
public Bundle getBundle(long l) {
return null;
}
@Override
public Bundle[] getBundles() {
return new Bundle[0];
}
@Override
public void addServiceListener(ServiceListener serviceListener, String s) throws InvalidSyntaxException {
}
@Override
public void addServiceListener(ServiceListener serviceListener) {
}
@Override
public void removeServiceListener(ServiceListener serviceListener) {
}
@Override
public void addBundleListener(BundleListener bundleListener) {
}
@Override
public void removeBundleListener(BundleListener bundleListener) {
}
@Override
public void addFrameworkListener(FrameworkListener frameworkListener) {
}
@Override
public void removeFrameworkListener(FrameworkListener frameworkListener) {
}
@Override
public ServiceRegistration<?> registerService(String[] strings, Object o, Dictionary<String, ?> dictionary) {
return null;
}
@Override
public ServiceRegistration<?> registerService(String s, Object o, Dictionary<String, ?> dictionary) {
return null;
}
@Override
public <S> ServiceRegistration<S> registerService(Class<S> aClass, S s, Dictionary<String, ?> dictionary) {
return null;
}
@Override
public ServiceReference<?>[] getServiceReferences(String s, String s1) throws InvalidSyntaxException {
return new ServiceReference<?>[0];
}
@Override
public ServiceReference<?>[] getAllServiceReferences(String s, String s1) throws InvalidSyntaxException {
return new ServiceReference<?>[0];
}
@Override
public ServiceReference<?> getServiceReference(String s) {
return null;
}
@Override
public <S> ServiceReference<S> getServiceReference(Class<S> aClass) {
return null;
}
@Override
public <S> Collection<ServiceReference<S>> getServiceReferences(Class<S> aClass, String s) throws InvalidSyntaxException {
return null;
}
@Override
public <S> S getService(ServiceReference<S> serviceReference) {
return (S) curatorFrameworkAtomicReference.get();
}
@Override
public boolean ungetService(ServiceReference<?> serviceReference) {
return false;
}
@Override
public File getDataFile(String s) {
return null;
}
@Override
public Filter createFilter(String s) throws InvalidSyntaxException {
return null;
}
@Override
public Bundle getBundle(String s) {
return null;
}
});
final LinkedBlockingQueue<DiscoveryEvent> discoveryEvents = new LinkedBlockingQueue<DiscoveryEvent>(10);
osgiFabricDiscoveryAgent.setDiscoveryListener(new DiscoveryListener() {
@Override
public void onServiceAdd(DiscoveryEvent discoveryEvent) {
discoveryEvents.offer(discoveryEvent);
}
@Override
public void onServiceRemove(DiscoveryEvent discoveryEvent) {
}
});
// will call into dummy bundle and get curator
osgiFabricDiscoveryAgent.addingService(null);
osgiFabricDiscoveryAgent.setGroupName("amq");
osgiFabricDiscoveryAgent.start();
DiscoveryEvent event = discoveryEvents.poll(5, TimeUnit.SECONDS);
LOG.info("event: " + event);
assertNotNull("got added service", event);
underTest.deleted("b");
// swap curator ref
CuratorFrameworkFactory.Builder builder = CuratorFrameworkFactory.builder().connectString("localhost:" + zkPort).sessionTimeoutMs(15000).retryPolicy(new RetryNTimes(5000, 1000));
curator = builder.build();
LOG.debug("Starting new curator " + curator);
curator.start();
curatorFrameworkAtomicReference.get().close();
curatorFrameworkAtomicReference.set(curator);
// will call into dummy bundle and get new curator ref
osgiFabricDiscoveryAgent.addingService(null);
// start broker again
underTest.curator = curator;
underTest.updated("b", props);
event = discoveryEvents.poll(5, TimeUnit.SECONDS);
LOG.info("new event: " + event);
assertNotNull("got newly added service", event);
underTest.deleted("b");
}
use of io.fabric8.docker.client.Config in project fabric8 by jboss-fuse.
the class DeploymentAgentTest method testFeatureRepoResolution.
@Test
@SuppressWarnings("unchecked")
public void testFeatureRepoResolution() throws Exception {
CommandSupport.executeCommand("fabric:create --force --clean -n --wait-for-provisioning");
// We are just want to use a feature repository that is not part of the distribution.
CommandSupport.executeCommand("fabric:profile-create --parent feature-camel test-profile");
CommandSupport.executeCommand("fabric:version-create --parent 1.0 1.1");
CommandSupport.executeCommand("fabric:profile-edit --repository mvn:io.fabric8.examples.fabric-camel-dosgi/features/" + System.getProperty("fabric.version") + "/xml/features test-profile 1.1");
CommandSupport.executeCommand("fabric:profile-edit --feature fabric-dosgi test-profile 1.1");
// We remove all repositories from agent config but the maven central to rely on the fabric-maven-proxy.
// Also remove local repository
CommandSupport.executeCommand("fabric:profile-edit --pid io.fabric8.agent/org.ops4j.pax.url.mvn.repositories=http://repo1.maven.org/maven2@id=m2central default 1.1");
CommandSupport.executeCommand("fabric:profile-edit --pid test-profile 1.1");
BundleContext moduleContext = ServiceLocator.getSystemContext();
ServiceProxy<FabricService> fabricProxy = ServiceProxy.createServiceProxy(moduleContext, FabricService.class);
try {
FabricService fabricService = fabricProxy.getService();
Set<Container> containers = ContainerBuilder.create().withName("smoke_cnt_a").withProfiles("test-profile").assertProvisioningResult().build(fabricService);
try {
// We want to remove all repositories from fabric-agent.
for (Container container : containers) {
CommandSupport.executeCommand("fabric:container-upgrade 1.1 " + container.getId());
System.out.flush();
}
ProvisionSupport.provisioningSuccess(containers, ProvisionSupport.PROVISION_TIMEOUT);
CommandSupport.executeCommand("fabric:container-list");
for (Container container : containers) {
CommandSupport.executeCommand("fabric:container-connect -u admin -p admin " + container.getId() + " osgi:list");
CommandSupport.executeCommand("fabric:container-connect -u admin -p admin " + container.getId() + " config:proplist --pid org.ops4j.pax.url.mvn");
System.out.flush();
}
} finally {
ContainerBuilder.stop(fabricService, containers);
}
} finally {
fabricProxy.close();
}
}
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);
}
}
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);
}
}
}
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;
}
Aggregations