Search in sources :

Example 11 with PortMapping

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;
    }
}
Also used : JSONObject(org.json.JSONObject) ContainerPort(io.fabric8.kubernetes.api.model.ContainerPort) ArrayList(java.util.ArrayList) JSONArray(org.json.JSONArray) PortMapping(io.fabric8.maven.docker.access.PortMapping) BuildImageConfiguration(io.fabric8.maven.docker.config.BuildImageConfiguration)

Example 12 with PortMapping

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());
        }
    };
}
Also used : ImageConfiguration(io.fabric8.maven.docker.config.ImageConfiguration) Verifications(mockit.Verifications)

Example 13 with PortMapping

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);
        }
    });
}
Also used : RestartPolicy(io.fabric8.maven.docker.config.RestartPolicy) Arrays(java.util.Arrays) NetworkConfig(io.fabric8.maven.docker.config.NetworkConfig) Date(java.util.Date) ImageConfiguration(io.fabric8.maven.docker.config.ImageConfiguration) Logger(io.fabric8.maven.docker.util.Logger) DockerAccessException(io.fabric8.maven.docker.access.DockerAccessException) ContainerShutdownDescriptor(io.fabric8.maven.docker.service.ContainerTracker.ContainerShutdownDescriptor) HashMap(java.util.HashMap) Callable(java.util.concurrent.Callable) LogOutputSpecFactory(io.fabric8.maven.docker.log.LogOutputSpecFactory) ContainerNamingUtil(io.fabric8.maven.docker.util.ContainerNamingUtil) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Container(io.fabric8.maven.docker.model.Container) DockerAccess(io.fabric8.maven.docker.access.DockerAccess) VolumeBindingUtil.resolveRelativeVolumeBindings(io.fabric8.maven.docker.util.VolumeBindingUtil.resolveRelativeVolumeBindings) GavLabel(io.fabric8.maven.docker.util.GavLabel) WaitUtil(io.fabric8.maven.docker.wait.WaitUtil) Map(java.util.Map) StopMode(io.fabric8.maven.docker.config.StopMode) ExecDetails(io.fabric8.maven.docker.model.ExecDetails) Network(io.fabric8.maven.docker.model.Network) NetworkCreateConfig(io.fabric8.maven.docker.access.NetworkCreateConfig) RunImageConfiguration(io.fabric8.maven.docker.config.RunImageConfiguration) ContainerNetworkingConfig(io.fabric8.maven.docker.access.ContainerNetworkingConfig) Properties(java.util.Properties) Collection(java.util.Collection) Set(java.util.Set) RunVolumeConfiguration(io.fabric8.maven.docker.config.RunVolumeConfiguration) RetryPolicy(net.jodah.failsafe.RetryPolicy) ExecException(io.fabric8.maven.docker.access.ExecException) VolumeConfiguration(io.fabric8.maven.docker.config.VolumeConfiguration) PortMapping(io.fabric8.maven.docker.access.PortMapping) PortBindingException(io.fabric8.maven.docker.model.PortBindingException) ContainerHostConfig(io.fabric8.maven.docker.access.ContainerHostConfig) File(java.io.File) ContainerDetails(io.fabric8.maven.docker.model.ContainerDetails) Failsafe(net.jodah.failsafe.Failsafe) ExecutionException(java.util.concurrent.ExecutionException) List(java.util.List) ChronoUnit(java.time.temporal.ChronoUnit) ContainerCreateConfig(io.fabric8.maven.docker.access.ContainerCreateConfig) StringJoiner(java.util.StringJoiner) EnvUtil(io.fabric8.maven.docker.util.EnvUtil) Arguments(io.fabric8.maven.docker.config.Arguments) StartOrderResolver(io.fabric8.maven.docker.util.StartOrderResolver) WaitTimeoutException(io.fabric8.maven.docker.wait.WaitTimeoutException) Container(io.fabric8.maven.docker.model.Container)

Example 14 with PortMapping

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);
    });
}
Also used : RunService(io.fabric8.maven.docker.service.RunService) LogDispatcher(io.fabric8.maven.docker.log.LogDispatcher) PortMapping(io.fabric8.maven.docker.access.PortMapping) Properties(java.util.Properties) RunImageConfiguration(io.fabric8.maven.docker.config.RunImageConfiguration) StartContainerExecutor(io.fabric8.maven.docker.service.helper.StartContainerExecutor)

Example 15 with PortMapping

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"));
}
Also used : Expectations(mockit.Expectations) RunService(io.fabric8.maven.docker.service.RunService) LogOutputSpecFactory(io.fabric8.maven.docker.log.LogOutputSpecFactory) Properties(java.util.Properties) Date(java.util.Date) GavLabel(io.fabric8.maven.docker.util.GavLabel) QueryService(io.fabric8.maven.docker.service.QueryService) PortMapping(io.fabric8.maven.docker.access.PortMapping) File(java.io.File) RunImageConfiguration(io.fabric8.maven.docker.config.RunImageConfiguration) ContainerDetails(io.fabric8.maven.docker.model.ContainerDetails) Test(org.junit.Test)

Aggregations

PortMapping (io.fabric8.maven.docker.access.PortMapping)8 ContainerCreateConfig (io.fabric8.maven.docker.access.ContainerCreateConfig)7 Properties (java.util.Properties)5 ContainerHostConfig (io.fabric8.maven.docker.access.ContainerHostConfig)4 ImageConfiguration (io.fabric8.maven.docker.config.ImageConfiguration)4 RunImageConfiguration (io.fabric8.maven.docker.config.RunImageConfiguration)4 GavLabel (io.fabric8.maven.docker.util.GavLabel)4 Date (java.util.Date)4 Arguments (io.fabric8.maven.docker.config.Arguments)3 PortBindingException (io.fabric8.maven.docker.model.PortBindingException)3 Gson (com.google.gson.Gson)2 ContainerNetworkingConfig (io.fabric8.maven.docker.access.ContainerNetworkingConfig)2 NetworkConfig (io.fabric8.maven.docker.config.NetworkConfig)2 RestartPolicy (io.fabric8.maven.docker.config.RestartPolicy)2 RunVolumeConfiguration (io.fabric8.maven.docker.config.RunVolumeConfiguration)2 LogOutputSpecFactory (io.fabric8.maven.docker.log.LogOutputSpecFactory)2 Container (io.fabric8.maven.docker.model.Container)2 ContainerDetails (io.fabric8.maven.docker.model.ContainerDetails)2 RunService (io.fabric8.maven.docker.service.RunService)2 File (java.io.File)2