Skip to content

Commit

Permalink
Replacing some scripts by gradle plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
dartartem committed Aug 29, 2018
1 parent 1c9ca2b commit 71bcb93
Show file tree
Hide file tree
Showing 7 changed files with 206 additions and 12 deletions.
16 changes: 6 additions & 10 deletions build-and-test-all.sh
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,6 @@ echo KEEP_RUNNING=$KEEP_RUNNING

. ./set-env.sh

initializeDynamoDB() {
./initialize-dynamodb.sh
}

# TODO Temporarily

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

./wait-for-mysql.sh
./gradlew waitForMySql

echo mysql is started

initializeDynamoDB
./gradlew initDynamoDb

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

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

${DOCKER_COMPOSE?} up -d dynamodblocal mysql

./wait-for-mysql.sh
./gradlew waitForMySql

echo mysql is started

initializeDynamoDB
./gradlew initDynamoDb

${DOCKER_COMPOSE?} up -d

Expand All @@ -83,11 +79,11 @@ else

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

./wait-for-mysql.sh
./gradlew waitForMySql

echo mysql is started

initializeDynamoDB
./gradlew initDynamoDb

${DOCKER_COMPOSE?} up -d --build

Expand Down
4 changes: 3 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ buildscript {
dependencies {
classpath "org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}"
}

apply plugin: WaitForMySqlPlugin
apply plugin: InitDynamoDbPlugin
}

plugins {
Expand All @@ -14,7 +17,6 @@ plugins {
subprojects {

apply plugin: "java"

sourceCompatibility = '1.8'
targetCompatibility = '1.8'

Expand Down
10 changes: 9 additions & 1 deletion buildSrc/build.gradle
Original file line number Diff line number Diff line change
@@ -1 +1,9 @@
test.enabled=false
test.enabled=false

repositories {
mavenCentral()
}

dependencies {
compile 'mysql:mysql-connector-java:5.1.39'
}
90 changes: 90 additions & 0 deletions buildSrc/src/main/groovy/InitDynamoDb.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import org.gradle.api.DefaultTask;
import org.gradle.api.tasks.TaskAction;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class InitDynamoDb extends DefaultTask {

@TaskAction
public void initDynamoDb() {
System.out.println("Initializing dynamodb...");

String endPoint = System.getenv("AWS_DYNAMODB_ENDPOINT_URL");

System.out.println("Checking if table already exists...");

if (!checkIfTableAlreadyExist(endPoint)) {
System.out.println("Table does not exist, creating...");
initDb(endPoint);
} else {
System.out.println("Table already exists, do nothing");
}
}

private boolean checkIfTableAlreadyExist(String endPoint) {
String cmd = String.format("aws dynamodb --region us-west-1 --endpoint-url %s describe-table --table-name ftgo-order-history", endPoint == null ? "" : endPoint);
return exec(cmd);
}

private void initDb(String endPoint) {
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);
if (exec(cmd)) {
System.out.println("Initialization succeed");
} else {
System.out.println("Initialization failed");
}
}

private boolean exec(String command) {
Process p;

try {
p = Runtime.getRuntime().exec(command);
} catch (IOException e) {
System.out.println("command execution failed");
throw new RuntimeException(e);
}

try {
p.waitFor();
} catch (InterruptedException e) {
System.out.println("Waiting for command failed.");
}

String result;
try {
result = readFromStream(p.getInputStream());
} catch (IOException e) {
System.out.println("Result read failed");
throw new RuntimeException(e);
}

if (result.isEmpty()) {
try {
result = readFromStream(p.getErrorStream());
System.out.println(result);
} catch (IOException e) {
System.out.println("Error read failed");
}
return false;
} else {
System.out.println(result);
return true;
}
}

private String readFromStream(InputStream inputStream) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));

StringBuilder sb = new StringBuilder();
String line = "";
while ((line = reader.readLine())!= null) {
sb.append(line + "\n");
}
return sb.toString();
}

}
9 changes: 9 additions & 0 deletions buildSrc/src/main/groovy/InitDynamoDbPlugin.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import org.gradle.api.Plugin;
import org.gradle.api.Project;

public class InitDynamoDbPlugin implements Plugin<Project> {
@Override
public void apply(Project project) {
project.getTasks().create("initDynamoDb", InitDynamoDb.class);
}
}
80 changes: 80 additions & 0 deletions buildSrc/src/main/groovy/WaitForMySql.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import org.gradle.api.DefaultTask;
import org.gradle.api.tasks.TaskAction;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class WaitForMySql extends DefaultTask {

@TaskAction
public void waitForMySql() {
System.out.println("Waiting for MySql...");

loadDriver();

waitForConnection();
}

private void loadDriver() {
try {
System.out.println("Trying to initialize driver");

String datasourceDriverClassName = System.getenv("SPRING_DATASOURCE_DRIVER_CLASS_NAME");
Class.forName(datasourceDriverClassName);

System.out.println("Initialization succeed");
} catch (ClassNotFoundException e) {
System.out.println("Initialization failed");

throw new RuntimeException(e);
}
}

private void waitForConnection() {
while (true) {
try {
System.out.println("Trying to connect...");

String datasourceUrl = System.getenv("SPRING_DATASOURCE_URL");
String datasourceUsername = System.getenv("SPRING_DATASOURCE_USERNAME");
String datasourcePassword = System.getenv("SPRING_DATASOURCE_PASSWORD");

Connection connection = DriverManager.getConnection(datasourceUrl, datasourceUsername, datasourcePassword);

System.out.println("Connection succeed");

closeConnection(connection);
break;
} catch (SQLException e) {
System.out.println("Connection failed");

sleep();
}
}
}

private void closeConnection(Connection connection) {
try {
System.out.println("Trying to close connection");

connection.close();

System.out.println("Connection closed");
} catch (SQLException e) {
System.out.println("Failed to close connection, see stack trace:");

e.printStackTrace();
}
}

private void sleep() {
System.out.println("sleeping for 5 seconds...");

try {
Thread.sleep(5000);
} catch (InterruptedException ie) {
throw new RuntimeException(ie);
}
}
}
9 changes: 9 additions & 0 deletions buildSrc/src/main/groovy/WaitForMySqlPlugin.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import org.gradle.api.Plugin;
import org.gradle.api.Project;

public class WaitForMySqlPlugin implements Plugin<Project> {
@Override
public void apply(Project project) {
project.getTasks().create("waitForMySql", WaitForMySql.class);
}
}

0 comments on commit 71bcb93

Please sign in to comment.