Skip to content

Commit

Permalink
Misc tidyup
Browse files Browse the repository at this point in the history
  • Loading branch information
grkvlt committed Apr 20, 2016
1 parent db59087 commit 24679f6
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 76 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ protected Map<String, Object> obtainProvisioningFlags(MachineProvisioningLocatio
flags.put(JcloudsLocationConfig.TEMPLATE_BUILDER.getName(), template);

customizers.add(SoftLayerSameVlanLocationCustomizer.forScope(getApplicationId()));
customizers.add(SoftLayerHostnameCustomizer.instanceOf());
customizers.add(JcloudsHostnameCustomizer.instanceOf());
}

// Save updated customizers list
Expand Down Expand Up @@ -497,7 +497,7 @@ public DockerHostLocation createLocation(Map<String, ?> flags) {
// The 'flags' contains jcloudsLocation and portForwarder already
DockerHostLocation location = getManagementContext().getLocationManager().createLocation(LocationSpec.create(DockerHostLocation.class)
.parent(infrastructure.getDynamicLocation())
.displayName("Docker Host("+getId()+")")
.displayName("Docker Host ("+getId()+")")
.configure(flags)
.configure("owner", getProxy())
.configure("machine", machine)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ public Boolean call() {
// Setup SoftLayer hostname after reboot
MachineProvisioningLocation<?> provisioner = getEntity().sensors().get(SoftwareProcess.PROVISIONING_LOCATION);
if (DockerUtils.isJcloudsLocation(provisioner, SoftLayerConstants.SOFTLAYER_PROVIDER_NAME)) {
SoftLayerHostnameCustomizer.instanceOf().customize((JcloudsLocation) provisioner, (ComputeService) null, (JcloudsMachineLocation) location);
JcloudsHostnameCustomizer.instanceOf().customize((JcloudsLocation) provisioner, (ComputeService) null, (JcloudsMachineLocation) location);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@
/**
* Fix hostname issues on SoftLayer.
*/
public class SoftLayerHostnameCustomizer extends BasicJcloudsLocationCustomizer {
public class JcloudsHostnameCustomizer extends BasicJcloudsLocationCustomizer {

public static final Logger LOG = LoggerFactory.getLogger(SoftLayerHostnameCustomizer.class);
public static final Logger LOG = LoggerFactory.getLogger(JcloudsHostnameCustomizer.class);

public static SoftLayerHostnameCustomizer instanceOf() {
return new SoftLayerHostnameCustomizer();
public static JcloudsHostnameCustomizer instanceOf() {
return new JcloudsHostnameCustomizer();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
import org.apache.brooklyn.util.net.Cidr;
import org.apache.brooklyn.util.net.Protocol;
import org.apache.brooklyn.util.net.Urls;
import org.apache.brooklyn.util.os.Os;
import org.apache.brooklyn.util.ssh.IptablesCommands;
import org.apache.brooklyn.util.ssh.IptablesCommands.Chain;
import org.apache.brooklyn.util.ssh.IptablesCommands.Policy;
Expand Down Expand Up @@ -292,17 +293,14 @@ public int copyTo(final Map<String,?> props, final InputStream src, final String
if (entitySsh && dockerSsh) {
return super.copyTo(nonPortProps, src, destination);
} else {
File tmp = null;
try {
tmp = File.createTempFile(dockerContainer.getId(), Urls.getBasename(destination));
hostMachine.copyTo(nonPortProps, src, tmp.getAbsolutePath());
String tmp = Os.mergePaths("/tmp", Joiner.on('-').join(dockerContainer.getId(), Urls.getBasename(destination), Strings.makeRandomId(4)));
hostMachine.copyTo(nonPortProps, src, tmp);
copyFile(tmp, destination);
src.close();
return 0;
} catch (IOException ioe) {
throw Exceptions.propagate(ioe);
} finally {
if (tmp != null) tmp.delete();
}
}
}
Expand All @@ -315,46 +313,32 @@ public int copyTo(Map<String,?> props, File src, String destination) {
if (entitySsh && dockerSsh) {
return super.copyTo(nonPortProps, src, destination);
} else {
File tmp = null;
try {
tmp = File.createTempFile(dockerContainer.getId(), Urls.getBasename(destination));
hostMachine.copyTo(nonPortProps, src, tmp);
copyFile(tmp, destination);
return 0;
} catch (IOException ioe) {
throw Exceptions.propagate(ioe);
} finally {
if (tmp != null) tmp.delete();
}
String tmp = Os.mergePaths("/tmp", Joiner.on('-').join(dockerContainer.getId(), Urls.getBasename(destination), Strings.makeRandomId(4)));
hostMachine.copyTo(nonPortProps, src, tmp);
copyFile(tmp, destination);
return 0;
}
}

private void copyFile(File src, String dst) {
String cp = String.format("cp %s %s:%s", src.getAbsolutePath(), dockerContainer.getContainerId(), dst);
private void copyFile(String src, String dst) {
String cp = String.format("cp %s %s:%s", src, dockerContainer.getContainerId(), dst);
String output = getOwner().getDockerHost().runDockerCommand(cp);
LOG.info("Copied to {}:{} - result: {}", new Object[] { dockerContainer.getContainerId(), dst, output });
}

@Override
public int copyFrom(final Map<String,?> props, final String remote, final String local) {
Map<String,?> nonPortProps = Maps.filterKeys(props, Predicates.not(Predicates.containsPattern("port")));
boolean entitySsh = Boolean.TRUE.equals(entity.config().get(DockerContainer.DOCKER_USE_SSH));
boolean dockerSsh = Boolean.TRUE.equals(getOwner().config().get(DockerContainer.DOCKER_USE_SSH));
if (entitySsh && dockerSsh) {
return super.copyFrom(props, remote, local);
return super.copyFrom(nonPortProps, remote, local);
} else {
File tmp = null;
try {
tmp = File.createTempFile(dockerContainer.getId(), Urls.getBasename(local));
String cp = String.format("cp %s:%s %s", dockerContainer.getContainerId(), remote, tmp.getAbsolutePath());
String output = getOwner().getDockerHost().runDockerCommand(cp);
Map<String,?> nonPortProps = Maps.filterKeys(props, Predicates.not(Predicates.containsPattern("port")));
hostMachine.copyFrom(nonPortProps, tmp.getAbsolutePath(), local);
LOG.info("Copying from {}:{} to {} - result: {}", new Object[] { dockerContainer.getContainerId(), remote, local, output });
} catch (IOException ioe) {
throw Exceptions.propagate(ioe);
} finally {
if (tmp != null) tmp.delete();
}
String tmp = Os.mergePaths("/tmp", Joiner.on('-').join(dockerContainer.getId(), Urls.getBasename(local), Strings.makeRandomId(4)));
String cp = String.format("cp %s:%s %s", dockerContainer.getContainerId(), remote, tmp);
String output = getOwner().getDockerHost().runDockerCommand(cp);
hostMachine.copyFrom(nonPortProps, tmp, local);
LOG.info("Copying from {}:{} to {} - result: {}", new Object[] { dockerContainer.getContainerId(), remote, local, output });
return 0;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,17 +88,11 @@ public class DockerHostLocation extends AbstractLocation implements MachineProvi

public static final ConfigKey<String> LOCATION_NAME = ConfigKeys.newStringConfigKey("locationName");

public static final ConfigKey<SshMachineLocation> MACHINE = ConfigKeys.newConfigKey(
SshMachineLocation.class,
"machine");
public static final ConfigKey<SshMachineLocation> MACHINE = ConfigKeys.newConfigKey(SshMachineLocation.class, "machine");

public static final ConfigKey<PortForwarder> PORT_FORWARDER = ConfigKeys.newConfigKey(
PortForwarder.class,
"portForwarder");
public static final ConfigKey<PortForwarder> PORT_FORWARDER = ConfigKeys.newConfigKey(PortForwarder.class, "portForwarder");

public static final ConfigKey<JcloudsLocation> JCLOUDS_LOCATION = ConfigKeys.newConfigKey(
JcloudsLocation.class,
"jcloudsLocation");
public static final ConfigKey<JcloudsLocation> JCLOUDS_LOCATION = ConfigKeys.newConfigKey(JcloudsLocation.class, "jcloudsLocation");

@SetFromFlag("locationRegistrationId")
private String locationRegistrationId;
Expand Down Expand Up @@ -130,35 +124,35 @@ public void init() {
// TODO BasicLocationRebindsupport.addCustoms currently calls init() unfortunately!
// Don't checkNotNull in that situation - it could be this location is orphaned!
if (isRebinding()) {
dockerHost = (DockerHost) getConfig(OWNER);
machine = (SshMachineLocation) getConfig(MACHINE);
portForwarder = (PortForwarder) getConfig(PORT_FORWARDER);
jcloudsLocation = (JcloudsLocation) getConfig(JCLOUDS_LOCATION);
dockerHost = (DockerHost) config().get(OWNER);
machine = config().get(MACHINE);
portForwarder = config().get(PORT_FORWARDER);
jcloudsLocation = config().get(JCLOUDS_LOCATION);
} else {
dockerHost = (DockerHost) checkNotNull(getConfig(OWNER), "owner");
machine = (SshMachineLocation) checkNotNull(getConfig(MACHINE), "machine");
portForwarder = (PortForwarder) getConfig(PORT_FORWARDER);
jcloudsLocation = (JcloudsLocation) getConfig(JCLOUDS_LOCATION);
dockerHost = (DockerHost) checkNotNull(config().get(OWNER), "owner");
machine = checkNotNull(config().get(MACHINE), "machine");
portForwarder = config().get(PORT_FORWARDER);
jcloudsLocation = config().get(JCLOUDS_LOCATION);
}
}

@Override
public void rebind() {
super.rebind();

dockerHost = (DockerHost) getConfig(OWNER);
machine = (SshMachineLocation) getConfig(MACHINE);
portForwarder = (PortForwarder) getConfig(PORT_FORWARDER);
jcloudsLocation = (JcloudsLocation) getConfig(JCLOUDS_LOCATION);
dockerHost = (DockerHost) config().get(OWNER);
machine = config().get(MACHINE);
portForwarder = config().get(PORT_FORWARDER);
jcloudsLocation = config().get(JCLOUDS_LOCATION);

if (dockerHost != null && getConfig(LOCATION_NAME) != null) {
register();
}
}

@Override
public LocationDefinition register() {
String locationName = checkNotNull(getConfig(LOCATION_NAME), "config %s", LOCATION_NAME.getName());
String locationName = checkNotNull(config().get(LOCATION_NAME), "config %s", LOCATION_NAME.getName());

LocationDefinition check = getManagementContext().getLocationRegistry().getDefinedLocationByName(locationName);
if (check != null) {
Expand All @@ -171,13 +165,13 @@ public LocationDefinition register() {

LocationDefinition definition = new BasicLocationDefinition(locationName, locationSpec, ImmutableMap.<String, Object>of());
getManagementContext().getLocationRegistry().updateDefinedLocation(definition);

locationRegistrationId = definition.getId();
requestPersist();

return definition;
}

@Override
public void deregister() {
if (locationRegistrationId != null) {
Expand All @@ -186,7 +180,7 @@ public void deregister() {
requestPersist();
}
}

public DockerContainerLocation obtain() throws NoMachinesAvailableException {
return obtain(Maps.<String,Object>newLinkedHashMap());
}
Expand Down Expand Up @@ -229,9 +223,9 @@ public DockerContainerLocation obtain(Map<?,?> flags) throws NoMachinesAvailable

Optional<String> baseImage = Optional.fromNullable(entity.config().get(DockerAttributes.DOCKER_IMAGE_NAME));
String imageTag = Optional.fromNullable(entity.config().get(DockerAttributes.DOCKER_IMAGE_TAG)).or("latest");

boolean autoCheckpointImagePostInstall = Boolean.TRUE.equals(entity.config().get(DockerAttributes.AUTO_CHECKPOINT_DOCKER_IMAGE_POST_INSTALL));

// TODO incorporate more info (incl registry?)
String imageName;
if (autoCheckpointImagePostInstall) {
Expand All @@ -251,7 +245,7 @@ public DockerContainerLocation obtain(Map<?,?> flags) throws NoMachinesAvailable

if (dockerHost.getImageNamed(imageName, imageTag).isPresent()) {
assert autoCheckpointImagePostInstall : "random imageName "+imageName+" collision on host "+getOwner();

// Wait until committed before continuing - Brooklyn may be midway through its creation.
waitForImage(imageName);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,18 @@
import clocker.docker.location.DockerHostLocation;
import clocker.docker.location.strategy.BasicDockerPlacementStrategy;

import com.google.common.base.Functions;
import com.google.common.base.Function;
import com.google.common.base.Splitter;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterables;
import com.google.common.reflect.TypeToken;

import org.jclouds.compute.domain.NodeMetadata;

import org.apache.brooklyn.config.ConfigKey;
import org.apache.brooklyn.core.config.ConfigKeys;
import org.apache.brooklyn.location.jclouds.JcloudsSshMachineLocation;
import org.apache.brooklyn.location.ssh.SshMachineLocation;
import org.apache.brooklyn.util.collections.MutableSet;
import org.apache.brooklyn.util.core.flags.SetFromFlag;

Expand All @@ -51,11 +56,23 @@ public class LabelPlacementStrategy extends BasicDockerPlacementStrategy {
public boolean apply(DockerHostLocation input) {
Set<String> labels = MutableSet.copyOf(config().get(LABELS));
if (labels.isEmpty()) return true;
Set<String> tags = MutableSet.copyOf(Iterables.transform(input.getMachine().tags().getTags(), Functions.toStringFunction()));
labels.removeAll(tags);
LOG.debug("Host {} : Tags {} : Remaining {}",
new Object[] { input, Iterables.toString(tags), labels.isEmpty() ? "none" : Iterables.toString(labels) });

return labels.isEmpty();
SshMachineLocation ssh = input.getMachine();
if (ssh instanceof JcloudsSshMachineLocation) {
JcloudsSshMachineLocation jclouds = (JcloudsSshMachineLocation) ssh;
NodeMetadata metadata = jclouds.getOptionalNode().get();
Set<String> tags = MutableSet.copyOf(Iterables.transform(metadata.getTags(), new Function<String, String>() {
@Override
public String apply(String input) {
return Iterables.get(Splitter.on("=").split(input), 0);
}
}));
tags.addAll(metadata.getUserMetadata().keySet());
labels.removeAll(tags);
LOG.debug("Host {} : Tags {} : Remaining {}",
new Object[] { input, Iterables.toString(tags), labels.isEmpty() ? "none" : Iterables.toString(labels) });
return labels.isEmpty();
} else {
return false;
}
}
}

0 comments on commit 24679f6

Please sign in to comment.