use of org.eclipse.che.plugin.docker.client.json.ContainerCreated in project che by eclipse.
the class OpenShiftConnector method createContainer.
/**
* @param createContainerParams
* @return
* @throws IOException
*/
@Override
public ContainerCreated createContainer(CreateContainerParams createContainerParams) throws IOException {
String containerName = KubernetesStringUtils.convertToContainerName(createContainerParams.getContainerName());
String workspaceID = getCheWorkspaceId(createContainerParams);
// Generate workspaceID if CHE_WORKSPACE_ID env var does not exist
workspaceID = workspaceID.isEmpty() ? KubernetesStringUtils.generateWorkspaceID() : workspaceID;
// imageForDocker is the docker version of the image repository. It's needed for other
// OpenShiftConnector API methods, but is not acceptable as an OpenShift name
String imageForDocker = createContainerParams.getContainerConfig().getImage();
// imageStreamTagName is imageForDocker converted into a form that can be used
// in OpenShift
String imageStreamTagName = KubernetesStringUtils.convertPullSpecToTagName(imageForDocker);
// imageStreamTagName is not enough to fill out a pull spec; it is only the tag, so we
// have to get the ImageStreamTag from the tag, and then get the full ImageStreamTag name
// from that tag. This works because the tags used in Che are unique.
ImageStreamTag imageStreamTag = getImageStreamTagFromRepo(imageStreamTagName);
String imageStreamTagPullSpec = imageStreamTag.getMetadata().getName();
// Next we need to get the address of the registry where the ImageStreamTag is stored
String imageStreamName = KubernetesStringUtils.getImageStreamNameFromPullSpec(imageStreamTagPullSpec);
ImageStream imageStream = openShiftClient.imageStreams().inNamespace(openShiftCheProjectName).withName(imageStreamName).get();
if (imageStream == null) {
throw new OpenShiftException("ImageStream not found");
}
String registryAddress = imageStream.getStatus().getDockerImageRepository().split("/")[0];
// The above needs to be combined to form a pull spec that will work when defining a container.
String dockerPullSpec = String.format("%s/%s/%s", registryAddress, openShiftCheProjectName, imageStreamTagPullSpec);
Set<String> containerExposedPorts = createContainerParams.getContainerConfig().getExposedPorts().keySet();
Set<String> imageExposedPorts = inspectImage(InspectImageParams.create(imageForDocker)).getConfig().getExposedPorts().keySet();
Set<String> exposedPorts = getExposedPorts(containerExposedPorts, imageExposedPorts);
boolean runContainerAsRoot = runContainerAsRoot(imageForDocker);
String[] envVariables = createContainerParams.getContainerConfig().getEnv();
String[] volumes = createContainerParams.getContainerConfig().getHostConfig().getBinds();
Map<String, String> additionalLabels = createContainerParams.getContainerConfig().getLabels();
String containerID;
try {
createOpenShiftService(workspaceID, exposedPorts, additionalLabels);
String deploymentName = createOpenShiftDeployment(workspaceID, dockerPullSpec, containerName, exposedPorts, envVariables, volumes, runContainerAsRoot);
containerID = waitAndRetrieveContainerID(deploymentName);
if (containerID == null) {
throw new OpenShiftException("Failed to get the ID of the container running in the OpenShift pod");
}
} catch (IOException e) {
// Make sure we clean up deployment and service in case of an error -- otherwise Che can end up
// in an inconsistent state.
LOG.info("Error while creating Pod, removing deployment");
String deploymentName = CHE_OPENSHIFT_RESOURCES_PREFIX + workspaceID;
cleanUpWorkspaceResources(deploymentName);
openShiftClient.resource(imageStreamTag).delete();
throw e;
}
return new ContainerCreated(containerID, null);
}
use of org.eclipse.che.plugin.docker.client.json.ContainerCreated in project che by eclipse.
the class DockerProcessTest method setUp.
@BeforeMethod
public void setUp() throws Exception {
dockerConnectorConfiguration = new DockerConnectorConfiguration(new InitialAuthConfig(), new DefaultNetworkFinder());
docker = new DockerConnector(dockerConnectorConfiguration, new DockerConnectionFactory(dockerConnectorConfiguration), new DockerRegistryAuthResolver(null, null), new DockerApiVersionPathPrefixProvider("1.18"));
final ContainerCreated containerCreated = docker.createContainer(CreateContainerParams.create(new ContainerConfig().withImage("ubuntu").withCmd("tail", "-f", "/dev/null")));
container = containerCreated.getId();
docker.startContainer(StartContainerParams.create(containerCreated.getId()));
when(dockerConnectorProvider.get()).thenReturn(docker);
}
use of org.eclipse.che.plugin.docker.client.json.ContainerCreated in project che by eclipse.
the class MachineProviderImplTest method setUp.
@BeforeMethod
public void setUp() throws Exception {
when(dockerConnectorConfiguration.getDockerHostIp()).thenReturn("123.123.123.123");
provider = spy(new MachineProviderBuilder().build());
EnvironmentContext envCont = new EnvironmentContext();
envCont.setSubject(new SubjectImpl(USER_NAME, "userId", USER_TOKEN, false));
EnvironmentContext.setCurrent(envCont);
when(recipeRetriever.getRecipe(any(MachineConfig.class))).thenReturn(new RecipeImpl().withType(DOCKER_FILE_TYPE).withScript("FROM codenvy"));
when(dockerMachineFactory.createNode(anyString(), anyString())).thenReturn(dockerNode);
when(dockerConnector.createContainer(any(CreateContainerParams.class))).thenReturn(new ContainerCreated(CONTAINER_ID, new String[0]));
when(dockerConnector.inspectContainer(any(InspectContainerParams.class))).thenReturn(containerInfo);
when(containerInfo.getState()).thenReturn(containerState);
when(containerState.isRunning()).thenReturn(false);
}
use of org.eclipse.che.plugin.docker.client.json.ContainerCreated in project che by eclipse.
the class DockerConnectorTest method shouldBeAbleToCreateContainer.
@Test
public void shouldBeAbleToCreateContainer() throws IOException, JsonParseException {
CreateContainerParams createContainerParams = CreateContainerParams.create(new ContainerConfig());
ContainerCreated containerCreated = mock(ContainerCreated.class);
when(dockerResponse.getStatus()).thenReturn(RESPONSE_CREATED_CODE);
doReturn(containerCreated).when(dockerConnector).parseResponseStreamAndClose(inputStream, ContainerCreated.class);
ContainerCreated returnedContainerCreated = dockerConnector.createContainer(createContainerParams);
verify(dockerConnectionFactory).openConnection(any(URI.class));
verify(dockerConnection).method(REQUEST_METHOD_POST);
verify(dockerConnection).path("/containers/create");
verify(dockerConnection).header("Content-Type", MediaType.APPLICATION_JSON);
verify(dockerConnection).header(eq("Content-Length"), anyInt());
verify(dockerConnection).entity(any(byte[].class));
verify(dockerConnection).request();
verify(dockerResponse).getStatus();
verify(dockerResponse).getInputStream();
assertEquals(returnedContainerCreated, containerCreated);
}
Aggregations