-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb_build.cjs
73 lines (63 loc) · 1.89 KB
/
db_build.cjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// Usage: `node db_build.cjs .env`
const fs = require('fs');
const { spawnSync } = require('child_process');
// Docker 설치 확인
const dockerInstalled = checkDockerInstalled();
if (!dockerInstalled) {
console.log('Docker is not installed. Please install Docker and try again.');
process.exit(1);
}
// .env 파일 읽기
const envFilePath = process.argv[2];
if (!envFilePath) {
console.log('Please provide path to .env file as argument.');
process.exit(1);
}
const envVariables = readEnvFile(envFilePath);
if (!envVariables) {
console.log('Failed to read .env file.');
process.exit(1);
}
// Docker 컨테이너 실행
const dbPort = envVariables.DB_PORT || '3306';
const dbPassword = envVariables.DB_PASSWORD;
const dbDatabase = envVariables.DB_DATABASE;
const dbUsername = envVariables.DB_USERNAME;
const dockerArgs = [
'run', '-d',
'--name', 'test-db-container',
'-p', `${dbPort}:3306`,
'-e', `MYSQL_ROOT_PASSWORD=${dbPassword}`,
'-e', `MYSQL_DATABASE=${dbDatabase}`,
'-e', `MYSQL_USER=${dbUsername}`,
'-e', `MYSQL_PASSWORD=${dbPassword}`,
'mariadb:10.11.4-jammy',
'--bind-address=0.0.0.0'
];
const dockerProcess = spawnSync('docker', dockerArgs, { stdio: 'inherit' });
if (dockerProcess.error) {
console.log('Failed to start Docker container.');
process.exit(1);
}
function checkDockerInstalled() {
const result = spawnSync('docker', ['--version'], { stdio: 'pipe' });
return result.status === 0;
}
function readEnvFile(filePath) {
try {
const envFileContent = fs.readFileSync(filePath, 'utf8');
const envLines = envFileContent.trim().split('\n');
const envVariables = {};
for (const line of envLines) {
if (!line.startsWith('#')) {
const [key, value] = line.split('=');
envVariables[key] = value;
}
}
return envVariables;
} catch (err) {
return null;
}
}
// generated by gpt-3.5-turbo-0613
// prompted by github.com/d556f8