Skip to content

Commit

Permalink
Finish configuration for datasource
Browse files Browse the repository at this point in the history
We use druid as datasource, and finish the configuration for it.

This should solve #26.
  • Loading branch information
Kaiser-Yang committed Aug 20, 2024
1 parent 59e4ec3 commit a486dfa
Show file tree
Hide file tree
Showing 11 changed files with 122 additions and 7 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,6 @@ replay_pid*

# root directory for the file explorer
.root/

# this file should be generated by the helper script
application.properties
2 changes: 2 additions & 0 deletions README-zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,6 @@
| `postgresqlDatabaseName` | `string` | `"gcs"` | `Postgres` 数据库名称。 |
| `postgresqlHost` | `string` | `"localhost"` | `Postgres` 主机地址。 |
| `postgresqlPort` | `int` | `5432` | `Postgres` 端口。 |
| `druidLoginUsername` | `string` | `"druid"` | `Druid` 登录用户名。 |
| `druidLoginPassword` | `string` | `"druid"` | `Druid` 登录密码。 |

4 changes: 3 additions & 1 deletion config_default.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,7 @@
"postgresqlUserPassword": "postgres",
"postgresqlDatabaseName": "gcs",
"postgresqlHost": "localhost",
"postgresqlPort": 5432
"postgresqlPort": 5432,
"druidLoginUsername": "druid",
"druidLoginPassword": "druid"
}
14 changes: 14 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,20 @@
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
<version>2.6.0</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-3-starter</artifactId>
<version>1.2.23</version>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
</dependencies>

<build>
Expand Down
46 changes: 40 additions & 6 deletions script/deploy_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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')
Expand Down Expand Up @@ -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',
Expand All @@ -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):
Expand Down Expand Up @@ -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"
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/edu/cmipt/gcs/config/DruidConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package edu.cmipt.gcs.config;

import org.springframework.context.annotation.Configuration;

@Configuration
public class DruidConfig {}
5 changes: 5 additions & 0 deletions src/main/resources/application-dev.yml
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
debug: true
spring:
datasource:
druid:
test-on-borrow: true
test-on-return: true
5 changes: 5 additions & 0 deletions src/main/resources/application-prod.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
spring:
datasource:
druid:
test-on-borrow: false
test-on-return: false
6 changes: 6 additions & 0 deletions src/main/resources/application-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
debug: true
spring:
datasource:
druid:
test-on-borrow: true
test-on-return: true
Empty file.
38 changes: 38 additions & 0 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit a486dfa

Please sign in to comment.