-
Notifications
You must be signed in to change notification settings - Fork 2
/
ecis
executable file
·334 lines (276 loc) · 10.8 KB
/
ecis
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
#!/bin/bash
# --- BASH CONSTANTS ---------
bold=$(tput bold)
_bold=$(tput sgr0)
red=$(tput setaf 1)
green=$(tput setaf 2)
reset=$(tput sgr0)
# ----------------------------
# import generate_config_file and generate_config_file_with_urls functions
source generate_config.sh
SYS_URL='http://localhost:8080'
APP_YAML=app.yaml
FRONTEND_YAML=frontend/frontend.yaml
BACKEND_YAML=backend/backend.yaml
WORKER_YAML=backend/worker.yaml
SUPPORT_YAML=support/support.yaml
FEATURE_YAML=feature-toggles/feature.yaml
WEBCHAT_YAML=webchat/webchat.yaml
config_files=(
"frontend/config.js"
"support/config.js"
"landing/config.js"
"feature-toggles/config.js"
"webchat/config.js"
);
SW_FILE="frontend/sw.js"
git update-index --skip-worktree ${config_files[@]}
PY_ENV=backend/py_env
# Receive as a paramameter 'production' or 'development'
function setup_firebase_config {
echo ">> Setting up Firebase configuration to $1 mode"
ECIS_CONFIG_FOLDER="$HOME/ecis-config/$1"
FIREBASE_CONFIG_FILE="$ECIS_CONFIG_FOLDER/firebase-config.js"
FIREBASE_BACKEND_CONFIG_FILE="$ECIS_CONFIG_FOLDER/firebase_config.py"
export GOOGLE_APPLICATION_CREDENTIALS="$ECIS_CONFIG_FOLDER/firebase-private-key.json"
cp $FIREBASE_CONFIG_FILE frontend/
catch_error $? "Firebase on Frontend ok"
cp $FIREBASE_CONFIG_FILE support/
catch_error $? "Firebase on Support ok"
cp $FIREBASE_BACKEND_CONFIG_FILE backend/
catch_error $? "Firebase on Backend ok"
}
function setup_app_version {
APP_VERSION_FILE="backend/app_version.py"
echo 'APP_VERSION = "'$1'"' > $APP_VERSION_FILE
catch_error $? "Setup APP VERSION with success"
}
function set_cache_suffix_sw {
tmpfile=$(mktemp)
sed "10s|.*| const CACHE_SUFIX = '$1';|" $SW_FILE > "$tmpfile" && mv "$tmpfile" $SW_FILE
catch_error $? "CACHE_SUFIX on SW $1"
}
function git_commit {
set_cache_suffix_sw "master"
git add frontend/sw.js
read -p "${bold}$(echo -e 'Type commit message \n\b> ')${reset}" MSG
git commit -m "$MSG"
}
# Usage [ --error ] <message_to_log>
function log_message {
if [ "$1" == "--error" ]; then
echo "${red}[eCIS ERROR]${reset} $2"
else
echo "${green}[eCIS LOG]${reset} $1"
fi
}
# Receives two arguments, the first must be the last output of a command ($?),
# and the second, is an optional message to be logged if no errors are found.
function catch_error {
if [ $1 -ne 0 ]; then
echo "${red}[eCIS ERROR]${reset} The last command exited with error $1. Fix it then try again!"
exit $1;
fi
if [ ! -z "$2" ]; then
# Print the message passed as the second parameters
echo "${green}[eCIS LOG]${reset} $2"
else
echo "${green}[eCIS LOG]${reset} done"
fi
}
case "$1" in
commit)
git_commit
;;
git-all)
git add -A
git_commit
current_branch=$(git branch | grep "*" | awk '{print $2}')
git push origin $current_branch
;;
run)
APP_VERSION=$(git branch | grep "*" | awk '{print $2}')
set_cache_suffix_sw $APP_VERSION
echo "=========== Cleaning Environment ==========="
rm -rf $PY_ENV
catch_error $? "Removed Pyenv Folder"
rm -rf frontend/test/node_modules frontend/test/bower_components
rm -rf feature-toggles/test/node_modules feature-toggles/test/bower_components
catch_error $? "Removed node_modules and bower_components"
echo "=========== Starting Virtual Environment ==========="
virtualenv $PY_ENV
catch_error $? "Setup Virtual Env"
source $PY_ENV/bin/activate
catch_error $? "Activated"
echo "=========== Installing Dependencies ==========="
python -m pip install -r backend/requirements.txt
touch .env_cleaned # Flag to the test script knows that the env was cleaned
if which xdg-open > /dev/null
then
xdg-open $SYS_URL
elif which gnome-open > /dev/null
then
gnome-open $SYS_URL
fi
echo "=========== Starting Microservices ==========="
setup_app_version $APP_VERSION
setup_firebase_config development
generate_config_file local $APP_VERSION ${config_files[@]}
if [[ -n $2 ]] && [ $2 = "--enable_datastore_emulator" ] ; then
gcloud beta emulators datastore start --host-port=0.0.0.0:8586 &
emulator=True
other_parameter=$3
else
other_parameter=$2
emulator=False
fi
$(gcloud beta emulators datastore env-init)
dev_appserver.py $APP_YAML $FRONTEND_YAML $BACKEND_YAML $SUPPORT_YAML $FEATURE_YAML $WEBCHAT_YAML $WORKER_YAML -A development-cis --support_datastore_emulator=$emulator $other_parameter
$(gcloud beta emulators datastore env-unset)
;;
test)
case "$2" in
client)
echo "=========== Starting Fronted Tests Setup ==========="
cd frontend/test
if [ "$3" == "--clean" ]; then
rm -rf node_modules bower_components
fi
if [ ! -e node_modules ]; then
yarn add package.json
catch_error $? "Node modules installed with success"
else
log_message "Node modules already installed"
fi
echo "=========== Starting to run Fronted Tests ==========="
karma start --single-run
;;
server)
if [ "$3" == "--clean" ]; then
./setup_env_test clean
else
./setup_env_test
fi
if [ $? != 0 ]; then # Verify the last script output looking for an error
exit 1 # Stop the script execution
fi
echo "=========== Starting Backend Tests ==========="
source $PY_ENV/bin/activate
setup_app_version "master"
cd backend
echo 'FIREBASE_URL = "FIREBASE_URL"' > firebase_config.py
echo 'SERVER_KEY = "SERVER_KEY"' >> firebase_config.py
TEST_NAME="*test.py"
if [ "$3" == "--name" ] && [ ! -z "$4" ]; then
TEST_NAME=$4
fi
python -m unittest discover -v -p $TEST_NAME
;;
feature)
echo "=========== Starting Feature Toggles Tests Setup ==========="
cd feature-toggles/test
if [ "$3" == "--clean" ]; then
rm -rf node_modules bower_components
fi
if [ ! -e node_modules ]; then
yarn
catch_error $? "Node modules installed with success"
else
log_message "Node modules already installed"
fi
echo "=========== Starting to run Feature Toggles Tests ==========="
karma start --single-run
;;
esac
;;
deploy)
APP_VERSION=$(git tag | sort -V | tail -1)
read -p ">> Type de APP VERSION or use the ${bold}Latest${_bold} ${bold}$APP_VERSION${_bold}: " NEW_APP_VERSION
if [ "$NEW_APP_VERSION" != "" ]; then
APP_VERSION=$NEW_APP_VERSION
fi
git checkout tags/"$APP_VERSION"
catch_error $? "Git checked out to tag $APP_VERSION"
set_cache_suffix_sw $APP_VERSION
echo "${bold}>> Select the application to deploy:${_bold}"
options=("development-cis" "eciis-splab" "Other")
select opt in "${options[@]}"
do
case $opt in
"development-cis")
ENV="dev"
APP_NAME="development-cis"
SUPPORT_DOMAIN="support-dot-$APP_NAME.appspot.com"
FRONTEND_DOMAIN="frontend-dot-$APP_NAME.appspot.com"
FEATURE_DOMAIN="feature-dot-$APP_NAME.appspot.com"
break
;;
"eciis-splab")
ENV="prod"
APP_NAME="eciis-splab"
SUPPORT_DOMAIN="support.plataformacis.org"
FRONTEND_DOMAIN="frontend.plataformacis.org"
FEATURE_DOMAIN="feature.plataformacis.org"
break
;;
"Other")
read -p "${bold}>> Type the app name:${_bold} " APP_NAME
break
;;
*) echo invalid option;;
esac
done
gcloud config set project $APP_NAME
echo "${bold}>> Deploy will use the following GCloud configuration${_bold}"
gcloud config list
catch_error $? "GCloud project configured to $APP_NAME"
read -p "${bold}Is everything correct (Y/n)?${_bold} " ANSWER
if [ "$ANSWER" != "n" ]; then
echo "Yes"
else
exit 1
fi
DOMAIN="$APP_NAME.appspot.com"
BACKEND_DOMAIN="backend-dot-$DOMAIN"
ENVIRONMENT="development"
read -p "${bold}Choose which environment to use: (development) ${_bold} " NEW_ENVIRONMENT
if [ "$NEW_ENVIRONMENT" != "" ]; then
ENVIRONMENT=$NEW_ENVIRONMENT
fi
setup_firebase_config $ENVIRONMENT
catch_error $? "Firebase setup with success"
setup_app_version $APP_VERSION
echo "=========== Starting Google App Engine Deployment ==========="
if [ ! -z $2 ]; then
version=$2
urls=(
$version"."$BACKEND_DOMAIN
$version"."$DOMAIN
$version"."$SUPPORT_DOMAIN
$version"."$FRONTEND_DOMAIN
$version"."$FEATURE_DOMAIN
)
generate_config_file_with_urls $APP_VERSION ${urls[@]} ${config_files[@]}
if [ ! -z $3 ]; then # Especified one or more yaml configuration files
gcloud app deploy --version $version --no-promote $3
else
gcloud app deploy --version $version --no-promote $APP_YAML $FRONTEND_YAML $BACKEND_YAML $SUPPORT_YAML $FEATURE_YAML $WEBCHAT_YAML $WORKER_YAML queue.yaml
fi
else
generate_config_file $ENV $APP_VERSION ${config_files[@]}
gcloud app deploy $APP_YAML $FRONTEND_YAML $BACKEND_YAML $SUPPORT_YAML $FEATURE_YAML $WEBCHAT_YAML $WORKER_YAML
fi
exit 0
;;
github-prs)
python parse_github_prs.py $2
;;
*)
echo "$1 is not a ecis command."
echo "Usage: $0 [ run | test | deploy ]"
echo "- run [ --enable-sendmail ]"
echo "- test [ server | client ]"
echo "---- test server [ --name <your_test.py> ]"
echo "- deploy [ version_name ]"
exit 1
esac