private static Object findConfigValue(Entity entity, String configKeyName) { Map<ConfigKey<?>, Object> config = ((EntityInternal)entity).getAllConfig(); for (Entry<ConfigKey<?>, Object> entry : config.entrySet()) { if (configKeyName.equals(entry.getKey().getName())) return entry.getValue(); } return null; }
public static boolean isManaged(Entity e) { return ((EntityInternal)e).getManagementSupport().isDeployed() && ((EntityInternal)e).getManagementContext().isRunning(); }
@Override public void run() { ((EntityInternal)application).destroy(); mgmt.getEntityManager().unmanage(application); } });
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); } if (locations.size() != 1 || Iterables.getOnlyElement(locations)==null) throw new IllegalArgumentException("Expected one non-null location when starting "+entity()+", but given "+locations); return Iterables.getOnlyElement(locations); }
DynamicTasks.queue("pre-stop", new Callable<String>() { public String call() { if (entity().getAttribute(SoftwareProcess.SERVICE_STATE)==Lifecycle.STOPPED) { log.debug("Skipping stop of entity "+entity()+" when already stopped"); return "Already stopped"; } entity().setAttribute(SoftwareProcess.SERVICE_STATE, Lifecycle.STOPPING); entity().setAttribute(SoftwareProcess.SERVICE_UP, false); preStopCustom(); return null; }});
private ManagementContext mgmt() { return ((EntityInternal)entity).getManagementContext(); }
/** extracts the values for the main brooklyn.ssh.config.* config keys (i.e. those declared in ConfigKeys) * as declared on the entity, and inserts them in a map using the unprefixed state, for ssh. */ /* currently this is computed for each call, which may be wasteful, but it is reliable in the face of config changes. * we could cache the Map. note that we do _not_ cache (or even own) the SshTool; * the SshTool is created or re-used by the SshMachineLocation making use of these properties */ protected Map<String, Object> getSshFlags() { Map<String, Object> result = Maps.newLinkedHashMap(); StringConfigMap globalConfig = ((EntityInternal)getEntity()).getManagementContext().getConfig(); Map<ConfigKey<?>, Object> mgmtConfig = globalConfig.getAllConfig(); Map<ConfigKey<?>, Object> entityConfig = ((EntityInternal)getEntity()).getAllConfig(); Map<ConfigKey<?>, Object> allConfig = MutableMap.<ConfigKey<?>, Object>builder().putAll(mgmtConfig).putAll(entityConfig).build(); for (ConfigKey<?> key : allConfig.keySet()) { if (key.getName().startsWith(SshTool.BROOKLYN_CONFIG_KEY_PREFIX)) { // have to use raw config to test whether the config is set Object val = ((EntityInternal)getEntity()).getConfigMap().getRawConfig(key); if (val!=null) { val = getEntity().getConfig(key); } else { val = globalConfig.getRawConfig(key); if (val!=null) val = globalConfig.getConfig(key); } if (val!=null) { result.put(ConfigUtils.unprefixedKey(SshTool.BROOKLYN_CONFIG_KEY_PREFIX, key).getName(), val); } } } return result; }
recursively(e, new Predicate<EntityInternal>() { public boolean apply(EntityInternal it) { if (shouldSkipUnmanagement(it)) return false; it.getManagementSupport().onManagementStopping(info); return true; } });
MachineProvisioningLocation<MachineLocation> provisioner = entity().getAttribute(SoftwareProcess.PROVISIONING_LOCATION); if (Iterables.isEmpty(entity().getLocations())) { log.debug("No machine decommissioning necessary for "+entity()+" - no locations"); return "No machine decommissioning necessary for - no locations"; entity().removeLocations(ImmutableList.of(machine)); entity().setAttribute(Attributes.HOSTNAME, null); entity().setAttribute(Attributes.ADDRESS, null); entity().setAttribute(Attributes.SUBNET_HOSTNAME, null); entity().setAttribute(Attributes.SUBNET_ADDRESS, null); if (provisioner != null) provisioner.release((MachineLocation)machine); } catch (Throwable t) {
log.info("Stopping {} in {}", entity(), entity().getLocations()); }}); if (entity().getAttribute(SoftwareProcess.SERVICE_STATE)==Lifecycle.STOPPED) { return; throw Exceptions.propagate((Throwable)stoppingProcess.getUnchecked()); entity().setAttribute(SoftwareProcess.SERVICE_UP, false); entity().setAttribute(SoftwareProcess.SERVICE_STATE, Lifecycle.STOPPED);
DynamicTasks.queue("pre-start", new Runnable() { public void run() { MachineLocation machine = machineS.get(); log.info("Starting {} on machine {}", entity(), machine); entity().addLocations(ImmutableList.of((Location)machine)); // elsewhere we rely on (public) hostname being set _after_ subnet_hostname // (to prevent the tiny possibility of races resulting in hostname being returned // simply because subnet is still being looked up) Optional<String> lh = Machines.getSubnetHostname(machine); Optional<String> la = Machines.getSubnetIp(machine); if (lh.isPresent()) entity().setAttribute(Attributes.SUBNET_HOSTNAME, lh.get()); if (la.isPresent()) entity().setAttribute(Attributes.SUBNET_ADDRESS, la.get()); entity().setAttribute(Attributes.HOSTNAME, machine.getAddress().getHostName()); entity().setAttribute(Attributes.ADDRESS, machine.getAddress().getHostAddress()); preStartCustom(machine); }}); }
protected void startWithChefSoloAsync() { // TODO make directories more configurable (both for ssh-drivers and for this) String installDir = Urls.mergePaths(AbstractSoftwareProcessSshDriver.BROOKLYN_HOME_DIR, "chef-install"); String runDir = Urls.mergePaths(AbstractSoftwareProcessSshDriver.BROOKLYN_HOME_DIR, "apps/"+entity().getApplicationId()+"/chef-entities/"+entity().getId()); DynamicTasks.queue( ChefSoloTasks.installChef(installDir, false), ChefSoloTasks.installCookbooks(installDir, ChefConfigs.getRequiredConfig(entity(), CHEF_COOKBOOKS), false)); DynamicTasks.queue(ChefSoloTasks.buildChefFile(runDir, installDir, "launch", ChefConfigs.getRequiredConfig(entity(), CHEF_RUN_LIST), entity().getConfig(CHEF_LAUNCH_ATTRIBUTES))); DynamicTasks.queue(ChefSoloTasks.runChef(runDir, "launch", entity().getConfig(CHEF_RUN_CONVERGE_TWICE))); }
public void setJmxUrl() { ((EntityInternal)entity).setAttribute(JMX_URL, getJmxUrl()); }
DynamicTasks.queue("stopping (machine)", new Callable<String>() { public String call() { if (entity().getAttribute(SoftwareProcess.SERVICE_STATE)==Lifecycle.STOPPED) { log.debug("Skipping stop of entity "+entity()+" when already stopped"); return "Already stopped"; } return stopAnyProvisionedMachines(); }});
protected void postStartCustom() { super.postStartCustom(); // now run the creation script String creationScript; String creationScriptUrl = entity().getConfig(PostgreSqlNode.CREATION_SCRIPT_URL); if (creationScriptUrl != null) creationScript = ResourceUtils.create(entity()).getResourceAsString(creationScriptUrl); else creationScript = entity().getConfig(PostgreSqlNode.CREATION_SCRIPT_CONTENTS); entity().invoke(PostgreSqlNodeChefImpl.EXECUTE_SCRIPT, ConfigBag.newInstance().configure(ExecuteScriptEffectorBody.SCRIPT, creationScript).getAllConfig()).getUnchecked(); // and finally connect sensors ((PostgreSqlNodeChefImpl)entity()).connectSensors(); } protected void preStopCustom() {
builder.isTopLevelApp = (entity instanceof Application && entity.getParent() == null); Map<ConfigKey<?>, Object> localConfig = ((EntityInternal)entity).getConfigMap().getLocalConfig(); for (Map.Entry<ConfigKey<?>, Object> entry : localConfig.entrySet()) { ConfigKey<?> key = checkNotNull(entry.getKey(), localConfig); Map<AttributeSensor, Object> allAttributes = ((EntityInternal)entity).getAllAttributes(); for (Map.Entry<AttributeSensor, Object> entry : allAttributes.entrySet()) { AttributeSensor<?> key = checkNotNull(entry.getKey(), allAttributes);
protected void startWithKnifeAsync() { DynamicTasks.queue( ChefServerTasks.knifeConvergeTask() .knifeRunList(Strings.join(Preconditions.checkNotNull(entity().getConfig(ChefConfig.CHEF_RUN_LIST), "%s must be supplied for %s", ChefConfig.CHEF_RUN_LIST, entity()), ",")) .knifeAddAttributes(entity().getConfig(CHEF_LAUNCH_ATTRIBUTES)) .knifeRunTwice(entity().getConfig(CHEF_RUN_CONVERGE_TWICE)) ); }
/** logs a warning if an entity has a value for a config key */ public static void warnOnIgnoringConfig(Entity entity, ConfigKey<?> key) { if (((EntityInternal)entity).getConfigMap().getRawConfig(key)!=null) log.warn("Ignoring "+key+" set on "+entity+" ("+entity.getConfig(key)+")"); }
protected void addLocations(RebindContext rebindContext, EntityMemento memento) { for (String id : memento.getLocations()) { Location loc = rebindContext.getLocation(id); if (loc != null) { ((EntityInternal)entity).addLocations(ImmutableList.of(loc)); } else { LOG.warn("Location not found; discarding location {} of entity {}({})", new Object[] {id, memento.getType(), memento.getId()}); } } }
@SuppressWarnings("unchecked") protected synchronized T find() { if (entity != null) return entity; if (referrer == null) throw new IllegalStateException("EntityReference "+id+" should have been initialised with a reference parent"); entity = (T) ((EntityInternal)referrer).getManagementContext().getEntityManager().getEntity(id); return entity; }