Search in sources :

Example 1 with ServerConfImpl

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);
        }
    }
}
Also used : ServerException(org.eclipse.che.api.core.ServerException) AgentException(org.eclipse.che.api.agent.server.exception.AgentException) AgentImpl(org.eclipse.che.api.agent.shared.model.impl.AgentImpl) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ServerConfImpl(org.eclipse.che.api.machine.server.model.impl.ServerConfImpl) ServerConf2(org.eclipse.che.api.core.model.workspace.ServerConf2)

Example 2 with ServerConfImpl

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);
}
Also used : ServerImpl(org.eclipse.che.api.machine.server.model.impl.ServerImpl) HashMap(java.util.HashMap) ServerPropertiesImpl(org.eclipse.che.api.machine.server.model.impl.ServerPropertiesImpl) ServerConfImpl(org.eclipse.che.api.machine.server.model.impl.ServerConfImpl) Test(org.testng.annotations.Test)

Example 3 with ServerConfImpl

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));
}
Also used : ServerConf(org.eclipse.che.api.core.model.machine.ServerConf) ServerImpl(org.eclipse.che.api.machine.server.model.impl.ServerImpl) Matchers.anyString(org.mockito.Matchers.anyString) ServerConfImpl(org.eclipse.che.api.machine.server.model.impl.ServerConfImpl) HashSet(java.util.HashSet) Test(org.testng.annotations.Test)

Example 4 with ServerConfImpl

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;
}
Also used : ServerImpl(org.eclipse.che.api.machine.server.model.impl.ServerImpl) ServerProperties(org.eclipse.che.api.core.model.machine.ServerProperties) List(java.util.List) ServerPropertiesImpl(org.eclipse.che.api.machine.server.model.impl.ServerPropertiesImpl) ServerConfImpl(org.eclipse.che.api.machine.server.model.impl.ServerConfImpl) LinkedHashMap(java.util.LinkedHashMap)

Example 5 with ServerConfImpl

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;
}
Also used : ServerConfImpl(org.eclipse.che.api.machine.server.model.impl.ServerConfImpl)

Aggregations

ServerConfImpl (org.eclipse.che.api.machine.server.model.impl.ServerConfImpl)12 ServerImpl (org.eclipse.che.api.machine.server.model.impl.ServerImpl)7 Test (org.testng.annotations.Test)7 HashMap (java.util.HashMap)5 ServerPropertiesImpl (org.eclipse.che.api.machine.server.model.impl.ServerPropertiesImpl)5 Matchers.anyString (org.mockito.Matchers.anyString)3 HashSet (java.util.HashSet)2 List (java.util.List)2 ServerConf (org.eclipse.che.api.core.model.machine.ServerConf)2 ServerConf2 (org.eclipse.che.api.core.model.workspace.ServerConf2)2 PortBinding (org.eclipse.che.plugin.docker.client.json.PortBinding)2 BeforeMethod (org.testng.annotations.BeforeMethod)2 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 AgentException (org.eclipse.che.api.agent.server.exception.AgentException)1 AgentImpl (org.eclipse.che.api.agent.shared.model.impl.AgentImpl)1 ServerException (org.eclipse.che.api.core.ServerException)1 Machine (org.eclipse.che.api.core.model.machine.Machine)1 ServerProperties (org.eclipse.che.api.core.model.machine.ServerProperties)1