@Override public String call() { if (entity().getAttribute(SoftwareProcess.SERVICE_STATE_ACTUAL) == Lifecycle.STOPPED) { log.debug("Skipping stop of entity " + entity() + " when already stopped"); return "Already stopped"; } ServiceStateLogic.setExpectedState(entity(), Lifecycle.STOPPING); entity().sensors().set(SoftwareProcess.SERVICE_UP, false); preStopCustom(); return null; } }
@Override public void run() { DynamicTasks.waitForLast(); entity().sensors().set(SoftwareProcess.INSTALL_DIR, (String)null); entity().config().set(SoftwareProcess.INSTALL_UNIQUE_LABEL, (String)null); entity().config().putAll(parameters.getAllConfig()); entity().sensors().set(BrooklynNode.DOWNLOAD_URL, entity().getConfig(DOWNLOAD_URL)); // Setting SUGGESTED_VERSION will result in an new empty INSTALL_FOLDER, but clear it // just in case the user specified already installed version. ((BrooklynNodeDriver)((DriverDependentEntity<?>)entity()).getDriver()).clearInstallDir(); } }).build());
@Override protected Feed getInstanceFromId(Optional<Entity> entity, String id) { if (id == null || !entity.isPresent()) return null; for (Feed feed : ((EntityInternal)entity).feeds().getFeeds()) { if (id.equals(feed.getId())) { return feed; } } return null; } }
public static void unmanage(Entity entity) { if (((EntityInternal)entity).getManagementSupport().isDeployed()) { ((EntityInternal)entity).getManagementContext().getEntityManager().unmanage(entity); } }
protected String getNodeName() { // (node name is needed so we can node delete it) // TODO would be better if CHEF_NODE_NAME were a freemarker template, could access entity.id, or hostname, etc, // in addition to supporting hard-coded node names (which is all we support so far). String nodeName = entity().getConfig(ChefConfig.CHEF_NODE_NAME); if (Strings.isNonBlank(nodeName)) return Strings.makeValidFilename(nodeName); // node name is taken from ID of this entity, if not specified return entity().getId(); }
@SuppressWarnings({ "unchecked", "deprecation" }) protected void startWithChefSoloAsync() { String baseDir = MachineLifecycleEffectorTasks.resolveOnBoxDir(entity(), Machines.findUniqueMachineLocation(entity().getLocations(), SshMachineLocation.class).get()); String installDir = Urls.mergePaths(baseDir, "installs/chef"); ConfigBag.newInstance( entity().getConfig(CHEF_COOKBOOK_URLS) ) .putIfAbsent( entity().getConfig(CHEF_COOKBOOK_URLS) ) .getAllConfig(); if (cookbooks.isEmpty()) if (Strings.isNonBlank(primary)) attrs.at(primary); attrs.at("config"); attrs.put( entity().config().getBag().getAllConfig() ); attrs.root().put((Map<?,?>)Tasks.resolveDeepValue(entity().getConfig(CHEF_LAUNCH_ATTRIBUTES), Object.class, entity().getExecutionContext())); } catch (Exception e) { Exceptions.propagate(e); } Collection<? extends String> runList = entity().getConfig(CHEF_LAUNCH_RUN_LIST); if (runList==null) runList = entity().getConfig(CHEF_RUN_LIST); if (runList==null) { if (Strings.isNonBlank(primary)) runList = ImmutableList.of(primary+"::"+"start"); "apps/"+entity().getApplicationId()+"/chef/entities/"+entity().getEntityType().getSimpleName()+"_"+entity().getId()); DynamicTasks.queue(ChefSoloTasks.runChef(runDir, "launch", entity().getConfig(CHEF_RUN_CONVERGE_TWICE)));
public static <K, V> V put(Entity entity, AttributeSensor<Map<K,V>> attribute, K key, V value) { Map<K, V> oldMap = entity.getAttribute(attribute); Map<K, V> newMap = MutableMap.copyOf(oldMap); V oldVal = newMap.put(key, value); ((EntityInternal)entity).sensors().set(attribute, newMap); return oldVal; }
/** replaces the attributes underneath the rootAttribute parameter with the given value; * see {@link #addLaunchAttributesMap(EntitySpec, Map)} for richer functionality */ public static void setLaunchAttribute(EntityInternal entity, String rootAttribute, Object value) { entity.config().set(ChefConfig.CHEF_LAUNCH_ATTRIBUTES.subKey(rootAttribute), value); }
@Test public void testRemoveSensorThroughEntity() throws Exception{ entity.sensors().set(TEST_SENSOR, "abc"); entity.removeAttribute(TEST_SENSOR); assertFalse(entity.getEntityType().getSensors().contains(TEST_SENSOR), "sensors="+entity.getEntityType().getSensors()); assertEquals(entity.getAttribute(TEST_SENSOR), null); assertEventuallyListenerEventsEqual(ImmutableList.of(BasicSensorEvent.ofUnchecked(SENSOR_ADDED, entity, TEST_SENSOR), BasicSensorEvent.ofUnchecked(SENSOR_REMOVED, entity, TEST_SENSOR))); }
@Override protected ExecutionContext getExecutionContext(BrooklynObject bo) { // TODO expose ((AbstractEntityAdjunct)bo).execution ? Entity entity = ((AbstractEntityAdjunct)bo).entity; return (entity != null) ? ((EntityInternal)entity).getExecutionContext() : null; }
public ClassLoaderUtils(Class<?> callingClass, Entity entity) { checkNotNull(callingClass, "callingClass"); this.classLoader = getValidClassLoader(callingClass.getClassLoader()); this.entity = checkNotNull(entity, "entity"); this.mgmt = ((EntityInternal)entity).getManagementContext(); }
@Test public void testAddSensorValueThroughEntity() throws Exception{ entity.sensors().set(TEST_SENSOR, "abc"); assertEquals(entity.getEntityType().getSensors(), ImmutableSet.builder().addAll(DEFAULT_SENSORS).add(TEST_SENSOR).build()); assertEventuallyListenerEventsEqual(ImmutableList.of(BasicSensorEvent.ofUnchecked(SENSOR_ADDED, entity, TEST_SENSOR))); }
@Test public void testMergesKeyFromRuntimeParentWithOwnDeprecated() throws Exception { EntityInternal entity = app.addChild(EntitySpec.create(TestEntity.class) .configure("confMapDeepMerge", ImmutableMap.of("mykey", "myval"))); EntityInternal child = entity.addChild(EntitySpec.create(MyEntity.class) .configure("oldConfMapDeepMerge", ImmutableMap.of("mykey2", "myval2"))); assertEquals(child.config().get(MyEntity.CONF_MAP_DEEP_MERGE), ImmutableMap.of("mykey", "myval", "mykey2", "myval2")); }
@Test public void testAddSensors() throws Exception{ entity.getMutableEntityType().addSensor(TEST_SENSOR); assertEquals(entity.getEntityType().getSensors(), ImmutableSet.builder().addAll(DEFAULT_SENSORS).add(TEST_SENSOR).build()); assertEventuallyListenerEventsEqual(ImmutableList.of(BasicSensorEvent.ofUnchecked(SENSOR_ADDED, entity, TEST_SENSOR))); }
private Entity getMasterNode() { return entity().getAttribute(BrooklynCluster.MASTER_NODE); } }
protected Location getLocation(@Nullable Collection<? extends Location> locations) { if (locations==null || locations.isEmpty()) locations = entity().getLocations(); if (locations.isEmpty()) { MachineProvisioningLocation<?> provisioner = entity().getAttribute(SoftwareProcess.PROVISIONING_LOCATION); if (provisioner!=null) locations = Arrays.<Location>asList(provisioner); } locations = Locations.getLocationsCheckingAncestors(locations, entity()); Maybe<MachineLocation> ml = Locations.findUniqueMachineLocation(locations); if (ml.isPresent()) return ml.get(); if (locations.isEmpty()) throw new IllegalArgumentException("No locations specified when starting "+entity()); if (locations.size() != 1 || Iterables.getOnlyElement(locations)==null) throw new IllegalArgumentException("Ambiguous locations detected when starting "+entity()+": "+locations); return Iterables.getOnlyElement(locations); }
@Override public void apply(EntityLocal entity) { ((EntityInternal)entity).getMutableEntityType().addEffector(effector); }
@Test public void testDeletesOrphanedFeeds() throws Exception { EntityInternal entity = origApp.addChild(EntitySpec.create(TestEntity.class).impl(MyEntity.class)); Feed feed = entity.feeds().add(new MyFeed()); MementoTweaker tweaker = new MementoTweaker(new Deletions().entities(entity.getId())); assertTransformDeletes(new Deletions().feeds(feed.getId()), tweaker); }
@Test public void testEffectorBodyAddedImplicitlyButBodylessSignatureInvoked() throws Exception { EntityInternal doubler = app.createAndManageChild(EntitySpec.create(TestEntity.class)); // add it doubler.getMutableEntityType().addEffector(DOUBLE_1); // invoke it, but using something with equivalent name (and signature -- though only name is used currently) // ensures that the call picks up the body by looking in the actual entity Assert.assertEquals(doubler.invoke(DOUBLE_BODYLESS, MutableMap.of("numberToDouble", 3)).get(), (Integer)6); }
private Object invokeEffectorNamed(String effectorName, ConfigBag params) { LOG.info("{} invoking effector on {}, effector={}, parameters={}", new Object[]{this, entity(), effectorName, params}); Maybe<Effector<?>> effector = entity().getEntityType().getEffectorByName(effectorName); if (effector.isAbsent()) { throw new IllegalStateException("Cannot find effector " + effectorName); } return entity().invoke(effector.get(), params.getAllConfig()).getUnchecked(); }