Skip to content

Commit

Permalink
Sets CHE_WORKSPACE_ID and CHE_MACHINE_NAME in all machines started by…
Browse files Browse the repository at this point in the history
… Che (eclipse-che#4649)

* Set CHE_WORKSPACE_ID and CHE_MACHINE_NAME in all machines started through CHE
it is required for single port /reverse proxy strategy

Change-Id: Ib2cb987e594929151de4c26b614b91d788d19869
Signed-off-by: Florent BENOIT <fbenoit@codenvy.com>
  • Loading branch information
benoitf committed Apr 3, 2017
1 parent c798642 commit 45da3d9
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,15 @@ public class DockerInstanceRuntimeInfo implements MachineRuntimeInfo {
public static final String API_ENDPOINT_URL_VARIABLE = "CHE_API";

/**
* Environment variable that will be setup in developer machine will contain ID of a workspace for which this machine has been created
* Environment variable that will contain ID of a workspace for which this machine has been created
*/
public static final String CHE_WORKSPACE_ID = "CHE_WORKSPACE_ID";

/**
* Environment variable that will contain Name of the machine
*/
public static final String CHE_MACHINE_NAME = "CHE_MACHINE_NAME";

/**
* Default HOSTNAME that will be added in all docker containers that are started. This host will container the Docker host's ip
* reachable inside the container.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,7 @@ private String createContainer(String workspaceId,
(long)(service.getMemLimit() * memorySwapMultiplier);

addSystemWideContainerSettings(workspaceId,
machineName,
isDev,
service);

Expand Down Expand Up @@ -563,6 +564,7 @@ private void addStaticDockerConfiguration(ContainerConfig config) {
}

private void addSystemWideContainerSettings(String workspaceId,
String machineName,
boolean isDev,
CheServiceImpl composeService) throws IOException {
List<String> portsToExpose;
Expand All @@ -573,13 +575,16 @@ private void addSystemWideContainerSettings(String workspaceId,
volumes = devMachineSystemVolumes;

env = new HashMap<>(devMachineEnvVariables);
env.put(DockerInstanceRuntimeInfo.CHE_WORKSPACE_ID, workspaceId);
env.put(DockerInstanceRuntimeInfo.USER_TOKEN, getUserToken(workspaceId));
} else {
portsToExpose = commonMachinePortsToExpose;
env = commonMachineEnvVariables;
env = new HashMap<>(commonMachineEnvVariables);
volumes = commonMachineSystemVolumes;
}
// register workspace ID and Machine Name
env.put(DockerInstanceRuntimeInfo.CHE_WORKSPACE_ID, workspaceId);
env.put(DockerInstanceRuntimeInfo.CHE_MACHINE_NAME, machineName);

composeService.getExpose().addAll(portsToExpose);
composeService.getEnvironment().putAll(env);
composeService.getVolumes().addAll(volumes);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1073,6 +1073,32 @@ public void shouldAddWorkspaceIdEnvVariableOnDevInstanceCreationFromRecipe() thr
". Found " + Arrays.toString(argumentCaptor.getValue().getContainerConfig().getEnv()));
}

@Test
public void shouldAddMachineNameEnvVariableOnDevInstanceCreationFromRecipe() throws Exception {
String wsId = "myWs";
createInstanceFromRecipe(true, wsId);
ArgumentCaptor<CreateContainerParams> argumentCaptor = ArgumentCaptor.forClass(CreateContainerParams.class);
verify(dockerConnector).createContainer(argumentCaptor.capture());
assertTrue(asList(argumentCaptor.getValue().getContainerConfig().getEnv())
.contains(DockerInstanceRuntimeInfo.CHE_MACHINE_NAME + "=" + MACHINE_NAME),
"Machine Name variable is missing. Required " + DockerInstanceRuntimeInfo.CHE_MACHINE_NAME + "=" +
MACHINE_NAME +
". Found " + Arrays.toString(argumentCaptor.getValue().getContainerConfig().getEnv()));
}

@Test
public void shouldAddMachineNameEnvVariableOnNonDevInstanceCreationFromRecipe() throws Exception {
String wsId = "myWs";
createInstanceFromRecipe(false, wsId);
ArgumentCaptor<CreateContainerParams> argumentCaptor = ArgumentCaptor.forClass(CreateContainerParams.class);
verify(dockerConnector).createContainer(argumentCaptor.capture());
assertTrue(asList(argumentCaptor.getValue().getContainerConfig().getEnv())
.contains(DockerInstanceRuntimeInfo.CHE_MACHINE_NAME + "=" + MACHINE_NAME),
"Machine Name variable is missing. Required " + DockerInstanceRuntimeInfo.CHE_MACHINE_NAME + "=" +
MACHINE_NAME +
". Found " + Arrays.toString(argumentCaptor.getValue().getContainerConfig().getEnv()));
}

@Test
public void shouldAddWorkspaceIdEnvVariableOnDevInstanceCreationFromSnapshot() throws Exception {
String wsId = "myWs";
Expand All @@ -1087,14 +1113,14 @@ public void shouldAddWorkspaceIdEnvVariableOnDevInstanceCreationFromSnapshot() t
}

@Test
public void shouldNotAddWorkspaceIdEnvVariableOnNonDevInstanceCreationFromRecipe() throws Exception {
public void shouldAddWorkspaceIdEnvVariableOnNonDevInstanceCreationFromRecipe() throws Exception {
String wsId = "myWs";
createInstanceFromRecipe(false, wsId);
ArgumentCaptor<CreateContainerParams> argumentCaptor = ArgumentCaptor.forClass(CreateContainerParams.class);
verify(dockerConnector).createContainer(argumentCaptor.capture());
assertFalse(asList(argumentCaptor.getValue().getContainerConfig().getEnv())
assertTrue(asList(argumentCaptor.getValue().getContainerConfig().getEnv())
.contains(DockerInstanceRuntimeInfo.CHE_WORKSPACE_ID + "=" + wsId),
"Non dev machine should not contains " + DockerInstanceRuntimeInfo.CHE_WORKSPACE_ID);
"Non dev machine should contains " + DockerInstanceRuntimeInfo.CHE_WORKSPACE_ID);
}

@Test
Expand Down

0 comments on commit 45da3d9

Please sign in to comment.