Skip to content
This repository has been archived by the owner on Jun 22, 2018. It is now read-only.

Commit

Permalink
Merge pull request #304 from ContainerSolutions/envVars
Browse files Browse the repository at this point in the history
feature: Environment variables for all AbstractContainers
  • Loading branch information
sadovnikov committed Mar 17, 2016
2 parents 882b17d + 178ed3e commit 0ade498
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 20 deletions.
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ deps:

clean:
./gradlew clean
docker rmi containersol/minimesos:latest
docker rmi containersol/mesos-hello-world-scheduler:latest
docker rmi containersol/mesos-hello-world-executor:latest
-docker rmi containersol/minimesos:latest
-docker rmi containersol/mesos-hello-world-scheduler:latest
-docker rmi containersol/mesos-hello-world-executor:latest

build:
./gradlew build --info --stacktrace
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

import java.security.SecureRandom;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.concurrent.*;

import static com.jayway.awaitility.Awaitility.await;
Expand All @@ -38,6 +40,8 @@ public abstract class AbstractContainer {

protected DockerClient dockerClient;

protected Map<String, String> envVars = new TreeMap<>();

protected AbstractContainer(DockerClient dockerClient) {
this.dockerClient = dockerClient;
this.uuid = Integer.toUnsignedString(new SecureRandom().nextInt());
Expand Down Expand Up @@ -228,6 +232,10 @@ public String getClusterId() {
return (cluster != null) ? cluster.getClusterId() : null;
}

protected String[] createEnvironment() {
return envVars.entrySet().stream().map(e -> e.getKey() + "=" + e.getValue()).toArray(String[]::new);
}

private class ContainerIsRunning implements Callable<Boolean> {

private String containerId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,12 @@ protected CreateContainerCmd dockerCommand() {
String gatewayIpAddress = DockerContainersUtil.getGatewayIpAddress(dockerClient);
portBindings.bind(consulDNSPort, Ports.Binding(gatewayIpAddress, DNS_PORT));

envVars.put("SERVICE_IGNORE", "1");

return dockerClient.createContainerCmd(config.getImageName() + ":" + config.getImageTag())
.withName(getName())
.withPortBindings(portBindings)
.withEnv(createEnvironment())
.withExposedPorts(consulHTTPPort, consulDNSPort);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.apache.log4j.Logger;

import java.util.ArrayList;
import java.util.Map;
import java.util.TreeMap;

/**
Expand Down Expand Up @@ -91,8 +92,8 @@ protected CreateContainerCmd dockerCommand() {
}

@Override
public TreeMap<String, String> getDefaultEnvVars() {
TreeMap<String,String> envs = new TreeMap<>();
public Map<String, String> getDefaultEnvVars() {
Map<String,String> envs = new TreeMap<>();
envs.put("MESOS_RESOURCES", getResources());
envs.put("MESOS_PORT", String.valueOf(getPortNumber()));
envs.put("MESOS_MASTER", getFormattedZKAddress());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.mashape.unirest.request.GetRequest;
import org.json.JSONObject;

import java.util.Map;
import java.util.TreeMap;

/**
Expand All @@ -37,7 +38,7 @@ protected MesosContainer(DockerClient dockerClient, MesosCluster cluster, String

public abstract int getPortNumber();

protected abstract TreeMap<String, String> getDefaultEnvVars();
protected abstract Map<String, String> getDefaultEnvVars();

@Override
protected void pullImage() {
Expand All @@ -58,13 +59,13 @@ public String getMesosImageName() {
}

protected String[] createMesosLocalEnvironment() {
TreeMap<String, String> map = getDefaultEnvVars();
map.putAll(getSharedEnvVars());
return map.entrySet().stream().map(e -> e.getKey() + "=" + e.getValue()).toArray(String[]::new);
envVars.putAll(getDefaultEnvVars());
envVars.putAll(getSharedEnvVars());
return createEnvironment();
}

protected TreeMap<String, String> getSharedEnvVars() {
TreeMap<String,String> envs = new TreeMap<>();
protected Map<String, String> getSharedEnvVars() {
Map<String, String> envs = new TreeMap<>();
envs.put("GLOG_v", "1");
envs.put("MESOS_EXECUTOR_REGISTRATION_TIMEOUT", "5mins");
envs.put("MESOS_CONTAINERIZERS", "docker,mesos");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ public int getPortNumber() {
}

@Override
public TreeMap<String, String> getDefaultEnvVars() {
TreeMap<String, String> envs = new TreeMap<>();
public Map<String, String> getDefaultEnvVars() {
Map<String, String> envs = new TreeMap<>();
envs.put("MESOS_QUORUM", "1");
envs.put("MESOS_ZK", getFormattedZKAddress());
envs.put("MESOS_LOGGING_LEVEL", getLoggingLevel());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;

public class ConsulRegistrationTest {

Expand Down Expand Up @@ -52,4 +53,21 @@ public void testRegisterServiceWithConsul() throws UnirestException {
assertEquals(HelloWorldContainer.SERVICE_PORT, service.getInt("ServicePort"));
}

@Test
public void testConsulShouldBeIgnored() throws UnirestException {
String ipAddress = DockerContainersUtil.getIpAddress(dockerClient, CLUSTER.getConsulContainer().getContainerId());
String url = String.format("http://%s:%d/v1/catalog/services", ipAddress, ConsulConfig.CONSUL_HTTP_PORT);

JSONArray body = Unirest.get(url).asJson().getBody().getArray();
assertEquals(1, body.length());

JSONObject service = body.getJSONObject(0);
assertFalse(service.has("consul-server-8300"));
assertFalse(service.has("consul-server-8301"));
assertFalse(service.has("consul-server-8302"));
assertFalse(service.has("consul-server-8400"));
assertFalse(service.has("consul-server-8500"));
assertFalse(service.has("consul-server-8600"));
}

}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.containersol.minimesos;

import com.containersol.minimesos.cluster.MesosCluster;
import com.containersol.minimesos.mesos.DockerClientFactory;
import com.containersol.minimesos.mesos.ClusterArchitecture;
import com.containersol.minimesos.mesos.DockerClientFactory;
import com.containersol.minimesos.mesos.MesosMaster;
import com.containersol.minimesos.mesos.ZooKeeper;
import com.github.dockerjava.api.DockerClient;
Expand All @@ -11,8 +11,6 @@
import org.junit.ClassRule;
import org.junit.Test;

import java.util.TreeMap;

public class FlagsTest {

public static final String aclExampleJson = "{ \"run_tasks\": [ { \"principals\": { \"values\": [\"foo\", \"bar\"] }, \"users\": { \"values\": [\"alice\"] } } ] }";
Expand Down Expand Up @@ -46,11 +44,11 @@ protected MesosMasterEnvVars(DockerClient dockerClient, ZooKeeper zooKeeperConta

@Override
protected String[] createMesosLocalEnvironment() {
TreeMap<String, String> envs = getDefaultEnvVars();
envs.put("MESOS_AUTHENTICATE", "true");
envs.put("MESOS_ACLS", aclExampleJson);
envVars.putAll(getDefaultEnvVars());
envVars.put("MESOS_AUTHENTICATE", "true");
envVars.put("MESOS_ACLS", aclExampleJson);

return envs.entrySet().stream().map(e -> e.getKey() + "=" + e.getValue()).toArray(String[]::new);
return createEnvironment();
}
}
}
Expand Down

0 comments on commit 0ade498

Please sign in to comment.