From f18dd5bed3639882ded33bf22c3346993905c67d Mon Sep 17 00:00:00 2001 From: John Mazzitelli Date: Sat, 3 Oct 2009 10:51:58 -0400 Subject: extend the script plugin so I can pick up its capabilities to get avail, discovery, version checking --- .classpath | 22 +- pom.xml | 351 ++++++++++----------- .../org/rhq/plugins/puppet/PuppetComponent.java | 95 +----- .../org/rhq/plugins/puppet/PuppetDiscovery.java | 139 +++++--- src/main/resources/META-INF/rhq-plugin.xml | 30 +- 5 files changed, 319 insertions(+), 318 deletions(-) diff --git a/.classpath b/.classpath index ee6a949..8905a9b 100644 --- a/.classpath +++ b/.classpath @@ -1,10 +1,12 @@ - - - - - - - - - - + + + + + + + + + + + + diff --git a/pom.xml b/pom.xml index 7710ebd..526a10f 100644 --- a/pom.xml +++ b/pom.xml @@ -1,179 +1,174 @@ - - - 4.0.0 - - - org.rhq - rhq-plugins-parent - 1.4.0-SNAPSHOT - - - org.rhq - puppet-plugin - jar - - RHQ puppet Plugin - Content push via puppet - - - TODO - 1.4.0-SNAPSHOT - - - - - - maven-compiler-plugin - - 1.5 - 1.5 - - - - - - - - - - dev - - - ../../.. - ${rhq.rootDir}/${rhq.defaultDevContainerPath} - ${rhq.containerDir}/jbossas/server/default/deploy/${rhq.earName}/rhq-downloads/rhq-plugins - - - - - - - maven-antrun-plugin - 1.1 - - - - deploy - compile - - - - - *** Updating ${deployment.file}... - - - - - run - - - - - deploy-jar-meta-inf - package - - - - *** Updating META-INF dir in ${deployment.file}... - - - - - - - - - - - run - - - - - undeploy - clean - - - - *** Deleting ${deployment.file}... - - - - - run - - - - - - - - - - - - - - - - - false - - jboss - JBoss Repository - http://repository.jboss.org/maven2/ - - - - - - - - false - - jboss - JBoss Plugin Repository - http://repository.jboss.org/maven2/ - - - - - - - commons-logging - commons-logging - 1.1 - - - org.rhq - rhq-core-domain - ${rhq.version} - provided - - - org.rhq - rhq-core-plugin-api - ${rhq.version} - provided - - - org.rhq - rhq-core-native-system - ${rhq.version} - provided - - - - - + + + 4.0.0 + + + org.rhq + rhq-plugins-parent + 1.4.0-SNAPSHOT + + + org.rhq + puppet-plugin + jar + + RHQ puppet Plugin + Content push via puppet + + + TODO + 1.4.0-SNAPSHOT + + + + + + maven-compiler-plugin + + 1.5 + 1.5 + + + + + + + + dev + + + ../../.. + ${rhq.rootDir}/${rhq.defaultDevContainerPath} + ${rhq.containerDir}/jbossas/server/default/deploy/${rhq.earName}/rhq-downloads/rhq-plugins + + + + + + + maven-antrun-plugin + 1.1 + + + + deploy + compile + + + + + *** Updating ${deployment.file}... + + + + + run + + + + + deploy-jar-meta-inf + package + + + + *** Updating META-INF dir in ${deployment.file}... + + + + + + + + + + + run + + + + + undeploy + clean + + + + *** Deleting ${deployment.file}... + + + + + run + + + + + + + + + + + + + + + + + false + + jboss + JBoss Repository + http://repository.jboss.org/maven2/ + + + + + + + + false + + jboss + JBoss Plugin Repository + http://repository.jboss.org/maven2/ + + + + + + + commons-logging + commons-logging + 1.1 + + + org.rhq + rhq-core-domain + ${rhq.version} + provided + + + org.rhq + rhq-core-plugin-api + ${rhq.version} + provided + + + org.rhq + rhq-core-native-system + ${rhq.version} + provided + + + + org.rhq + rhq-script-plugin + ${rhq.version} + provided + + + \ No newline at end of file diff --git a/src/main/java/org/rhq/plugins/puppet/PuppetComponent.java b/src/main/java/org/rhq/plugins/puppet/PuppetComponent.java index 5dcd657..b311f98 100644 --- a/src/main/java/org/rhq/plugins/puppet/PuppetComponent.java +++ b/src/main/java/org/rhq/plugins/puppet/PuppetComponent.java @@ -1,112 +1,47 @@ - package org.rhq.plugins.puppet; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.rhq.core.domain.configuration.Configuration; -import org.rhq.core.domain.measurement.AvailabilityType; import org.rhq.core.pluginapi.inventory.CreateChildResourceFacet; import org.rhq.core.pluginapi.inventory.CreateResourceReport; import org.rhq.core.pluginapi.inventory.DeleteResourceFacet; import org.rhq.core.pluginapi.inventory.InvalidPluginConfigurationException; import org.rhq.core.pluginapi.inventory.ResourceComponent; -import org.rhq.core.pluginapi.inventory.ResourceContext; -import org.rhq.core.pluginapi.operation.OperationContext; import org.rhq.core.pluginapi.operation.OperationFacet; import org.rhq.core.pluginapi.operation.OperationResult; +import org.rhq.core.system.ProcessExecutionResults; +import org.rhq.core.system.SystemInfo; +import org.rhq.plugins.script.ScriptServerComponent; - -public class PuppetComponent implements ResourceComponent -, OperationFacet -, CreateChildResourceFacet -, DeleteResourceFacet -{ +public class PuppetComponent extends ScriptServerComponent implements ResourceComponent, OperationFacet, CreateChildResourceFacet, + DeleteResourceFacet { + private final Log log = LogFactory.getLog(this.getClass()); - private static final int CHANGEME = 1; // TODO remove or change this - - - - - /** - * Return availability of this resource - * @see ResourceComponent#getAvailability() - */ - public AvailabilityType getAvailability() { - // TODO supply real implementation - return AvailabilityType.UP; - } - - - /** - * Start the resource connection - * @see org.rhq.core.pluginapi.inventory.ResourceComponent#start(org.rhq.core.pluginapi.inventory.ResourceContext) - */ - public void start(ResourceContext context) throws InvalidPluginConfigurationException, Exception { - - Configuration conf = context.getPluginConfiguration(); - // TODO add code to start the resource / connection to it - - - } - - - /** - * Tear down the rescource connection - * @see org.rhq.core.pluginapi.inventory.ResourceComponent#stop() - */ - public void stop() { - + // these are reused from our generic script plugin + protected static final String PLUGINCONFIG_EXECUTABLE = ScriptServerComponent.PLUGINCONFIG_EXECUTABLE; - } - - - - - - public void startOperationFacet(OperationContext context) { - - } - - - /** - * Invokes the passed operation on the managed resource - * @param name Name of the operation - * @param params The method parameters - * @return An operation result - * @see org.rhq.core.pluginapi.operation.OperationFacet - */ public OperationResult invokeOperation(String name, Configuration params) throws Exception { + // TODO OperationResult res = new OperationResult(); if ("dummyOperation".equals(name)) { - // TODO implement me - } return res; } - - - /** - * Create a child resource - * @see org.rhq.core.pluginapi.inventory.CreateChildResourceFacet - */ - public CreateResourceReport createResource(CreateResourceReport report) - { - // TODO supply code to create a child resource - + public CreateResourceReport createResource(CreateResourceReport report) { return null; // TODO change this } - /** - * Delete a child resource - * @see org.rhq.core.pluginapi.inventory.DeleteResourceFacet - */ - public void deleteResource() throws Exception - { + public void deleteResource() throws Exception { // TODO supply code to delete a child resource } + protected static ProcessExecutionResults executeExecutable(SystemInfo sysInfo, Configuration pluginConfig, + String args, long wait, boolean captureOutput) throws InvalidPluginConfigurationException { + return ScriptServerComponent.executeExecutable(sysInfo, pluginConfig, args, wait, captureOutput); + } } diff --git a/src/main/java/org/rhq/plugins/puppet/PuppetDiscovery.java b/src/main/java/org/rhq/plugins/puppet/PuppetDiscovery.java index 4a10f66..fb0133c 100644 --- a/src/main/java/org/rhq/plugins/puppet/PuppetDiscovery.java +++ b/src/main/java/org/rhq/plugins/puppet/PuppetDiscovery.java @@ -1,46 +1,95 @@ -package org.rhq.plugins.puppet; - -import java.util.HashSet; -import java.util.Set; - -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; - -import org.rhq.core.pluginapi.inventory.DiscoveredResourceDetails; -import org.rhq.core.pluginapi.inventory.ResourceDiscoveryComponent; -import org.rhq.core.pluginapi.inventory.ResourceDiscoveryContext; - - -/** - * Discovery class - */ -public class PuppetDiscovery implements ResourceDiscoveryComponent { - - - private final Log log = LogFactory.getLog(this.getClass()); - - - /** - * Run the discovery - */ - public Set discoverResources(ResourceDiscoveryContext discoveryContext) throws Exception { - - Set discoveredResources = new HashSet(); - - /** - * TODO: do your discovery here - * A discovered resource must have a unique key, that must - * stay the same when the resource is discovered the next - * time - */ - DiscoveredResourceDetails detail = null; // new DiscoveredResourceDetails( ); - - - // Add to return values - discoveredResources.add(detail); - log.info("Discovered new ... TODO "); // TODO change - - return discoveredResources; - - } +package org.rhq.plugins.puppet; + +import java.io.File; +import java.util.HashSet; +import java.util.Set; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +import org.rhq.core.domain.configuration.Configuration; +import org.rhq.core.domain.configuration.PropertySimple; +import org.rhq.core.domain.resource.ResourceType; +import org.rhq.core.pluginapi.inventory.DiscoveredResourceDetails; +import org.rhq.core.pluginapi.inventory.ResourceDiscoveryComponent; +import org.rhq.core.pluginapi.inventory.ResourceDiscoveryContext; +import org.rhq.core.system.ProcessExecution; +import org.rhq.core.system.ProcessExecutionResults; +import org.rhq.core.system.SystemInfo; +import org.rhq.plugins.script.ScriptDiscoveryComponent; + +/** + * Puppet client discovery class. + */ +public class PuppetDiscovery extends ScriptDiscoveryComponent implements ResourceDiscoveryComponent { + + private final Log log = LogFactory.getLog(this.getClass()); + + @Override + public Set discoverResources(ResourceDiscoveryContext discoveryContext) { + + String executable = checkExecutables(discoveryContext.getSystemInformation()); + + // if null, it means puppet is not installed or we could not auto-detect its location + if (executable == null) { + return new HashSet(); + } + + // create our plugin config with the executable path, and use the manual-add facet to create our details + Configuration pluginConfig = discoveryContext.getDefaultPluginConfiguration(); + pluginConfig.put(new PropertySimple(PuppetComponent.PLUGINCONFIG_EXECUTABLE, executable)); + + Set discoveredResources = new HashSet(); + DiscoveredResourceDetails details = discoverResource(pluginConfig, discoveryContext); + if (details != null) { + discoveredResources.add(details); + } + + return discoveredResources; + } + + @Override + public DiscoveredResourceDetails discoverResource(Configuration config, ResourceDiscoveryContext context) { + DiscoveredResourceDetails details = super.discoverResource(config, context); + if (details != null) { + details.setResourceName("Puppet Client"); + log.info("Discovered new puppet client at [" + + config.getSimpleValue(PuppetComponent.PLUGINCONFIG_EXECUTABLE, "?") + "]"); + } + return details; + } + + @Override + protected String determineDescription(ResourceDiscoveryContext context, Configuration pluginConfig) { + return "Puppet client that applies recipes to the system"; + } + + private String checkExecutables(SystemInfo systemInfo) { + // Make sure puppet is actually on the system + // First we see if its in a typical place. + String[] executables = new String[] { "/usr/bin/puppet", "/bin/puppet" }; + for (String executable : executables) { + File file = new File(executable); + if (file.canRead()) { + return executable; + } + } + + // We can't find it, use 'which' to see if it knows + String executable = null; + + try { + ProcessExecution processExecution = new ProcessExecution("which"); + processExecution.setArguments(new String[] { "puppet" }); + processExecution.setCaptureOutput(true); + + ProcessExecutionResults executionResults = systemInfo.executeProcess(processExecution); + String capturedOutput = executionResults.getCapturedOutput(); + executable = (((capturedOutput == null) || "".equals(capturedOutput)) ? null : capturedOutput.trim()); + } catch (Exception e) { + log.debug("'which' failed to determine where the puppet executable is. Cause: " + e); + } + + return executable; + } } \ No newline at end of file diff --git a/src/main/resources/META-INF/rhq-plugin.xml b/src/main/resources/META-INF/rhq-plugin.xml index a1124d7..955a09d 100644 --- a/src/main/resources/META-INF/rhq-plugin.xml +++ b/src/main/resources/META-INF/rhq-plugin.xml @@ -6,18 +6,38 @@ xmlns="urn:xmlns:rhq-plugin" xmlns:c="urn:xmlns:rhq-configuration"> + + + supportsManualAdd="true" + description="The puppet client that will be invoked to process puppet recipes"> - - + + + + + + + + + + + + + + + + + + + + + - - -- cgit