modules/plugins/jboss-as-5/src/main/java/org/rhq/plugins/jbossas5/deploy/RemoteDownloader.java | 20 + modules/plugins/jboss-as-5/src/main/java/org/rhq/plugins/jbossas5/deploy/ScriptDeployer.java | 52 ++++ modules/plugins/jboss-as-5/src/main/java/org/rhq/plugins/jbossas5/helper/CreateChildResourceFacetDelegate.java | 2 modules/plugins/jboss-as-5/src/main/java/org/rhq/plugins/jbossas5/script/ScriptComponent.java | 105 +++++++++- 4 files changed, 172 insertions(+), 7 deletions(-)
New commits: commit 689adfded272d5021c61a05e67acd87b0717205a Author: Lukas Krejci lkrejci@redhat.com Date: Thu Nov 18 14:16:50 2010 +0100
BZ 568917 - Implementing ContentFacet on ScriptComponent so that the support for uploading a new script is complete.
diff --git a/modules/plugins/jboss-as-5/src/main/java/org/rhq/plugins/jbossas5/deploy/RemoteDownloader.java b/modules/plugins/jboss-as-5/src/main/java/org/rhq/plugins/jbossas5/deploy/RemoteDownloader.java index dd408d9..39ed50c 100644 --- a/modules/plugins/jboss-as-5/src/main/java/org/rhq/plugins/jbossas5/deploy/RemoteDownloader.java +++ b/modules/plugins/jboss-as-5/src/main/java/org/rhq/plugins/jbossas5/deploy/RemoteDownloader.java @@ -46,15 +46,20 @@ import org.rhq.plugins.jbossas5.util.ManagedComponentUtils; public class RemoteDownloader implements PackageDownloader {
private ResourceContext<?> resourceContext; + private boolean resourceExists; private ProfileServiceConnection profileServiceConnection; private static final Log LOG = LogFactory.getLog(RemoteDownloader.class);
/** - * @param resourceContext + * @param resourceContext the resource context if the resource exists or + * the parent resource context if the resource doesn't exist yet + * @param resourceExists this tells the downloader which API to use. This has to go + * hand in hand with the resourceContext that is being provided. * @param profileServiceConnection */ - public RemoteDownloader(ResourceContext<?> resourceContext, ProfileServiceConnection profileServiceConnection) { - this.resourceContext = resourceContext; + public RemoteDownloader(ResourceContext<?> resourceContext, boolean resourceExists, ProfileServiceConnection profileServiceConnection) { + this.resourceContext = resourceContext; + this.resourceExists = resourceExists; this.profileServiceConnection = profileServiceConnection; }
@@ -77,8 +82,13 @@ public class RemoteDownloader implements PackageDownloader { os = new BufferedOutputStream(new FileOutputStream(contentCopy)); ContentContext contentContext = resourceContext.getContentContext(); ContentServices contentServices = contentContext.getContentServices(); - contentServices.downloadPackageBitsForChildResource(contentContext, resourceType.getName(), key, os); - + + if (resourceExists) { + contentServices.downloadPackageBits(contentContext, key, os, true); + } else { + contentServices.downloadPackageBitsForChildResource(contentContext, resourceType.getName(), key, os); + } + return contentCopy; } catch (IOException e) { throw new IllegalStateException("Failed to copy the deployed archive to destination.", e); diff --git a/modules/plugins/jboss-as-5/src/main/java/org/rhq/plugins/jbossas5/deploy/ScriptDeployer.java b/modules/plugins/jboss-as-5/src/main/java/org/rhq/plugins/jbossas5/deploy/ScriptDeployer.java index 3897832..52331c0 100644 --- a/modules/plugins/jboss-as-5/src/main/java/org/rhq/plugins/jbossas5/deploy/ScriptDeployer.java +++ b/modules/plugins/jboss-as-5/src/main/java/org/rhq/plugins/jbossas5/deploy/ScriptDeployer.java @@ -22,6 +22,8 @@ package org.rhq.plugins.jbossas5.deploy; import java.io.File;
import org.rhq.core.domain.configuration.Configuration; +import org.rhq.core.domain.content.transfer.ContentResponseResult; +import org.rhq.core.domain.content.transfer.DeployIndividualPackageResponse; import org.rhq.core.domain.content.transfer.ResourcePackageDetails; import org.rhq.core.domain.resource.CreateResourceStatus; import org.rhq.core.domain.resource.ResourceType; @@ -96,4 +98,54 @@ public class ScriptDeployer implements Deployer { } }
+ public DeployIndividualPackageResponse update(ResourcePackageDetails existingPackage, ResourceType resourceType) { + File script = null; + DeployIndividualPackageResponse response = new DeployIndividualPackageResponse(existingPackage.getKey()); + + try { + + ResourcePackageDetails details = existingPackage; + script = downloader.prepareArchive(details.getKey(), resourceType); + + String targetFileName = script.getName(); + + Configuration deploymentConfig = details.getDeploymentTimeConfiguration(); + if (deploymentConfig != null) { + targetFileName = deploymentConfig.getSimpleValue(PROP_TARGET_FILE_NAME, targetFileName); + } + + File target = new File(binDir, targetFileName); + + if (!ScriptFileFinder.getScriptFileFilter(systemInfo).accept(target)) { + response.setResult(ContentResponseResult.FAILURE); + response.setErrorMessage("The supplied filename has a wrong extension. One of: " + + ScriptFileFinder.getSupportedFileExtensions(systemInfo) + " was expected."); + + return response; + } + + if (!target.exists()) { + response.setResult(ContentResponseResult.FAILURE); + response.setErrorMessage("A file called '" + targetFileName + + "' already exists in the server bin directory."); + + return response; + } + + FileUtil.copyFile(script, target); + + response.setResult(ContentResponseResult.SUCCESS); + + return response; + } catch (Exception e) { + response.setErrorMessage(e.getMessage()); + response.setResult(ContentResponseResult.FAILURE); + + return response; + } finally { + if (script != null) { + downloader.destroyArchive(script); + } + } + } } diff --git a/modules/plugins/jboss-as-5/src/main/java/org/rhq/plugins/jbossas5/helper/CreateChildResourceFacetDelegate.java b/modules/plugins/jboss-as-5/src/main/java/org/rhq/plugins/jbossas5/helper/CreateChildResourceFacetDelegate.java index 1998ba8..5a9bed1 100644 --- a/modules/plugins/jboss-as-5/src/main/java/org/rhq/plugins/jbossas5/helper/CreateChildResourceFacetDelegate.java +++ b/modules/plugins/jboss-as-5/src/main/java/org/rhq/plugins/jbossas5/helper/CreateChildResourceFacetDelegate.java @@ -150,7 +150,7 @@ public class CreateChildResourceFacetDelegate implements CreateChildResourceFace ProfileServiceConnection profileServiceConnection = this.component.getConnection();
PackageDownloader downloader = runningEmbedded() ? new LocalDownloader() : new RemoteDownloader( - component.getResourceContext(), profileServiceConnection); + component.getResourceContext(), false, profileServiceConnection);
if (ScriptComponent.TYPE_NAME.equals(resourceType.getName())) { String jbossHome = component.getResourceContext().getPluginConfiguration() diff --git a/modules/plugins/jboss-as-5/src/main/java/org/rhq/plugins/jbossas5/script/ScriptComponent.java b/modules/plugins/jboss-as-5/src/main/java/org/rhq/plugins/jbossas5/script/ScriptComponent.java index deaf284..fd283f5 100644 --- a/modules/plugins/jboss-as-5/src/main/java/org/rhq/plugins/jbossas5/script/ScriptComponent.java +++ b/modules/plugins/jboss-as-5/src/main/java/org/rhq/plugins/jbossas5/script/ScriptComponent.java @@ -23,11 +23,16 @@ package org.rhq.plugins.jbossas5.script;
import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; import java.io.IOException; +import java.io.InputStream; import java.util.ArrayList; +import java.util.HashSet; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; +import java.util.Set; import java.util.StringTokenizer; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -37,7 +42,17 @@ import org.apache.commons.logging.LogFactory; import org.jetbrains.annotations.NotNull; import org.rhq.core.domain.configuration.Configuration; import org.rhq.core.domain.configuration.PropertySimple; +import org.rhq.core.domain.content.PackageDetailsKey; +import org.rhq.core.domain.content.PackageType; +import org.rhq.core.domain.content.transfer.ContentResponseResult; +import org.rhq.core.domain.content.transfer.DeployIndividualPackageResponse; +import org.rhq.core.domain.content.transfer.DeployPackageStep; +import org.rhq.core.domain.content.transfer.DeployPackagesResponse; +import org.rhq.core.domain.content.transfer.RemovePackagesResponse; +import org.rhq.core.domain.content.transfer.ResourcePackageDetails; import org.rhq.core.domain.measurement.AvailabilityType; +import org.rhq.core.pluginapi.content.ContentFacet; +import org.rhq.core.pluginapi.content.ContentServices; import org.rhq.core.pluginapi.inventory.DeleteResourceFacet; import org.rhq.core.pluginapi.inventory.ResourceComponent; import org.rhq.core.pluginapi.inventory.ResourceContext; @@ -47,15 +62,23 @@ import org.rhq.core.pluginapi.util.ProcessExecutionUtility; import org.rhq.core.system.ProcessExecution; import org.rhq.core.system.ProcessExecutionResults; import org.rhq.core.system.SystemInfo; +import org.rhq.core.util.MessageDigestGenerator; import org.rhq.plugins.jbossas5.ApplicationServerComponent; +import org.rhq.plugins.jbossas5.ApplicationServerPluginConfigurationProperties; +import org.rhq.plugins.jbossas5.connection.ProfileServiceConnection; +import org.rhq.plugins.jbossas5.deploy.Deployer; +import org.rhq.plugins.jbossas5.deploy.RemoteDownloader; +import org.rhq.plugins.jbossas5.deploy.ScriptDeployer;
/** * A JON service that provides the ability to execute a script. * * @author Ian Springer + * @author Lukas Krejci */ -public class ScriptComponent implements ResourceComponent<ApplicationServerComponent>, OperationFacet, DeleteResourceFacet { +public class ScriptComponent implements ResourceComponent<ApplicationServerComponent>, OperationFacet, DeleteResourceFacet, ContentFacet { public static final String TYPE_NAME = "Script"; + public static final String PACKAGE_TYPE = "script";
public static final String PATH_CONFIG_PROP = "path"; public static final String ENVIRONMENT_VARIABLES_CONFIG_PROP = "environmentVariables"; @@ -67,6 +90,9 @@ public class ScriptComponent implements ResourceComponent<ApplicationServerCompo private static final String EXIT_CODE_RESULT_PROP = "exitCode"; private static final String OUTPUT_RESULT_PROP = "output";
+ private static final String PACKAGE_VERSION = "none"; + private static final String PACKAGE_ARCHITECTURE = "noarch"; + private final Log log = LogFactory.getLog(this.getClass());
private ResourceContext<ApplicationServerComponent> resourceContext; @@ -142,6 +168,83 @@ public class ScriptComponent implements ResourceComponent<ApplicationServerCompo } }
+ public List<DeployPackageStep> generateInstallationSteps(ResourcePackageDetails packageDetails) { + return null; + } + + public DeployPackagesResponse deployPackages(Set<ResourcePackageDetails> packages, ContentServices contentServices) { + DeployPackagesResponse response = new DeployPackagesResponse(); + if (packages.size() != 1) { + log.warn("Request to deploy a script containing multiple files, which is obivously illegal: " + packages); + response.setOverallRequestResult(ContentResponseResult.FAILURE); + response.setOverallRequestErrorMessage("Only one script can be updated at a time."); + return response; + } + + try { + String jbossHomeDir = resourceContext.getParentResourceComponent().getResourceContext().getPluginConfiguration().getSimpleValue(ApplicationServerPluginConfigurationProperties.HOME_DIR, null); + SystemInfo systemInfo = resourceContext.getSystemInformation(); + ProfileServiceConnection profileServiceConnection = resourceContext.getParentResourceComponent().getConnection(); + + ScriptDeployer deployer = new ScriptDeployer(jbossHomeDir, systemInfo, new RemoteDownloader(resourceContext, true, profileServiceConnection)); + ResourcePackageDetails packageDetails = packages.iterator().next(); + + DeployIndividualPackageResponse scriptUpdateResult = deployer.update(packageDetails, resourceContext.getResourceType()); + + response.setOverallRequestResult(scriptUpdateResult.getResult()); + response.addPackageResponse(scriptUpdateResult); + } catch (Exception e) { + response.setOverallRequestErrorMessage(e.getMessage()); + response.setOverallRequestResult(ContentResponseResult.FAILURE); + } + + return response; + } + + public RemovePackagesResponse removePackages(Set<ResourcePackageDetails> packages) { + throw new UnsupportedOperationException("Cannot remove a package backing a script."); + } + + public Set<ResourcePackageDetails> discoverDeployedPackages(PackageType type) { + Set<ResourcePackageDetails> results = new HashSet<ResourcePackageDetails>(); + if (PACKAGE_TYPE.equals(type.getName())) { + File scriptFile = new File(resourceContext.getResourceKey()); + + PackageDetailsKey key = new PackageDetailsKey(scriptFile.getName(), PACKAGE_VERSION, PACKAGE_TYPE, PACKAGE_ARCHITECTURE); + ResourcePackageDetails details = new ResourcePackageDetails(key); + details.setDisplayName(scriptFile.getName()); + details.setFileName(scriptFile.getAbsolutePath()); + details.setFileSize(scriptFile.length()); + details.setLocation(scriptFile.getAbsolutePath()); + details.setFileCreatedDate(scriptFile.lastModified()); + details.setInstallationTimestamp(System.currentTimeMillis()); + try { + details.setSHA256(new MessageDigestGenerator(MessageDigestGenerator.SHA_256).calcDigestString(scriptFile)); + } catch (IOException e) { + log.warn("Failed to compute the SHA256 digest of the script: " + scriptFile.getAbsolutePath(), e); + } + results.add(details); + } + + return results; + } + + public InputStream retrievePackageBits(ResourcePackageDetails packageDetails) { + String jbossHomeDir = resourceContext.getParentResourceComponent().getResourceContext().getPluginConfiguration().getSimpleValue(ApplicationServerPluginConfigurationProperties.HOME_DIR, null); + File binDir = new File(jbossHomeDir, "bin"); + + String scriptName = packageDetails.getKey().getName(); + + File script = new File(binDir, scriptName); + + try { + return new FileInputStream(script); + } catch (FileNotFoundException e) { + log.warn("Failed to retrieve package bits for script " + packageDetails, e); + return null; + } + } + private void setCommandLineArguments(Configuration params, ProcessExecution processExecution) { List<String> processExecutionArguments = processExecution.getArguments(); if (null == processExecutionArguments) {
rhq-commits@lists.fedorahosted.org