Skip to content
This repository has been archived by the owner on Jul 11, 2022. It is now read-only.

Commit

Permalink
Merge pull request #163 from lzoubek/bugs/1188751
Browse files Browse the repository at this point in the history
Bug 1188751 - Deploying Versioned WAR package from repository to JBOSS EAP 6  deployment resource fails
  • Loading branch information
lzoubek committed Apr 1, 2015
2 parents f41bcf9 + 2b77882 commit 48e5671
Show file tree
Hide file tree
Showing 7 changed files with 309 additions and 78 deletions.
Expand Up @@ -38,9 +38,9 @@ abstract public class AbstractVersionedSubsystemDiscovery extends SubsystemDisco
static private final String PATTERN_DISABLE = "disable";
static private final String PATTERN_PROP = "rhq.as7.VersionedSubsystemDiscovery.pattern";

static protected final Matcher MATCHER;
static public final Matcher MATCHER;
static protected final String SUBDEPLOYMENT_TYPE = "Subdeployment";
static protected boolean DISABLED = false;
static public boolean DISABLED = false;

static {
Matcher m = null;
Expand Down
Expand Up @@ -23,7 +23,6 @@

import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
Expand Down Expand Up @@ -66,10 +65,9 @@
import org.rhq.core.pluginapi.operation.OperationFacet;
import org.rhq.core.pluginapi.operation.OperationResult;
import org.rhq.core.pluginapi.util.StartScriptConfiguration;
import org.rhq.modules.plugins.jbossas7.helper.Deployer;
import org.rhq.modules.plugins.jbossas7.json.Address;
import org.rhq.modules.plugins.jbossas7.json.CompositeOperation;
import org.rhq.modules.plugins.jbossas7.json.Operation;
import org.rhq.modules.plugins.jbossas7.json.PROPERTY_VALUE;
import org.rhq.modules.plugins.jbossas7.json.ReadAttribute;
import org.rhq.modules.plugins.jbossas7.json.ReadChildrenNames;
import org.rhq.modules.plugins.jbossas7.json.ReadResource;
Expand Down Expand Up @@ -544,64 +542,12 @@ protected CreateResourceReport deployContent(CreateResourceReport report) {
public CreateResourceReport runDeploymentMagicOnServer(CreateResourceReport report, String runtimeName,
String deploymentName, String hash) {

boolean toServerGroup = context.getResourceKey().contains("server-group=");
LOG.info(
"Deploying [" + deploymentName + " (runtimeName=" + runtimeName + ")] (toDomainOnly=" + !toServerGroup
+ ")...");
LOG.info("Deploying [" + deploymentName + " (runtimeName=" + runtimeName + ")] ...");

ASConnection connection = getASConnection();
Deployer deployer = new Deployer(deploymentName, runtimeName, hash, getASConnection());

Operation step1 = new Operation("add", "deployment", deploymentName);
// step1.addAdditionalProperty("hash", new PROPERTY_VALUE("BYTES_VALUE", hash));
List<Object> content = new ArrayList<Object>(1);
Map<String, Object> contentValues = new HashMap<String, Object>();
contentValues.put("hash", new PROPERTY_VALUE("BYTES_VALUE", hash));
content.add(contentValues);
step1.addAdditionalProperty("content", content);

step1.addAdditionalProperty("name", deploymentName);
step1.addAdditionalProperty("runtime-name", runtimeName);

String resourceKey;
Result result;

CompositeOperation cop = new CompositeOperation();
cop.addStep(step1);
/*
* We need to check here if this is an upload to /deployment only
* or if this should be deployed to a server group too
*/

if (!toServerGroup) {

// if standalone, then :deploy the deployment anyway
if (context.getResourceType().getName().contains("Standalone")) {
Operation step2 = new Operation("deploy", step1.getAddress());
cop.addStep(step2);
}

result = connection.execute(cop, 300);
resourceKey = step1.getAddress().getPath();

} else {

Address serverGroupAddress = new Address(context.getResourceKey());
serverGroupAddress.add("deployment", deploymentName);
Operation step2 = new Operation("add", serverGroupAddress);

cop.addStep(step2);

Operation step3 = new Operation("deploy", serverGroupAddress);
cop.addStep(step3);

resourceKey = serverGroupAddress.getPath();

if (verbose) {
LOG.info("Deploy operation: " + cop);
}

result = connection.execute(cop, 300);
}
Result result = deployer.deployToServer(context.getResourceType().getName().contains("Standalone"));
String resourceKey = deployer.getNewResourceKey();

if ((!result.isSuccess())) {
String failureDescription = result.getFailureDescription();
Expand Down
Expand Up @@ -32,6 +32,7 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;

import org.codehaus.jackson.JsonNode;

Expand All @@ -58,10 +59,12 @@
import org.rhq.core.util.ByteUtil;
import org.rhq.core.util.file.ContentFileInfo;
import org.rhq.core.util.file.JarContentFileInfo;
import org.rhq.modules.plugins.jbossas7.helper.Deployer;
import org.rhq.modules.plugins.jbossas7.json.Address;
import org.rhq.modules.plugins.jbossas7.json.Operation;
import org.rhq.modules.plugins.jbossas7.json.ReadAttribute;
import org.rhq.modules.plugins.jbossas7.json.ReadResource;
import org.rhq.modules.plugins.jbossas7.json.Remove;
import org.rhq.modules.plugins.jbossas7.json.Result;

/**
Expand Down Expand Up @@ -156,6 +159,21 @@ public List<DeployPackageStep> generateInstallationSteps(ResourcePackageDetails
return new ArrayList<DeployPackageStep>();
}

/**
* create new Deployer instance for {@link #deployPackages(Set, ContentServices)} code branch which *only* handles package update
* in case of Versioned Deployments
* @see {@link #deployPackages(Set, ContentServices)} and {@link DomainDeploymentComponent#createDeployerForPackageUpdate(String, String, String)}
* @param deploymentName
* @param runtimeName
* @param hash
* @return new Deployer which correctly undeploys original package and then deploys new package
*/
protected Deployer createDeployerForPackageUpdate(String deploymentName, String runtimeName, String hash) {
Deployer deployer = new Deployer(deploymentName, runtimeName, hash, getASConnection());
deployer.addBeforeDeployStep(new Remove(getAddress()));
return deployer;
}

@Override
public DeployPackagesResponse deployPackages(Set<ResourcePackageDetails> packages, ContentServices contentServices) {
getLog().debug("Starting deployment..");
Expand All @@ -180,7 +198,7 @@ public DeployPackagesResponse deployPackages(Set<ResourcePackageDetails> package
}
ResourceType resourceType = context.getResourceType();

getLog().info("Deploying " + resourceType.getName() + " Resource with key [" + detail.getKey() + "]...");
getLog().info("Deploying " + resourceType.getName() + " to Resource with key [" + detail.getKey() + "]...");

try {
contentServices.downloadPackageBits(context.getContentContext(), detail.getKey(), out, true);
Expand All @@ -206,19 +224,86 @@ public DeployPackagesResponse deployPackages(Set<ResourcePackageDetails> package
String hash = resultNode.get("BYTES_VALUE").getTextValue();

try {
Redeployer redeployer = new Redeployer(detail.getKey().getName(), hash, getASConnection());
Result result = redeployer.redeployOnServer();
if (result.isRolledBack()) {
response.setOverallRequestResult(ContentResponseResult.FAILURE);
response.setOverallRequestErrorMessage("Rolled Back: " + result.getFailureDescription());
} else {
response.setOverallRequestResult(ContentResponseResult.SUCCESS);
//we just deployed a different file on the AS7 server, so let's refresh ourselves
deploymentFile = determineDeploymentFile();
DeployIndividualPackageResponse packageResponse = new DeployIndividualPackageResponse(detail.getKey(),
ContentResponseResult.SUCCESS);
response.addPackageResponse(packageResponse);
Result result = null;
// this deployment can be versioned
// resource deployment name could be deployment-1.0.0.jar, but resourceKey (and name) would be deployment.jar and version 1.0.0
// if there is an attempt to deploy deployment-2.0.0.jar, we need to undeploy deployment-1.0.0.jar and then deploy the new content
// Sipmly redeploy won't work, because deployment-2.0.0.jar is not present on server

//detect whether we're dealing with versioned deployments
if (!AbstractVersionedSubsystemDiscovery.DISABLED) {

Result readResource = getASConnection().execute(new ReadResource(getAddress()));
Map<String, Object> resourceMap = (Map<String, Object>) readResource.getResult();

String resourceDeploymentName = (String) resourceMap.get("name");
String newDeploymentName = detail.getKey().getName();

Matcher versionedResourceMatch = AbstractVersionedSubsystemDiscovery.MATCHER.pattern().matcher(
resourceDeploymentName);

if (versionedResourceMatch.matches()) {
// we're dealing with versioned deployment resource
String versionedDeploymentName = versionedResourceMatch.group(1);

Matcher newContentMatch = AbstractVersionedSubsystemDiscovery.MATCHER.pattern().matcher(
newDeploymentName);
if (newContentMatch.matches()) {
// we're strict and only undeploy/deploy in case we're dealing with same deployments (base names match)
if (versionedDeploymentName.equals(newContentMatch.group(1))) {

String runtimeName = (String) resourceMap.get("runtime-name");
// preserver runtime-name only if it differs from deploymentName - it was explicitly defined at deploy time
if (runtimeName.equals(resourceDeploymentName)) {
runtimeName = newDeploymentName;
}
Boolean enabled = (Boolean) resourceMap.get("enabled");
if (enabled == null) {
enabled = false; // enabled attribute is null if we're dealing with DomainDeployment
}

Deployer deployer = createDeployerForPackageUpdate(newDeploymentName, runtimeName, hash);
result = deployer.deployToServer(enabled);
} else {
response.setOverallRequestResult(ContentResponseResult.FAILURE);
response
.setOverallRequestErrorMessage("Failed to update package. Attempt to replace content of versioned resource with key="
+ resourceDeploymentName
+ " with package key="
+ newDeploymentName
+ " Given package key does not match.");
}
} else {
response.setOverallRequestResult(ContentResponseResult.FAILURE);
response
.setOverallRequestErrorMessage("Failed to update package. This resource is versioned deployment and updating it's content by unversioned package is not allowed.");
}

}
// else this resource is not versioned: deployment we default to redeploy

}
if (response.getOverallRequestResult() == null) {

// if none of undeploy/deploy conditions were met
if (result == null) {
Redeployer redeployer = new Redeployer(detail.getKey().getName(), hash, getASConnection());
result = redeployer.redeployOnServer();
}

if (result.isRolledBack()) {
response.setOverallRequestResult(ContentResponseResult.FAILURE);
response.setOverallRequestErrorMessage("Rolled Back: " + result.getFailureDescription());
} else {
response.setOverallRequestResult(ContentResponseResult.SUCCESS);
//we just deployed a different file on the AS7 server, so let's refresh ourselves
deploymentFile = determineDeploymentFile();
DeployIndividualPackageResponse packageResponse = new DeployIndividualPackageResponse(
detail.getKey(), ContentResponseResult.SUCCESS);
response.addPackageResponse(packageResponse);
}
}


} catch (Exception e) {
response.setOverallRequestResult(ContentResponseResult.FAILURE);
Expand Down Expand Up @@ -251,7 +336,8 @@ public Set<ResourcePackageDetails> discoverDeployedPackages(PackageType type) {
return Collections.emptySet();
}

String name = getDeploymentName();
String deploymentName = getDeploymentName();
String name = String.valueOf(deploymentName);
String sha256 = getSHA256(deploymentFile);
String version = getVersion(sha256);

Expand All @@ -260,7 +346,7 @@ public Set<ResourcePackageDetails> discoverDeployedPackages(PackageType type) {

details.setDisplayVersion(getDisplayVersion(deploymentFile));
details.setFileCreatedDate(null); //TODO figure this out from Sigar somehow?
details.setFileName(name);
details.setFileName(deploymentName);
details.setFileSize(deploymentFile.length());
details.setInstallationTimestamp(deploymentFile.lastModified());
details.setLocation(deploymentFile.getAbsolutePath());
Expand Down Expand Up @@ -462,4 +548,5 @@ private String getDeploymentName() {

return (String) res.getResult();
}

}
Expand Up @@ -36,12 +36,14 @@
import org.rhq.core.pluginapi.configuration.ConfigurationUpdateReport;
import org.rhq.core.pluginapi.operation.OperationFacet;
import org.rhq.core.pluginapi.operation.OperationResult;
import org.rhq.modules.plugins.jbossas7.helper.Deployer;
import org.rhq.modules.plugins.jbossas7.json.Address;
import org.rhq.modules.plugins.jbossas7.json.CompositeOperation;
import org.rhq.modules.plugins.jbossas7.json.Operation;
import org.rhq.modules.plugins.jbossas7.json.ReadChildrenNames;
import org.rhq.modules.plugins.jbossas7.json.ReadChildrenResources;
import org.rhq.modules.plugins.jbossas7.json.ReadResource;
import org.rhq.modules.plugins.jbossas7.json.Remove;
import org.rhq.modules.plugins.jbossas7.json.Result;

/**
Expand Down Expand Up @@ -173,6 +175,33 @@ private List<String> findAssignedServerGroups() {
return groups;
}

@Override
protected Deployer createDeployerForPackageUpdate(String deploymentName, String runtimeName, String hash) {
Deployer deployer = new Deployer(deploymentName, runtimeName, hash, getASConnection());
// we need to find server-groups where this deployment is assigned
Configuration config = Configuration.builder().build();
loadAssignedServerGroups(config);
String originalDeploymentName = getManagementNodeName();
// then we add steps to remove from them, and at the same time we add steps to assign new deployment to same groups with same parameters
for (Property prop : config.getList("*1").getList()) {
PropertyMap map = (PropertyMap) prop;
String sgName = map.getSimpleValue("server-group", null);
String sgRuntimeName = map.getSimpleValue("runtime-name", null);
if (originalDeploymentName.equals(sgRuntimeName)) {
sgRuntimeName = null; // runtimeName was equal to deployment Name => not defined at deploy time
}
boolean sgEnabled = map.getSimple("enabled").getBooleanValue();
deployer.addBeforeDeployStep(createServerGroupAssignmentStep("remove", sgName, null, false));
// new assign-to-group step refers to new deploymentName
deployer.addAfterDeployStep(createServerGroupAssignmentStep("add", sgName, deploymentName, sgRuntimeName,
sgEnabled));
}

// this has to be the last beforeDeploy step as it would fail in case deployment is assigned to some server-group
deployer.addBeforeDeployStep(new Remove(getAddress()));
return deployer;
}

@SuppressWarnings("unchecked")
private void loadAssignedServerGroups(Configuration configuration) {
String managementNodeName = getManagementNodeName();
Expand Down Expand Up @@ -201,11 +230,11 @@ private void loadAssignedServerGroups(Configuration configuration) {
}
}

private Operation createServerGroupAssignmentStep(String action, String serverGroup, String runtimeName,
private Operation createServerGroupAssignmentStep(String action, String serverGroup,String deploymentName, String runtimeName,
boolean enabled) {
Address addr = new Address();
addr.add("server-group", serverGroup);
addr.add("deployment", getManagementNodeName());
addr.add("deployment", deploymentName);
Operation op = new Operation(action, addr);
if ("add".equals(action)) {
if (runtimeName != null && !runtimeName.isEmpty()) {
Expand All @@ -215,6 +244,11 @@ private Operation createServerGroupAssignmentStep(String action, String serverGr
}
return op;
}

private Operation createServerGroupAssignmentStep(String action, String serverGroup, String runtimeName,
boolean enabled) {
return createServerGroupAssignmentStep(action, serverGroup, getManagementNodeName(), runtimeName, enabled);
}

@SuppressWarnings("unchecked")
@Override
Expand Down
Expand Up @@ -342,6 +342,7 @@ private BundleHandoverResponse handleDeployment(BundleHandoverRequest handoverRe
return BundleHandoverResponse.success();
}

// TODO use Deployer
Operation addDeploymentStep = new Operation("add", "deployment", filename);
List<Object> addDeploymentContentProperty = new ArrayList<Object>(1);
Map<String, Object> contentValues = new HashMap<String, Object>();
Expand Down
Expand Up @@ -126,6 +126,7 @@ public DeployPackagesResponse deployPackages(Set<ResourcePackageDetails> package
JsonNode uploadResult = uploadConnection.finishUpload();

if (uploadResult.has(OUTCOME)) {
// TODO use Deployer class
String outcome = uploadResult.get(OUTCOME).getTextValue();
if (outcome.equals(SUCCESS)) { // Upload was successful, so now add the file to the server group
JsonNode resultNode = uploadResult.get("result");
Expand Down

0 comments on commit 48e5671

Please sign in to comment.