Adds SetHostnameCustomizer.MACHINE_FILTER config Defaults to only executing against machines of type SshMachineLocation. But can be set to exclude other machines as needed.
Project: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/commit/790f55cb Tree: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/tree/790f55cb Diff: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/diff/790f55cb Branch: refs/heads/master Commit: 790f55cb0120b47fe942519b90b1bb554a367dab Parents: 474d95b Author: Aled Sage <[email protected]> Authored: Thu Oct 22 20:59:15 2015 +0100 Committer: Aled Sage <[email protected]> Committed: Thu Oct 22 20:59:15 2015 +0100 ---------------------------------------------------------------------- .../entity/machine/SetHostnameCustomizer.java | 38 ++++++++-- .../machine/SetHostnameCustomizerTest.java | 74 +++++++++++++++++++- 2 files changed, 105 insertions(+), 7 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/790f55cb/software/base/src/main/java/org/apache/brooklyn/entity/machine/SetHostnameCustomizer.java ---------------------------------------------------------------------- diff --git a/software/base/src/main/java/org/apache/brooklyn/entity/machine/SetHostnameCustomizer.java b/software/base/src/main/java/org/apache/brooklyn/entity/machine/SetHostnameCustomizer.java index 56d2bcd..54ffa72 100644 --- a/software/base/src/main/java/org/apache/brooklyn/entity/machine/SetHostnameCustomizer.java +++ b/software/base/src/main/java/org/apache/brooklyn/entity/machine/SetHostnameCustomizer.java @@ -20,6 +20,8 @@ package org.apache.brooklyn.entity.machine; import static com.google.common.base.Preconditions.checkArgument; +import java.util.Arrays; + import org.apache.brooklyn.api.location.BasicMachineLocationCustomizer; import org.apache.brooklyn.api.location.MachineLocation; import org.apache.brooklyn.config.ConfigKey; @@ -38,8 +40,11 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.google.common.base.Joiner; +import com.google.common.base.Predicate; +import com.google.common.base.Predicates; import com.google.common.collect.ImmutableMap; import com.google.common.collect.Iterables; +import com.google.common.reflect.TypeToken; /** * Sets the hostname on an ssh'able machine. Currently only CentOS and RHEL are supported. @@ -86,6 +91,12 @@ public class SetHostnameCustomizer extends BasicMachineLocationCustomizer { "hostname.local.address", "Host address, as known on the local box. Config is set on the location."); + public static final ConfigKey<Predicate<? super MachineLocation>> MACHINE_FILTER = ConfigKeys.newConfigKey( + new TypeToken<Predicate<? super MachineLocation>>() {}, + "machineFilter", + "A filter to say which machines this should be applied to", + Predicates.instanceOf(SshMachineLocation.class)); + private final ConfigBag config; public SetHostnameCustomizer(ConfigBag config) { @@ -95,11 +106,23 @@ public class SetHostnameCustomizer extends BasicMachineLocationCustomizer { @Override public void customize(MachineLocation machine) { - String localHostname = setLocalHostname((SshMachineLocation) machine); - machine.config().set(LOCAL_HOSTNAME, localHostname); + if (config.get(MACHINE_FILTER).apply(machine)) { + log.info("SetHostnameCustomizer setting hostname on "+machine); + } else { + log.info("SetHostnameCustomizer ignoring non-ssh machine "+machine); + return; + } - String localIp = execHostnameMinusI((SshMachineLocation) machine); - machine.config().set(LOCAL_IP, localIp); + try { + String localHostname = setLocalHostname((SshMachineLocation) machine); + machine.config().set(LOCAL_HOSTNAME, localHostname); + + String localIp = execHostnameMinusI((SshMachineLocation) machine); + machine.config().set(LOCAL_IP, localIp); + } catch (Exception e) { + log.info("SetHostnameCustomizer failed to set hostname on "+machine+" (rethrowing)", e); + throw e; + } } protected String generateHostname(SshMachineLocation machine) { @@ -200,6 +223,11 @@ public class SetHostnameCustomizer extends BasicMachineLocationCustomizer { protected ProcessTaskWrapper<Integer> exec(SshMachineLocation machine, boolean asRoot, String... cmds) { SshEffectorTaskFactory<Integer> taskFactory = SshEffectorTasks.ssh(machine, cmds); if (asRoot) taskFactory.runAsRoot(); - return DynamicTasks.queue(taskFactory).block(); + ProcessTaskWrapper<Integer> result = DynamicTasks.queue(taskFactory).block(); + if (result.get() != 0) { + throw new IllegalStateException("SetHostnameCustomizer got exit code "+result.get()+" executing on machine "+machine + +"; cmds="+Arrays.asList(cmds)+"; stdout="+result.getStdout()+"; stderr="+result.getStderr()); + } + return result; } } http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/790f55cb/software/base/src/test/java/org/apache/brooklyn/entity/machine/SetHostnameCustomizerTest.java ---------------------------------------------------------------------- diff --git a/software/base/src/test/java/org/apache/brooklyn/entity/machine/SetHostnameCustomizerTest.java b/software/base/src/test/java/org/apache/brooklyn/entity/machine/SetHostnameCustomizerTest.java index 11981ce..2a36017 100644 --- a/software/base/src/test/java/org/apache/brooklyn/entity/machine/SetHostnameCustomizerTest.java +++ b/software/base/src/test/java/org/apache/brooklyn/entity/machine/SetHostnameCustomizerTest.java @@ -19,17 +19,32 @@ package org.apache.brooklyn.entity.machine; import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertFalse; +import static org.testng.Assert.fail; -import java.net.InetAddress; +import java.util.List; +import java.util.Map; import java.util.Set; +import org.apache.brooklyn.api.entity.EntitySpec; import org.apache.brooklyn.api.location.LocationSpec; +import org.apache.brooklyn.api.location.MachineLocation; +import org.apache.brooklyn.core.entity.BrooklynConfigKeys; +import org.apache.brooklyn.core.location.Machines; import org.apache.brooklyn.core.test.BrooklynAppUnitTestSupport; +import org.apache.brooklyn.location.byon.FixedListMachineProvisioningLocation; +import org.apache.brooklyn.location.jclouds.JcloudsLocation; import org.apache.brooklyn.location.ssh.SshMachineLocation; +import org.apache.brooklyn.location.winrm.WinRmMachineLocation; +import org.apache.brooklyn.util.collections.MutableSet; import org.apache.brooklyn.util.core.config.ConfigBag; +import org.apache.brooklyn.util.exceptions.Exceptions; +import org.apache.brooklyn.util.text.StringPredicates; import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; +import com.google.common.base.Functions; +import com.google.common.base.Predicates; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableSet; @@ -42,7 +57,7 @@ public class SetHostnameCustomizerTest extends BrooklynAppUnitTestSupport { super.setUp(); customizer = new SetHostnameCustomizer(ConfigBag.newInstance()); } - + @Test public void testGeneratedHostnameUsesPrivateIp() throws Exception { SshMachineLocation machine = mgmt.getLocationManager().createLocation(LocationSpec.create(SshMachineLocation.class) @@ -76,6 +91,52 @@ public class SetHostnameCustomizerTest extends BrooklynAppUnitTestSupport { assertEquals(customizer.generateHostname(machine), "ip-none-"+machine.getId()); } + + @Test + public void testCustomizerIgnoresNonSshMachines() throws Exception { + WinRmMachineLocation machine = mgmt.getLocationManager().createLocation(LocationSpec.create(WinRmMachineLocation.class) + .configure("address", "4.3.2.1")); + + // Confirm not called (as that would cause error); and visual inspection that logs a nice message. + customizer.customize(machine); + } + + @Test + public void testCustomizerIgnoresNonMatchingMachine() throws Exception { + SshMachineLocation machine = mgmt.getLocationManager().createLocation(LocationSpec.create(SshMachineLocationThrowsException.class) + .configure("address", "4.3.2.1")); + + customizer = new SetHostnameCustomizer(ConfigBag.newInstance() + .configure(SetHostnameCustomizer.MACHINE_FILTER, Predicates.not(Predicates.<MachineLocation>equalTo(machine)))); + + // Confirm not called (as that would cause error); and visual inspection that logs a nice message. + customizer.customize(machine); + } + + @Test + public void testCustomizerPropagatesException() throws Exception { + SshMachineLocation machine = mgmt.getLocationManager().createLocation(LocationSpec.create(SshMachineLocationThrowsException.class) + .configure("address", "4.3.2.1")); + + FixedListMachineProvisioningLocation<?> provisioningLoc = mgmt.getLocationManager().createLocation(LocationSpec.create(FixedListMachineProvisioningLocation.class) + .configure("machines", MutableSet.of(machine))); + + MachineEntity entity = app.createAndManageChild(EntitySpec.create(MachineEntity.class) + .configure(BrooklynConfigKeys.SKIP_ON_BOX_BASE_DIR_RESOLUTION, true) + .configure(MachineEntity.PROVISIONING_PROPERTIES.subKey(JcloudsLocation.MACHINE_LOCATION_CUSTOMIZERS.getName()), ImmutableSet.of(customizer))); + + try { + entity.start(ImmutableList.of(provisioningLoc)); + fail(); + } catch (RuntimeException e) { + if (Exceptions.getFirstThrowableMatching(e, Predicates.compose(StringPredicates.containsLiteral("simulated failure"), Functions.toStringFunction())) == null) { + throw e; + }; + assertFalse(Machines.findUniqueMachineLocation(entity.getLocations(), SshMachineLocation.class).isPresent()); + } + + } + public static class SshMachineLocationWithNoPublicIps extends SshMachineLocation { public SshMachineLocationWithNoPublicIps() { } @@ -84,4 +145,13 @@ public class SetHostnameCustomizerTest extends BrooklynAppUnitTestSupport { return ImmutableSet.<String>of(); } } + + public static class SshMachineLocationThrowsException extends SshMachineLocation { + public SshMachineLocationThrowsException() { + } + @Override + public int execScript(Map<String,?> props, String summaryForLogging, List<String> commands, Map<String,?> env) { + throw new RuntimeException("simulated failure"); + } + } }
