Skip to content

Commit

Permalink
[ALS-7051] BDC: Deploy Data Dictionary API (#21)
Browse files Browse the repository at this point in the history
* Add dynamic environment variables for datasource

Included support for setting DATASOURCE_URL and DATASOURCE_USERNAME through --env-file. This change allows the database configuration to be dynamically modified without altering the Dockerfile, improving flexibility and deployment ease.

* Add support for additional environment variables

Introduced new ARG directives in Dockerfile to handle DATASOURCE_URL, DATASOURCE_USERNAME, and SPRING_PROFILE. This addition ensures these variables can be passed at build time, improving flexibility in configuration.

* Increase log verbosity for debugging

Enabled DEBUG log level for AWS Secrets Manager SQL and Spring framework to facilitate easier debugging. This will provide more detailed logs for datasource and application behaviors.

* Temporary datasource verification class

Remove after testing

* Update JDBC URL scheme for PostgreSQL connection

* Update database URL in application-bdc.properties

Changed the database path in the datasource URL from 'auth' to 'dict' to ensure connectivity with the correct database. This adjustment is necessary for the application's dictionary service to function properly.

* Add application properties for dictionary weights service

Introduce a new configuration file for the dictionary weights service. This file includes PostgreSQL datasource settings and a filename for weights.

* Update Dockerfile and config for AWS Secrets Manager integration

Added environment variables and updated entrypoint in Dockerfile. Modified pom.xml to include AWS Secrets Manager JDBC dependency. Updated application properties to use AWS Secrets Manager for database credentials.

* Add DataSourceVerifier to check DB connection on startup

New class `DataSourceVerifier` created to ensure that the database connection is successfully established when the application context is refreshed. It logs messages indicating the success or failure of this verification.

* Update src/main/resources/application-bdc.properties

* Update database URL and add schema property in config

* Remove unused datasource schema property

The spring.datasource.schema property was not in use and has been removed to clean up the `application-bdc.properties` file. This helps maintain the configuration file by eliminating unnecessary properties.

* Update datasource URLs to include currentSchema parameter

This change adds the `currentSchema=dict` parameter to the JDBC URLs in the `application-bdc.properties` files. This adjustment ensures proper schema usage for database connections in both the main and dictionaryweights modules.

* Update dictionaryweights/src/main/resources/application-bdc.properties

* revert

* Update src/main/resources/application-bdc.properties
  • Loading branch information
Gcolon021 authored Sep 13, 2024
1 parent 7125911 commit 34017f1
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 4 deletions.
11 changes: 10 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,14 @@ COPY --from=build target/dictionary-*.jar /dictionary.jar
# Time zone
ENV TZ="US/Eastern"

ARG DATASOURCE_URL
ARG DATASOURCE_USERNAME
ARG SPRING_PROFILE

# If a --env-file is passed in, you can override these values
ENV DATASOURCE_URL=${DATASOURCE_URL}
ENV DATASOURCE_USERNAME=${DATASOURCE_USERNAME}
ENV SPRING_PROFILE=${SPRING_PROFILE}

# Default to no profile
ENTRYPOINT java $DEBUG_VARS $PROXY_VARS -Xmx8192m -jar /dictionary.jar --spring.profiles.active=${SPRING_PROFILE:-}
ENTRYPOINT java $DEBUG_VARS $PROXY_VARS -Xmx8192m -jar /dictionary.jar --spring.profiles.active=${SPRING_PROFILE:-}
11 changes: 10 additions & 1 deletion dictionaryweights/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,13 @@ FROM amazoncorretto:22-alpine
# Copy jar and access token from maven build
COPY --from=build target/dictionaryweights-*.jar /dictionaryweights.jar

ENTRYPOINT java -jar /dictionaryweights.jar
ARG DATASOURCE_URL
ARG DATASOURCE_USERNAME
ARG SPRING_PROFILE

# If a --env-file is passed in, you can override these values
ENV DATASOURCE_URL=${DATASOURCE_URL}
ENV DATASOURCE_USERNAME=${DATASOURCE_USERNAME}
ENV SPRING_PROFILE=${SPRING_PROFILE}

ENTRYPOINT java -jar /dictionaryweights.jar --spring.profiles.active=${SPRING_PROFILE:-}
5 changes: 5 additions & 0 deletions dictionaryweights/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@
<artifactId>postgresql</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.amazonaws.secretsmanager</groupId>
<artifactId>aws-secretsmanager-jdbc</artifactId>
<version>2.0.2</version>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package edu.harvard.dbmi.avillach.dictionary.datasource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Service;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;

@Service
public class DataSourceVerifier {

private static final Logger LOG = LoggerFactory.getLogger(DataSourceVerifier.class);

private final DataSource dataSource;

@Autowired
public DataSourceVerifier(DataSource dataSource) {
this.dataSource = dataSource;
}

@EventListener(ContextRefreshedEvent.class)
public void verifyDataSourceConnection() {
try (Connection connection = dataSource.getConnection()) {
if (connection != null) {
LOG.info("Datasource connection verified successfully.");
} else {
LOG.info("Failed to obtain a connection from the datasource.");
}
} catch (SQLException e) {
LOG.info("Error verifying datasource connection: {}", e.getMessage());
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
spring.application.name=dictionaryweights
spring.main.web-application-type=none

spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
spring.datasource.driver-class-name=com.amazonaws.secretsmanager.sql.AWSSecretsManagerPostgreSQLDriver
spring.datasource.url=jdbc-secretsmanager:postgresql://${DATASOURCE_URL}/picsure?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&autoReconnectForPools=true&currentSchema=dict
spring.datasource.username=${DATASOURCE_USERNAME}

weights.filename=/weights.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package edu.harvard.dbmi.avillach.dictionary.datasource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Service;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;

@Service
public class DataSourceVerifier {

private static final Logger LOG = LoggerFactory.getLogger(DataSourceVerifier.class);

private final DataSource dataSource;

@Autowired
public DataSourceVerifier(DataSource dataSource) {
this.dataSource = dataSource;
}

@EventListener(ContextRefreshedEvent.class)
public void verifyDataSourceConnection() {
try (Connection connection = dataSource.getConnection()) {
if (connection != null) {
LOG.info("Datasource connection verified successfully.");
} else {
LOG.info("Failed to obtain a connection from the datasource.");
}
} catch (SQLException e) {
LOG.info("Error verifying datasource connection: {}", e.getMessage());
}
}

}
4 changes: 2 additions & 2 deletions src/main/resources/application-bdc.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
spring.application.name=dictionary
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
spring.datasource.driver-class-name=com.amazonaws.secretsmanager.sql.AWSSecretsManagerPostgreSQLDriver
spring.datasource.url=jdbc-secretsmanager:postgres://${DATASOURCE_URL}/auth?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&autoReconnectForPools=true
spring.datasource.url=jdbc-secretsmanager:postgresql://${DATASOURCE_URL}/picsure?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&autoReconnectForPools=true&currentSchema=dict
spring.datasource.username=${DATASOURCE_USERNAME}
server.port=80
server.port=80

0 comments on commit 34017f1

Please sign in to comment.