Skip to content
This repository has been archived by the owner on Sep 21, 2021. It is now read-only.

Commit

Permalink
Actually using the DOCKER_HOST env var (also with tcp), fixes #762
Browse files Browse the repository at this point in the history
  • Loading branch information
diemol committed Dec 2, 2018
1 parent d28d6f0 commit 8d4cac5
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 55 deletions.
2 changes: 1 addition & 1 deletion scripts/entry.sh
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ else
if [ "${USE_KUBERNETES}" == "false" ]; then
# Make sure Docker works (with sudo) before continuing
docker --version
sudo docker images elgalu/selenium >/dev/null
sudo docker -H ${DOCKER_HOST} images elgalu/selenium >/dev/null
# Replace the current process with zalenium.sh
exec sudo --preserve-env ./zalenium.sh "$@"
else
Expand Down
15 changes: 7 additions & 8 deletions scripts/zalenium.sh
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ export -f WaitForVideosTransferred
EnsureDockerWorks()
{
log "Ensuring docker works..."
if ! docker ps >/dev/null; then
if ! docker -H ${DOCKER_HOST} ps >/dev/null; then
echo "Docker seems to be not working properly, check the above error."
exit 1
fi
Expand Down Expand Up @@ -210,7 +210,7 @@ DockerTerminate()
elif [[ $KUBERNETES_ENABLED == "true" ]]; then
RANDOM_USER_GA_ID=k8s-$(echo -n $HOSTNAME$KUBERNETES_SERVICE_HOST | md5sum)
else
RANDOM_USER_GA_ID=docker-$(docker info 2>&1 | grep -Po '(?<=^ID: )(\w{4}:.+)')
RANDOM_USER_GA_ID=docker-$(docker -H ${DOCKER_HOST} info 2>&1 | grep -Po '(?<=^ID: )(\w{4}:.+)')
fi

# Gathering the options used to start Zalenium, in order to learn about the used options
Expand Down Expand Up @@ -250,15 +250,14 @@ StartUp()
if [ ${KUBERNETES_ENABLED} == "false" ]; then
EnsureDockerWorks
CONTAINER_ID=$(grep docker /proc/self/cgroup | head -n 1 | grep -o -E '[0-9a-f]{64}' | tail -n 1)
CONTAINER_NAME=$(docker inspect ${CONTAINER_ID} | jq -r '.[0].Name' | sed 's/\///g')
EnsureCleanEnv
CONTAINER_NAME=$(docker -H ${DOCKER_HOST} inspect ${CONTAINER_ID} | jq -r '.[0].Name' | sed 's/\///g')

log "Ensuring docker-selenium is available..."
DOCKER_SELENIUM_IMAGE_COUNT=$(docker images ${SELENIUM_IMAGE_NAME} --quiet | wc -l)
DOCKER_SELENIUM_IMAGE_COUNT=$(docker -H ${DOCKER_HOST} images ${SELENIUM_IMAGE_NAME} --quiet | wc -l)
if [ ${DOCKER_SELENIUM_IMAGE_COUNT} -eq 0 ]; then
if [ ${PULL_SELENIUM_IMAGE:-false} == "true" ]; then
echo "Pulling docker-selenium's image: ${SELENIUM_IMAGE_NAME}"
docker pull ${SELENIUM_IMAGE_NAME}
docker -H ${DOCKER_HOST} pull ${SELENIUM_IMAGE_NAME}
else
echo "Seems that docker-selenium's image has not been pulled yet"
echo "Please run 'docker pull elgalu/selenium', or use your own compatible image via --seleniumImageName"
Expand Down Expand Up @@ -354,7 +353,7 @@ StartUp()
elif [[ $KUBERNETES_ENABLED == "true" ]]; then
RANDOM_USER_GA_ID=k8s-$(echo -n $HOSTNAME$KUBERNETES_SERVICE_HOST | md5sum)
else
RANDOM_USER_GA_ID=docker-$(docker info 2>&1 | grep -Po '(?<=^ID: )(\w{4}:.+)')
RANDOM_USER_GA_ID=docker-$(docker -H ${DOCKER_HOST} info 2>&1 | grep -Po '(?<=^ID: )(\w{4}:.+)')
fi

export ZALENIUM_GA_API_VERSION=${GA_API_VERSION}
Expand Down Expand Up @@ -522,7 +521,7 @@ StartUp()

DisplayDataProcessingAgreement
if [ ${KUBERNETES_ENABLED} == "false" ]; then
docker info >docker_info.txt 2>&1
docker -H ${DOCKER_HOST} info >docker_info.txt 2>&1

# Random ID generated by each docker installation, not related to the user nor the machine
DOCKER_CLIENT_VERSION=$(docker -v)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ public interface ContainerClient {

String getLatestDownloadedImage(String imageName);

int getRunningContainers(String image);

ContainerCreationStatus createContainer(String zaleniumContainerName, String image, Map<String, String> envVars, String nodePort);

void initialiseContainerEnvironment();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,13 @@ public class DockerContainerClient implements ContainerClient {

private static final Environment defaultEnvironment = new Environment();
private static Environment env = defaultEnvironment;
/** Number of times to attempt to create a container when the generated name is not unique. */
/**
* Number of times to attempt to create a container when the generated name is not unique.
*/
private static final int NAME_COLLISION_RETRIES = 10;
private final Logger logger = LoggerFactory.getLogger(DockerContainerClient.class.getName());
private final GoogleAnalyticsApi ga = new GoogleAnalyticsApi();
private DockerClient dockerClient = new DefaultDockerClient("unix:///var/run/docker.sock");
private DockerClient dockerClient = new DefaultDockerClient(dockerHost);
private String nodeId;
private String zaleniumNetwork;
private List<String> zaleniumExtraHosts;
Expand All @@ -80,6 +82,7 @@ public class DockerContainerClient implements ContainerClient {
private boolean isZaleniumPrivileged = true;
private static String seleniumContainerCpuLimit;
private static String seleniumContainerMemoryLimit;
private static String dockerHost;
private ImmutableMap<String, String> storageOpt;
private AtomicBoolean pullSeleniumImageChecked = new AtomicBoolean(false);
private AtomicBoolean isZaleniumPrivilegedChecked = new AtomicBoolean(false);
Expand All @@ -95,6 +98,8 @@ private static void readConfigurationFromEnvVariables() {
String memoryLimit = env.getEnvVariable(ZALENIUM_SELENIUM_CONTAINER_MEMORY_LIMIT);
setSeleniumContainerMemoryLimit(memoryLimit);

String dockerHost = env.getStringEnvVariable("DOCKER_HOST", "unix:///var/run/docker.sock");
setDockerHost(dockerHost);
}

@VisibleForTesting
Expand Down Expand Up @@ -134,18 +139,18 @@ private String getContainerId(String zaleniumContainerName, URL remoteUrl) {
if (DOCKER_NETWORK_HOST_MODE_NAME.equalsIgnoreCase(networkMode)) {
try {
return dockerClient.inspectContainer(container.id())
.config()
.exposedPorts()
.stream()
.anyMatch(port -> port.equalsIgnoreCase(String.valueOf(remoteUrl.getPort())));
.config()
.exposedPorts()
.stream()
.anyMatch(port -> port.equalsIgnoreCase(String.valueOf(remoteUrl.getPort())));
} catch (DockerException | InterruptedException e) {
logger.debug(nodeId + " Error while getting containerId", e);
ga.trackException(e);
}
}
NetworkSettings networkSettings = container.networkSettings();
return networkSettings.networks().values().stream()
.anyMatch(network -> Objects.equals(network.ipAddress(), remoteUrl.getHost()));
.anyMatch(network -> Objects.equals(network.ipAddress(), remoteUrl.getHost()));
})
.findFirst().map(Container::id).orElse(null);
} else {
Expand Down Expand Up @@ -252,6 +257,11 @@ private static void setSeleniumContainerMemoryLimit(String seleniumContainerMemo
DockerContainerClient.seleniumContainerMemoryLimit = seleniumContainerMemoryLimit;
}

private static void setDockerHost(String dockerHost) {
// https://github.com/spotify/docker-client/issues/946
DockerContainerClient.dockerHost = dockerHost.replace("tcp", "http");
}

private static String getSeleniumContainerCpuLimit() {
return seleniumContainerCpuLimit;
}
Expand All @@ -260,23 +270,6 @@ private static String getSeleniumContainerMemoryLimit() {
return seleniumContainerMemoryLimit;
}

public int getRunningContainers(String image) {
try {
List<Container> containerList = dockerClient.listContainers(withStatusRunning(), withStatusCreated());
int numberOfDockerSeleniumContainers = 0;
for (Container container : containerList) {
if (container.image().contains(image) && !"exited".equalsIgnoreCase(container.state())) {
numberOfDockerSeleniumContainers++;
}
}
return numberOfDockerSeleniumContainers;
} catch (InterruptedException | DockerException e) {
logger.warn(nodeId + " Error while getting number of running containers", e);
ga.trackException(e);
}
return 0;
}

public ContainerCreationStatus createContainer(String zaleniumContainerName, String image, Map<String, String> envVars,
String nodePort) {
return createContainer(zaleniumContainerName, image, envVars, nodePort, NAME_COLLISION_RETRIES);
Expand Down Expand Up @@ -333,7 +326,7 @@ private ContainerCreationStatus createContainer(String zaleniumContainerName, St
if (!Strings.isNullOrEmpty(cpuLimit)) {
hostConfigBuilder.nanoCpus(Long.valueOf(cpuLimit));
}
if(!Strings.isNullOrEmpty(memoryLimit)) {
if (!Strings.isNullOrEmpty(memoryLimit)) {
hostConfigBuilder.memory(Long.valueOf(memoryLimit));
}

Expand Down Expand Up @@ -590,7 +583,7 @@ private synchronized String getZaleniumNetwork(String zaleniumContainerName) {

if (zaleniumContainerId == null) {
logger.warn(String.format("Couldn't find selenium container with name or containing: %s, check that the env " +
"variable ZALENIUM_CONTAINER_NAME has an appropriate value", zaleniumContainerName));
"variable ZALENIUM_CONTAINER_NAME has an appropriate value", zaleniumContainerName));
}
try {
ContainerInfo containerInfo = dockerClient.inspectContainer(zaleniumContainerId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ public void onClose(int code,
exec.close();
}

logger.info(String.format("%s completed %s", containerId, Arrays.toString(command)));
logger.debug(String.format("%s completed %s", containerId, Arrays.toString(command)));
logger.debug(String.format("%s %s", containerId, baos.toString()));

return null;
Expand All @@ -293,22 +293,6 @@ public String getLatestDownloadedImage(String imageName) {
return imageName;
}

@Override
public int getRunningContainers(String image) {
PodList list = client.pods().withLabels(createdByZaleniumMap).list();
logger.debug("Pods in the list " + list.getItems().size());

int count=0;
for (Pod pod : list.getItems()) {
String phase = pod.getStatus().getPhase();
if ("Running".equalsIgnoreCase(phase) || "Pending".equalsIgnoreCase(phase)) {
count++;
}
}

return count;
}

@Override
public ContainerCreationStatus createContainer(String zaleniumContainerName, String image, Map<String, String> envVars,
String nodePort) {
Expand Down Expand Up @@ -354,7 +338,7 @@ public void initialiseContainerEnvironment() {
deleteSeleniumPods();

// Register a shutdown hook to cleanup pods
Runtime.getRuntime().addShutdownHook(new Thread(this::deleteSeleniumPods, "KubernetsContainerClient shutdown hook"));
Runtime.getRuntime().addShutdownHook(new Thread(this::deleteSeleniumPods, "KubernetesContainerClient shutdown hook"));
}

@Override
Expand Down

0 comments on commit 8d4cac5

Please sign in to comment.