use of io.fabric8.maven.docker.access.PortMapping in project fabric8-maven-plugin by fabric8io.
the class ContainerHandler method getContainerPorts.
private List<ContainerPort> getContainerPorts(ImageConfiguration imageConfig) {
BuildImageConfiguration buildConfig = imageConfig.getBuildConfiguration();
List<String> ports = buildConfig.getPorts();
if (!ports.isEmpty()) {
List<ContainerPort> ret = new ArrayList<>();
PortMapping portMapping = new PortMapping(ports, project.getProperties());
JSONArray portSpecs = portMapping.toJson();
for (int i = 0; i < portSpecs.length(); i++) {
JSONObject portSpec = portSpecs.getJSONObject(i);
ret.add(extractContainerPort(portSpec));
}
return ret;
} else {
return null;
}
}
use of io.fabric8.maven.docker.access.PortMapping in project docker-maven-plugin by fabric8io.
the class CopyMojoTest method thenContainerIsCreated.
private void thenContainerIsCreated(ImageConfiguration image, String namePattern) throws DockerAccessException {
new Verifications() {
{
final ImageConfiguration containerImage;
runService.createContainer(containerImage = withCapture(), (PortMapping) any, projectGavLabel, (Properties) any, (File) any, namePattern, (Date) any);
times = 1;
assertEquals(image.getName(), containerImage.getName());
}
};
}
use of io.fabric8.maven.docker.access.PortMapping in project docker-maven-plugin by fabric8io.
the class RunService method updateMappedPortsAndAddresses.
private void updateMappedPortsAndAddresses(String containerId, PortMapping mappedPorts) throws DockerAccessException {
RetryPolicy<Void> retryPolicy = new RetryPolicy<Void>().withMaxAttempts(20).withBackoff(10, 100, ChronoUnit.MILLIS).handle(PortBindingException.class).onFailedAttempt(f -> log.debug("Failed to update mapped ports for container %s (attempt %d), retrying", containerId, f.getAttemptCount())).onRetriesExceeded(f -> log.warn("Failed to update mapped ports for container %s after %d retries", containerId, f.getAttemptCount()));
Failsafe.with(retryPolicy).run(() -> {
Container container = queryService.getMandatoryContainer(containerId);
if (container.isRunning()) {
mappedPorts.updateProperties(container.getPortBindings());
} else {
log.warn("Container %s is not running anymore, can not extract dynamic ports", containerId);
}
});
}
use of io.fabric8.maven.docker.access.PortMapping in project docker-maven-plugin by fabric8io.
the class StartMojo method startImage.
private void startImage(final ImageConfiguration imageConfig, final ServiceHub hub, final ExecutorCompletionService<StartedContainer> startingContainers, final PortMapping.PropertyWriteHelper portMappingPropertyWriteHelper) throws IOException {
final RunService runService = hub.getRunService();
final Properties projProperties = project.getProperties();
final RunImageConfiguration runConfig = imageConfig.getRunConfiguration();
final PortMapping portMapping = runService.createPortMapping(runConfig, projProperties);
final LogDispatcher dispatcher = getLogDispatcher(hub);
StartContainerExecutor startExecutor = new StartContainerExecutor.Builder().exposeContainerProps(exposeContainerProps).dispatcher(dispatcher).follow(follow).log(log).portMapping(portMapping).gavLabel(getGavLabel()).projectProperties(project.getProperties()).basedir(project.getBasedir()).imageConfig(imageConfig).serviceHub(hub).logOutputSpecFactory(serviceHubFactory.getLogOutputSpecFactory()).showLogs(showLogs).containerNamePattern(containerNamePattern).buildTimestamp(getBuildTimestamp()).build();
startingContainers.submit(() -> {
String containerId = startExecutor.startContainer();
// Update port-mapping writer
portMappingPropertyWriteHelper.add(portMapping, runConfig.getPortPropertyFile());
return new StartedContainer(imageConfig, containerId);
});
}
use of io.fabric8.maven.docker.access.PortMapping in project docker-maven-plugin by fabric8io.
the class StartContainerExecutorTest method testStartContainers.
@Test
public void testStartContainers(@Mocked ServiceHub hub, @Mocked DockerAccess dockerAccess, @Mocked ContainerTracker containerTracker, @Mocked Logger log) throws IOException, ExecException {
// Given
new Expectations() {
{
dockerAccess.createContainer((ContainerCreateConfig) any, anyString);
result = "container-name";
dockerAccess.getContainer(anyString);
result = new ContainerDetails(JsonFactory.newJsonObject("{\"NetworkSettings\":{\"IPAddress\":\"192.168.1.2\"}}"));
QueryService queryService = new QueryService(dockerAccess);
hub.getQueryService();
result = queryService;
hub.getRunService();
result = new RunService(dockerAccess, queryService, containerTracker, new LogOutputSpecFactory(true, true, null), log);
}
};
Properties projectProps = new Properties();
StartContainerExecutor startContainerExecutor = new StartContainerExecutor.Builder().serviceHub(hub).projectProperties(projectProps).portMapping(new PortMapping(Collections.emptyList(), projectProps)).gavLabel(new GavLabel("io.fabric8:test:0.1.0")).basedir(new File("/tmp/foo")).containerNamePattern("test-").buildTimestamp(new Date()).exposeContainerProps("docker.container").imageConfig(new ImageConfiguration.Builder().name("name").alias("alias").runConfig(new RunImageConfiguration.Builder().build()).build()).build();
// When
String containerId = startContainerExecutor.startContainer();
// Then
assertEquals("container-name", containerId);
assertEquals("container-name", projectProps.getProperty("docker.container.alias.id"));
assertEquals("192.168.1.2", projectProps.getProperty("docker.container.alias.ip"));
}
Aggregations