-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrun
executable file
·318 lines (303 loc) · 11.4 KB
/
run
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
#!/bin/bash
# Check if command is provided
if [ -z "$1" ]
then
echo "No command provided. Usage: ./run command"
exit 1
fi
source .env
# Set command
command=$1
PWD=$(pwd)
# Check if docker-compose or docker compose is available
if command -v docker-compose &> /dev/null
then
COMPOSE_COMMAND="docker-compose"
elif command -v docker &> /dev/null && docker compose version &> /dev/null
then
COMPOSE_COMMAND="docker compose"
else
echo "Neither docker-compose nor docker compose is available on this system."
exit 1
fi
HEADLESS=${HEADLESS}
# Set Docker container ID
container_id=webcentral
currentDirInContainer=$(docker exec $container_id pwd)
print_usage() {
printf "run script: -v <command>"
}
# Parse options
while getopts 'abf:v' flag; do
case "${flag}" in
v) HEADLESS=0 ;;
*) print_usage
exit 1 ;;
esac
done
shift $((OPTIND -1))
# Execute different actions based on the command
case $command in
"build")
if [ -z "$2" ]
then
echo "Please Specify as a second argument, which docker-environment you want to build. Possible options are: dev, prod"
exit 1
else
if [ "$2" = "dev" ]; then
echo "Building development environment..."
$COMPOSE_COMMAND -f docker-compose.yml -f docker-compose.dev.yml build
exit $?
fi
if [ "$2" = "prod" ]; then
echo "Building production environment..."
$COMPOSE_COMMAND -f docker-compose.yml -f docker-compose.prod.yml build
exit $?
fi
fi
;;
"copy_files_to_server")
# Check if a directory was provided
echo "Please make sure that you have name-resolving for server ip-address activated."
echo "The name vserver should resolve to the ip-address of the server."
echo "In linux, that can be done by changing the /etc/hosts file."
if [ -z "$1" ]
then
echo "Please provide a directory, which should be copied to the server. Usage: ./run copy_files_to_server path/to/directory"
exit 1
fi
# Set the local directory
local_dir=$1
# Set the remote directory
remote_dir="~/src/01_application/webcentral_app/media"
# Set the server username and hostname
server="djangoadmin@vserver"
# Copy the files
scp -r $local_dir $server:$remote_dir
# Check if the copy was successful
if [ $? -eq 0 ]
then
echo "Files copied successfully."
else
echo "An error occurred while copying the files."
fi
;;
"copy_media_files_into_prod")
echo "This command can be excuted on the server to copy all media-files into the production environment-container."
docker cp ~/src/media/. webcentral:/home/$WEBCENTRAL_UNPRIVILEGED_USER/webcentral/media/
;;
"down")
$COMPOSE_COMMAND -f docker-compose.yml -f docker-compose.dev.yml down
;;
"data_import")
if [ -z "$2" ]
then
echo "No path to an input-filename is provided. Usage: ./run data_import path/to/input-filename"
exit 1
fi
if [ -z "$3" ]
then
# Get current working directory inside the webcentral container
currentDirInContainer=$(docker exec $container_id pwd)
docker exec -w $currentDirInContainer/src/ -it $container_id python manage.py data_import ../../$2
fi
;;
"delete_db")
$COMPOSE_COMMAND -f docker-compose.yml -f docker-compose.dev.yml down --volumes
;;
"dump_db")
if [ -z "$2" ]
then
echo "No path to an output-filename is provided. Using filename postgres/dump.sql"
docker exec -i database pg_dump -U ${POSTGRES_USER} ${POSTGRES_DB} > postgres/dump.sql
else
echo "Dumping database to file $2"
docker exec -i database pg_dump -U ${POSTGRES_USER} ${POSTGRES_DB} > $2
fi
;;
"migrate")
echo "Running Django migrations..."
docker exec -it $container_id python src/manage.py migrate
;;
"makemigrations")
echo "Creating Django migrations..."
docker exec -it $container_id python src/manage.py makemigrations
;;
"shell")
echo "Opening Django Shell..."
docker exec -it $container_id python src/manage.py shell
;;
"restore_db")
source .env
if [ -z "$2" ]
then
echo "No path to an input-filename is provided. Using file $DATABASE_PLAIN_SQL_FILE"
cat postgres/${DATABASE_PLAIN_SQL_FILE} | docker exec -i database psql -U ${POSTGRES_USER} -d ${POSTGRES_DB}
else
echo "Restoring database with file $2"
cat $2 | docker exec -i database psql -U ${POSTGRES_USER} -d ${POSTGRES_DB}
fi
;;
"clear_sessions")
docker exec -it $container_id python src/manage.py clear_sessions
;;
"test")
if [ -z "$2" ]
then
echo "test-command usage: ./run test <type> <opt1> <opt2>\ntype: Selenium"
echo "opt1: Optional parameter specifing which Testclass to execute"
echo "opt2: Optional parameter specifing the testmethod to execute"
exit 0
fi
if [ "$2" = "Database" ]; then
# Get current working directory inside the webcentral container
docker exec -w $currentDirInContainer/src/ -it $container_id python manage.py test tests/ testDatabaseFilling.checkDifferencesInDatabase
exit 1
fi
if [ "$2" = "Selenium" ]; then
set -a
. ./.env
set +a
cd webcentral/test/
if [ -d "testing_venv" ]; then
. testing_venv/bin/activate
else
python -m venv testing_venv
. testing_venv/bin/activate
pip install -r requirements_testing.txt
fi
cd 06_system_test/
if [ -n "$3" ]; then
if [ -n "$4" ]; then
siteUnderTest=http://127.0.0.1:8000 HEADLESS=$HEADLESS python testrunner.py --test_file $3 --test_method $4
else
siteUnderTest=http://127.0.0.1:8000 HEADLESS=$HEADLESS python testrunner.py --test_file $3
fi
else
siteUnderTest=http://127.0.0.1:8000 HEADLESS=$HEADLESS python testrunner.py
fi
exit $?
deactivate
cd ../../../
fi
if [ "$2" = "System" ]; then
python3 02_work_doc/10_test/02_docker/testDockerCompose.py
fi
;;
"up")
if [ -z "$2" ]
then
echo "Please Specify as a second argument, which docker-environment you want to start. Possible options are: dev, prod"
exit 1
else
if [ "$2" = "dev" ]; then
echo "Starting development environment..."
$COMPOSE_COMMAND -f docker-compose.yml -f docker-compose.dev.yml up
exit 1
fi
if [ "$2" = "prod" ]; then
echo "Startin production environment..."
$COMPOSE_COMMAND -f docker-compose.yml -f docker-compose.prod.yml up
exit 1
fi
fi
;;
"build_initial")
if [ "$2" == "dev" ];
then
npx webpack --mode=development
$COMPOSE_COMMAND -f docker-compose.yml -f docker-compose.dev.yml build
fi
if [ "$2" == "prod" ];
then
echo "Creating css from scss-files:"
npx webpack --mode=production
$COMPOSE_COMMAND -f docker-compose.yml -f docker-compose.prod.yml build
fi
;;
"up_initial")
if [ -z "$2" ]
then
echo "Please Specify as a second argument, which docker-environment you want to start. Possible options are: dev, prod"
exit 1
else
if [ "$2" = "dev" ]; then
if [ -z "$3" ]
then
echo "Please provide a filename of a .sql database-dunp inside the postgres/ folder."
exit 1
else
$COMPOSE_COMMAND -f docker-compose.yml -f docker-compose.dev.yml up -d
sleep 1
cat $3 | docker exec -i database psql -U ${POSTGRES_USER} -d ${POSTGRES_DB}
$COMPOSE_COMMAND -f docker-compose.yml -f docker-compose.dev.yml down
$COMPOSE_COMMAND -f docker-compose.yml -f docker-compose.dev.yml up -d
echo "Exit code: $?"
exit $?
fi
fi
if [ "$2" = "prod" ]; then
if [ -z "$3" ]
then
echo "Please provide a filename of a .sql database-dunp inside the postgres/ folder."
exit 1
else
echo "Starting production environment..."
$COMPOSE_COMMAND -f docker-compose.yml -f docker-compose.prod.yml up -d
sleep 2
cat $3 | docker exec -i database psql -U ${POSTGRES_USER} -d ${POSTGRES_DB}
$COMPOSE_COMMAND -f docker-compose.yml -f docker-compose.prod.yml down
$COMPOSE_COMMAND -f docker-compose.yml -f docker-compose.prod.yml up -d
docker cp $PWD/01_application/webcentral_app/media/. webcentral:/home/$WEBCENTRAL_UNPRIVILEGED_USER/webcentral/media/
echo "Exit code: $?"
exit $?
fi
fi
fi
;;
"up_debug")
$COMPOSE_COMMAND -f docker-compose.yml -f docker-compose.dev.yml run --service-ports webcentral
;;
"webcentral_shell")
echo "Opening wecentral-container shell"
docker exec -it $container_id bash
;;
"makemessages")
docker exec -w $currentDirInContainer/src/ -it $container_id python manage.py makemessages -l en
exit $?
;;
"compilemessages")
docker exec -w $currentDirInContainer/src/ -it $container_id python manage.py compilemessages
exit $?
;;
"update_translation_fields")
docker exec -w $currentDirInContainer/src/ -it $container_id python manage.py update_translation_fields
exit $?
;;
"sync_translation_fields")
docker exec -w $currentDirInContainer/src/ -it $container_id python manage.py sync_translation_fields
exit $?
;;
"pre_enargus")
if [ $# -ne 3 ]; then
echo "pre_enargus: Execute the preprocessing step to convert xml-enargus data to csv-data\n./run pre_enargus <source-xml-file> <target-csv-file>"
exit 0
else
docker exec -it $container_id python doc/01_data/01_pre_pro/pre_enargus.py $2 $3
exit $?
fi
;;
"pre_modul")
if [ $# -ne 3 ]; then
echo "pre_modul: Execute the preprocessing step to convert xlsx-modul data to csv-data\n./run pre_modul <source-xlsx-file> <target-csv-file>"
exit 0
else
docker exec -it $container_id python doc/01_data/01_pre_pro/pre_modul.py $2 $3
exit $?
fi
;;
*)
echo "Unknown command: $command"
exit 1
;;
esac