private boolean belongsInServerPool(Entity member) { if (member.sensors().get(Attributes.HOSTNAME) == null) { LOG.debug("Members of {}, checking {}, eliminating because hostname not yet set", this, member); return false; } if (!getMembers().contains(member)) { LOG.debug("Members of {}, checking {}, eliminating because not member", this, member); return false; } LOG.debug("Members of {}, checking {}, approving", this, member); return true; }
@Override protected Optional<HostAndPort> getMappedEndpoint(Entity source, MachineLocation machine, int port) { String address = source.sensors().get(Attributes.SUBNET_ADDRESS); if (Strings.isNonBlank(address)) { return Optional.of(HostAndPort.fromParts(address, port)); } else { return Optional.absent(); } } }
/** specifies the supplied timeout if the condition is met */ public <T2> Builder<T,V> timeoutIf(Entity source, AttributeSensor<T2> sensor, Predicate<? super T2> predicate, Duration val) { if (predicate.apply(source.sensors().get(sensor))) timeout(val); return this; } public Builder<T,V> onTimeoutReturn(V val) {
@Override public void onException(Exception exception) { log.error("Detected exception while retrieving Chef attributes from entity " + entity.getDisplayName(), exception); for (AttributeSensor<?> attribute : chefAttributeSensors.values()) { if (!attribute.getName().startsWith(CHEF_ATTRIBUTE_PREFIX)) continue; entity.sensors().set(attribute, null); } }
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))) .or(Optional.fromNullable(target.config().get(DockerContainer.DOCKER_IMAGE_NAME))) .or(Optional.fromNullable(target.getId())) .transform(DockerUtils.ALLOWED) .orNull(); }
@Override public Map<String, String> getShellEnvironment() { Entity etcdNode = getEntity().config().get(CalicoNode.ETCD_NODE); HostAndPort etcdAuthority = HostAndPort.fromParts(etcdNode.sensors().get(Attributes.SUBNET_ADDRESS), etcdNode.sensors().get(EtcdNode.ETCD_CLIENT_PORT)); Map<String, String> environment = MutableMap.copyOf(super.getShellEnvironment()); environment.put("ETCD_AUTHORITY", etcdAuthority.toString()); return environment; }
@Override public void run() { Map<?, ?> actual = entity.sensors().get(mapSensor); String errMsg = "actual="+actual+"; notExpected="+keys; for (Object key : keys) { assertFalse(actual.containsKey(key), errMsg); } }}); }
/** * Convenience for ensuring an individual sensor is set from its config key * (e.g. sub-classes of DynamicWebAppCluster that don't want to set HTTP_PORT etc!) */ public static <T> T apply(Entity entity, AttributeSensorAndConfigKey<?,T> key) { T v = entity.getAttribute(key); if (v!=null) return v; v = key.getAsSensorValue(entity); if (v!=null) entity.sensors().set(key, v); return v; }
@Override public void run() { Map<?, ?> actual = entity.sensors().get(mapSensor); String errMsg = "actual="+actual+"; expected="+expected; for (Map.Entry<?,?> entry : expected.entrySet()) { assertTrue(actual.containsKey(entry.getKey()), errMsg); assertEquals(actual.get(entry.getKey()), entry.getValue(), errMsg); } }}); }
@Override public Boolean call() throws Exception { if (cluster.getMembers().isEmpty()) return false; if (Lifecycle.RUNNING != cluster.sensors().get(SERVICE_STATE_ACTUAL)) return false; for (Entity member : cluster.getMembers()) if (!Boolean.TRUE.equals(member.sensors().get(SERVICE_UP))) return false; return true; } }
private void assertState(Entity entity, Lifecycle expectedState, Lifecycle state) { Transition actualExpectedState = entity.sensors().get(Attributes.SERVICE_STATE_EXPECTED); Lifecycle actualState = entity.sensors().get(Attributes.SERVICE_STATE_ACTUAL); Boolean actualIsUp = entity.sensors().get(Attributes.SERVICE_UP); String msg = "actualExpectedState="+actualExpectedState+", actualState="+actualState+", actualIsUp="+actualIsUp; if (expectedState != null) { assertEquals(actualExpectedState.getState(), expectedState, msg); } else { assertTrue(actualExpectedState == null || actualExpectedState.getState() == null, msg); } assertEquals(actualState, state, msg); }
@Override public Object call() { return app.sensors().set(Sensors.newStringSensor("myOtherSensor"), "myObject"); }}); assertEquals(entity.config().get(TestEntity.CONF_OBJECT), "myObject");
public static void configurePortMappings(Entity entity) { DockerContainer container = (DockerContainer) entity.sensors().get(DockerContainer.CONTAINER); DockerContainerLocation location = (DockerContainerLocation) container.sensors().get(DockerContainer.DYNAMIC_LOCATION); Set<Integer> ports = DockerUtils.getContainerPorts(entity); for (Integer portNumber : ports) { location.obtainSpecificPort(portNumber); } }
public JmxHelper(Entity entity) { this(toJmxUrl(entity), entity, entity.getAttribute(UsesJmx.JMX_USER), entity.getAttribute(UsesJmx.JMX_PASSWORD)); if (entity.getAttribute(UsesJmx.JMX_URL) == null) { entity.sensors().set(UsesJmx.JMX_URL, url); } }
@Override public Object call() { return app.sensors().set(Sensors.newStringSensor("myOtherSensor"), "myObject"); }}); assertEquals(app.config().get(TestEntity.CONF_OBJECT), "myObject");
@Test public void testDeferredDslObjectAsFirstArgument() throws Exception { final Entity app = createAndStartApplication( "services:", "- type: " + BasicApplication.class.getName(), " location: localhost", " brooklyn.config:", " dest: $brooklyn:attributeWhenReady(\"targetValue\").config(\"spec.final\")"); AttributeSensor<Location> targetValueSensor = Sensors.newSensor(Location.class, "targetValue"); app.sensors().set(targetValueSensor, Iterables.getOnlyElement(app.getLocations())); assertEquals(getConfigEventually(app, DEST), "localhost"); }
@Test public void testRegexReplacementWithAttributeWhenReady() throws Exception { Entity testEntity = setupAndCheckTestEntityInBasicYamlWith( " brooklyn.config:", " test.regex.config: $brooklyn:regexReplacement($brooklyn:attributeWhenReady(\"test.regex.source\"), $brooklyn:attributeWhenReady(\"test.regex.pattern\"), $brooklyn:attributeWhenReady(\"test.regex.replacement\"))" ); testEntity.sensors().set(Sensors.newStringSensor("test.regex.source"), "somefooname"); testEntity.sensors().set(Sensors.newStringSensor("test.regex.pattern"), "foo"); testEntity.sensors().set(Sensors.newStringSensor("test.regex.replacement"), "bar"); Assert.assertEquals("somebarname", testEntity.getConfig(ConfigKeys.newStringConfigKey("test.regex.config"))); }
@Test public void testDeferredDslChainingOnSensor() throws Exception { final Entity app = createAndStartApplication( "services:", "- type: " + BasicApplication.class.getName(), " brooklyn.config:", " dest: $brooklyn:attributeWhenReady(\"targetValue\").isSupplierEvaluated()"); AttributeSensor<TestDslSupplierValue> targetValueSensor = Sensors.newSensor(TestDslSupplierValue.class, "targetValue"); app.sensors().set(targetValueSensor, new TestDslSupplierValue()); assertEquals(getConfigEventually(app, DEST), Boolean.TRUE); }
@Test public void testDslEntityWhereIdRetrievedFromAttributeWhenReadyDsl() throws Exception { Entity testEntity = setupAndCheckTestEntityInBasicYamlWith( " id: x", " brooklyn.config:", " test.confObject: $brooklyn:entity(attributeWhenReady(\"mySensor\"))"); testEntity.sensors().set(Sensors.newStringSensor("mySensor"), "x"); Assert.assertEquals(getConfigInTask(testEntity, TestEntity.CONF_OBJECT), testEntity); Entity e2 = rebind(testEntity); Assert.assertEquals(getConfigInTask(e2, TestEntity.CONF_OBJECT), e2); }
@Override public boolean isRunning() { String customCommand = getEntity().config().get(VanillaSoftwareProcess.CHECK_RUNNING_COMMAND); if (Strings.isBlank(customCommand)) { return getOwnerEntity().sensors().get(Startable.SERVICE_UP); } else { return newScript(CHECK_RUNNING).body.append(customCommand).execute() == 0; } }