This repository has been archived by the owner on Jul 11, 2022. It is now read-only.
/
BaseServerComponent.java
1150 lines (1016 loc) · 47.5 KB
/
BaseServerComponent.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* RHQ Management Platform
* Copyright (C) 2005-2014 Red Hat, Inc.
* All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation version 2 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
package org.rhq.modules.plugins.wildfly10;
import static java.util.concurrent.TimeUnit.HOURS;
import static java.util.concurrent.TimeUnit.SECONDS;
import static org.rhq.core.domain.measurement.AvailabilityType.DOWN;
import static org.rhq.core.domain.measurement.AvailabilityType.UP;
import static org.rhq.core.pluginapi.bundle.BundleHandoverResponse.FailureType.EXECUTION;
import static org.rhq.core.pluginapi.bundle.BundleHandoverResponse.FailureType.INVALID_PARAMETER;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeoutException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.codehaus.jackson.map.ObjectMapper;
import org.jetbrains.annotations.NotNull;
import org.jboss.sasl.util.UsernamePasswordHashUtil;
import org.rhq.core.domain.configuration.Configuration;
import org.rhq.core.domain.configuration.PropertySimple;
import org.rhq.core.domain.measurement.AvailabilityType;
import org.rhq.core.domain.measurement.MeasurementDataTrait;
import org.rhq.core.domain.measurement.MeasurementReport;
import org.rhq.core.domain.measurement.MeasurementScheduleRequest;
import org.rhq.core.pluginapi.bundle.BundleHandoverFacet;
import org.rhq.core.pluginapi.bundle.BundleHandoverRequest;
import org.rhq.core.pluginapi.bundle.BundleHandoverResponse;
import org.rhq.core.pluginapi.event.log.LogFileEventResourceComponentHelper;
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.measurement.MeasurementFacet;
import org.rhq.core.pluginapi.operation.OperationResult;
import org.rhq.core.pluginapi.util.StartScriptConfiguration;
import org.rhq.core.system.ProcessExecutionResults;
import org.rhq.core.system.ProcessInfo;
import org.rhq.core.util.PropertiesFileUpdate;
import org.rhq.core.util.StringUtil;
import org.rhq.core.util.file.FileUtil;
import org.rhq.modules.plugins.wildfly10.helper.HostConfiguration;
import org.rhq.modules.plugins.wildfly10.helper.HostPort;
import org.rhq.modules.plugins.wildfly10.helper.JBossCliConfiguration;
import org.rhq.modules.plugins.wildfly10.helper.ServerPluginConfiguration;
import org.rhq.modules.plugins.wildfly10.json.Address;
import org.rhq.modules.plugins.wildfly10.json.ComplexResult;
import org.rhq.modules.plugins.wildfly10.json.ExpressionResolver;
import org.rhq.modules.plugins.wildfly10.json.Operation;
import org.rhq.modules.plugins.wildfly10.json.ReadAttribute;
import org.rhq.modules.plugins.wildfly10.json.ReadResource;
import org.rhq.modules.plugins.wildfly10.json.Result;
import org.rhq.modules.plugins.wildfly10.json.ResultFailedException;
import org.rhq.modules.plugins.wildfly10.json.SecurityRealmNotReadyException;
import org.rhq.modules.plugins.wildfly10.json.UnauthorizedException;
import org.rhq.modules.plugins.wildfly10.util.ProcessExecutionLogger;
/**
* Base component for functionality that is common to Standalone Servers and Host Controllers.
*
* @author Heiko W. Rupp
* @author Ian Springer
*/
public abstract class BaseServerComponent<T extends ResourceComponent<?>> extends BaseComponent<T> implements
MeasurementFacet, BundleHandoverFacet {
private static final Log LOG = LogFactory.getLog(BaseServerComponent.class);
protected static final long MAX_TIMEOUT_FAILURE_WAIT = 5 * 60 * 1000L;
/**
* timeout (seconds) used in bundle:handover deployment action to make sure server is running
*/
protected static final Integer BUNDLE_HANDOVER_SERVER_CHECK_TIMEOUT = Integer.getInteger(
"as7.plugin.handover-deployment.server-check-timeout.seconds", 120);
protected static final String BUNDLE_HANDOVER_SERVER_CHECK_TIMEOUT_PARAM = "waitForServer";
private ASConnection connection;
private LogFileEventResourceComponentHelper logFileEventDelegate;
private StartScriptConfiguration startScriptConfig;
private ServerPluginConfiguration serverPluginConfig;
private AvailabilityType previousAvailabilityType;
private String releaseVersion;
private String aSHostName;
private long lastManagementInterfaceReply = 0;
private ExpressionResolver expressionResolver;
@Override
public void start(ResourceContext<T> resourceContext) throws Exception {
super.start(resourceContext);
serverPluginConfig = new ServerPluginConfiguration(pluginConfiguration);
serverPluginConfig.validate();
if ("0.0.0.0".equals(serverPluginConfig.getHostname())) {
LOG.warn("Management host is set to 0.0.0.0 on server " + resourceContext.getResourceKey()
+ ". Please change this to avoid operation failures");
}
connection = new ASConnection(ASConnectionParams.createFrom(serverPluginConfig));
setASHostName(findASDomainHostName());
getAvailability();
logFileEventDelegate = new LogFileEventResourceComponentHelper(context);
logFileEventDelegate.startLogFileEventPollers();
startScriptConfig = new StartScriptConfiguration(pluginConfiguration);
expressionResolver = new ExpressionResolver(connection, getHostAddress());
}
@Override
public void stop() {
connection.shutdown();
logFileEventDelegate.stopLogFileEventPollers();
previousAvailabilityType = null;
}
@Override
public AvailabilityType getAvailability() {
AvailabilityType availabilityType;
boolean securityRealmReady = true;
boolean accessPermission = true;
try {
try {
readAttribute(getHostAddress(), "name", AVAIL_OP_TIMEOUT_SECONDS);
availabilityType = UP;
lastManagementInterfaceReply = new Date().getTime();
} catch (ResultFailedException e) {
LOG.warn("Domain host name seems to be changed");
setASHostName(findASDomainHostName());
LOG.info("Detected domain host name [" + getASHostName() + "]");
readAttribute(getHostAddress(), "name");
availabilityType = UP;
} catch (SecurityRealmNotReadyException e) {
previousAvailabilityType = DOWN;
availabilityType = DOWN;
securityRealmReady = false;
} catch (UnauthorizedException e) {
previousAvailabilityType = DOWN;
availabilityType = DOWN;
accessPermission = false;
}
} catch (TimeoutException e) {
long now = new Date().getTime();
if (now - lastManagementInterfaceReply > MAX_TIMEOUT_FAILURE_WAIT) {
availabilityType = DOWN;
} else {
ProcessInfo processInfo = context.getNativeProcess();
if (processInfo != null && processInfo.priorSnaphot().isRunning()) {
availabilityType = previousAvailabilityType;
} else {
availabilityType = DOWN;
}
}
} catch (InvalidPluginConfigurationException e) {
throw e;
} catch (Exception e) {
if (LOG.isDebugEnabled()) {
LOG.debug(getResourceDescription() + ": exception while checking availability", e);
}
availabilityType = DOWN;
}
if (!securityRealmReady) {
throw new InvalidPluginConfigurationException("The security realm of the HTTP management interface"
+ " is not ready to process requests. This usually indicates that no user is configured");
}
if (!accessPermission) {
throw new InvalidPluginConfigurationException("User authenticated via HTTP management interface does"
+ " not have sufficient permissions. This can be fixed by either choosing different credential"
+ " or assigning roles in EAP Administration Console.");
}
if (availabilityType == DOWN) {
releaseVersion = null;
} else if (previousAvailabilityType != UP) {
onAvailGoesUp();
}
previousAvailabilityType = availabilityType;
return availabilityType;
}
/**
* this method get's called when availability was DOWN and now is UP.
* When overriding, don't forget to call super impl.
*/
protected void onAvailGoesUp() {
validateServerAttributes();
if (LOG.isDebugEnabled()) {
LOG.debug(getResourceDescription() + " has just come UP.");
}
}
private void validateServerAttributes() throws InvalidPluginConfigurationException {
// Validate the base dir (e.g. /opt/jboss-as-7.1.1.Final/standalone).
File runtimeBaseDir;
File baseDir = null;
try {
String runtimeBaseDirString = readAttribute(getEnvironmentAddress(), getBaseDirAttributeName());
// Canonicalize both paths before comparing them!
runtimeBaseDir = new File(runtimeBaseDirString).getCanonicalFile();
File baseDirTmp = serverPluginConfig.getBaseDir();
if (baseDirTmp != null) { // may be null for manually added servers
baseDir = baseDirTmp.getCanonicalFile();
}
} catch (Exception e) {
runtimeBaseDir = null;
baseDir = null;
LOG.error("Failed to validate base dir for " + getResourceDescription() + ".", e);
}
if ((runtimeBaseDir != null) && (baseDir != null)) {
if (!runtimeBaseDir.equals(baseDir)) {
throw new InvalidPluginConfigurationException("The server listening on "
+ serverPluginConfig.getHostname() + ":" + serverPluginConfig.getPort() + " has base dir ["
+ runtimeBaseDir + "], but the base dir we expected was [" + baseDir
+ "]. Perhaps the management hostname or port has been changed for the server with base dir ["
+ baseDir + "].");
}
}
// Validate the config dir (e.g. /opt/jboss-as-7.1.1.Final/standalone/configuration).
File runtimeConfigDir;
File configDir = null;
try {
String runtimeConfigDirString = readAttribute(getEnvironmentAddress(), getConfigDirAttributeName());
// Canonicalize both paths before comparing them!
runtimeConfigDir = new File(runtimeConfigDirString).getCanonicalFile();
File configDirTmp = serverPluginConfig.getConfigDir();
if (configDirTmp != null) { // may be null for manually added servers
configDir = configDirTmp.getCanonicalFile();
}
} catch (Exception e) {
runtimeConfigDir = null;
configDir = null;
LOG.error("Failed to validate config dir for " + getResourceDescription() + ".", e);
}
if ((runtimeConfigDir != null) && (configDir != null)) {
if (!runtimeConfigDir.equals(configDir)) {
throw new InvalidPluginConfigurationException("The server listening on "
+ serverPluginConfig.getHostname() + ":" + serverPluginConfig.getPort() + " has config dir ["
+ runtimeConfigDir + "], but the config dir we expected was [" + configDir
+ "]. Perhaps the management hostname or port has been changed for the server with config dir ["
+ configDir + "].");
}
}
// Validate the mode (e.g. STANDALONE or DOMAIN).
String runtimeMode;
try {
runtimeMode = readAttribute("launch-type");
} catch (Exception e) {
runtimeMode = null;
LOG.error("Failed to validate mode for " + getResourceDescription() + ".", e);
}
if (runtimeMode != null) {
String mode = getMode().name();
if (!runtimeMode.equals(mode)) {
throw new InvalidPluginConfigurationException("The original mode discovered for this server was "
+ getMode() + ", but the server is now reporting its mode is [" + runtimeMode + "].");
}
}
// Validate the product type (e.g. AS or EAP).
String expectedRuntimeProductName = pluginConfiguration.getSimpleValue("expectedRuntimeProductName");
String runtimeProductName;
try {
runtimeProductName = readAttribute(getHostAddress(), "product-name");
} catch (Exception e) {
throw new InvalidPluginConfigurationException("Failed to validate product type for "
+ getResourceDescription(), e);
}
if (!runtimeProductName.equals(expectedRuntimeProductName)) {
if(serverPluginConfig.getProductType() != JBossProductType.WILDFLY10) {
throw new InvalidPluginConfigurationException("The original product type discovered for this server was "
+ expectedRuntimeProductName + ", but the server is now reporting its product type is ["
+ runtimeProductName + "]");
}
}
}
public ServerPluginConfiguration getServerPluginConfiguration() {
return serverPluginConfig;
}
public StartScriptConfiguration getStartScriptConfiguration() {
return startScriptConfig;
}
@Override
public ASConnection getASConnection() {
return connection;
}
@Override
public void setConnection(ASConnection connection) {
this.connection = connection;
}
@NotNull
protected abstract AS7Mode getMode();
/**
* Restart the server by first executing a 'shutdown' operation via the management API and then calling
* the {@link #startServer} method to start it again.
*
* @param parameters Parameters to pass to the (recursive) invocation of #invokeOperation
* @return State of execution
* @throws Exception If anything goes wrong
*/
protected OperationResult restartServer(Configuration parameters) throws Exception {
OperationResult operationResult = new OperationResult();
if (isManuallyAddedServer(operationResult, "Restarting")) {
return operationResult;
}
List<String> errors = validateStartScriptPluginConfigProps();
if (!errors.isEmpty()) {
OperationResult result = new OperationResult();
setErrorMessage(result, errors);
return result;
}
OperationResult tmp = invokeOperation("shutdown", parameters);
if (tmp.getErrorMessage() != null) {
tmp.setErrorMessage("Restart failed while attempting to shut down: " + tmp.getErrorMessage());
return tmp;
}
context.getAvailabilityContext().requestAvailabilityCheck();
return startServer();
}
protected boolean waitUntilDown() throws InterruptedException {
boolean notAnswering = false;
while (!notAnswering) {
Operation op = new ReadAttribute(new Address(), "release-version");
try {
Result res = getASConnection().execute(op);
if (!res.isSuccess()) { // If op succeeds, server is not down
notAnswering = true;
}
} catch (Exception e) {
notAnswering = true;
}
if (!notAnswering) {
if (context.getComponentInvocationContext().isInterrupted()) {
// Operation canceled or timed out
throw new InterruptedException();
}
Thread.sleep(SECONDS.toMillis(1));
}
}
// BZ 893802: wait until server (the process) is really down
HostConfiguration hostConfig = getHostConfig();
// commandLine instance is not important for determining whether the HostPort is local or not
AS7CommandLine commandLine = new AS7CommandLine(new String[] { "java", "foo.Main",
"org.jboss.as.host-controller" });
HostPort hostPort = hostConfig.getDomainControllerHostPort(commandLine);
if (hostPort.isLocal) {
// lets be paranoid here
for (ProcessInfo processInfo = context.getNativeProcess();; processInfo = context.getNativeProcess()) {
if (processInfo == null) {
// Process not found, so it died, that's fine
break;
}
if (!processInfo.priorSnaphot().isRunning()) {
// Process info says process is no longer running, that's fine
break;
}
if (context.getComponentInvocationContext().isInterrupted()) {
// Operation canceled or timed out
throw new InterruptedException();
}
// Process is still running, wait a second and check again
Thread.sleep(SECONDS.toMillis(1));
}
}
return true;
}
/**
* Start the server by calling the start script defined in the plugin configuration.
*
* @return the result of the operation
*/
protected OperationResult startServer() throws InterruptedException {
OperationResult operationResult = new OperationResult();
if (isManuallyAddedServer(operationResult, "Starting")) {
return operationResult;
}
List<String> errors = validateStartScriptPluginConfigProps();
if (!errors.isEmpty()) {
setErrorMessage(operationResult, errors);
return operationResult;
}
ProcessExecutionResults results = ServerControl
.onServer(context.getPluginConfiguration(), getMode(), context.getSystemInformation())
.lifecycle().startServer();
ProcessExecutionLogger.logExecutionResults(results);
if (results.getError() != null) {
operationResult.setErrorMessage(results.getError().getMessage());
} else if (results.getExitCode() != null && results.getExitCode() != 0) {
operationResult.setErrorMessage("Start failed with error code " + results.getExitCode() + ":\n"
+ results.getCapturedOutput());
} else {
// Try to connect to the server - ping once per second, timing out after 20s.
boolean up = waitForServerToStart();
if (up) {
operationResult.setSimpleResult("Success");
} else {
operationResult.setErrorMessage("Was not able to start the server");
}
}
context.getAvailabilityContext().requestAvailabilityCheck();
return operationResult;
}
protected OperationResult setupCli(Configuration parameters) {
OperationResult result = new OperationResult();
ServerPluginConfiguration serverConfig = getServerPluginConfiguration();
File jbossCliXml = new File(new File(serverConfig.getHomeDir(), "bin"), "jboss-cli.xml");
try {
JBossCliConfiguration config = new JBossCliConfiguration(jbossCliXml, serverConfig);
StringBuilder response = new StringBuilder();
boolean madeChanges = false;
if (Boolean.parseBoolean(parameters.getSimpleValue("defaultController", "false"))) {
String m = config.configureDefaultController();
madeChanges |= m == null;
response.append(m == null ? "Setting up Default Controller" : "Default Controller skipped : " + m);
response.append("\n");
}
if (Boolean.parseBoolean(parameters.getSimpleValue("security", "false"))) {
String storeMethod = parameters.getSimpleValue("storePasswordMethod", "PLAIN");
String m = null;
String message = "Setting up Security";
if ("PLAIN".equals(storeMethod)) {
message += " (using plain text)";
m = config.configureSecurity();
} else {
message += " (using vault)";
m = config.configureSecurityUsingVault(getHostConfig());
}
madeChanges |= m == null;
response.append(m == null ? message : "Security skipped: " + m);
response.append("\n");
}
if (madeChanges) {
config.writeToFile();
response.append("Wrote changes to " + jbossCliXml);
result.setSimpleResult(response.toString());
} else {
result.setSimpleResult(jbossCliXml + " was not updated");
}
} catch (Exception e) {
getLog().error("Failed to setup CLI", e);
result.setErrorMessage("Failed to setup CLI : " + e.getMessage());
}
return result;
}
/**
* runs jboss-cli executable and returns its output
* @param parameters input configuration (either commands or file sipmle-property is expected)
* @return the result of the operation
* @throws InterruptedException
*/
protected OperationResult runCliCommand(Configuration parameters) throws InterruptedException {
OperationResult result = new OperationResult();
if (isManuallyAddedServer(result, "Executing jboss-cli")) {
return result;
}
long waitTime = Integer.parseInt(parameters.getSimpleValue("waitTime", "3600"));
if (waitTime <= 0) {
result.setErrorMessage("waitTime parameter must be positive integer");
return result;
}
ServerControl.Cli cli = ServerControl
.onServer(context.getPluginConfiguration(), getMode(), context.getSystemInformation())
.waitingFor(waitTime * 1000)
.killingOnTimeout(Boolean.parseBoolean(parameters.getSimpleValue("killOnTimeout", "false"))).cli();
ProcessExecutionResults results;
String commands = parameters.getSimpleValue("commands");
if (commands != null) {
results = cli.executeCliCommand(commands);
} else {
File script = new File(parameters.getSimpleValue("file"));
if (!script.isAbsolute()) {
script = new File(serverPluginConfig.getHomeDir(), script.getPath()).getAbsoluteFile();
}
results = cli.executeCliScript(script);
}
ProcessExecutionLogger.logExecutionResults(results);
result.setSimpleResult(results.getCapturedOutput());
if (results.getError() != null) {
result.setErrorMessage(results.getError().getMessage());
return result;
}
if (results.getExitCode() == null) {
result.setErrorMessage("jboss-cli execution timed out");
return result;
}
if (results.getExitCode() != 0) {
result.setErrorMessage("jboss-cli execution failed with error code " + results.getExitCode());
return result;
}
if (Boolean.parseBoolean(parameters.getSimpleValue("triggerAvailability", null))) {
context.getAvailabilityContext().requestAvailabilityCheck();
}
if (Boolean.parseBoolean(parameters.getSimpleValue("triggerDiscovery", null))) {
context.getInventoryContext().requestDeferredChildResourcesDiscovery();
}
return result;
}
public boolean isManuallyAddedServer() {
return pluginConfiguration.get("manuallyAdded") != null;
}
private boolean isManuallyAddedServer(OperationResult operationResult, String operation) {
if (isManuallyAddedServer()) {
operationResult.setErrorMessage(operation + " is not enabled for manually added servers");
return true;
}
return false;
}
private void setErrorMessage(OperationResult operationResult, List<String> errors) {
StringBuilder buffer = new StringBuilder("This Resource's connection properties contain errors: ");
for (int i = 0, errorsSize = errors.size(); i < errorsSize; i++) {
if (i != 0) {
buffer.append(", ");
}
String error = errors.get(i);
buffer.append('[').append(error).append(']');
}
operationResult.setErrorMessage(buffer.toString());
}
private List<String> validateStartScriptPluginConfigProps() {
List<String> errors = new ArrayList<String>();
File startScriptFile = getStartScriptFile();
if (!startScriptFile.exists()) {
errors.add("Start script '" + startScriptFile + "' does not exist.");
} else {
if (!startScriptFile.isFile()) {
errors.add("Start script '" + startScriptFile + "' is not a regular file.");
} else {
if (!startScriptFile.canRead()) {
errors.add("Start script '" + startScriptFile + "' is not readable.");
}
if (!startScriptFile.canExecute()) {
errors.add("Start script '" + startScriptFile + "' is not executable.");
}
}
}
return errors;
}
private File getStartScriptFile() {
File startScriptFile = startScriptConfig.getStartScript();
File homeDir = serverPluginConfig.getHomeDir();
if (startScriptFile != null) {
if (!startScriptFile.isAbsolute()) {
startScriptFile = new File(homeDir, startScriptFile.getPath());
}
} else {
// Use the default start script.
String startScriptFileName = getMode().getStartScriptFileName();
File binDir = new File(homeDir, "bin");
startScriptFile = new File(binDir, startScriptFileName);
}
return startScriptFile;
}
private boolean waitForServerToStart() throws InterruptedException {
boolean up = false;
while (!up) {
Operation op = new ReadAttribute(new Address(), "release-version");
try {
Result res = getASConnection().execute(op);
if (res.isSuccess()) { // If op succeeds, server is not down
up = true;
}
} catch (Exception e) {
//do absolutely nothing
//if an exception is thrown that means the server is still down, so consider this
//a single failed attempt, equivalent to res.isSuccess == false
}
if (!up) {
if (context.getComponentInvocationContext().isInterrupted()) {
// Operation canceled or timed out
throw new InterruptedException();
}
Thread.sleep(SECONDS.toMillis(1));
}
}
return true;
}
/**
* Do some post processing of the Result - especially the 'shutdown' operation needs a special
* treatment.
* @param name Name of the operation
* @param res Result of the operation vs. AS7
* @return OperationResult filled in from values of res
*/
protected OperationResult postProcessResult(String name, Result res) {
OperationResult operationResult = new OperationResult();
if (res == null) {
operationResult.setErrorMessage("No result received from server");
return operationResult;
}
if (name.equals("shutdown") || name.equals("restart") || name.equals("reload")) {
/*
* Shutdown, restart and reload need a special treatment, because after sending the operation, event if
* it succeeds, the server connection is sometimes closed and we can't read from it.
*/
if (!res.isSuccess()) {
if (StringUtil.isNotBlank(res.getFailureDescription())
&& res.getFailureDescription().startsWith(ASConnection.FAILURE_NO_RESPONSE)) {
operationResult.setSimpleResult("Success");
if (LOG.isDebugEnabled()) {
LOG.debug("Got no response for operation '" + name + "'. "
+ "This is considered ok, as the remote server sometimes closes the communications "
+ "channel before sending a reply");
}
} else {
operationResult.setErrorMessage(res.getFailureDescription());
}
} else {
operationResult.setSimpleResult("Success");
}
} else {
if (res.isSuccess()) {
if (res.getResult() != null)
operationResult.setSimpleResult(res.getResult().toString());
else
operationResult.setSimpleResult("-None provided by server-");
} else
operationResult.setErrorMessage(res.getFailureDescription());
}
return operationResult;
}
protected OperationResult installManagementUser(Configuration parameters, Configuration pluginConfig) {
String user = parameters.getSimpleValue("user", "");
String password = parameters.getSimpleValue("password", "");
OperationResult result = new OperationResult();
PropertySimple remoteProp = pluginConfig.getSimple("manuallyAdded");
if (remoteProp != null && remoteProp.getBooleanValue() != null && remoteProp.getBooleanValue()) {
result
.setErrorMessage("This is a manually added server. This operation can not be used to install a management user. Use the server's 'bin/add-user.sh'");
return result;
}
if (user.isEmpty() || password.isEmpty()) {
result.setErrorMessage("User and Password must not be empty");
return result;
}
File baseDir = serverPluginConfig.getBaseDir();
if (baseDir == null) {
result.setErrorMessage("'" + ServerPluginConfiguration.Property.BASE_DIR
+ "' plugin config prop is not set.");
return result;
}
HostConfiguration hostConfig = getHostConfig();
String realm = pluginConfig.getSimpleValue("realm", "ManagementRealm");
File propertiesFile = hostConfig.getSecurityPropertyFile(serverPluginConfig, realm);
if (!propertiesFile.canWrite()) {
result.setErrorMessage("Management users properties file [" + propertiesFile + "] is not writable.");
return result;
}
String encryptedPassword;
try {
UsernamePasswordHashUtil hashUtil = new UsernamePasswordHashUtil();
encryptedPassword = hashUtil.generateHashedHexURP(user, realm, password.toCharArray());
} catch (Exception e) {
throw new RuntimeException("Failed to encrypt password.", e);
}
boolean userAlreadyExisted;
try {
PropertiesFileUpdate propsFileUpdate = new PropertiesFileUpdate(propertiesFile.getPath());
userAlreadyExisted = propsFileUpdate.update(user, encryptedPassword);
} catch (Exception e) {
throw new RuntimeException("Failed to update management users properties file [" + propertiesFile + "].", e);
}
String verb = (userAlreadyExisted) ? "updated" : "added";
result.setSimpleResult("Management user [" + user + "] " + verb + ".");
LOG.info("Management user [" + user + "] " + verb + " for " + context.getResourceType().getName()
+ " server with key [" + context.getResourceKey() + "].");
context.getAvailabilityContext().requestAvailabilityCheck();
context.getInventoryContext().requestDeferredChildResourcesDiscovery();
return result;
}
/**
* Requests a deferred child resource discovery for sub-resources of this server.
*/
public void requestDeferredChildResourcesDiscovery() {
this.context.getInventoryContext().requestDeferredChildResourcesDiscovery();
}
@Override
public void getValues(MeasurementReport report, Set<MeasurementScheduleRequest> requests) throws Exception {
Set<MeasurementScheduleRequest> skmRequests = new HashSet<MeasurementScheduleRequest>(requests.size());
Set<MeasurementScheduleRequest> leftovers = new HashSet<MeasurementScheduleRequest>(requests.size());
String tempDirAttributeName = getTempDirAttributeName();
for (MeasurementScheduleRequest request : requests) {
String requestName = request.getName();
if (requestName.equals("startTime")) {
collectStartTimeTrait(report, request);
} else {
if ("active-patches".equals(requestName)) {
String patches = collectPatches();
if (patches != null) {
report.addData(new MeasurementDataTrait(request, patches));
}
} else if (tempDirAttributeName != null && requestName.equals(tempDirAttributeName)) {
collectEnvironmentTrait(report, request);
} else if (requestName.startsWith("_skm:")) { // handled below
skmRequests.add(request);
} else {
leftovers.add(request); // handled below
}
}
}
// Now handle the server kind traits.
if (skmRequests.size() > 0) {
collectServerKindTraits(report, skmRequests);
}
// Finally let our superclass handle the leftovers.
super.getValues(report, leftovers);
}
/**
* The release version as returned by the "release-version" attribute of the root node in the management model.
*/
public String getReleaseVersion() {
if (releaseVersion == null) {
releaseVersion = (String) getASConnection().execute(new ReadAttribute(new Address(), "release-version"))
.getResult();
}
return releaseVersion;
}
private void collectStartTimeTrait(MeasurementReport report, MeasurementScheduleRequest request) {
Address address = new Address(getHostAddress());
address.add("core-service", "platform-mbean");
address.add("type", "runtime");
Long startTime;
try {
startTime = readAttribute(address, "start-time", Long.class);
} catch (Exception e) {
startTime = null;
}
if (startTime != null) {
MeasurementDataTrait data = new MeasurementDataTrait(request, new Date(startTime).toString());
report.addData(data);
}
}
protected BundleHandoverResponse handleExecuteScript(BundleHandoverRequest handoverRequest) throws IOException {
Map<String, String> params = handoverRequest.getParams();
long waitTime;
String waitTimeParam = params.get("waitTime");
if (waitTimeParam != null) {
try {
waitTime = Long.parseLong(waitTimeParam);
if (waitTime <= 0) {
return BundleHandoverResponse.failure(INVALID_PARAMETER, "waitTime must greater than 0");
}
} catch (NumberFormatException e) {
return BundleHandoverResponse.failure(INVALID_PARAMETER, "waitTime is not a number");
}
} else {
waitTime = HOURS.toMillis(1);
}
boolean killOnTimeout = Boolean.parseBoolean(params.get("killOnTimeout"));
File scriptFile = null;
try {
scriptFile = File.createTempFile(handoverRequest.getFilename(), ".tmp", context.getTemporaryDirectory());
FileUtil.writeFile(handoverRequest.getContent(), scriptFile);
ProcessExecutionResults results = ServerControl //
.onServer( //
getServerPluginConfiguration().getPluginConfig(), //
getMode(), //
context.getSystemInformation() //
) //
.waitingFor(waitTime) //
.killingOnTimeout(killOnTimeout) //
.cli() //
.executeCliScript(scriptFile.getAbsoluteFile());
ProcessExecutionLogger.logExecutionResults(results);
Throwable error = results.getError();
if (error != null) {
return BundleHandoverResponse.failure(EXECUTION, error.getMessage(), error);
}
Integer exitCode = results.getExitCode();
if (exitCode == null) {
return BundleHandoverResponse.failure(EXECUTION, "Timeout waiting for completion of the CLI process");
}
if (exitCode != 0) {
return BundleHandoverResponse.failure(EXECUTION, "CLI process exit code is " + exitCode);
}
return BundleHandoverResponse.success();
} finally {
if (scriptFile != null) {
scriptFile.delete();
}
}
}
@NotNull
protected abstract Address getEnvironmentAddress();
@NotNull
protected abstract Address getHostAddress();
@NotNull
protected abstract String getBaseDirAttributeName();
@NotNull
protected abstract String getConfigDirAttributeName();
/**
* Default implentation. Override in concrete subclasses and return the the temporary directory attribute name,
* found on the node at {@link #getEnvironmentAddress()}.
*
* @return the temp dir attribute name or null if no such attribute exists
*/
protected String getTempDirAttributeName() {
return null;
}
protected void collectConfigTrait(MeasurementReport report, MeasurementScheduleRequest request) {
String value = readEnvironmentAttribute(request);
if (value != null) {
MeasurementDataTrait data = new MeasurementDataTrait(request, new File(value).getName());
report.addData(data);
}
}
protected void collectEnvironmentTrait(MeasurementReport report, MeasurementScheduleRequest request) {
String value = readEnvironmentAttribute(request);
if (value != null) {
MeasurementDataTrait data = new MeasurementDataTrait(request, value);
report.addData(data);
}
}
private String readEnvironmentAttribute(MeasurementScheduleRequest request) {
try {
return readAttribute(getEnvironmentAddress(), request.getName(), String.class);
} catch (Exception e) {
LOG.error("Failed to read attribute [" + request.getName() + "]: " + e, e);
return null;
}
}
private synchronized void setASHostName(String aSHostName) {
this.aSHostName = aSHostName;
}
/**
* gets AS domain host name (defult is master for HC) null for standalone;
* @return AS domain host name
*/
protected synchronized String getASHostName() {
return aSHostName;
}
/**
* @see #findASDomainHostName(ASConnection)
*/
protected String findASDomainHostName() {
if (getMode().equals(AS7Mode.STANDALONE)) {
return null;
}
return findASDomainHostName(getASConnection());
}
protected ExpressionResolver getExpressionResolver() {
return expressionResolver;
}
/**
* Reads local-host-name attribute
*
* @return name current host within EAP domain or null if we failed to read it
*/
public static String findASDomainHostName(ASConnection connection) {
ReadAttribute op = new ReadAttribute(new Address(), "local-host-name");
op.includeDefaults(true);
Result result = connection.execute(op);
if (result.isSuccess()) {
return result.getResult().toString();
}
return null;
}
private HostConfiguration getHostConfig() {
File configFile;
HostConfiguration hostConfig;
try {
String config = readAttribute(getEnvironmentAddress(), getMode().getHostConfigAttributeName());
configFile = new File(config);
} catch (Exception e) {
// This probably means the server is not running and/or authentication is not set up. Fallback to the
// host config file set in the plugin config during discovery.
// TODO (ips, 05/05/12): This is not ideal, because the user could have restarted the server with a
// different config file, since the time it was imported into inventory. The better
// thing to do here is to find the current server process and parse its command line
// to find the current config file name.
configFile = serverPluginConfig.getHostConfigFile();
if (configFile == null) {
throw new RuntimeException("Failed to determine config file path.", e);
}
}
try {
hostConfig = new HostConfiguration(configFile);
} catch (Exception e) {
throw new RuntimeException("Failed to parse configuration file [" + configFile + "].", e);
}
return hostConfig;
}
private void collectServerKindTraits(MeasurementReport report, Set<MeasurementScheduleRequest> skmRequests) {
Address address = new Address();
ReadResource op = new ReadResource(address);
op.includeRuntime(true);
ComplexResult res = getASConnection().executeComplex(op);
if (res.isSuccess()) {