Search in sources :

Example 1 with Network

use of io.fabric8.maven.docker.model.Network in project docker-maven-plugin by fabric8io.

the class LogMatchCallbackTest method matchingPartitialLineSucceeds.

@Test(expected = LogCallback.DoneException.class)
public void matchingPartitialLineSucceeds() throws Exception {
    final String patterString = "waiting for connections";
    final LogMatchCallback logMatchCallback = new LogMatchCallback(logger, callback, patterString);
    new Expectations() {

        {
            callback.matched();
            times = 1;
        }
    };
    logMatchCallback.log(1, new Timestamp(), "2017-11-21T12:44:43.678+0000 I NETWORK  [initandlisten] waiting for connections on port 27017");
}
Also used : Expectations(mockit.Expectations) Timestamp(io.fabric8.maven.docker.util.Timestamp) Test(org.junit.Test)

Example 2 with Network

use of io.fabric8.maven.docker.model.Network in project docker-maven-plugin by fabric8io.

the class StopMojo method getNetworksToRemove.

private Set<Network> getNetworksToRemove(QueryService queryService, PomLabel pomLabel) throws DockerAccessException {
    if (!autoCreateCustomNetworks) {
        return Collections.emptySet();
    }
    Set<Network> customNetworks = new HashSet<>();
    Set<Network> networks = queryService.getNetworks();
    for (ImageConfiguration image : getResolvedImages()) {
        final NetworkConfig config = image.getRunConfiguration().getNetworkingConfig();
        if (config.isCustomNetwork()) {
            Network network = getNetworkByName(networks, config.getCustomNetwork());
            if (network != null) {
                customNetworks.add(network);
                for (Container container : getContainersToStop(queryService, image)) {
                    if (!shouldStopContainer(container, pomLabel, image)) {
                        // it's sill in use don't collect it
                        customNetworks.remove(network);
                    }
                }
            }
        }
    }
    return customNetworks;
}
Also used : Container(io.fabric8.maven.docker.model.Container) ImageConfiguration(io.fabric8.maven.docker.config.ImageConfiguration) RunImageConfiguration(io.fabric8.maven.docker.config.RunImageConfiguration) Network(io.fabric8.maven.docker.model.Network) NetworkConfig(io.fabric8.maven.docker.config.NetworkConfig) HashSet(java.util.HashSet)

Example 3 with Network

use of io.fabric8.maven.docker.model.Network in project docker-maven-plugin by fabric8io.

the class DockerComposeServiceWrapper method getNetworkConfig.

NetworkConfig getNetworkConfig() {
    String net = asString("network_mode");
    if (net != null) {
        return new NetworkConfig(net);
    }
    Object networks = asObject("networks");
    if (networks == null) {
        return null;
    }
    if (networks instanceof List) {
        List<String> toJoin = (List<String>) networks;
        if (toJoin.size() > 1) {
            throwIllegalArgumentException("'networks:' Only one custom network to join is supported currently");
        }
        return new NetworkConfig(NetworkConfig.Mode.custom, toJoin.get(0));
    } else if (networks instanceof Map) {
        Map<String, Object> toJoin = (Map<String, Object>) networks;
        if (toJoin.size() > 1) {
            throwIllegalArgumentException("'networks:' Only one custom network to join is supported currently");
        }
        String custom = toJoin.keySet().iterator().next();
        NetworkConfig ret = new NetworkConfig(NetworkConfig.Mode.custom, custom);
        Object aliases = toJoin.get(custom);
        if (aliases != null) {
            if (aliases instanceof List) {
                for (String alias : (List<String>) aliases) {
                    ret.addAlias(alias);
                }
            } else if (aliases instanceof Map) {
                Map<String, List<String>> map = (Map<String, List<String>>) aliases;
                if (map.containsKey("aliases")) {
                    for (String alias : map.get("aliases")) {
                        ret.addAlias(alias);
                    }
                } else {
                    throwIllegalArgumentException("'networks:' Aliases must be given as a map of strings. 'aliases' key not founded");
                }
            } else {
                throwIllegalArgumentException("'networks:' No aliases entry found in network config map");
            }
        }
        return ret;
    } else {
        throwIllegalArgumentException("'networks:' must beu either a list or a map");
        return null;
    }
}
Also used : NetworkConfig(io.fabric8.maven.docker.config.NetworkConfig) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map)

Example 4 with Network

use of io.fabric8.maven.docker.model.Network in project docker-maven-plugin by fabric8io.

the class BuildService method buildImage.

/**
 * Build an image
 *
 * @param imageConfig the image configuration
 * @param params      mojo params for the project
 * @param noCache     if not null, dictate the caching behaviour. Otherwise its taken from the build configuration
 * @param buildArgs   docker build args
 * @throws DockerAccessException
 * @throws MojoExecutionException
 */
protected void buildImage(ImageConfiguration imageConfig, MojoParameters params, boolean noCache, boolean squash, Map<String, String> buildArgs, File dockerArchive) throws DockerAccessException, MojoExecutionException {
    String imageName = imageConfig.getName();
    ImageName.validate(imageName);
    BuildImageConfiguration buildConfig = imageConfig.getBuildConfiguration();
    String oldImageId = null;
    CleanupMode cleanupMode = buildConfig.cleanupMode();
    if (cleanupMode.isRemove()) {
        oldImageId = queryService.getImageId(imageName);
    }
    if (buildConfig.getDockerArchive() != null) {
        File tarArchive = buildConfig.getAbsoluteDockerTarPath(params);
        String archiveImageName = getArchiveImageName(buildConfig, tarArchive);
        long time = System.currentTimeMillis();
        docker.loadImage(imageName, tarArchive);
        log.info("%s: Loaded tarball in %s", buildConfig.getDockerArchive(), EnvUtil.formatDurationTill(time));
        if (archiveImageName != null && !archiveImageName.equals(imageName)) {
            docker.tag(archiveImageName, imageName, true);
        }
        return;
    }
    Map<String, String> mergedBuildMap = prepareBuildArgs(buildArgs, buildConfig);
    // auto is now supported by docker, consider switching?
    BuildOptions opts = new BuildOptions(buildConfig.getBuildOptions()).dockerfile(getDockerfileName(buildConfig)).forceRemove(cleanupMode.isRemove()).noCache(noCache).squash(squash).cacheFrom(buildConfig.getCacheFrom()).network(buildConfig.getNetwork()).buildArgs(mergedBuildMap);
    String newImageId = doBuildImage(imageName, dockerArchive, opts);
    log.info("%s: Built image %s", imageConfig.getDescription(), newImageId);
    removeDanglingImage(imageName, oldImageId, newImageId, cleanupMode, true);
}
Also used : BuildOptions(io.fabric8.maven.docker.access.BuildOptions) CleanupMode(io.fabric8.maven.docker.config.CleanupMode) File(java.io.File) BuildImageConfiguration(io.fabric8.maven.docker.config.BuildImageConfiguration)

Example 5 with Network

use of io.fabric8.maven.docker.model.Network in project docker-maven-plugin by fabric8io.

the class DockerAccessWithHcClient method listNetworks.

@Override
public List<Network> listNetworks() throws DockerAccessException {
    String url = urlBuilder.listNetworks();
    try {
        String response = delegate.get(url, HTTP_OK);
        JsonArray array = JsonFactory.newJsonArray(response);
        List<Network> networks = new ArrayList<>(array.size());
        for (int i = 0; i < array.size(); i++) {
            networks.add(new NetworksListElement(array.get(i).getAsJsonObject()));
        }
        return networks;
    } catch (IOException e) {
        throw new DockerAccessException(e.getMessage());
    }
}
Also used : JsonArray(com.google.gson.JsonArray) NetworksListElement(io.fabric8.maven.docker.model.NetworksListElement) Network(io.fabric8.maven.docker.model.Network) DockerAccessException(io.fabric8.maven.docker.access.DockerAccessException) ArrayList(java.util.ArrayList) IOException(java.io.IOException)

Aggregations

Network (io.fabric8.maven.docker.model.Network)6 DockerAccessException (io.fabric8.maven.docker.access.DockerAccessException)5 ImageConfiguration (io.fabric8.maven.docker.config.ImageConfiguration)5 ArrayList (java.util.ArrayList)5 NetworkConfig (io.fabric8.maven.docker.config.NetworkConfig)4 Container (io.fabric8.maven.docker.model.Container)4 IOException (java.io.IOException)4 RunImageConfiguration (io.fabric8.maven.docker.config.RunImageConfiguration)3 HashMap (java.util.HashMap)3 HashSet (java.util.HashSet)3 Container (io.fabric8.api.Container)2 Profile (io.fabric8.api.Profile)2 Pod (io.fabric8.kubernetes.api.model.Pod)2 Test (org.junit.Test)2 JsonArray (com.google.gson.JsonArray)1 JsonObject (com.google.gson.JsonObject)1 DataTable (io.cucumber.datatable.DataTable)1 Given (io.cucumber.java.en.Given)1 Then (io.cucumber.java.en.Then)1 When (io.cucumber.java.en.When)1