diff --git a/docker/src/main/java/clocker/docker/entity/container/DockerContainerImpl.java b/docker/src/main/java/clocker/docker/entity/container/DockerContainerImpl.java index 11543ea9..0cd2d94a 100644 --- a/docker/src/main/java/clocker/docker/entity/container/DockerContainerImpl.java +++ b/docker/src/main/java/clocker/docker/entity/container/DockerContainerImpl.java @@ -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() { @@ -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)); diff --git a/docker/src/main/java/clocker/docker/entity/util/DockerUtils.java b/docker/src/main/java/clocker/docker/entity/util/DockerUtils.java index 7cd7ad70..ccd9233c 100644 --- a/docker/src/main/java/clocker/docker/entity/util/DockerUtils.java +++ b/docker/src/main/java/clocker/docker/entity/util/DockerUtils.java @@ -283,22 +283,20 @@ public long build() { } } - public static Optional getContainerName(Entity target) { - Optional 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 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) { @@ -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); @@ -325,11 +322,11 @@ public static String getTargetAddress(Entity source, Entity target) { /* Generate the list of link environment variables. */ public static Map generateLinks(Entity source, Entity target, String alias) { Entities.waitForServiceUp(target); - Optional from = DockerUtils.getContainerName(source); - Optional 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 env = MutableMap.of(); MutableMap targetEnvironment = MutableMap.copyOf(target.getConfig(DockerContainer.DOCKER_CONTAINER_ENVIRONMENT.getConfigKey())); @@ -357,7 +354,7 @@ public static Map 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);