diff --git a/plugins/plugin-docker/che-plugin-docker-machine/src/main/java/org/eclipse/che/plugin/docker/machine/DockerInstanceRuntimeInfo.java b/plugins/plugin-docker/che-plugin-docker-machine/src/main/java/org/eclipse/che/plugin/docker/machine/DockerInstanceRuntimeInfo.java index 6a49ef6386..7c5cf50be7 100644 --- a/plugins/plugin-docker/che-plugin-docker-machine/src/main/java/org/eclipse/che/plugin/docker/machine/DockerInstanceRuntimeInfo.java +++ b/plugins/plugin-docker/che-plugin-docker-machine/src/main/java/org/eclipse/che/plugin/docker/machine/DockerInstanceRuntimeInfo.java @@ -13,10 +13,11 @@ package org.eclipse.che.plugin.docker.machine; import com.google.inject.assistedinject.Assisted; -import org.eclipse.che.api.core.model.machine.MachineConfig; +import org.eclipse.che.api.core.model.machine.OldMachineConfig; import org.eclipse.che.api.core.model.machine.MachineRuntimeInfo; -import org.eclipse.che.api.core.model.machine.ServerConf; -import org.eclipse.che.api.machine.server.model.impl.ServerConfImpl; +import org.eclipse.che.api.core.model.machine.OldServerConf; +import org.eclipse.che.api.machine.server.model.impl.OldServerConfImpl; +//import org.eclipse.che.api.machine.server.model.impl.ServerConfImpl; import org.eclipse.che.api.machine.server.model.impl.ServerImpl; import org.eclipse.che.plugin.docker.client.json.ContainerConfig; import org.eclipse.che.plugin.docker.client.json.ContainerInfo; @@ -74,21 +75,21 @@ public class DockerInstanceRuntimeInfo implements MachineRuntimeInfo { */ public static final String USER_TOKEN = "USER_TOKEN"; - private final ContainerInfo info; - private final Map serversConf; + private final ContainerInfo info; + private final Map serversConf; private final String internalHost; private final ServerEvaluationStrategyProvider provider; @Inject public DockerInstanceRuntimeInfo(@Assisted ContainerInfo containerInfo, - @Assisted MachineConfig machineConfig, + @Assisted OldMachineConfig machineConfig, @Assisted String internalHost, ServerEvaluationStrategyProvider provider, - @Named("machine.docker.dev_machine.machine_servers") Set devMachineSystemServers, - @Named("machine.docker.machine_servers") Set allMachinesSystemServers) { + @Named("machine.docker.dev_machine.machine_servers") Set devMachineSystemServers, + @Named("machine.docker.machine_servers") Set allMachinesSystemServers) { this.info = containerInfo; - Stream confStream = Stream.concat(machineConfig.getServers().stream(), allMachinesSystemServers.stream()); + Stream confStream = Stream.concat(machineConfig.getServers().stream(), allMachinesSystemServers.stream()); if (machineConfig.isDev()) { confStream = Stream.concat(confStream, devMachineSystemServers.stream()); } @@ -96,7 +97,7 @@ public class DockerInstanceRuntimeInfo implements MachineRuntimeInfo { this.serversConf = confStream.collect(toMap(srvConf -> srvConf.getPort().contains("/") ? srvConf.getPort() : srvConf.getPort() + "/tcp", - ServerConfImpl::new)); + OldServerConfImpl::new)); this.internalHost = internalHost; this.provider = provider; diff --git a/wsmaster/che-core-api-machine/src/main/java/org/eclipse/che/api/machine/server/model/impl/MachineImpl.java b/wsmaster/che-core-api-machine/src/main/java/org/eclipse/che/api/machine/server/model/impl/MachineImpl.java index 74b9175e6d..d04617ba16 100644 --- a/wsmaster/che-core-api-machine/src/main/java/org/eclipse/che/api/machine/server/model/impl/MachineImpl.java +++ b/wsmaster/che-core-api-machine/src/main/java/org/eclipse/che/api/machine/server/model/impl/MachineImpl.java @@ -32,7 +32,7 @@ public class MachineImpl implements Machine { } // public MachineImpl(Machine machineRuntime) { -// this(machineRuntime.getEnvVariables(), machineRuntime.getProperties(), machineRuntime.getServers()); +// this(machineRuntime.getProperties(), machineRuntime.getServers()); // } public MachineImpl(Map properties, diff --git a/wsmaster/che-core-api-workspace/src/main/java/org/eclipse/che/api/workspace/server/ServerRewritingStrategy.java b/wsmaster/che-core-api-workspace/src/main/java/org/eclipse/che/api/workspace/server/ServerRewritingStrategy.java new file mode 100644 index 0000000000..ddd67b10c0 --- /dev/null +++ b/wsmaster/che-core-api-workspace/src/main/java/org/eclipse/che/api/workspace/server/ServerRewritingStrategy.java @@ -0,0 +1,75 @@ +package org.eclipse.che.api.workspace.server; + +import org.eclipse.che.api.core.model.workspace.Warning; +import org.eclipse.che.api.core.model.workspace.runtime.Server; +import org.eclipse.che.api.machine.server.model.impl.ServerImpl; +import org.eclipse.che.api.workspace.server.spi.RuntimeIdentity; + +import java.net.MalformedURLException; +import java.net.URL; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * A strategy for rewriting Server URLs + * @author gazarenkov + */ +public abstract class ServerRewritingStrategy { + + public final Result rewrite(RuntimeIdentity identity, Map incoming) { + + Map outgoing = new HashMap<>(); + List warnings = new ArrayList<>(); + + for(Map.Entry entry : incoming.entrySet()) { + String name = entry.getKey(); + String strUrl = entry.getValue().getUrl(); + try { + URL url = new URL(strUrl); + ServerImpl server = new ServerImpl(rewriteURL(identity, name, url).toString()); + outgoing.put(name, server); + } catch (MalformedURLException e) { + warnings.add(new Warning() { + @Override + public int getCode() { + return 101; + } + + @Override + public String getMessage() { + return "Malformed URL for " + name + " : " + e.getMessage(); + } + }); + } + + } + + return new Result(outgoing, warnings); + + } + + protected abstract URL rewriteURL(RuntimeIdentity identity, String name, URL url) throws MalformedURLException; + + public static class Result { + + private final Map servers; + private final List warnings; + + protected Result(Map servers, List warnings) { + this.servers = servers; + this.warnings = warnings; + } + + public Map getServers() { + return servers; + } + + public List getWarnings() { + return warnings; + } + } + + +} diff --git a/wsmaster/che-core-api-workspace/src/main/java/org/eclipse/che/api/workspace/server/model/impl/ServerConfigImpl.java b/wsmaster/che-core-api-workspace/src/main/java/org/eclipse/che/api/workspace/server/model/impl/ServerConfigImpl.java index 7d05c78550..9816f0503b 100644 --- a/wsmaster/che-core-api-workspace/src/main/java/org/eclipse/che/api/workspace/server/model/impl/ServerConfigImpl.java +++ b/wsmaster/che-core-api-workspace/src/main/java/org/eclipse/che/api/workspace/server/model/impl/ServerConfigImpl.java @@ -22,7 +22,7 @@ import java.util.Objects; /** * @author Alexander Garagatyi */ -@Entity(name = "OldServerConf") +@Entity(name = "ServerConf") @Table(name = "serverconf") public class ServerConfigImpl implements ServerConfig { diff --git a/wsmaster/che-core-api-workspace/src/main/java/org/eclipse/che/api/workspace/server/spi/InternalRuntime.java b/wsmaster/che-core-api-workspace/src/main/java/org/eclipse/che/api/workspace/server/spi/InternalRuntime.java index 56307baa1b..f98ee86367 100644 --- a/wsmaster/che-core-api-workspace/src/main/java/org/eclipse/che/api/workspace/server/spi/InternalRuntime.java +++ b/wsmaster/che-core-api-workspace/src/main/java/org/eclipse/che/api/workspace/server/spi/InternalRuntime.java @@ -11,8 +11,15 @@ package org.eclipse.che.api.workspace.server.spi; import org.eclipse.che.api.core.model.workspace.Runtime; +import org.eclipse.che.api.core.model.workspace.Warning; import org.eclipse.che.api.core.model.workspace.runtime.Machine; +import org.eclipse.che.api.core.model.workspace.runtime.Server; +import org.eclipse.che.api.machine.server.model.impl.MachineImpl; +import org.eclipse.che.api.workspace.server.ServerRewritingStrategy; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; import java.util.Map; /** @@ -22,12 +29,19 @@ import java.util.Map; */ public abstract class InternalRuntime implements Runtime { - private final RuntimeContext context; + private final RuntimeContext context; + private final ServerRewritingStrategy serverRewritingStrategy; + private Map cachedExternalMachines; + private final List warnings = new ArrayList<>(); - public InternalRuntime(RuntimeContext context) { + public InternalRuntime(RuntimeContext context, ServerRewritingStrategy serverRewritingStrategy) { this.context = context; + this.serverRewritingStrategy = serverRewritingStrategy; } + public abstract Map getInternalMachines(); + + @Override public String getActiveEnv() { return context.getIdentity().getEnvName(); @@ -39,7 +53,32 @@ public abstract class InternalRuntime implements Runtime { } @Override - public abstract Map getMachines(); + public List getWarnings() { + return warnings; + } + + @Override + public Map getMachines() { + + if(cachedExternalMachines == null) { + cachedExternalMachines = new HashMap<>(); + for(Map.Entry entry : getInternalMachines().entrySet()) { + String key = entry.getKey(); + Machine machine = entry.getValue(); + ServerRewritingStrategy.Result result = serverRewritingStrategy.rewrite(context.getIdentity(), entry.getValue().getServers()); + Map newServers = result.getServers(); + MachineImpl newMachine = new MachineImpl(machine.getProperties(), newServers); + cachedExternalMachines.put(key, newMachine); + if(!result.getWarnings().isEmpty()) { + warnings.addAll(result.getWarnings()); + } + } + + } + return cachedExternalMachines; + + } + /** * @return some implementation specific properties if any @@ -53,5 +92,4 @@ public abstract class InternalRuntime implements Runtime { return context; } - } diff --git a/wsmaster/che-core-api-workspace/src/main/java/org/eclipse/che/api/workspace/server/spi/InternalEnvironmentConfig.java b/wsmaster/che-core-api-workspace/src/main/java/org/eclipse/che/api/workspace/server/spi/InternalRuntimeConfig.java similarity index 94% rename from wsmaster/che-core-api-workspace/src/main/java/org/eclipse/che/api/workspace/server/spi/InternalEnvironmentConfig.java rename to wsmaster/che-core-api-workspace/src/main/java/org/eclipse/che/api/workspace/server/spi/InternalRuntimeConfig.java index 23b0bd7f6d..7713c31d52 100644 --- a/wsmaster/che-core-api-workspace/src/main/java/org/eclipse/che/api/workspace/server/spi/InternalEnvironmentConfig.java +++ b/wsmaster/che-core-api-workspace/src/main/java/org/eclipse/che/api/workspace/server/spi/InternalRuntimeConfig.java @@ -25,13 +25,13 @@ import java.util.Map; * Environment configuration transformed to view useful for Infrastructure to create Runtime * @author gazarenkov */ -public class InternalEnvironmentConfig { +public class InternalRuntimeConfig { protected Map internalMachines; protected InternalRecipeConfig recipe; protected EnvironmentImpl config; - public InternalEnvironmentConfig(Environment environment, URL registryEndpoint) throws ApiException, IOException { + public InternalRuntimeConfig(Environment environment, URL registryEndpoint) throws ApiException, IOException { this.recipe = new InternalRecipeConfig(environment.getRecipe()); diff --git a/wsmaster/che-core-api-workspace/src/main/java/org/eclipse/che/api/workspace/server/spi/RuntimeContext.java b/wsmaster/che-core-api-workspace/src/main/java/org/eclipse/che/api/workspace/server/spi/RuntimeContext.java index 0de59dc925..56572e91c8 100644 --- a/wsmaster/che-core-api-workspace/src/main/java/org/eclipse/che/api/workspace/server/spi/RuntimeContext.java +++ b/wsmaster/che-core-api-workspace/src/main/java/org/eclipse/che/api/workspace/server/spi/RuntimeContext.java @@ -26,19 +26,19 @@ import java.util.Map; */ public abstract class RuntimeContext { - protected final Environment environment; - protected final InternalEnvironmentConfig internalEnv; - protected final RuntimeIdentity identity; - protected final RuntimeInfrastructure infrastructure; + protected final Environment environment; + protected final InternalRuntimeConfig internalRuntimeConfig; + protected final RuntimeIdentity identity; + protected final RuntimeInfrastructure infrastructure; // TODO other than WorkspaceStatus impl - protected WorkspaceStatus state; + protected WorkspaceStatus state; public RuntimeContext(Environment environment, RuntimeIdentity identity, RuntimeInfrastructure infrastructure, URL registryEndpoint) throws ValidationException, ApiException, IOException { this.environment = environment; this.identity = identity; this.infrastructure = infrastructure; - this.internalEnv = new InternalEnvironmentConfig(environment, registryEndpoint); + this.internalRuntimeConfig = new InternalRuntimeConfig(environment, registryEndpoint); } diff --git a/wsmaster/che-core-api-workspace/src/test/java/org/eclipse/che/api/workspace/server/WorkspaceRuntimeIntegrationTest.java b/wsmaster/che-core-api-workspace/src/test/java/org/eclipse/che/api/workspace/server/WorkspaceRuntimeIntegrationTest.java index 5551c7a6e0..30daf95f31 100644 --- a/wsmaster/che-core-api-workspace/src/test/java/org/eclipse/che/api/workspace/server/WorkspaceRuntimeIntegrationTest.java +++ b/wsmaster/che-core-api-workspace/src/test/java/org/eclipse/che/api/workspace/server/WorkspaceRuntimeIntegrationTest.java @@ -127,10 +127,10 @@ public class WorkspaceRuntimeIntegrationTest { // when(instance.getId()).thenReturn("machineId"); // when(instance.getConfig()).thenReturn(machineConfig); // -// CheServicesEnvironmentImpl internalEnv = new CheServicesEnvironmentImpl(); -// internalEnv.getServices().put("service1", new CheServiceImpl().withId("machineId")); +// CheServicesEnvironmentImpl internalRuntimeConfig = new CheServicesEnvironmentImpl(); +// internalRuntimeConfig.getServices().put("service1", new CheServiceImpl().withId("machineId")); // -// when(environmentParser.parse(any(Environment.class))).thenReturn(internalEnv); +// when(environmentParser.parse(any(Environment.class))).thenReturn(internalRuntimeConfig); // when(instanceProvider.startService(anyString(), // anyString(), // anyString(),