-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdevelopment.sh
executable file
·167 lines (141 loc) · 4.61 KB
/
development.sh
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#!/bin/bash
backend=false
frontend=false
build=false
seed=false
data=""
# Arguments parsing
while getopts ":bfcsd:" opt; do
case ${opt} in
b )
backend=true
;;
f )
frontend=true
;;
c )
build=true
;;
s )
seed=true
;;
d )
case "$OPTARG" in
small|medium|large|real)
if [ "$OPTARG" = "real" ]; then
data="realistic"
else
data="$OPTARG"
fi
echo "$data"
;;
* )
echo "Invalid data size provided. Size must be 'small', 'medium', 'large' or 'real."
exit 1
;;
esac
;;
: )
echo "Usage: $0 [-b] [-f] [-c] [-s <size>] [-d <size>]" 1>&2
exit 1
;;
\? )
echo "Usage: $0 [-b] [-f] [-c] [-s <size>] [-d <size>]" 1>&2
exit 1
;;
esac
done
echo "Checking environment file..."
# If clean build, remove .env and db.sqlite3
if [ "$build" = true ]; then
rm .env > /dev/null 2>&1
rm -f backend/db.sqlite3 > /dev/null 2>&1
fi
# Create environment file if it doesn't exist
if ! [ -f .env ]; then
cp .dev.env .env
sed -i "s/^DJANGO_SECRET_KEY=.*/DJANGO_SECRET_KEY=totally_random_key_string/" .env
sed -i "s,^DJANGO_ROOT_DIR=.*,DJANGO_ROOT_DIR=$PWD/backend," .env
echo "Created environment file"
fi
# Generate SSL certificates if they don't exist
echo "Checking for existing SSL certificates..."
if [ ! -f "data/nginx/ssl/private.key" ] || [ ! -f "data/nginx/ssl/certificate.crt" ]; then
echo "Generating SSL certificates..."
sudo openssl req -x509 -nodes -days 365 -newkey rsa:2048 \
-keyout data/nginx/ssl/private.key \
-out data/nginx/ssl/certificate.crt \
-subj "/C=BE/ST=/L=/O=/OU=/CN=" > /dev/null
echo "SSL certificates generated."
else
echo "SSL certificates already exist, skipping generation."
fi
# Build Docker images
if [ "$build" = true ]; then
echo "Building Docker images..."
echo "This can take a while..."
docker-compose -f development.yml build --no-cache
fi
# Create new database fixtures if flag is set
if [ "$seed" = true ]; then
# Cleanup function
cleanup() {
echo "Ctrl+C detected. Cleaning up..."
docker-compose -f development.yml down
exit 1
}
# Call cleanup function on SIGINT
trap cleanup SIGINT
echo "-------------------------------------"
echo "Creating database fixtures."
echo "This can take a long time..."
echo "Go do something else in the meantime!"
echo "-------------------------------------"
docker-compose -f development.yml up -d backend > /dev/null 2>&1
docker exec backend sh -c "python manage.py migrate" > /dev/null 2>&1
sizes=("small" "medium" "large")
for size in "${sizes[@]}"; do
echo "Seeding $size database..."
docker exec backend sh -c "python manage.py seed_db $size"
echo "Dumping $size fixtures..."
docker exec backend sh -c "python manage.py dumpdata api --output=api/fixtures/$size/$size.json"
docker exec backend sh -c "python manage.py dumpdata authentication --output=authentication/fixtures/$size/$size.json"
docker exec backend sh -c "python manage.py dumpdata notifications --output=notifications/fixtures/$size/$size.json"
done
echo "Cleaning up..."
docker-compose -f development.yml down
echo "Done."
exit 0
fi
# Seed database if data size is provided
if [ "$data" != "" ]; then
echo "--------------------------"
echo "Filling the database."
echo "This can take some time..."
echo "--------------------------"
echo "Starting the required services"
docker-compose -f development.yml up -d backend redis celery
echo "Clearing, Migrating & Populating the database"
docker-compose -f development.yml run backend sh -c "python manage.py flush --no-input; python manage.py migrate; python manage.py loaddata notifications/fixtures/$data/*; python manage.py loaddata authentication/fixtures/$data/*; python manage.py loaddata api/fixtures/$data/*;"
echo "Stopping the services"
docker-compose -f development.yml down
echo "Database filled."
fi
# Start services
echo "Starting services..."
docker-compose -f development.yml up -d
echo "-------------------------------------"
echo "Following logs..."
echo "Press CTRL + C to stop all containers"
echo "-------------------------------------"
# Follow logs based on flags
if [ "$backend" = true ]; then
docker-compose -f development.yml logs --follow --tail 50 backend
elif [ "$frontend" = true ]; then
docker-compose -f development.yml logs --follow --tail 50 frontend
else
docker-compose -f development.yml logs --follow --tail 50 backend frontend
fi
echo "Cleaning up..."
docker-compose -f development.yml down
echo "Done."