-
Notifications
You must be signed in to change notification settings - Fork 4
/
setup.sh
executable file
·175 lines (153 loc) · 4.97 KB
/
setup.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
173
174
#!/bin/bash -x
#### ---------------------------------
#### --- PROGRAM_TYPE: py, java, js --
#### ---------------------------------
#### MANDATORY: ONLY choose one here
PROGRAM_TYPE="py"
#PROGRAM_TYPE="java"
#PROGRAM_TYPE="js"
# -------------------------------
# ----------- Usage -------------
# -------------------------------
# To test on host machine:
# Run the Application:
# ./setup.sh
# -------------------------------
# -- debug use only --
verify=1
set -e
env
#### ---------------------------------
#### --- Process command arguments ---
#### ---------------------------------
#program_name=$(basename $0)
#function usage() {
# echo -e "Usage: $program_name <APP_RUN_DIR> <APP_RUN_CMD>"
#}
#usage
# -- debug use only --
verify=1
#### ---------------------------
#### --- APP: LOCATION ---
#### ---------------------------
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
if [[ "$DIR" =~ "/app" ]]; then
APP_HOME=${APP_HOME:-${DIR}}
else
# - find possilbe app directory:
APP_HOME=`realpath $(find ./ -name app)`
fi
echo "PWD=$PWD"
#### ---------------------------
#### --- APP: DATA Directory ---
#### ---------------------------
APP_DATA_DIR=${APP_DATA_DIR:-$HOME/data}
#### ---------------------------
#### --- APP: UTILITY ---
#### ---------------------------
function verifyDirectory() {
if [ $verify -eq 0 ]; then
return
fi
if [ "$1" = "" ] || [ ! -d "$1" ]; then
echo "*** ERROR ***: NOT_EXISTING: App's mandatory directory: $1: Can't continue! Abort!"
exit 1
fi
}
function verifyFile() {
if [ $verify -eq 0 ]; then
return
fi
if [ "$1" != "" ] && [ ! -s "$1" ]; then
echo "*** ERROR ***: NOT_FOUND: App's mandatory file: $1: Can't continue! Abort!"
exit 1
fi
}
function verifyCommand() {
if [ $verify -eq 0 ]; then
return
fi
if [ "$1" == "" ]; then
return
fi
if [ ! `which $1` ]; then
echo "*** ERROR ***: NOT_FOUND: App's mandatory tool: $1: Can't continue! Abort!"
exit 1
fi
}
function runCommands() {
if [ $verify -eq 0 ]; then
return
fi
CD_CMD=$1
EXE_CMD=$2
if [ "$1" != "" ] && [ -d "$1" ]; then
cd $1
shift
fi
if [ "$2" != "" ]; then
/bin/bash -c "$*"
fi
}
echo ">>>>>>>APP_RUN_CMD=$APP_RUN_CMD"
verifyCommand ${APP_RUN_CMD%% *}
# -------------------------------------------------------------------------------------
### --- APP: SETUP ---
#APP_HOME=${APP_HOME:-$HOME/app}
APP_HOME=${APP_HOME:-${DIR}}
### --- APP: RUN ---
APP_RUN_DIR=${APP_RUN_DIR:-$APP_HOME}
if [ "$PP_RUN_MAIN" != "" ]; then
APP_RUN_MAIN_base=$(basename $APP_RUN_MAIN)
APP_RUN_MAIN_dir=$(dirname $APP_RUN_MAIN)
if [ ${APP_RUN_MAIN_dir} = "." ] || [ ${APP_RUN_MAIN_dir} = "" ]; then
APP_RUN_MAIN_dir=${APP_RUN_DIR}
echo " >>>> APP_RUN_DIR=${APP_RUN_DIR}"
echo " >>>> APP_RUN_MAIN_dir=${APP_RUN_MAIN_dir}"
echo " >>>> APP_RUN_MAIN_base=${APP_RUN_MAIN_base}"
fi
fi
if [ "$APP_RUN_CMD" = "" ]; then
# Need to check APP_RUN_MAIN defined or not
if [ "$APP_RUN_MAIN_base" = "" ]; then
## AUTO-detection/finding of runnable main program Need to detect main python as best efforts or error out
#echo "*** ERROR: Neither APP_RUN_CMD nor APP_RUN_MAIN defined! Abort!"
#exit 1
# --- Auto detection and pick the first Python with main() to run --
ABS_APP_DIR="${APP_RUN_DIR%app*}app"
echo "ABS_APP_DIR=$ABS_APP_DIR"
FOUND_PROGRAM=`ls ${ABS_APP_DIR}/*.${PROGRAM_TYPE} | head -n 1 | awk '{print $1}' `
if [ "$FOUND_PROGRAM" != "" ]; then
APP_RUN_DIR=$(dirname $FOUND_PROGRAM)
APP_RUN_MAIN=$FOUND_PROGRAM
APP_RUN_CMD="python3 ${APP_RUN_MAIN}"
echo ">>> Auto detect (in app/) APP_RUN_DIR : $APP_RUN_DIR"
echo ">>> Auto detect (in app/) APP_RUN_MAIN: $APP_RUN_MAIN"
echo ">>> Auto detect (in app/) APP_RUN_CMD : $APP_RUN_CMD"
else
echo "*** ERROR ***: FAIL: Can't find any Python to run! Abort"
exit 1
fi
else
echo "--> APP_RUN_MAIN_base: ${APP_RUN_MAIN_base} will be used to launch Python as main."
echo ">>> Given (in .env) APP_RUN_DIR : $APP_RUN_DIR"
echo ">>> Given (in .env) APP_RUN_MAIN: $APP_RUN_MAIN"
echo ">>> Given (in .env) APP_RUN_CMD : $APP_RUN_CMD"
APP_RUN_CMD="python3 ${APP_RUN_MAIN}"
fi
else
echo "--> APP_RUN_CMD: ${APP_RUN_CMD} will be used to launch Python as main."
echo ">>> Given (in .env) APP_RUN_DIR : $APP_RUN_DIR"
echo ">>> Given (in .env) APP_RUN_MAIN: $APP_RUN_MAIN"
echo ">>> Given (in .env) APP_RUN_CMD : $APP_RUN_CMD"
fi
verifyDirectory $APP_HOME
verifyDirectory $APP_RUN_DIR
verifyFile $APP_RUN_MAIN
#### ---- Application ----
cd ${APP_RUN_DIR}
ls -al
#### ---- RUN Application ----
## e.g. Java Jar file
## cd ${APP_HOME}/dist && java -jar my_jar.jar
runCommands ${APP_RUN_DIR} ${APP_RUN_CMD}