forked from askomics/askomics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
startAskomics.sh
executable file
·140 lines (119 loc) · 3.98 KB
/
startAskomics.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
#! /bin/bash
dir_askomics=$(dirname "$0")
dir_config="$dir_askomics/configs"
dir_venv="$dir_askomics/venv"
dir_node_modules="$dir_askomics/node_modules"
python_ex="python3"
pyvenv="$python_ex -m venv"
pip="$python_ex -m pip"
gulp="$dir_node_modules/.bin/gulp"
python_flags="-s"
function usage() {
echo "Usage: $0 (-d { dev | prod })"
echo " -d deployment mode (default: production)"
echo " -r run only (without build javascript and python)"
echo " -b build only (python and js)"
}
# Default options
run=false
build=false
# Parse options -------------------------------------------
while getopts "ht:d:rb" option; do
case $option in
h)
usage
exit 0
;;
d)
depmode=$OPTARG
;;
r)
run=true
;;
b)
build=true
;;
esac
done
# Get flags and deployment mode ---------------------------
case $depmode in
prod|production|"")
depmode="production"
gulpmode="--prod"
pserve_flags="-q"
;;
dev|development)
depmode="development"
gulpmode="--reload"
pserve_flags="--reload"
;;
*)
echo "-d $depmode: wrong deployment mode"
usage
exit 1
esac
# Build python virtual environment ------------------------
# Check if venv is build. If not, build and activate it
# If yes, just activate it
activate="$dir_venv/bin/activate"
if [[ ! -f $activate ]]; then
echo "building python virtual environment ..."
$pyvenv $dir_venv
source $activate
$pip install --upgrade pip
$pip install -e .
if [ $? -ne 0 ]; then { echo "Failed, aborting." ; rm -rf $dir_venv ; exit 1; } fi
else
source $activate
fi
# Install JS dependancies if not installed ----------------
if [[ ! -d $dir_node_modules ]]; then
echo "javascript dependancies : npm install"
# check binary dependancies
npm -v >/dev/null 2>&1 || { echo "npm is required but is not installed. Aborting." >&2; exit 1; }
npm install
if [ $? -ne 0 ]; then { echo "Failed, aborting." ; rm -$dir_node_modules ; exit 1; } fi
fi
# Build config file ---------------------------------------
config_template_path="$dir_config/askomics.ini.template"
config_path="$dir_config/askomics.ini"
if [[ ! -f $config_path ]]; then
cp $config_template_path $config_path
fi
# Get environment variables -------------------------------
echo "Convert environment variables to ini file ..."
# This take ASKO_ env to update config in app:main section, only askomics. key
printenv | egrep "^ASKO_" | while read setting
do
key="askomics."$(echo $setting | egrep -o "^ASKO_[^=]+" | sed 's/^.\{5\}//g' | tr '[:upper:]' '[:lower:]')
value=$(echo $setting | egrep -o "=.*$" | sed 's/^=//g')
$python_ex config_updater.py -p $config_path -s "app:main" -k $key -v $value
done
# This take ASKOCONFIG_ env to update config in any sections
printenv | egrep "^ASKOCONFIG_" | while read setting
do
sed_setting=$(echo $setting | sed 's/\_colon\_/\:/g' | sed 's/\_hyphen\_/\-/g' | sed 's/\_dot\_/\./g')
section=$(echo $sed_setting | egrep -o "^ASKOCONFIG_[^=]+" | sed 's/^.\{11\}//g' | cut -d "_" -f 1)
key=$(echo $sed_setting | egrep -o "^ASKOCONFIG_[^=]+" | sed 's/^.\{11\}//g' | sed "s/$section\_//g")
value=$(echo $sed_setting | egrep -o "=.*$" | sed 's/^=//g')
$python_ex config_updater.py -p $config_path -s $section -k $key -v $value
done
# Build Javascript ----------------------------------------
# deploy JS if not run only option or if there is no js
if [[ $run == false ]]; then
echo "deploying javascript ..."
if [[ $depmode == "development" ]]; then
$gulp $gulpmode &
else
echo "$gulp $gulpmode"
$gulp $gulpmode
fi
fi
# Run Askomics --------------------------------------------
pserve="$dir_venv/bin/pserve"
askomics="$python_ex $python_flags $pserve $config_path $pserve_flags"
if [[ $build == false ]]; then
echo "starting askomics ..."
echo "$askomics"
$askomics
fi