Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jclouds.docker.compute.strategy;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.collect.Iterables.find;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.Pattern;
import javax.annotation.Resource;
import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Singleton;
import com.google.common.base.Charsets;
import com.google.common.base.Predicate;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import org.jclouds.compute.ComputeServiceAdapter;
import org.jclouds.compute.domain.Hardware;
import org.jclouds.compute.domain.HardwareBuilder;
import org.jclouds.compute.domain.Processor;
import org.jclouds.compute.domain.Template;
import org.jclouds.compute.options.TemplateOptions;
import org.jclouds.compute.reference.ComputeServiceConstants;
import org.jclouds.docker.DockerApi;
import org.jclouds.docker.compute.options.DockerTemplateOptions;
import org.jclouds.docker.domain.Config;
import org.jclouds.docker.domain.Container;
import org.jclouds.docker.domain.ContainerSummary;
import org.jclouds.docker.domain.HostConfig;
import org.jclouds.docker.domain.Image;
import org.jclouds.docker.domain.ImageSummary;
import org.jclouds.docker.options.AttachOptions;
import org.jclouds.docker.options.CreateImageOptions;
import org.jclouds.docker.options.ListContainerOptions;
import org.jclouds.docker.options.RemoveContainerOptions;
import org.jclouds.docker.util.DockerInputStream;
import org.jclouds.docker.util.StdStreamData;
import org.jclouds.domain.Location;
import org.jclouds.domain.LoginCredentials;
import org.jclouds.logging.Logger;
import org.jclouds.util.Closeables2;
/**
* defines the connection between the {@link org.jclouds.docker.DockerApi} implementation and
* the jclouds {@link org.jclouds.compute.ComputeService}
*/
@Singleton
public class DockerComputeServiceAdapter implements
ComputeServiceAdapter {
/**
* Some Docker versions returns host prefix even for images from Docker hub in repoTags field. We use this constant
* to correctly identify requested image name.
*/
public static final String PREFIX_DOCKER_HUB_HOST = "docker.io/";
/**
* (Optional) Suffix used, when image version is not used during searching images.
*/
public static final String SUFFIX_LATEST_VERSION = ":latest";
private static final String PATTERN_IMAGE_PREFIX = "^(" + Pattern.quote(PREFIX_DOCKER_HUB_HOST) + ")?";
private static final String PATTERN_IMAGE_SUFFIX = "(" + Pattern.quote(SUFFIX_LATEST_VERSION) + ")?$";
@Resource
@Named(ComputeServiceConstants.COMPUTE_LOGGER)
protected Logger logger = Logger.NULL;
private final DockerApi api;
@Inject
public DockerComputeServiceAdapter(DockerApi api) {
this.api = checkNotNull(api, "api");
}
@SuppressWarnings({ "rawtypes", "unchecked" })
@Override
public NodeAndInitialCredentials createNodeWithGroupEncodedIntoName(String group, String name,
Template template) {
checkNotNull(template, "template was null");
TemplateOptions options = template.getOptions();
checkNotNull(options, "template options was null");
String imageId = checkNotNull(template.getImage().getId(), "template image id must not be null");
String loginUser = template.getImage().getDefaultCredentials().getUser();
String loginUserPassword = template.getImage().getDefaultCredentials().getOptionalPassword().or("password");
DockerTemplateOptions templateOptions = DockerTemplateOptions.class.cast(options);
Config containerConfig = null;
Config.Builder containerConfigBuilder = templateOptions.getConfigBuilder();
if (containerConfigBuilder == null) {
containerConfigBuilder = Config.builder().image(imageId);
containerConfigBuilder.entrypoint(templateOptions.getEntrypoint());
containerConfigBuilder.cmd(templateOptions.getCommands());
containerConfigBuilder.memory(templateOptions.getMemory());
containerConfigBuilder.hostname(templateOptions.getHostname());
containerConfigBuilder.cpuShares(templateOptions.getCpuShares());
containerConfigBuilder.openStdin(templateOptions.getOpenStdin());
containerConfigBuilder.env(templateOptions.getEnv());
if (!templateOptions.getVolumes().isEmpty()) {
Map volumes = Maps.newLinkedHashMap();
for (String containerDir : templateOptions.getVolumes().values()) {
volumes.put(containerDir, Maps.newHashMap());
}
containerConfigBuilder.volumes(volumes);
}
HostConfig.Builder hostConfigBuilder = HostConfig.builder()
.publishAllPorts(true)
.privileged(templateOptions.getPrivileged());
if (!templateOptions.getPortBindings().isEmpty()) {
Map>> portBindings = Maps.newHashMap();
for (Map.Entry entry : templateOptions.getPortBindings().entrySet()) {
portBindings.put(entry.getValue() + "/tcp",
Lists.