use of org.eclipse.che.api.machine.server.model.impl.ServerConfImpl in project che by eclipse.
the class CheEnvironmentEngine method addAgentsProvidedServers.
private void addAgentsProvidedServers(MachineImpl machine, List<String> agentKeys) throws ServerException {
for (String agentKey : agentKeys) {
try {
AgentImpl agent = new AgentImpl(agentRegistry.getAgent(AgentKeyImpl.parse(agentKey)));
for (Map.Entry<String, ? extends ServerConf2> entry : agent.getServers().entrySet()) {
String ref = entry.getKey();
ServerConf2 conf2 = entry.getValue();
ServerConfImpl conf = new ServerConfImpl(ref, conf2.getPort(), conf2.getProtocol(), conf2.getProperties().get("path"));
machine.getConfig().getServers().add(conf);
}
} catch (AgentException e) {
throw new ServerException(e);
}
}
}
use of org.eclipse.che.api.machine.server.model.impl.ServerConfImpl in project che by eclipse.
the class ServerEvaluationStrategyTest method shouldAddRefUrlProtocolPathToServerFromMachineConfig.
@Test
public void shouldAddRefUrlProtocolPathToServerFromMachineConfig() throws Exception {
// given
prepareStrategyAndContainerInfoMocks();
serverConfs.put("8080/tcp", new ServerConfImpl("myserv1", "8080/tcp", "http", null));
serverConfs.put("9090/udp", new ServerConfImpl("myserv2", "9090/udp", "dhcp", "/some/path"));
final HashMap<String, ServerImpl> expectedServers = new HashMap<>();
expectedServers.put("8080/tcp", new ServerImpl("myserv1", "http", DEFAULT_HOSTNAME + ":32100", "http://" + DEFAULT_HOSTNAME + ":32100", new ServerPropertiesImpl(null, DEFAULT_HOSTNAME + ":32100", "http://" + DEFAULT_HOSTNAME + ":32100")));
expectedServers.put("9090/udp", new ServerImpl("myserv2", "dhcp", DEFAULT_HOSTNAME + ":32101", "dhcp://" + DEFAULT_HOSTNAME + ":32101/some/path", new ServerPropertiesImpl("/some/path", DEFAULT_HOSTNAME + ":32101", "dhcp://" + DEFAULT_HOSTNAME + ":32101/some/path")));
// when
final Map<String, ServerImpl> servers = strategy.getServers(containerInfo, DEFAULT_HOSTNAME, serverConfs);
// then
assertEquals(servers, expectedServers);
}
use of org.eclipse.che.api.machine.server.model.impl.ServerConfImpl in project che by eclipse.
the class DockerInstanceRuntimeInfoTest method shouldPassCommonServerConfigsOnGetServersForNonDevMachine.
@Test
public void shouldPassCommonServerConfigsOnGetServersForNonDevMachine() throws Exception {
// given
Set<ServerConf> commonSystemServersConfigs = new HashSet<>();
commonSystemServersConfigs.add(new ServerConfImpl("sysServer1-tcp", "4301/tcp", "http", "/some/path"));
commonSystemServersConfigs.add(new ServerConfImpl("sysServer2-udp", "4302/udp", "dhcp", null));
commonSystemServersConfigs.add(new ServerConfImpl("sysServer1-udp", "4301/udp", null, "some/path"));
Set<ServerConf> devSystemServersConfigs = singleton(new ServerConfImpl("devSysServer1-tcp", "4305/tcp", "http", null));
List<ServerConf> serversConfFromMachineConf = singletonList(new ServerConfImpl("machineConfServer1-tcp", "4306/tcp", "http", null));
when(machineConfig.getServers()).thenAnswer(invocation -> serversConfFromMachineConf);
when(machineConfig.isDev()).thenReturn(false);
runtimeInfo = new DockerInstanceRuntimeInfo(containerInfo, machineConfig, DEFAULT_HOSTNAME, provider, devSystemServersConfigs, commonSystemServersConfigs);
// when
Map<String, ServerImpl> servers = runtimeInfo.getServers();
// then
assertEquals(servers, serversMap);
verify(serverEvaluationStrategy).getServers(eq(containerInfo), eq(DEFAULT_HOSTNAME), serversCaptor.capture());
assertEquals(serversCaptor.getValue(), serversToMap(commonSystemServersConfigs, serversConfFromMachineConf));
}
use of org.eclipse.che.api.machine.server.model.impl.ServerConfImpl in project che by eclipse.
the class ServerEvaluationStrategy method getServers.
/**
* Constructs a map of {@link ServerImpl} from provided parameters, using selected strategy
* for evaluating addresses and ports.
*
* <p>Keys consist of port number and transport protocol (tcp or udp) separated by
* a forward slash (e.g. 8080/tcp)
*
* @param containerInfo
* the {@link ContainerInfo} describing the container.
* @param internalHost
* alternative hostname to use, if address cannot be obtained from containerInfo
* @param serverConfMap
* additional Map of {@link ServerConfImpl}. Configurations here override those found
* in containerInfo.
* @return a Map of the servers exposed by the container.
*/
public Map<String, ServerImpl> getServers(ContainerInfo containerInfo, String internalHost, Map<String, ServerConfImpl> serverConfMap) {
Map<String, List<PortBinding>> portBindings;
Map<String, String> labels = Collections.emptyMap();
if (containerInfo.getNetworkSettings() != null && containerInfo.getNetworkSettings().getPorts() != null) {
portBindings = containerInfo.getNetworkSettings().getPorts();
} else {
// If we can't get PortBindings, we can't return servers.
return Collections.emptyMap();
}
if (containerInfo.getConfig() != null && containerInfo.getConfig().getLabels() != null) {
labels = containerInfo.getConfig().getLabels();
}
Map<String, String> internalAddressesAndPorts = getInternalAddressesAndPorts(containerInfo, internalHost);
Map<String, String> externalAddressesAndPorts = getExternalAddressesAndPorts(containerInfo, internalHost);
Map<String, ServerImpl> servers = new LinkedHashMap<>();
for (String portProtocol : portBindings.keySet()) {
String internalAddressAndPort = internalAddressesAndPorts.get(portProtocol);
String externalAddressAndPort = externalAddressesAndPorts.get(portProtocol);
ServerConfImpl serverConf = getServerConfImpl(portProtocol, labels, serverConfMap);
// Add protocol and path to internal/external address, if applicable
String internalUrl = null;
String externalUrl = null;
if (serverConf.getProtocol() != null) {
String pathSuffix = serverConf.getPath();
if (pathSuffix != null && !pathSuffix.isEmpty()) {
if (pathSuffix.charAt(0) != '/') {
pathSuffix = "/" + pathSuffix;
}
} else {
pathSuffix = "";
}
internalUrl = serverConf.getProtocol() + "://" + internalAddressAndPort + pathSuffix;
externalUrl = serverConf.getProtocol() + "://" + externalAddressAndPort + pathSuffix;
}
ServerProperties properties = new ServerPropertiesImpl(serverConf.getPath(), internalAddressAndPort, internalUrl);
servers.put(portProtocol, new ServerImpl(serverConf.getRef(), serverConf.getProtocol(), externalAddressAndPort, externalUrl, properties));
}
return servers;
}
use of org.eclipse.che.api.machine.server.model.impl.ServerConfImpl in project che by eclipse.
the class ServerEvaluationStrategy method getServerConfImpl.
/**
* Gets the {@link ServerConfImpl} object associated with {@code portProtocol}.
* The provided labels should have keys matching e.g.
*
* <p>{@code che:server:<portProtocol>:[ref|path|protocol]}
*
* @param portProtocol
* the port binding associated with the server
* @param labels
* a map holding the relevant values for reference, protocol, and path
* for the given {@code portProtocol}
* @param serverConfMap
* a map of {@link ServerConfImpl} with {@code portProtocol} as
* keys.
* @return {@code ServerConfImpl}, obtained from {@code serverConfMap} if possible,
* or from {@code labels} if there is no entry in {@code serverConfMap}.
*/
private ServerConfImpl getServerConfImpl(String portProtocol, Map<String, String> labels, Map<String, ServerConfImpl> serverConfMap) {
// Label can be specified without protocol -- e.g. 4401 refers to 4401/tcp
String port = portProtocol.substring(0, portProtocol.length() - 4);
ServerConfImpl serverConf;
// provided serverConf map takes precedence
if (serverConfMap.get(portProtocol) != null) {
serverConf = serverConfMap.get(portProtocol);
} else if (serverConfMap.get(port) != null) {
serverConf = serverConfMap.get(port);
} else {
String ref, protocol, path;
ref = labels.get(String.format(SERVER_CONF_LABEL_REF_KEY, portProtocol));
if (ref == null) {
ref = labels.get(String.format(SERVER_CONF_LABEL_REF_KEY, port));
}
protocol = labels.get(String.format(SERVER_CONF_LABEL_PROTOCOL_KEY, portProtocol));
if (protocol == null) {
protocol = labels.get(String.format(SERVER_CONF_LABEL_PROTOCOL_KEY, port));
}
path = labels.get(String.format(SERVER_CONF_LABEL_PATH_KEY, portProtocol));
if (path == null) {
path = labels.get(String.format(SERVER_CONF_LABEL_PATH_KEY, port));
}
serverConf = new ServerConfImpl(ref, portProtocol, protocol, path);
}
if (serverConf.getRef() == null) {
// Add default reference to server if it was not set above
serverConf.setRef("Server-" + portProtocol.replace('/', '-'));
}
return serverConf;
}
Aggregations