Skip to content

Commit 7104909

Browse files
Docker compose import (#3191)
* fixed docker-compose.dev.yaml * added new update * Update package-lock.json * Update docker-compose.dev.yaml according to code rabbit * Update docker-compose.dev.yaml * code is working
1 parent 4f58aa5 commit 7104909

File tree

6 files changed

+93
-53
lines changed

6 files changed

+93
-53
lines changed

.husky/post-merge

100755100644
File mode changed.

.husky/pre-commit

100755100644
File mode changed.

Dockerfile.dev

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Stage 1: Build stage
2-
FROM node:20.10.0
2+
FROM node:23.7.0
33

44
WORKDIR /usr/src/app
55

docker-compose.dev.yaml

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,22 @@
11
version: "3.8"
22
services:
33
mongo:
4-
# No light weight image is available for mongo
5-
image: mongo:latest
4+
# No light weight image is available for mongo
5+
image: mongo
66
container_name: mongo
77
restart: unless-stopped
88
healthcheck:
9-
test: mongosh --eval "try { rs.status().ok } catch (e) { 0 }"
10-
interval: 30s
9+
test: ["CMD", "mongosh", "--eval", "db.runCommand({ ping: 1 }).ok"]
10+
interval: 10s
1111
timeout: 5s
12-
retries: 30
12+
retries: 10
1313
deploy:
1414
resources:
1515
limits:
1616
memory: 1G
17-
cpus: '1.0'
17+
cpus: "1.0"
1818
ports:
19-
- "127.0.0.1:27017:27017"
20-
19+
- "27017:27017"
2120
volumes:
2221
- mongo_data:/data/db
2322
- ./scripts/docker/init-mongo.sh:/init-mongo.sh
@@ -33,27 +32,27 @@ services:
3332
resources:
3433
limits:
3534
memory: 512M
36-
cpus: '0.5'
35+
cpus: "0.5"
3736
volumes:
3837
- redis-data:/data/redis
3938
networks:
4039
- talawa-network
4140

4241
minio:
43-
# No light weight image is available
42+
# No light weight image is available
4443
image: minio/minio
4544
ports:
4645
- "9000:9000"
4746
- "9001:9001"
48-
environment:
47+
environment:
4948
- MINIO_ROOT_USER=${MINIO_ROOT_USER}
5049
- MINIO_ROOT_PASSWORD=${MINIO_ROOT_PASSWORD}
5150
command: server /data --console-address ":9001"
5251
deploy:
5352
resources:
5453
limits:
5554
memory: 512M
56-
cpus: '0.5'
55+
cpus: "0.5"
5756
volumes:
5857
- minio-data:/data
5958
networks:
@@ -68,6 +67,7 @@ services:
6867
- "4000:4000"
6968
volumes:
7069
- /usr/src/app/node_modules
70+
- ./scripts/docker/init-data-import.sh:/init-data-import.sh
7171
depends_on:
7272
mongo:
7373
condition: service_healthy
@@ -76,14 +76,16 @@ services:
7676
minio:
7777
condition: service_started
7878
environment:
79-
- MONGO_DB_URL=mongodb://mongo:27017/talawa-api?replicaSet=rs0&directConnection=true
79+
- MONGO_DB_URL=mongodb://mongo:27017/talawa-dev?replicaSet=rs0
80+
- NODE_ENV=development
8081
- REDIS_HOST=redis-stack-server
8182
- REDIS_PORT=6379
8283
deploy:
8384
resources:
8485
limits:
8586
memory: 1G
86-
cpus: '1.0'
87+
cpus: "1.0"
88+
entrypoint: ["/bin/bash", "/init-data-import.sh"]
8789
networks:
8890
- talawa-network
8991

@@ -99,12 +101,19 @@ services:
99101
- $PWD/site:/srv
100102
- caddy_data:/data
101103
- caddy_config:/config
104+
healthcheck:
105+
test: ["CMD", "curl", "-f", "http://localhost:4000/graphql"]
106+
interval: 10s
107+
timeout: 5s
108+
retries: 5
102109
deploy:
103110
resources:
104111
limits:
105112
memory: 256M
106-
cpus: '0.25'
107-
113+
cpus: "0.25"
114+
networks:
115+
- talawa-network
116+
108117
volumes:
109118
redis-data:
110119
minio-data:
@@ -113,4 +122,4 @@ volumes:
113122
caddy_config:
114123

115124
networks:
116-
talawa-network:
125+
talawa-network:

scripts/cloud-api-demo/reset_database.py

100644100755
Lines changed: 53 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,63 +2,81 @@
22
import argparse
33
import subprocess
44
import os
5-
5+
import shutil
66

77
def execute_command(cmd):
8-
"""Execute a shell command.
8+
"""Execute a shell command, capturing and printing output.
99
1010
Args:
1111
cmd: A list of strings representing the command and its arguments.
1212
1313
Raises:
14+
FileNotFoundError: If npm executable is not found
1415
SystemExit: If the command returns a non-zero exit code.
1516
"""
16-
process = subprocess.run(cmd, capture_output=True, text=True)
1717

18-
if process.returncode != 0:
19-
print(f"Error occurred: {process.stderr}")
20-
raise SystemExit(1)
21-
else:
22-
print(process.stdout)
18+
npm_path = shutil.which("npm") # Find the npm executable
19+
if npm_path is None:
20+
raise FileNotFoundError("npm executable not found.")
2321

22+
if cmd[0] == "npm":
23+
cmd[0] = npm_path
2424

25-
def main():
26-
"""Main function to parse command-line arguments and execute commands.
25+
print(f"Executing command: {cmd}")
26+
print(f"Current working directory: {os.getcwd()}")
2727

28-
Args:
29-
None
28+
process = subprocess.run(cmd, capture_output=True, text=True, env=os.environ, check=True) # check=True raises exception for non-zero return code
3029

31-
Raises:
32-
SystemExit: If any of the executed commands returns a non-zero exit code.
33-
"""
30+
print(process.stdout) # Print stdout regardless of success/failure
31+
if process.returncode != 0:
32+
print(f"Error occurred: {process.stderr}")
33+
raise SystemExit(process.returncode) # Raise SystemExit with the return code
34+
35+
def main():
36+
"""Main function to parse command-line arguments and execute commands."""
3437
parser = argparse.ArgumentParser(description="Database reset script.")
35-
parser.add_argument(
36-
"--mongo-container", help="MongoDB container name", required=True
37-
)
38+
parser.add_argument("--mongo-container", help="MongoDB container name", required=True)
3839
parser.add_argument("--mongo-db", help="MongoDB database name", required=True)
3940
parser.add_argument("--repo-dir", help="Repository directory", required=True)
4041

4142
args = parser.parse_args()
4243

43-
# Use docker exec command to drop the specified MongoDB database
44-
execute_command(
45-
[
46-
"docker",
47-
"exec",
48-
"-it",
49-
args.mongo_container,
50-
"mongosh",
51-
"--eval",
52-
f"db.getSiblingDB('{args.mongo_db}').dropDatabase()",
53-
]
54-
)
44+
try: # added try-except block
45+
print(f"Dropping database: {args.mongo_db} in container: {args.mongo_container}")
46+
execute_command(
47+
[
48+
"docker",
49+
"exec",
50+
"-it", # Consider removing -it if not needed for interaction
51+
args.mongo_container,
52+
"mongosh",
53+
"--eval",
54+
f"db.getSiblingDB('{args.mongo_db}').dropDatabase()",
55+
]
56+
)
57+
print(f"Changing directory to: {args.repo_dir}")
58+
os.chdir(args.repo_dir)
59+
60+
print("Stopping existing containers...")
61+
execute_command(["docker","compose", "-f", "docker-compose.dev.yaml", "down"])
62+
63+
print("Starting containers using docker-compose...")
64+
execute_command(["docker","compose", "-f", "docker-compose.dev.yaml", "up","--build","-d"])
5565

56-
# Changing to repo dir
57-
os.chdir(args.repo_dir)
66+
print("Database reset and services restarted successfully.")
5867

59-
# Run a command to import sample data using npm
60-
execute_command(["npm", "run", "import:sample-data"])
6168

69+
except FileNotFoundError as e:
70+
print(f"Error: {e}")
71+
SystemExit(1)
72+
except subprocess.CalledProcessError as e: # catch subprocess errors
73+
print(f"Command execution failed with return code {e.returncode}")
74+
print(f"Stdout: {e.stdout}")
75+
print(f"Stderr: {e.stderr}")
76+
SystemExit(e.returncode)
77+
except SystemExit as e:
78+
print(f"A critical error occurred. Exiting with code {e}")
79+
exit(e) # Exit with the captured code
6280

6381
if __name__ == "__main__":
64-
main()
82+
main()

scripts/docker/init-data-import.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/bin/bash
2+
set -e
3+
4+
# Import sample data
5+
echo "Importing sample data..."
6+
npm run import:sample-data || {
7+
echo "Sample data import failed."
8+
exit 1
9+
}
10+
echo "Sample data import successful."
11+
12+
# Start the API server
13+
npm run dev

0 commit comments

Comments
 (0)