forked from tracim/tracim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_dev_backend.sh
executable file
·172 lines (154 loc) · 4.86 KB
/
run_dev_backend.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
168
169
170
171
172
#!/bin/bash
set -e
show_help () {
cat <<EOF
Usage: $0 [--database=(sqlite|postgresql|mysql|mariadb)] dev|(cypress (open|run))
$0
Run the Tracim backend for development purposes
$0 cypress open
Run the Tracim backend for functional tests then launch Cypress in a GUI
$0 cypress run
Run the Tracim backend for functional tests then launch the full Cypress test suite in the terminal
EOF
}
database_type="sqlite"
while :; do
case "$1" in
-h|--help)
show_help
exit
;;
-d=?*|--database=?*)
# Delete everything up to "=" and assign the remainder.
database_type=${1#*=}
;;
-?*)
echo "Unknown option $1"
exit 1
;;
*)
break
;;
esac
shift
done
script_dir=$(realpath "$(dirname "$0")")
set +e
backend_pid=$(pgrep pserve)
set -e
cypress_arg="$2"
mode="$1"
export DATABASE_NAME="tracim"
database_dir="$script_dir/backend"
database_service=
if [ -z "$mode" ]; then
mode="dev"
fi
if [ "$mode" != "cypress" ] && [ "$mode" != "dev" ]; then
echo "Unknown mode '$mode' Possible modes are 'cypress' or 'dev'"
exit 1
fi
if [ -n "$backend_pid" ]; then
echo "Error: A Tracim development server seems to be running with PID $backend_pid, please exit it before running this command"
exit 1
fi
if [ "$mode" = "cypress" ]; then
if ! [ -s "$script_dir/functionnal_tests/cypress.json" ]; then
cat <<EOF
It seems you haven't configured Cypress yet. The following command needs to be run before continuing:
cd "$script_dir"; ./setup_functionnal_tests.sh
EOF
read -p "Do you want me to run it for you? [Y/n] " -r answer
if [ "$answer" = "y" ] || [ "$answer" = "Y" ] || [ "$answer" = "" ]; then
(cd "$script_dir"; if ! ./setup_functionnal_tests.sh; then
echo "Setting up Cypress failed, exiting."
exit 1
fi)
else
exit 1
fi
fi
if [ -z "$cypress_arg" ]; then
echo "cypress mode needs an argument, ('open' or 'run')"
exit 1
fi
if [ "$database_type" != "sqlite" ]; then
echo "cypress mode only supports sqlite currently"
exit 1
fi
# Run tracim with a specified sqlite database as cypress resets db using a file copy
export DATABASE_NAME="tracim_cypress"
database_dir="/tmp"
fi
case "$database_type" in
sqlite)
export TRACIM_SQLALCHEMY__URL="$database_type:///$database_dir/${DATABASE_NAME}.sqlite"
database_service=
sleep=
;;
postgresql)
export TRACIM_SQLALCHEMY__URL="$database_type://user:secret@localhost:5432/${DATABASE_NAME}?client_encoding=utf8"
database_service=$database_type
;;
mariadb)
export TRACIM_SQLALCHEMY__URL="mysql+pymysql://user:secret@localhost:3307/${DATABASE_NAME}"
database_service=$database_type
;;
mysql)
export TRACIM_SQLALCHEMY__URL="mysql+pymysql://user:secret@localhost:3306/${DATABASE_NAME}"
database_service=$database_type
;;
*)
echo "Unknown database type $database_type"
exit 1
;;
esac
echo "Database type: '$database_type', service: '$database_service'"
teardown () {
if [ -n "$backend_pid" ]; then kill "$backend_pid"; fi
pushd "$script_dir/backend"
docker compose stop pushpin
}
run_docker_services () {
if [ -z "$database_service" ]; then
docker compose up -d pushpin
else
docker compose up -d pushpin "$database_service"
sleep 2
fi
}
pushd "$script_dir/backend"
# Use comparison with "-z" as we want to check empty strings
if [ -z "$VIRTUAL_ENV" ] && [ -z "$NO_VIRTUAL_ENV" ]; then
. "$script_dir/backend/env/bin/activate"
fi
trap teardown HUP INT TERM
if [ "$mode" = "cypress" ]; then
export TRACIM_TRANSLATION_SERVICE__ENABLED=True
export TRACIM_TRANSLATION_SERVICE__PROVIDER=test
export TRACIM_USER__SELF_REGISTRATION__ENABLED=True
run_docker_services "$sleep"
tracimcli db delete --force || true
tracimcli db init || true
cp /tmp/${DATABASE_NAME}.sqlite /tmp/${DATABASE_NAME}.sqlite.tmp
pserve development.ini > /tmp/${DATABASE_NAME}.log 2>&1 &
backend_pid=$!
popd
pushd "$script_dir/functionnal_tests"
yarn run "cypress-$cypress_arg"
teardown
else
# disable CSP header for development (tracim dev builds use eval()).
export TRACIM_CONTENT_SECURITY_POLICY__ENABLED=False
# NOTE: by default the mysql/mariadb do save their database in a tmpfs.
# disabling this for manual tests/dev in order to retain the database between launches
export TMPFS_DIR=/tmp
run_docker_services "$sleep"
if tracimcli db init; then
echo "Tagging database schema"
else
echo "Upgrading database schema"
alembic -c development.ini upgrade head
fi
pserve development.ini
fi