From a486dfab0823edbc0e93858764bdefa3aa11667d Mon Sep 17 00:00:00 2001
From: Kaiser-Yang <624626089@qq.com>
Date: Tue, 20 Aug 2024 14:46:36 +0800
Subject: [PATCH] Finish configuration for datasource
We use druid as datasource, and finish the configuration for it.
This should solve #26.
---
.gitignore | 3 ++
README-zh.md | 2 +
config_default.json | 4 +-
pom.xml | 14 ++++++
script/deploy_helper.py | 46 ++++++++++++++++---
.../edu/cmipt/gcs/config/DruidConfig.java | 6 +++
src/main/resources/application-dev.yml | 5 ++
src/main/resources/application-prod.yml | 5 ++
src/main/resources/application-test.yml | 6 +++
src/main/resources/application.properties | 0
src/main/resources/application.yml | 38 +++++++++++++++
11 files changed, 122 insertions(+), 7 deletions(-)
create mode 100644 src/main/java/edu/cmipt/gcs/config/DruidConfig.java
delete mode 100644 src/main/resources/application.properties
diff --git a/.gitignore b/.gitignore
index a99912a..3ef82de 100644
--- a/.gitignore
+++ b/.gitignore
@@ -62,3 +62,6 @@ replay_pid*
# root directory for the file explorer
.root/
+
+# this file should be generated by the helper script
+application.properties
diff --git a/README-zh.md b/README-zh.md
index e885c13..0867a35 100644
--- a/README-zh.md
+++ b/README-zh.md
@@ -41,4 +41,6 @@
| `postgresqlDatabaseName` | `string` | `"gcs"` | `Postgres` 数据库名称。 |
| `postgresqlHost` | `string` | `"localhost"` | `Postgres` 主机地址。 |
| `postgresqlPort` | `int` | `5432` | `Postgres` 端口。 |
+| `druidLoginUsername` | `string` | `"druid"` | `Druid` 登录用户名。 |
+| `druidLoginPassword` | `string` | `"druid"` | `Druid` 登录密码。 |
diff --git a/config_default.json b/config_default.json
index 73e1469..b41eccd 100644
--- a/config_default.json
+++ b/config_default.json
@@ -45,5 +45,7 @@
"postgresqlUserPassword": "postgres",
"postgresqlDatabaseName": "gcs",
"postgresqlHost": "localhost",
- "postgresqlPort": 5432
+ "postgresqlPort": 5432,
+ "druidLoginUsername": "druid",
+ "druidLoginPassword": "druid"
}
diff --git a/pom.xml b/pom.xml
index e51396e..a45a587 100644
--- a/pom.xml
+++ b/pom.xml
@@ -94,6 +94,20 @@
springdoc-openapi-starter-webmvc-ui
2.6.0
+
+ com.alibaba
+ druid-spring-boot-3-starter
+ 1.2.23
+
+
+ org.postgresql
+ postgresql
+ runtime
+
+
+ org.springframework.boot
+ spring-boot-starter-jdbc
+
diff --git a/script/deploy_helper.py b/script/deploy_helper.py
index d72e3bb..6676a89 100644
--- a/script/deploy_helper.py
+++ b/script/deploy_helper.py
@@ -185,7 +185,7 @@ def deploy_with_sys_v_init(config):
command_checker(res, f"Failed to start {config.serviceName} with boot")
-def active_profile(config):
+def activate_profile(config):
profile_format = f"spring.profiles.active={parse_iterable_into_str(config.profiles, sep=',')}"
log_debug(f"Profile format: {profile_format}")
try:
@@ -203,6 +203,34 @@ def active_profile(config):
command_checker(1, f"Error: {e}")
+def config_datasource(config):
+ datasource_map_config = {
+ "username": config.postgresqlUserName,
+ "password": config.postgresqlUserPassword,
+ "url": f"jdbc:postgresql://{config.postgresqlHost}:{config.postgresqlPort}/{config.postgresqlDatabaseName}",
+ "stat-view-servlet.login-username": config.druidLoginUsername,
+ "stat-view-servlet.login-password": config.druidLoginPassword,
+ }
+ datasource_format = "spring.datasource.druid.{0}={1}"
+ log_debug(f"Datasource format: {datasource_format}")
+ try:
+ lines = None
+ if os.path.exists(application_config_file_path):
+ with open(application_config_file_path, 'r') as f:
+ lines = f.readlines()
+ with open(application_config_file_path, 'w') as f:
+ if lines:
+ for line in lines:
+ if not line.startswith('spring.datasource.druid'):
+ f.write(line)
+ for key, value in datasource_map_config.items():
+ f.write(datasource_format.format(key, value))
+ f.write('\n')
+ log_debug(f"Datasource config: {datasource_format.format(key, value)}")
+ except Exception as e:
+ command_checker(1, f"Error: {e}")
+
+
def init_database(config):
create_or_update_user("postgres", config.postgresUserPassword)
res = os.system(f'{sudo_cmd} service postgresql start')
@@ -277,6 +305,9 @@ def init_database(config):
out, err = process.communicate()
command_checker(process.returncode, f"Failed to create the database: {err}")
log_info(f"Database {config.postgresqlDatabaseName} has been created")
+ run_shell_script = True
+ else:
+ run_shell_script = False
# grant the user
log_info(f"Granting the user in database: {config.postgresqlUserName}")
process = subprocess.Popen(['su', '-c',
@@ -291,10 +322,13 @@ def init_database(config):
out, err = process.communicate()
command_checker(process.returncode, f"Failed to grant the user in database: {err}")
log_info(f"User {config.postgresqlUserName} has been granted in database")
- res = os.system(f'bash database/database_deploy.sh {config.postgresqlUserName} '
- f'{config.postgresqlDatabaseName} {config.postgresqlHost} '
- f'{config.postgresqlPort} {config.postgresqlUserPassword}')
- command_checker(res, f"Failed to deploy the database")
+ # Do not run the shell script if the database had been created before
+ if run_shell_script:
+ res = os.system(f'bash database/database_deploy.sh {config.postgresqlUserName} '
+ f'{config.postgresqlDatabaseName} {config.postgresqlHost} '
+ f'{config.postgresqlPort} {config.postgresqlUserPassword}')
+ command_checker(res, f"Failed to deploy the database")
+ config_datasource(config)
def create_or_update_user(username, password):
@@ -326,7 +360,7 @@ def deploy_on_ubuntu(config):
essential_packages.remove('systemd')
apt_install_package(parse_iterable_into_str(essential_packages))
init_database(config)
- active_profile(config)
+ activate_profile(config)
skip_test = ""
if config.skipTest:
skip_test = "-Dmaven.test.skip=true"
diff --git a/src/main/java/edu/cmipt/gcs/config/DruidConfig.java b/src/main/java/edu/cmipt/gcs/config/DruidConfig.java
new file mode 100644
index 0000000..2fbde4e
--- /dev/null
+++ b/src/main/java/edu/cmipt/gcs/config/DruidConfig.java
@@ -0,0 +1,6 @@
+package edu.cmipt.gcs.config;
+
+import org.springframework.context.annotation.Configuration;
+
+@Configuration
+public class DruidConfig {}
diff --git a/src/main/resources/application-dev.yml b/src/main/resources/application-dev.yml
index 98b2266..f8dd622 100644
--- a/src/main/resources/application-dev.yml
+++ b/src/main/resources/application-dev.yml
@@ -1 +1,6 @@
debug: true
+spring:
+ datasource:
+ druid:
+ test-on-borrow: true
+ test-on-return: true
diff --git a/src/main/resources/application-prod.yml b/src/main/resources/application-prod.yml
index e69de29..8a8a720 100644
--- a/src/main/resources/application-prod.yml
+++ b/src/main/resources/application-prod.yml
@@ -0,0 +1,5 @@
+spring:
+ datasource:
+ druid:
+ test-on-borrow: false
+ test-on-return: false
diff --git a/src/main/resources/application-test.yml b/src/main/resources/application-test.yml
index e69de29..f8dd622 100644
--- a/src/main/resources/application-test.yml
+++ b/src/main/resources/application-test.yml
@@ -0,0 +1,6 @@
+debug: true
+spring:
+ datasource:
+ druid:
+ test-on-borrow: true
+ test-on-return: true
diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties
deleted file mode 100644
index e69de29..0000000
diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml
index 3c00c76..78226d1 100644
--- a/src/main/resources/application.yml
+++ b/src/main/resources/application.yml
@@ -1,6 +1,44 @@
spring:
application:
name: gcs
+ datasource:
+ druid:
+ type: com.alibaba.druid.pool.DruidDataSource
+ driver-class-name: org.postgresql.Driver
+ initial-size: 5
+ min-idle: 5
+ max-active: 20
+ max-wait: 6000 # unit: ms
+ time-between-eviction-runs-millis: 60000
+ min-evication-idle-time-millis: 600000 # min alive time of a connection
+ max-evication-idle-time-millis: 1200000 # max alive time of a connetcion
+ validation-query: SELECT 1
+ test-while-idle: true
+ async-init: true
+ keep-alive: true
+ filters:
+ stat:
+ enable: true
+ log-slow-sql: true
+ slow-sql-millis: 1000
+ wall:
+ enable: true
+ log-violation: true
+ throw-exception: false
+ config:
+ drop-table-allow: false
+ delete-allow: false
+ web-stat-filter:
+ enabled: true
+ url-pattern: /*
+ exclusions: "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*"
+ session-stat-enable: true
+ session-stat-max-count: 1000
+ stat-view-servlet:
+ enabled: true
+ url-pattern: /druid/*
+ reset-enable: false
+ allow: # empty means allow all
logging:
include-application-name: false