Skip to content

Commit 71bcb93

Browse files
committed
Replacing some scripts by gradle plugins
1 parent 1c9ca2b commit 71bcb93

File tree

7 files changed

+206
-12
lines changed

7 files changed

+206
-12
lines changed

build-and-test-all.sh

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,6 @@ echo KEEP_RUNNING=$KEEP_RUNNING
2727

2828
. ./set-env.sh
2929

30-
initializeDynamoDB() {
31-
./initialize-dynamodb.sh
32-
}
33-
3430
# TODO Temporarily
3531

3632
./build-contracts.sh
@@ -40,11 +36,11 @@ initializeDynamoDB() {
4036
${DOCKER_COMPOSE?} down -v
4137
${DOCKER_COMPOSE?} up -d --build dynamodblocal mysql
4238

43-
./wait-for-mysql.sh
39+
./gradlew waitForMySql
4440

4541
echo mysql is started
4642

47-
initializeDynamoDB
43+
./gradlew initDynamoDb
4844

4945
${DOCKER_COMPOSE?} up -d --build eventuate-local-cdc-service tram-cdc-service
5046

@@ -68,11 +64,11 @@ if [ -z "$ASSEMBLE_ONLY" ] ; then
6864

6965
${DOCKER_COMPOSE?} up -d dynamodblocal mysql
7066

71-
./wait-for-mysql.sh
67+
./gradlew waitForMySql
7268

7369
echo mysql is started
7470

75-
initializeDynamoDB
71+
./gradlew initDynamoDb
7672

7773
${DOCKER_COMPOSE?} up -d
7874

@@ -83,11 +79,11 @@ else
8379

8480
${DOCKER_COMPOSE?} up -d --build dynamodblocal mysql
8581

86-
./wait-for-mysql.sh
82+
./gradlew waitForMySql
8783

8884
echo mysql is started
8985

90-
initializeDynamoDB
86+
./gradlew initDynamoDb
9187

9288
${DOCKER_COMPOSE?} up -d --build
9389

build.gradle

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ buildscript {
55
dependencies {
66
classpath "org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}"
77
}
8+
9+
apply plugin: WaitForMySqlPlugin
10+
apply plugin: InitDynamoDbPlugin
811
}
912

1013
plugins {
@@ -14,7 +17,6 @@ plugins {
1417
subprojects {
1518

1619
apply plugin: "java"
17-
1820
sourceCompatibility = '1.8'
1921
targetCompatibility = '1.8'
2022

buildSrc/build.gradle

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,9 @@
1-
test.enabled=false
1+
test.enabled=false
2+
3+
repositories {
4+
mavenCentral()
5+
}
6+
7+
dependencies {
8+
compile 'mysql:mysql-connector-java:5.1.39'
9+
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
import org.gradle.api.DefaultTask;
2+
import org.gradle.api.tasks.TaskAction;
3+
4+
import java.io.BufferedReader;
5+
import java.io.IOException;
6+
import java.io.InputStream;
7+
import java.io.InputStreamReader;
8+
9+
public class InitDynamoDb extends DefaultTask {
10+
11+
@TaskAction
12+
public void initDynamoDb() {
13+
System.out.println("Initializing dynamodb...");
14+
15+
String endPoint = System.getenv("AWS_DYNAMODB_ENDPOINT_URL");
16+
17+
System.out.println("Checking if table already exists...");
18+
19+
if (!checkIfTableAlreadyExist(endPoint)) {
20+
System.out.println("Table does not exist, creating...");
21+
initDb(endPoint);
22+
} else {
23+
System.out.println("Table already exists, do nothing");
24+
}
25+
}
26+
27+
private boolean checkIfTableAlreadyExist(String endPoint) {
28+
String cmd = String.format("aws dynamodb --region us-west-1 --endpoint-url %s describe-table --table-name ftgo-order-history", endPoint == null ? "" : endPoint);
29+
return exec(cmd);
30+
}
31+
32+
private void initDb(String endPoint) {
33+
String cmd = String.format("aws dynamodb create-table --region us-west-2 --endpoint-url %s --cli-input-json file://./dynamodblocal-init/ftgo-order-history.json", endPoint == null ? "" : endPoint);
34+
if (exec(cmd)) {
35+
System.out.println("Initialization succeed");
36+
} else {
37+
System.out.println("Initialization failed");
38+
}
39+
}
40+
41+
private boolean exec(String command) {
42+
Process p;
43+
44+
try {
45+
p = Runtime.getRuntime().exec(command);
46+
} catch (IOException e) {
47+
System.out.println("command execution failed");
48+
throw new RuntimeException(e);
49+
}
50+
51+
try {
52+
p.waitFor();
53+
} catch (InterruptedException e) {
54+
System.out.println("Waiting for command failed.");
55+
}
56+
57+
String result;
58+
try {
59+
result = readFromStream(p.getInputStream());
60+
} catch (IOException e) {
61+
System.out.println("Result read failed");
62+
throw new RuntimeException(e);
63+
}
64+
65+
if (result.isEmpty()) {
66+
try {
67+
result = readFromStream(p.getErrorStream());
68+
System.out.println(result);
69+
} catch (IOException e) {
70+
System.out.println("Error read failed");
71+
}
72+
return false;
73+
} else {
74+
System.out.println(result);
75+
return true;
76+
}
77+
}
78+
79+
private String readFromStream(InputStream inputStream) throws IOException {
80+
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
81+
82+
StringBuilder sb = new StringBuilder();
83+
String line = "";
84+
while ((line = reader.readLine())!= null) {
85+
sb.append(line + "\n");
86+
}
87+
return sb.toString();
88+
}
89+
90+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import org.gradle.api.Plugin;
2+
import org.gradle.api.Project;
3+
4+
public class InitDynamoDbPlugin implements Plugin<Project> {
5+
@Override
6+
public void apply(Project project) {
7+
project.getTasks().create("initDynamoDb", InitDynamoDb.class);
8+
}
9+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import org.gradle.api.DefaultTask;
2+
import org.gradle.api.tasks.TaskAction;
3+
4+
import java.sql.Connection;
5+
import java.sql.DriverManager;
6+
import java.sql.SQLException;
7+
8+
public class WaitForMySql extends DefaultTask {
9+
10+
@TaskAction
11+
public void waitForMySql() {
12+
System.out.println("Waiting for MySql...");
13+
14+
loadDriver();
15+
16+
waitForConnection();
17+
}
18+
19+
private void loadDriver() {
20+
try {
21+
System.out.println("Trying to initialize driver");
22+
23+
String datasourceDriverClassName = System.getenv("SPRING_DATASOURCE_DRIVER_CLASS_NAME");
24+
Class.forName(datasourceDriverClassName);
25+
26+
System.out.println("Initialization succeed");
27+
} catch (ClassNotFoundException e) {
28+
System.out.println("Initialization failed");
29+
30+
throw new RuntimeException(e);
31+
}
32+
}
33+
34+
private void waitForConnection() {
35+
while (true) {
36+
try {
37+
System.out.println("Trying to connect...");
38+
39+
String datasourceUrl = System.getenv("SPRING_DATASOURCE_URL");
40+
String datasourceUsername = System.getenv("SPRING_DATASOURCE_USERNAME");
41+
String datasourcePassword = System.getenv("SPRING_DATASOURCE_PASSWORD");
42+
43+
Connection connection = DriverManager.getConnection(datasourceUrl, datasourceUsername, datasourcePassword);
44+
45+
System.out.println("Connection succeed");
46+
47+
closeConnection(connection);
48+
break;
49+
} catch (SQLException e) {
50+
System.out.println("Connection failed");
51+
52+
sleep();
53+
}
54+
}
55+
}
56+
57+
private void closeConnection(Connection connection) {
58+
try {
59+
System.out.println("Trying to close connection");
60+
61+
connection.close();
62+
63+
System.out.println("Connection closed");
64+
} catch (SQLException e) {
65+
System.out.println("Failed to close connection, see stack trace:");
66+
67+
e.printStackTrace();
68+
}
69+
}
70+
71+
private void sleep() {
72+
System.out.println("sleeping for 5 seconds...");
73+
74+
try {
75+
Thread.sleep(5000);
76+
} catch (InterruptedException ie) {
77+
throw new RuntimeException(ie);
78+
}
79+
}
80+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import org.gradle.api.Plugin;
2+
import org.gradle.api.Project;
3+
4+
public class WaitForMySqlPlugin implements Plugin<Project> {
5+
@Override
6+
public void apply(Project project) {
7+
project.getTasks().create("waitForMySql", WaitForMySql.class);
8+
}
9+
}

0 commit comments

Comments
 (0)