Skip to content

Commit

Permalink
Set container name sensor to id by default
Browse files Browse the repository at this point in the history
  • Loading branch information
grkvlt committed May 4, 2016
1 parent 8e82130 commit 7c9346c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,7 @@ public void init() {

@Override
public String getDisplayName() {
return String.format("Container (%s)",
Iterables.getFirst(Optional.presentInstances(ImmutableList.of(
Optional.fromNullable(sensors().get(DockerContainer.DOCKER_CONTAINER_NAME)),
Optional.fromNullable(config().get(DockerContainer.DOCKER_IMAGE_NAME)))),
getId()));
return String.format("Container (%s)", DockerUtils.getUniqueContainerName(this));
}

protected void connectSensors() {
Expand Down Expand Up @@ -267,7 +263,7 @@ private DockerTemplateOptions getDockerTemplateOptions() {
Boolean useHostDns = Objects.firstNonNull(entity.config().get(DOCKER_USE_HOST_DNS_NAME), Boolean.FALSE);
String hostname = getDockerHost().sensors().get(Attributes.HOSTNAME);
String address = getDockerHost().sensors().get(Attributes.ADDRESS);
String container = DockerUtils.getUniqueContainerName(entity).or(Optional.fromNullable(getDockerContainerName())).get();
String container = DockerUtils.getUniqueContainerName(entity);
String name = (!useHostDns || hostname.equalsIgnoreCase(address)) ? container : hostname;
options.hostname(name);
options.nodeNames(ImmutableList.of(name));
Expand Down
31 changes: 14 additions & 17 deletions docker/src/main/java/clocker/docker/entity/util/DockerUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -283,22 +283,20 @@ public long build() {
}
}

public static Optional<String> getContainerName(Entity target) {
Optional<String> unique = getUniqueContainerName(target);
if (unique.isPresent()) {
String name = unique.get();
String suffix = "_" + target.getId();
return Optional.of(Strings.removeFromEnd(name, suffix));
} else {
return Optional.absent();
}
public static String getContainerName(Entity target) {
String unique = getUniqueContainerName(target);
String suffix = "_" + target.getId();
return Strings.removeFromEnd(unique, suffix);
}

public static Optional<String> getUniqueContainerName(Entity target) {
public static String getUniqueContainerName(Entity target) {
return Optional.fromNullable(target.sensors().get(DockerContainer.DOCKER_CONTAINER_NAME))
.or(Optional.fromNullable(target.config().get(DockerContainer.DOCKER_CONTAINER_NAME)))
.or(Optional.fromNullable(getContainerNameFromPlan(target)))
.transform(DockerUtils.ALLOWED);
.or(Optional.fromNullable(target.config().get(DockerContainer.DOCKER_IMAGE_NAME)))
.or(Optional.fromNullable(target.getId()))
.transform(DockerUtils.ALLOWED)
.orNull();
}

private static String getContainerNameFromPlan(Entity target) {
Expand All @@ -314,8 +312,7 @@ private static String getContainerNameFromPlan(Entity target) {
/* Generate the address to use to talk to another target entity. */
public static String getTargetAddress(Entity source, Entity target) {
boolean local = source.sensors().get(SoftwareProcess.PROVISIONING_LOCATION).equals(target.sensors().get(SoftwareProcess.PROVISIONING_LOCATION));
List networks = target.sensors().get(SdnAttributes.ATTACHED_NETWORKS);
if ( local ) {
if (local) {
return target.sensors().get(Attributes.SUBNET_ADDRESS);
} else {
return target.sensors().get(Attributes.ADDRESS);
Expand All @@ -325,11 +322,11 @@ public static String getTargetAddress(Entity source, Entity target) {
/* Generate the list of link environment variables. */
public static Map<String, Object> generateLinks(Entity source, Entity target, String alias) {
Entities.waitForServiceUp(target);
Optional<String> from = DockerUtils.getContainerName(source);
Optional<String> to = DockerUtils.getContainerName(target);
String from = DockerUtils.getContainerName(source);
String to = DockerUtils.getContainerName(target);
boolean local = source.sensors().get(SoftwareProcess.PROVISIONING_LOCATION).equals(target.sensors().get(SoftwareProcess.PROVISIONING_LOCATION));
List networks = target.sensors().get(SdnAttributes.ATTACHED_NETWORKS);
if (to.isPresent()) {
if (Strings.isNonBlank(to)) {
// Copy explicitly defined environment variables from target to source
Map<String, Object> env = MutableMap.of();
MutableMap<String, Object> targetEnvironment = MutableMap.copyOf(target.getConfig(DockerContainer.DOCKER_CONTAINER_ENVIRONMENT.getConfigKey()));
Expand Down Expand Up @@ -357,7 +354,7 @@ public static Map<String, Object> generateLinks(Entity source, Entity target, St
}
for (Integer port : ports.keySet()) {
Integer containerPort = ports.get(port);
env.put(String.format("%S_NAME", alias), String.format("/%s/%s", from.or(source.getId()), alias));
env.put(String.format("%S_NAME", alias), String.format("/%s/%s", from, alias));
env.put(String.format("%S_PORT", alias), String.format("tcp://%s:%d", address, port));
env.put(String.format("%S_PORT_%d_TCP", alias, containerPort), String.format("tcp://%s:%d", address, port));
env.put(String.format("%S_PORT_%d_TCP_ADDR", alias, containerPort), address);
Expand Down

0 comments on commit 7c9346c

Please sign in to comment.