@@ -102,18 +102,18 @@ public class TarantoolCartridgeContainer extends GenericContainer<TarantoolCartr
102
102
private static final String ENV_TARANTOOL_RUNDIR = "TARANTOOL_RUNDIR" ;
103
103
private static final String ENV_TARANTOOL_DATADIR = "TARANTOOL_DATADIR" ;
104
104
private static final String ENV_TARANTOOL_INSTANCES_FILE = "TARANTOOL_INSTANCES_FILE" ;
105
+ private final CartridgeConfigParser instanceFileParser ;
106
+ private final TarantoolContainerClientHelper clientHelper ;
105
107
private boolean useFixedPorts = false ;
106
-
107
108
private String routerHost = ROUTER_HOST ;
108
109
private int routerPort = ROUTER_PORT ;
109
110
private int apiPort = API_PORT ;
110
111
private String routerUsername = CARTRIDGE_USERNAME ;
111
112
private String routerPassword = CARTRIDGE_PASSWORD ;
112
113
private String directoryResourcePath = SCRIPT_RESOURCE_DIRECTORY ;
113
114
private String instanceDir = INSTANCE_DIR ;
114
- private final CartridgeConfigParser instanceFileParser ;
115
- private final String topologyConfigurationFile ;
116
- private final TarantoolContainerClientHelper clientHelper ;
115
+ private String topologyConfigurationFile ;
116
+ private String replicasetsFileName ;
117
117
118
118
/**
119
119
* Create a container with default image and specified instances file from the classpath resources. Assumes that
@@ -166,7 +166,6 @@ public TarantoolCartridgeContainer(String dockerFile, String buildImageName,
166
166
this (dockerFile , buildImageName , instancesFile , topologyConfigurationFile , Collections .emptyMap ());
167
167
}
168
168
169
-
170
169
/**
171
170
* Create a container with specified image and specified instances file from the classpath resources. By providing
172
171
* the result Cartridge container image name, you can cache the image and avoid rebuilding on each test run (the
@@ -186,13 +185,22 @@ public TarantoolCartridgeContainer(String dockerFile, String buildImageName, Str
186
185
instancesFile , topologyConfigurationFile );
187
186
}
188
187
188
+
189
189
private TarantoolCartridgeContainer (Future <String > image , String instancesFile , String topologyConfigurationFile ) {
190
190
super (image );
191
191
if (instancesFile == null || instancesFile .isEmpty ()) {
192
192
throw new IllegalArgumentException ("Instance file name must not be null or empty" );
193
193
}
194
+ if (topologyConfigurationFile == null || topologyConfigurationFile .isEmpty ()) {
195
+ throw new IllegalArgumentException ("Topology configuration file must not be null or empty" );
196
+ }
197
+ String fileType = topologyConfigurationFile .substring (topologyConfigurationFile .lastIndexOf ('.' ) + 1 );
198
+ if (fileType .equals ("lua" )) {
199
+ this .topologyConfigurationFile = topologyConfigurationFile ;
200
+ }else {
201
+ this .replicasetsFileName = topologyConfigurationFile .substring (topologyConfigurationFile .lastIndexOf ('/' )+1 );
202
+ }
194
203
this .instanceFileParser = new CartridgeConfigParser (instancesFile );
195
- this .topologyConfigurationFile = topologyConfigurationFile ;
196
204
this .clientHelper = new TarantoolContainerClientHelper (this );
197
205
}
198
206
@@ -435,7 +443,6 @@ protected void configure() {
435
443
if (!getDirectoryBinding ().isEmpty ()) {
436
444
withFileSystemBind (getDirectoryBinding (), getInstanceDir (), BindMode .READ_WRITE );
437
445
}
438
-
439
446
if (useFixedPorts ) {
440
447
for (Integer port : instanceFileParser .getExposablePorts ()) {
441
448
addFixedExposedPort (port , port );
@@ -451,21 +458,55 @@ protected void containerIsStarting(InspectContainerResponse containerInfo) {
451
458
}
452
459
453
460
private boolean setupTopology () {
454
- try {
455
- executeScript (topologyConfigurationFile ).get ();
456
- // The client connection will be closed after that command
457
- } catch (Exception e ) {
458
- if (e instanceof ExecutionException ) {
459
- if (e .getCause () instanceof TimeoutException ) {
460
- return true ;
461
- // Do nothing, the cluster is reloading
462
- } else if (e .getCause () instanceof TarantoolConnectionException ) {
463
- // Probably cluster is not ready
464
- logger ().error ("Failed to setup topology: {}" , e .getMessage ());
465
- return false ;
461
+ if (topologyConfigurationFile == null ) {
462
+ String runDirPath = null ;
463
+ try {
464
+ Container .ExecResult envVariablesContainer = this .execInContainer ("env" );
465
+ String stdout = envVariablesContainer .getStdout ();
466
+ int exitCode = envVariablesContainer .getExitCode ();
467
+ if (exitCode != 0 ) {
468
+ logger ().error ("Failed to bootstrap replica sets topology: {}" , stdout );
469
+ }
470
+ int startInd = stdout .lastIndexOf (ENV_TARANTOOL_RUNDIR + "=" );
471
+ try {
472
+ runDirPath = stdout .substring (startInd ,
473
+ stdout .indexOf ('\n' , startInd )).split ("=" )[1 ];
474
+ } catch (Exception e ) {
475
+ logger ().error ("Missing dir-run environment variable: {}" , e .getMessage ());
476
+ }
477
+
478
+ } catch (Exception e ) {
479
+ logger ().error ("Failed to get environment variables: {}" , e .getMessage ());
480
+ }
481
+
482
+ try {
483
+ Container .ExecResult lsResult = this .execInContainer ("cartridge" , "replicasets" , "--run-dir=" + runDirPath ,
484
+ "--file=" + this .replicasetsFileName , "setup" , "--bootstrap-vshard" );
485
+ String stdout = lsResult .getStdout ();
486
+ int exitCode = lsResult .getExitCode ();
487
+ if (exitCode != 0 ) {
488
+ logger ().error ("Failed to bootstrap replica sets topology: {}" , stdout );
489
+ }
490
+ } catch (Exception e ) {
491
+ logger ().error ("Failed to bootstrap replica sets topology: {}" , e .getMessage ());
492
+ }
493
+ } else {
494
+ try {
495
+ executeScript (topologyConfigurationFile ).get ();
496
+ // The client connection will be closed after that command
497
+ } catch (Exception e ) {
498
+ if (e instanceof ExecutionException ) {
499
+ if (e .getCause () instanceof TimeoutException ) {
500
+ return true ;
501
+ // Do nothing, the cluster is reloading
502
+ } else if (e .getCause () instanceof TarantoolConnectionException ) {
503
+ // Probably cluster is not ready
504
+ logger ().error ("Failed to setup topology: {}" , e .getMessage ());
505
+ return false ;
506
+ }
507
+ } else {
508
+ throw new RuntimeException ("Failed to change the app topology" , e );
466
509
}
467
- } else {
468
- throw new RuntimeException ("Failed to change the app topology" , e );
469
510
}
470
511
}
471
512
return true ;
0 commit comments