private ManagementContext mgmt() { return ((EntityInternal)entity).getManagementContext(); }
@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; }
@SuppressWarnings("unchecked") protected synchronized Collection<T> find() { if (entities!=null) return entities.values(); if (referrer == null) throw new IllegalStateException("EntityReference should have been initialised with a reference parent"); Map<String,T> result = new LinkedHashMap<String,T>(); for (String it : entityRefs) { Entity e = ((EntityInternal)referrer).getManagementContext().getEntityManager().getEntity(it); if (e==null) { LOG.warn("unable to find {}, referred to by {}", it, referrer); } else { result.put(e.getId(), (T)e); } } entities = result; return entities.values(); } }
public String getInstallDir() { // Cache it; evaluate lazily (and late) to ensure managementContext.config is accessible and completed its setup // Caching has the benefit that the driver is usable, even if the entity is unmanaged (useful in some tests!) if (installDir == null) { String installBasedir = ((EntityInternal)entity).getManagementContext().getConfig().getFirst("brooklyn.dirs.install"); if (installBasedir == null) installBasedir = DEFAULT_INSTALL_BASEDIR; if (installBasedir.endsWith(File.separator)) installBasedir.substring(0, installBasedir.length()-1); installDir = elvis(entity.getConfig(SoftwareProcess.SUGGESTED_INSTALL_DIR), installBasedir+"/"+getEntityVersionLabel("/")); } return installDir; }
public static DownloadResolver newDownloader(EntityDriver driver, Map<String,?> properties) { EntityInternal internal = (EntityInternal) driver.getEntity(); return internal.getManagementContext().getEntityDownloadsManager().newDownloader(driver, properties); }
public static boolean isManaged(Entity e) { return ((EntityInternal)e).getManagementSupport().isDeployed() && ((EntityInternal)e).getManagementContext().isRunning(); }
public String getRunDir() { if (runDir == null) { String runBasedir = ((EntityInternal)entity).getManagementContext().getConfig().getFirst("brooklyn.dirs.run"); if (runBasedir == null) runBasedir = DEFAULT_RUN_BASEDIR; if (runBasedir.endsWith(File.separator)) runBasedir.substring(0, runBasedir.length()-1); runDir = elvis(entity.getConfig(SoftwareProcess.SUGGESTED_RUN_DIR), runBasedir+"/"+entity.getApplication().getId()+"/"+"entities"+"/"+ getEntityVersionLabel()+"_"+entity.getId()); } return runDir; }
public static void unmanage(Entity entity) { if (((EntityInternal)entity).getManagementSupport().isDeployed()) { ((EntityInternal)entity).getManagementContext().getEntityManager().unmanage(entity); } }
@Override public void install() { //mysql-${version}-${driver.osTag}.tar.gz DownloadResolver resolver = ((EntityInternal)entity).getManagementContext().getEntityDownloadsManager().newDownloader( this, ImmutableMap.of("filename", getInstallFilename())); List<String> urls = resolver.getTargets(); String saveAs = resolver.getFilename(); _expandedInstallDir = getInstallDir()+"/"+resolver.getUnpackedDirectoryName(format("mysql-%s-%s", getVersion(), getOsTag())); List<String> commands = new LinkedList<String>(); commands.add(BashCommands.INSTALL_TAR); commands.add(BashCommands.INSTALL_CURL); commands.add("echo installing extra packages"); commands.add(installPackage(ImmutableMap.of("yum", "libgcc_s.so.1"), null)); commands.add(installPackage(ImmutableMap.of("yum", "libaio.so.1 libncurses.so.5", "apt", "libaio1 libaio-dev"), null)); // these deps are needed on some OS versions but others don't need them so ignore failures (ok(...)) commands.add(ok(installPackage(ImmutableMap.of("yum", "libaio", "apt", "ia32-libs"), null))); commands.add("echo finished installing extra packages"); commands.addAll(commandsToDownloadUrlsAs(urls, saveAs)); commands.add(format("tar xfvz %s", saveAs)); newScript(INSTALLING). body.append(commands).execute(); }
@Override public void install() { // mariadb-${version}-${driver.osTag}.tar.gz ManagementContext managementContext = ((EntityInternal) entity).getManagementContext(); DownloadResolverManager downloadManager = managementContext.getEntityDownloadsManager(); DownloadResolver resolver = downloadManager.newDownloader( this, ImmutableMap.of("filename", getInstallFilename())); List<String> urls = resolver.getTargets(); String saveAs = resolver.getFilename(); expandedInstallDir = getInstallDir() + "/" + resolver.getUnpackedDirectoryName(format("mariadb-%s-%s", getVersion(), getOsTag())); List<String> commands = new LinkedList<String>(); commands.add(BashCommands.INSTALL_TAR); commands.add(BashCommands.INSTALL_CURL); commands.add("echo installing extra packages"); commands.add(installPackage(ImmutableMap.of("yum", "libgcc_s.so.1"), null)); commands.add(installPackage(ImmutableMap.of("yum", "libaio.so.1 libncurses.so.5", "apt", "libaio1 libaio-dev"), null)); // these deps are needed on some OS versions but others don't need them so ignore failures (ok(...)) commands.add(ok(installPackage(ImmutableMap.of("yum", "libaio", "apt", "ia32-libs"), null))); commands.add("echo finished installing extra packages"); commands.addAll(BashCommands.commandsToDownloadUrlsAs(urls, saveAs)); commands.add(format("tar xfvz %s", saveAs)); newScript(INSTALLING). body.append(commands).execute(); }
@Override public void install() { DownloadResolver resolver = ((EntityInternal)entity).getManagementContext().getEntityDownloadsManager().newDownloader(this); List<String> urls = resolver.getTargets(); String saveAs = resolver.getFilename(); expandedInstallDir = getInstallDir() + "/" + resolver.getUnpackedDirectoryName(format("monit-%s", getVersion())); List<String> commands = ImmutableList.<String>builder() .add(BashCommands.INSTALL_TAR) .add(BashCommands.INSTALL_CURL) .add(BashCommands.commandToDownloadUrlsAs(urls, saveAs)) .add(format("tar xfvz %s", saveAs)) .build(); newScript(INSTALLING) .failOnNonZeroResultCode() .body .append(commands) .execute(); }
public synchronized void attemptLegacyAutodeployment(String effectorName) { if (managementContext!=null) { log.warn("Autodeployment suggested but not required for "+entity+"."+effectorName); return; } if (entity instanceof Application) { log.warn("Autodeployment with new management context triggered for "+entity+"."+effectorName+" -- will not be supported in future. Explicit manage call required."); if (initialManagementContext != null) { initialManagementContext.getEntityManager().manage(entity); } else { Entities.startManagement(entity); } return; } if ("start".equals(effectorName)) { Entity e=entity; if (e.getParent()!=null && ((EntityInternal)e.getParent()).getManagementSupport().isDeployed()) { log.warn("Autodeployment in parent's management context triggered for "+entity+"."+effectorName+" -- will not be supported in future. Explicit manage call required."); ((EntityInternal)e.getParent()).getManagementContext().getEntityManager().manage(entity); return; } } log.warn("Autodeployment not available for "+entity+"."+effectorName); }
public static <T> Task<T> invokeEffectorAsync(Entity entity, Effector<T> eff, Map<String,?> parameters) { String id = entity.getId(); String name = eff.getName(); if (log.isDebugEnabled()) log.debug("Invoking-async effector {} on {}", new Object[] {name, entity}); if (log.isTraceEnabled()) log.trace("Invoking-async effector {} on {} with args {}", new Object[] {name, entity, parameters}); EntityManagementSupport mgmtSupport = ((EntityInternal)entity).getManagementSupport(); if (!mgmtSupport.isDeployed()) { mgmtSupport.attemptLegacyAutodeployment(name); } ManagementContextInternal mgmtContext = (ManagementContextInternal) ((EntityInternal)entity).getManagementContext(); // FIXME seems brittle to have the listeners in the Utils method; better to move into the context.invokeEff // (or whatever the last mile before invoking the effector is) mgmtSupport.getEntityChangeListener().onEffectorStarting(eff); try { return mgmtContext.invokeEffector(entity, eff, parameters); } finally { // FIXME this is really Effector submitted mgmtSupport.getEntityChangeListener().onEffectorCompleted(eff); } }
/** brings this entity under management iff its ancestor is managed, returns true in that case; * otherwise returns false in the expectation that the ancestor will become managed, * or throws exception if it has no parent or a non-application root * (will throw if e is an Application; see also {@link #startManagement(Entity)} ) */ public static boolean manage(Entity e) { Entity o = e.getParent(); Entity eum = e; //highest unmanaged ancestor if (o==null) throw new IllegalStateException("Can't manage "+e+" because it is an orphan"); while (o.getParent()!=null) { if (!isManaged(o)) eum = o; o = o.getParent(); } if (isManaged(o)) { ((EntityInternal)o).getManagementContext().getEntityManager().manage(eum); return true; } if (!(o instanceof Application)) throw new IllegalStateException("Can't manage "+e+" because it is not rooted at an application"); return false; }
/** * Invokes a method effector so that its progress is tracked. For internal use only, when we know the effector is backed by a method which is local. */ public static <T> T invokeMethodEffector(Entity entity, Effector<T> eff, Object[] args) { String name = eff.getName(); try { if (log.isDebugEnabled()) log.debug("Invoking effector {} on {}", new Object[] {name, entity}); if (log.isTraceEnabled()) log.trace("Invoking effector {} on {} with args {}", new Object[] {name, entity, args}); EntityManagementSupport mgmtSupport = ((EntityInternal)entity).getManagementSupport(); if (!mgmtSupport.isDeployed()) { mgmtSupport.attemptLegacyAutodeployment(name); } ManagementContextInternal mgmtContext = (ManagementContextInternal) ((EntityInternal)entity).getManagementContext(); mgmtSupport.getEntityChangeListener().onEffectorStarting(eff); try { return mgmtContext.invokeEffectorMethodSync(entity, eff, args); } finally { mgmtSupport.getEntityChangeListener().onEffectorCompleted(eff); } } catch (Exception e) { handleEffectorException(entity, eff, e); // (won't return below) return null; } }
/** brings this entity under management, creating a local management context if necessary * (assuming root is an application). * returns existing management context if there is one (non-deployment), * or new local mgmt context if not, * or throwing exception if root is not an application * <p> * callers are recommended to use {@link #manage(Entity)} instead unless they know * a plain-vanilla non-root management context is sufficient (e.g. in tests) * <p> * this method may change, but is provided as a stop-gap to prevent ad-hoc things * being done in the code which are even more likely to break! */ public static ManagementContext startManagement(Entity e) { Entity o = e; Entity eum = e; //highest unmanaged ancestor while (o.getParent()!=null) { if (!isManaged(o)) eum = o; o = o.getParent(); } if (isManaged(o)) { ManagementContext mgmt = ((EntityInternal)o).getManagementContext(); mgmt.getEntityManager().manage(eum); return mgmt; } if (!(o instanceof Application)) throw new IllegalStateException("Can't manage "+e+" because it is not rooted at an application"); ManagementContext mgmt = new LocalManagementContext(); mgmt.getEntityManager().manage(o); return mgmt; }
/** 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; }