forked from microservices-patterns/ftgo-application
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replacing some scripts by gradle plugins
- Loading branch information
Showing
7 changed files
with
206 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |