32
32
import java .io .FileOutputStream ;
33
33
import java .io .IOException ;
34
34
import java .io .OutputStream ;
35
+ import java .io .StringWriter ;
35
36
import java .nio .file .Files ;
36
37
import java .nio .file .Path ;
37
38
import java .util .ArrayList ;
@@ -61,17 +62,18 @@ public class CcmBridge implements AutoCloseable {
61
62
62
63
private static final Logger LOG = LoggerFactory .getLogger (CcmBridge .class );
63
64
64
- public static final Version VERSION =
65
- Objects .requireNonNull (Version .parse (System .getProperty ("ccm.version" , "4.0.0" )));
65
+ public static final Boolean SCYLLA_ENABLEMENT = Boolean .getBoolean ("ccm.scylla" );
66
+
67
+ public static final String CCM_VERSION_PROPERTY = System .getProperty ("ccm.version" , "4.0.0" );
68
+
69
+ public static final Version VERSION = Objects .requireNonNull (parseCcmVersion ());
66
70
67
71
public static final String INSTALL_DIRECTORY = System .getProperty ("ccm.directory" );
68
72
69
73
public static final String BRANCH = System .getProperty ("ccm.branch" );
70
74
71
75
public static final Boolean DSE_ENABLEMENT = Boolean .getBoolean ("ccm.dse" );
72
76
73
- public static final Boolean SCYLLA_ENABLEMENT = Boolean .getBoolean ("ccm.scylla" );
74
-
75
77
public static final Boolean SCYLLA_ENTERPRISE =
76
78
String .valueOf (VERSION .getMajor ()).matches ("\\ d{4}" );
77
79
@@ -232,6 +234,45 @@ private static boolean isWindows() {
232
234
return System .getProperty ("os.name" , "" ).toLowerCase (Locale .US ).contains ("win" );
233
235
}
234
236
237
+ private static Version parseCcmVersion () {
238
+ String versionString = CCM_VERSION_PROPERTY ;
239
+ Version result = null ;
240
+ try {
241
+ result = Version .parse (versionString );
242
+ return result ;
243
+ } catch (IllegalArgumentException ex ) {
244
+ LOG .warn (
245
+ "Failed to parse ccm.version property \' {}\' as Version instance. Attempting to fetch it through \' ccm node versionfrombuild\' " ,
246
+ versionString );
247
+ }
248
+ Path configDir = null ;
249
+ try {
250
+ configDir = Files .createTempDirectory ("ccmParseVer" );
251
+ configDir .toFile ().deleteOnExit ();
252
+ execute (
253
+ CommandLine .parse (
254
+ String .format (
255
+ "ccm create get_version -n 1 %s --version %s --config-dir=%s" ,
256
+ (SCYLLA_ENABLEMENT ? "--scylla" : " " ), versionString , configDir )));
257
+ String output =
258
+ execute (
259
+ CommandLine .parse (
260
+ String .format ("ccm node1 versionfrombuild --config-dir=%s" , configDir )));
261
+ result = Version .parse (output .trim ());
262
+ LOG .info ("Cluster reports that {} corresponds to version {}" , versionString , result );
263
+ } catch (IOException e ) {
264
+ throw new RuntimeException (e );
265
+ } finally {
266
+ try {
267
+ if (configDir != null ) {
268
+ execute (CommandLine .parse ("ccm remove get_version --config-dir=" + configDir ));
269
+ }
270
+ } catch (Exception ignored ) {
271
+ }
272
+ }
273
+ return result ;
274
+ }
275
+
235
276
public Optional <Version > getScyllaVersion () {
236
277
return SCYLLA_ENABLEMENT ? Optional .of (VERSION ) : Optional .empty ();
237
278
}
@@ -264,7 +305,15 @@ public Version getCassandraVersion() {
264
305
}
265
306
}
266
307
267
- private String getCcmVersionString (Version version ) {
308
+ private String getCcmVersionString (String propertyString ) {
309
+ Version version = null ;
310
+ try {
311
+ version = Version .parse (propertyString );
312
+ } catch (IllegalArgumentException ex ) {
313
+ // In case it's not a recognized version pattern, use raw string.
314
+ // If parseCcmVersion has not failed execution it should be usable.
315
+ return propertyString ;
316
+ }
268
317
if (SCYLLA_ENABLEMENT ) {
269
318
// Scylla OSS versions before 5.1 had RC versioning scheme of 5.0.rc3.
270
319
// Scylla OSS versions after (and including 5.1) have RC versioning of 5.1.0-rc3.
@@ -307,7 +356,7 @@ public void create() {
307
356
createOptions .add ("-v git:" + BRANCH .trim ().replaceAll ("\" " , "" ));
308
357
309
358
} else {
310
- createOptions .add ("-v " + getCcmVersionString (VERSION ));
359
+ createOptions .add ("-v " + getCcmVersionString (CCM_VERSION_PROPERTY ));
311
360
}
312
361
if (DSE_ENABLEMENT ) {
313
362
createOptions .add ("--dse" );
@@ -497,21 +546,25 @@ private void executeCheckLogError() {
497
546
execute (CommandLine .parse (command ));
498
547
}
499
548
500
- private void execute (CommandLine cli ) {
501
- LOG .info ("Executing: " + cli );
549
+ private static String execute (CommandLine cli ) {
550
+ Logger logger = CcmBridge .LOG ;
551
+ logger .info ("Executing: " + cli );
502
552
ExecuteWatchdog watchDog = new ExecuteWatchdog (TimeUnit .MINUTES .toMillis (10 ));
553
+ StringWriter sw = new StringWriter ();
503
554
try (LogOutputStream outStream =
504
555
new LogOutputStream () {
505
556
@ Override
506
557
protected void processLine (String line , int logLevel ) {
507
- LOG .info ("ccmout> {}" , line );
558
+ logger .info ("ccmout> {}" , line );
559
+ sw .append (line ).append (System .lineSeparator ());
508
560
}
509
561
};
510
562
LogOutputStream errStream =
511
563
new LogOutputStream () {
512
564
@ Override
513
565
protected void processLine (String line , int logLevel ) {
514
- LOG .error ("ccmerr> {}" , line );
566
+ logger .error ("ccmerr> {}" , line );
567
+ sw .append (line ).append (System .lineSeparator ());
515
568
}
516
569
}) {
517
570
Executor executor = new DefaultExecutor ();
@@ -521,7 +574,8 @@ protected void processLine(String line, int logLevel) {
521
574
522
575
int retValue = executor .execute (cli , ENVIRONMENT_MAP );
523
576
if (retValue != 0 ) {
524
- LOG .error ("Non-zero exit code ({}) returned from executing ccm command: {}" , retValue , cli );
577
+ logger .error (
578
+ "Non-zero exit code ({}) returned from executing ccm command: {}" , retValue , cli );
525
579
}
526
580
} catch (IOException ex ) {
527
581
if (watchDog .killedProcess ()) {
@@ -530,6 +584,7 @@ protected void processLine(String line, int logLevel) {
530
584
throw new RuntimeException ("The command '" + cli + "' failed to execute" , ex );
531
585
}
532
586
}
587
+ return sw .toString ();
533
588
}
534
589
535
590
@ Override
0 commit comments