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

Commit 48e5671

Browse files
committed
Merge pull request #163 from lzoubek/bugs/1188751
Bug 1188751 - Deploying Versioned WAR package from repository to JBOSS EAP 6 deployment resource fails
2 parents f41bcf9 + 2b77882 commit 48e5671

File tree

7 files changed

+309
-78
lines changed

7 files changed

+309
-78
lines changed

modules/plugins/jboss-as-7/src/main/java/org/rhq/modules/plugins/jbossas7/AbstractVersionedSubsystemDiscovery.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@ abstract public class AbstractVersionedSubsystemDiscovery extends SubsystemDisco
3838
static private final String PATTERN_DISABLE = "disable";
3939
static private final String PATTERN_PROP = "rhq.as7.VersionedSubsystemDiscovery.pattern";
4040

41-
static protected final Matcher MATCHER;
41+
static public final Matcher MATCHER;
4242
static protected final String SUBDEPLOYMENT_TYPE = "Subdeployment";
43-
static protected boolean DISABLED = false;
43+
static public boolean DISABLED = false;
4444

4545
static {
4646
Matcher m = null;

modules/plugins/jboss-as-7/src/main/java/org/rhq/modules/plugins/jbossas7/BaseComponent.java

Lines changed: 5 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323

2424
import java.io.OutputStream;
2525
import java.util.ArrayList;
26-
import java.util.HashMap;
2726
import java.util.List;
2827
import java.util.Map;
2928
import java.util.Set;
@@ -66,10 +65,9 @@
6665
import org.rhq.core.pluginapi.operation.OperationFacet;
6766
import org.rhq.core.pluginapi.operation.OperationResult;
6867
import org.rhq.core.pluginapi.util.StartScriptConfiguration;
68+
import org.rhq.modules.plugins.jbossas7.helper.Deployer;
6969
import org.rhq.modules.plugins.jbossas7.json.Address;
70-
import org.rhq.modules.plugins.jbossas7.json.CompositeOperation;
7170
import org.rhq.modules.plugins.jbossas7.json.Operation;
72-
import org.rhq.modules.plugins.jbossas7.json.PROPERTY_VALUE;
7371
import org.rhq.modules.plugins.jbossas7.json.ReadAttribute;
7472
import org.rhq.modules.plugins.jbossas7.json.ReadChildrenNames;
7573
import org.rhq.modules.plugins.jbossas7.json.ReadResource;
@@ -544,64 +542,12 @@ protected CreateResourceReport deployContent(CreateResourceReport report) {
544542
public CreateResourceReport runDeploymentMagicOnServer(CreateResourceReport report, String runtimeName,
545543
String deploymentName, String hash) {
546544

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

552-
ASConnection connection = getASConnection();
547+
Deployer deployer = new Deployer(deploymentName, runtimeName, hash, getASConnection());
553548

554-
Operation step1 = new Operation("add", "deployment", deploymentName);
555-
// step1.addAdditionalProperty("hash", new PROPERTY_VALUE("BYTES_VALUE", hash));
556-
List<Object> content = new ArrayList<Object>(1);
557-
Map<String, Object> contentValues = new HashMap<String, Object>();
558-
contentValues.put("hash", new PROPERTY_VALUE("BYTES_VALUE", hash));
559-
content.add(contentValues);
560-
step1.addAdditionalProperty("content", content);
561-
562-
step1.addAdditionalProperty("name", deploymentName);
563-
step1.addAdditionalProperty("runtime-name", runtimeName);
564-
565-
String resourceKey;
566-
Result result;
567-
568-
CompositeOperation cop = new CompositeOperation();
569-
cop.addStep(step1);
570-
/*
571-
* We need to check here if this is an upload to /deployment only
572-
* or if this should be deployed to a server group too
573-
*/
574-
575-
if (!toServerGroup) {
576-
577-
// if standalone, then :deploy the deployment anyway
578-
if (context.getResourceType().getName().contains("Standalone")) {
579-
Operation step2 = new Operation("deploy", step1.getAddress());
580-
cop.addStep(step2);
581-
}
582-
583-
result = connection.execute(cop, 300);
584-
resourceKey = step1.getAddress().getPath();
585-
586-
} else {
587-
588-
Address serverGroupAddress = new Address(context.getResourceKey());
589-
serverGroupAddress.add("deployment", deploymentName);
590-
Operation step2 = new Operation("add", serverGroupAddress);
591-
592-
cop.addStep(step2);
593-
594-
Operation step3 = new Operation("deploy", serverGroupAddress);
595-
cop.addStep(step3);
596-
597-
resourceKey = serverGroupAddress.getPath();
598-
599-
if (verbose) {
600-
LOG.info("Deploy operation: " + cop);
601-
}
602-
603-
result = connection.execute(cop, 300);
604-
}
549+
Result result = deployer.deployToServer(context.getResourceType().getName().contains("Standalone"));
550+
String resourceKey = deployer.getNewResourceKey();
605551

606552
if ((!result.isSuccess())) {
607553
String failureDescription = result.getFailureDescription();

modules/plugins/jboss-as-7/src/main/java/org/rhq/modules/plugins/jbossas7/DeploymentComponent.java

Lines changed: 102 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import java.util.List;
3333
import java.util.Map;
3434
import java.util.Set;
35+
import java.util.regex.Matcher;
3536

3637
import org.codehaus.jackson.JsonNode;
3738

@@ -58,10 +59,12 @@
5859
import org.rhq.core.util.ByteUtil;
5960
import org.rhq.core.util.file.ContentFileInfo;
6061
import org.rhq.core.util.file.JarContentFileInfo;
62+
import org.rhq.modules.plugins.jbossas7.helper.Deployer;
6163
import org.rhq.modules.plugins.jbossas7.json.Address;
6264
import org.rhq.modules.plugins.jbossas7.json.Operation;
6365
import org.rhq.modules.plugins.jbossas7.json.ReadAttribute;
6466
import org.rhq.modules.plugins.jbossas7.json.ReadResource;
67+
import org.rhq.modules.plugins.jbossas7.json.Remove;
6568
import org.rhq.modules.plugins.jbossas7.json.Result;
6669

6770
/**
@@ -156,6 +159,21 @@ public List<DeployPackageStep> generateInstallationSteps(ResourcePackageDetails
156159
return new ArrayList<DeployPackageStep>();
157160
}
158161

162+
/**
163+
* create new Deployer instance for {@link #deployPackages(Set, ContentServices)} code branch which *only* handles package update
164+
* in case of Versioned Deployments
165+
* @see {@link #deployPackages(Set, ContentServices)} and {@link DomainDeploymentComponent#createDeployerForPackageUpdate(String, String, String)}
166+
* @param deploymentName
167+
* @param runtimeName
168+
* @param hash
169+
* @return new Deployer which correctly undeploys original package and then deploys new package
170+
*/
171+
protected Deployer createDeployerForPackageUpdate(String deploymentName, String runtimeName, String hash) {
172+
Deployer deployer = new Deployer(deploymentName, runtimeName, hash, getASConnection());
173+
deployer.addBeforeDeployStep(new Remove(getAddress()));
174+
return deployer;
175+
}
176+
159177
@Override
160178
public DeployPackagesResponse deployPackages(Set<ResourcePackageDetails> packages, ContentServices contentServices) {
161179
getLog().debug("Starting deployment..");
@@ -180,7 +198,7 @@ public DeployPackagesResponse deployPackages(Set<ResourcePackageDetails> package
180198
}
181199
ResourceType resourceType = context.getResourceType();
182200

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

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

208226
try {
209-
Redeployer redeployer = new Redeployer(detail.getKey().getName(), hash, getASConnection());
210-
Result result = redeployer.redeployOnServer();
211-
if (result.isRolledBack()) {
212-
response.setOverallRequestResult(ContentResponseResult.FAILURE);
213-
response.setOverallRequestErrorMessage("Rolled Back: " + result.getFailureDescription());
214-
} else {
215-
response.setOverallRequestResult(ContentResponseResult.SUCCESS);
216-
//we just deployed a different file on the AS7 server, so let's refresh ourselves
217-
deploymentFile = determineDeploymentFile();
218-
DeployIndividualPackageResponse packageResponse = new DeployIndividualPackageResponse(detail.getKey(),
219-
ContentResponseResult.SUCCESS);
220-
response.addPackageResponse(packageResponse);
227+
Result result = null;
228+
// this deployment can be versioned
229+
// resource deployment name could be deployment-1.0.0.jar, but resourceKey (and name) would be deployment.jar and version 1.0.0
230+
// 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
231+
// Sipmly redeploy won't work, because deployment-2.0.0.jar is not present on server
232+
233+
//detect whether we're dealing with versioned deployments
234+
if (!AbstractVersionedSubsystemDiscovery.DISABLED) {
235+
236+
Result readResource = getASConnection().execute(new ReadResource(getAddress()));
237+
Map<String, Object> resourceMap = (Map<String, Object>) readResource.getResult();
238+
239+
String resourceDeploymentName = (String) resourceMap.get("name");
240+
String newDeploymentName = detail.getKey().getName();
241+
242+
Matcher versionedResourceMatch = AbstractVersionedSubsystemDiscovery.MATCHER.pattern().matcher(
243+
resourceDeploymentName);
244+
245+
if (versionedResourceMatch.matches()) {
246+
// we're dealing with versioned deployment resource
247+
String versionedDeploymentName = versionedResourceMatch.group(1);
248+
249+
Matcher newContentMatch = AbstractVersionedSubsystemDiscovery.MATCHER.pattern().matcher(
250+
newDeploymentName);
251+
if (newContentMatch.matches()) {
252+
// we're strict and only undeploy/deploy in case we're dealing with same deployments (base names match)
253+
if (versionedDeploymentName.equals(newContentMatch.group(1))) {
254+
255+
String runtimeName = (String) resourceMap.get("runtime-name");
256+
// preserver runtime-name only if it differs from deploymentName - it was explicitly defined at deploy time
257+
if (runtimeName.equals(resourceDeploymentName)) {
258+
runtimeName = newDeploymentName;
259+
}
260+
Boolean enabled = (Boolean) resourceMap.get("enabled");
261+
if (enabled == null) {
262+
enabled = false; // enabled attribute is null if we're dealing with DomainDeployment
263+
}
264+
265+
Deployer deployer = createDeployerForPackageUpdate(newDeploymentName, runtimeName, hash);
266+
result = deployer.deployToServer(enabled);
267+
} else {
268+
response.setOverallRequestResult(ContentResponseResult.FAILURE);
269+
response
270+
.setOverallRequestErrorMessage("Failed to update package. Attempt to replace content of versioned resource with key="
271+
+ resourceDeploymentName
272+
+ " with package key="
273+
+ newDeploymentName
274+
+ " Given package key does not match.");
275+
}
276+
} else {
277+
response.setOverallRequestResult(ContentResponseResult.FAILURE);
278+
response
279+
.setOverallRequestErrorMessage("Failed to update package. This resource is versioned deployment and updating it's content by unversioned package is not allowed.");
280+
}
281+
282+
}
283+
// else this resource is not versioned: deployment we default to redeploy
284+
221285
}
286+
if (response.getOverallRequestResult() == null) {
287+
288+
// if none of undeploy/deploy conditions were met
289+
if (result == null) {
290+
Redeployer redeployer = new Redeployer(detail.getKey().getName(), hash, getASConnection());
291+
result = redeployer.redeployOnServer();
292+
}
293+
294+
if (result.isRolledBack()) {
295+
response.setOverallRequestResult(ContentResponseResult.FAILURE);
296+
response.setOverallRequestErrorMessage("Rolled Back: " + result.getFailureDescription());
297+
} else {
298+
response.setOverallRequestResult(ContentResponseResult.SUCCESS);
299+
//we just deployed a different file on the AS7 server, so let's refresh ourselves
300+
deploymentFile = determineDeploymentFile();
301+
DeployIndividualPackageResponse packageResponse = new DeployIndividualPackageResponse(
302+
detail.getKey(), ContentResponseResult.SUCCESS);
303+
response.addPackageResponse(packageResponse);
304+
}
305+
}
306+
222307

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

254-
String name = getDeploymentName();
339+
String deploymentName = getDeploymentName();
340+
String name = String.valueOf(deploymentName);
255341
String sha256 = getSHA256(deploymentFile);
256342
String version = getVersion(sha256);
257343

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

261347
details.setDisplayVersion(getDisplayVersion(deploymentFile));
262348
details.setFileCreatedDate(null); //TODO figure this out from Sigar somehow?
263-
details.setFileName(name);
349+
details.setFileName(deploymentName);
264350
details.setFileSize(deploymentFile.length());
265351
details.setInstallationTimestamp(deploymentFile.lastModified());
266352
details.setLocation(deploymentFile.getAbsolutePath());
@@ -462,4 +548,5 @@ private String getDeploymentName() {
462548

463549
return (String) res.getResult();
464550
}
551+
465552
}

modules/plugins/jboss-as-7/src/main/java/org/rhq/modules/plugins/jbossas7/DomainDeploymentComponent.java

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,14 @@
3636
import org.rhq.core.pluginapi.configuration.ConfigurationUpdateReport;
3737
import org.rhq.core.pluginapi.operation.OperationFacet;
3838
import org.rhq.core.pluginapi.operation.OperationResult;
39+
import org.rhq.modules.plugins.jbossas7.helper.Deployer;
3940
import org.rhq.modules.plugins.jbossas7.json.Address;
4041
import org.rhq.modules.plugins.jbossas7.json.CompositeOperation;
4142
import org.rhq.modules.plugins.jbossas7.json.Operation;
4243
import org.rhq.modules.plugins.jbossas7.json.ReadChildrenNames;
4344
import org.rhq.modules.plugins.jbossas7.json.ReadChildrenResources;
4445
import org.rhq.modules.plugins.jbossas7.json.ReadResource;
46+
import org.rhq.modules.plugins.jbossas7.json.Remove;
4547
import org.rhq.modules.plugins.jbossas7.json.Result;
4648

4749
/**
@@ -173,6 +175,33 @@ private List<String> findAssignedServerGroups() {
173175
return groups;
174176
}
175177

178+
@Override
179+
protected Deployer createDeployerForPackageUpdate(String deploymentName, String runtimeName, String hash) {
180+
Deployer deployer = new Deployer(deploymentName, runtimeName, hash, getASConnection());
181+
// we need to find server-groups where this deployment is assigned
182+
Configuration config = Configuration.builder().build();
183+
loadAssignedServerGroups(config);
184+
String originalDeploymentName = getManagementNodeName();
185+
// 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
186+
for (Property prop : config.getList("*1").getList()) {
187+
PropertyMap map = (PropertyMap) prop;
188+
String sgName = map.getSimpleValue("server-group", null);
189+
String sgRuntimeName = map.getSimpleValue("runtime-name", null);
190+
if (originalDeploymentName.equals(sgRuntimeName)) {
191+
sgRuntimeName = null; // runtimeName was equal to deployment Name => not defined at deploy time
192+
}
193+
boolean sgEnabled = map.getSimple("enabled").getBooleanValue();
194+
deployer.addBeforeDeployStep(createServerGroupAssignmentStep("remove", sgName, null, false));
195+
// new assign-to-group step refers to new deploymentName
196+
deployer.addAfterDeployStep(createServerGroupAssignmentStep("add", sgName, deploymentName, sgRuntimeName,
197+
sgEnabled));
198+
}
199+
200+
// this has to be the last beforeDeploy step as it would fail in case deployment is assigned to some server-group
201+
deployer.addBeforeDeployStep(new Remove(getAddress()));
202+
return deployer;
203+
}
204+
176205
@SuppressWarnings("unchecked")
177206
private void loadAssignedServerGroups(Configuration configuration) {
178207
String managementNodeName = getManagementNodeName();
@@ -201,11 +230,11 @@ private void loadAssignedServerGroups(Configuration configuration) {
201230
}
202231
}
203232

204-
private Operation createServerGroupAssignmentStep(String action, String serverGroup, String runtimeName,
233+
private Operation createServerGroupAssignmentStep(String action, String serverGroup,String deploymentName, String runtimeName,
205234
boolean enabled) {
206235
Address addr = new Address();
207236
addr.add("server-group", serverGroup);
208-
addr.add("deployment", getManagementNodeName());
237+
addr.add("deployment", deploymentName);
209238
Operation op = new Operation(action, addr);
210239
if ("add".equals(action)) {
211240
if (runtimeName != null && !runtimeName.isEmpty()) {
@@ -215,6 +244,11 @@ private Operation createServerGroupAssignmentStep(String action, String serverGr
215244
}
216245
return op;
217246
}
247+
248+
private Operation createServerGroupAssignmentStep(String action, String serverGroup, String runtimeName,
249+
boolean enabled) {
250+
return createServerGroupAssignmentStep(action, serverGroup, getManagementNodeName(), runtimeName, enabled);
251+
}
218252

219253
@SuppressWarnings("unchecked")
220254
@Override

modules/plugins/jboss-as-7/src/main/java/org/rhq/modules/plugins/jbossas7/HostControllerComponent.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,7 @@ private BundleHandoverResponse handleDeployment(BundleHandoverRequest handoverRe
342342
return BundleHandoverResponse.success();
343343
}
344344

345+
// TODO use Deployer
345346
Operation addDeploymentStep = new Operation("add", "deployment", filename);
346347
List<Object> addDeploymentContentProperty = new ArrayList<Object>(1);
347348
Map<String, Object> contentValues = new HashMap<String, Object>();

modules/plugins/jboss-as-7/src/main/java/org/rhq/modules/plugins/jbossas7/ServerGroupComponent.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ public DeployPackagesResponse deployPackages(Set<ResourcePackageDetails> package
126126
JsonNode uploadResult = uploadConnection.finishUpload();
127127

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

0 commit comments

Comments
 (0)