forked from flojoy-ai/studio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflojoy
executable file
·376 lines (322 loc) · 10.1 KB
/
flojoy
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
#!/bin/bash
#
# Run all services required by Flojoy Studio
success_color='\033[38;5;71m'
warning_color='\033[38;5;208m'
error_color='\033[0;31m'
info_color='\033[0;34m'
general_color='\033[38;5;212m'
default_color='\033[0m'
info_mark=👉
check_mark=✔
alert_mark=⚠️
error_mark=❌
is_command_successful=0
success_msg()
{
message=$1
echo -e "$success_color$check_mark $message $default_color"
echo ''
}
info_msg()
{
message=$1
echo -e "$info_color$info_mark $message $default_color"
echo ''
}
warning_msg()
{
message=$1
echo -e "$warning_color$alert_mark $message $default_color"
echo ''
}
error_msg()
{
message=$1
echo -e "$error_color$error_mark $message $default_color"
echo ''
echo ''
}
echo ''
echo ''
echo ' ============================================================'
echo ' || Welcome to Flojoy! ||'
echo ' || ||'
echo ' || For Installation, Follow the Link Below ||'
echo ' || https://docs.flojoy.io/getting-started/install/ ||'
echo ' || ||'
echo ' ============================================================'
echo ''
venv=""
djangoPort=8000
initNodePackages=true
initPythonPackages=true
is_ci=false
test_ci=false
initSubmodule=true
enableSentry=true
enableTelemetry=false
# Gives Feedback if the command run is successful or failed, if failed it exits the execution.
feedback()
{
is_successful=$1
message=$2
help_message=$3
if [ "$is_successful" -eq 0 ]; then
success_msg "$message"
else
error_msg "$help_message"
exit 1
fi
}
wait_for_redis()
{
info_msg "Waiting for Redis server to start..."
ATTEMPTS=0
MAX_ATTEMPTS=20
while [ $ATTEMPTS -lt $MAX_ATTEMPTS ]; do
redis-cli ping >> /dev/null 2>&1
if [ $? == 0 ]; then
feedback 0 'Redis server started succesfully...' ''
break
else
sleep 1
ATTEMPTS=$((ATTEMPTS+1))
fi
done
if [ $ATTEMPTS -eq $MAX_ATTEMPTS ]; then
feedback 1 '' "Could not start Redis after $ATTEMPTS attempts..."
fi
}
# Help function
helpFunction()
{
echo ""
echo "Usage: $0 -n -p -r -v venv-path"
echo " -r: shuts down existing Redis server and spins up a fresh one"
echo " -v: path to a Python virtual environment"
echo " -n: To NOT install npm packages"
echo " -p: To NOT install python packages"
echo " -s: To NOT update submodules"
echo " -S: To NOT enable Sentry"
echo " -t: To enable local CI tests"
echo " -T: To enable Telemetry"
echo 1 # Exit script after printing help
}
# Parse command-line arguments
while [ $# -gt 0 ]
do
key="$1"
case $key in
-n)
initNodePackages=false
shift
;;
-p)
initPythonPackages=false
shift
;;
-r)
initRedis=true
shift
;;
-v)
venv="$2"
shift
shift
;;
-c)
is_ci=true
shift
;;
-T)
enableTelemetry=true
shift
;;
-S)
enableSentry=false
shift
;;
-t)
test_ci=true #run with CI=true, allows to properly run tests locally
shift
;;
-s)
initSubmodule=false # don't update submodules, prevents overwriting local changes
shift
;;
*) # unknown option
echo "Unknown option: $1"
helpFunction
exit 1
;;
esac
done
# checking if flojoy.yaml file exists
CWD="$PWD"
createFlojoyDirectoryWithYmlFile()
{
FOLDER=$HOME/.flojoy
FILE=$HOME/.flojoy/flojoy.yaml
if test -d "$FOLDER"; then
if test -f "$FILE"; then
info_msg "$FILE exists."
echo "PATH: $CWD" > $FILE
feedback $? "Updated file path in flojoy.yaml file." "Couldn't update file path in flojoy.yaml file, check the permission or sign in as root user"
else
info_msg "file flojoy.yaml in directory $FOLDER does not exists. "
touch $FILE && echo "PATH: $CWD" > $FILE
feedback $? "Successfully created flojoy.yaml file in $FOLDER directory." "Couldn't create flojoy.yaml file in $FOLDER directory, check the permission or sign in as root user"
fi
else
info_msg "directory ~/.flojoy/flojoy.yaml does not exists. "
mkdir "$FOLDER" && touch "$FILE" && echo "PATH: $CWD" > "$FILE"
feedback $? "Created new $FOLDER directory with flojoy.yaml file." 'Failed to create file in the home directory, check the permission or sign in as root user'
fi
CREDENTIALS_FILE=$FOLDER/credentials
if ! test -f "$CREDENTIALS_FILE"; then
warning_msg " Warning: Credentials are not set for your project! You can set credentials by creating a file named 'credentials' in the directory '~/.flojoy' and adding your credentials to the file."
else
if ! grep -q 'FRONTIER_API_KEY' "$CREDENTIALS_FILE" >/dev/null 2>&1; then
warning_msg " Warning: Frontier API key not set for your project! To set Frontier API key, simply follow this pattern in the '~/.flojoy/credentials' file: FRONTIER_API_KEY:<your key>"
fi
fi
}
createFlojoyDirectoryWithYmlFile
# update submodules
if [ "$initSubmodule" = true ]; then
git submodule update --init --recursive >/dev/null 2>&1
feedback $? 'Updated submodules successfully' 'Failed to update submodules, check if git is installed correctly and configured with your github account.'
fi
# checking virtual environment
venvCmd=""
if [ -n "$venv" ]
then
info_msg "Virtual env path is provided, will use: ${venv}"
venvCmd="source ${venv}/bin/activate"
if [ "$(echo -n $venv | tail -c 1)" = '/' ];then
venvCmd="source ${venv}bin/activate"
fi
else
info_msg "No virtual env provided"
fi
# Check if Python, Pip, or npm is missing.
if [ $is_ci = false ];then
source check-dependencies
# Call the function to check for dependencies
missing_dependencies=$(check_dependencies)
# If there are missing dependencies, print the list of them
if [ -n "$missing_dependencies" ]; then
error_msg "$missing_dependencies"
exit 1
fi
fi
# Install python packages
if [ $initPythonPackages = true ]
then
cd $CWD
info_msg "Flag -p is not provided, Python packages will be installed from requirements.txt file"
if [ -n "$venv" ];then
pip_cmd="pip install -r requirements.txt"
else
pip_cmd="python3 -m pip install -r requirements.txt"
fi
set -o pipefail
$venvCmd && $pip_cmd 2>&1 | while IFS= read -r line; do
if [[ $line == Requirement* ]]; then
echo "$line" | awk -F'in' '{print $1}'
elif [[ $line == WARNING* ]]; then
echo ''
else
echo "$line"
fi
done
feedback $? 'Python packages installed successfully!' 'Failed to install Python packages. Look at error details printed above.'
fi
# Install node packages
if [ $initNodePackages = true ]
then
info_msg "Argument -n is not provided, Node packages will be installed from package.json"
set -o pipefail
cd "$CWD" && npm install 2>&1 | grep -v 'WARN'
feedback $? 'Installed Node packages successfully.' 'Node package installation failed! Look at error details printed above'
fi
# creating system links
createSystemLinks
feedback $is_command_successful 'Created symlinks successfully!' 'Creating symlinks failed, check your PYTHON/WATCH or src folder, maybe one of them is missing'
# jsonify python functions
$venvCmd && python3 write_python_metadata.py
feedback $? 'Jsonified Python functions and written to JS-readable directory' 'Error occurred while Jsonifying Python functions. Check errors printed above!'
# Generate Manifest
$venvCmd && python3 generate_manifest.py
feedback $? 'Successfully generated manifest for Python nodes to frontend' 'Failed to generate manifest for Python nodes. Check errors printed above!'
if [ $is_ci = true ];then
exit 0
fi
# initializing new Redis Instance
if [ "$initRedis" ]
then
npx ttab -a iTerm2 -t 'Redis-CLI' "redis-cli SHUTDOWN; sleep 2; redis-cli FLUSHALL; exit"
feedback $? 'Successfully shut down existing Redis server and cleared Redis memory...' 'Failed to shut down Redis server. redis-cli error: check if redis-cli is running or Redis is installed in your local machine'
info_msg 'Spinning up a fresh Redis server...'
npx ttab -a iTerm2 -t 'Redis-CLI' "redis-server"
wait_for_redis
else
if [ $is_ci = false ]; then
redis-cli ping >> /dev/null 2>&1
if [ $? != 0 ]; then
info_msg "Redis is not running, trying to start Redis server..."
npx ttab -t 'Redis' "redis-server"
wait_for_redis
fi
fi
fi
# Closing All RQ Workers
$venvCmd && python3 close-all-rq-workers.py >/dev/null
feedback $? 'Closed all existing rq workers (if any).' 'Error occured while closing all existing rq workers (if any): seems like rq package is not installed! Try running this script without -p argument to install required python packages.'
# SHowing RQ WOrker Info
info_msg 'Rq info after closing:'
$venvCmd && rq info
echo ''
# Setup Sentry env var
if [ $enableSentry = true ]
then
info_msg "Sentry will be enabled!"
export FLOJOY_ENABLE_SENTRY=1
else
info_msg "Sentry will be disabled!"
export FLOJOY_ENABLE_SENTRY=0
fi
# Setup Telemetry
if [ $enableTelemetry = true ]
then
info_msg "Telemetry will be enabled!"
export FLOJOY_ENABLE_TELEMETRY=1
else
info_msg "Telemetry will be disabled!"
export FLOJOY_ENABLE_TELEMETRY=0
fi
cd $CWD
# Checking for YOLOV3 Weights
CWD="$PWD"
FILE=$PWD/PYTHON/utils/object_detection/yolov3.weights
if test -f "$FILE"; then
info_msg "$FILE exists."
else
touch "$PWD"/PYTHON/utils/object_detection/yolov3.weights
wget -O "$PWD"/PYTHON/utils/object_detection/yolov3.weights https://pjreddie.com/media/files/yolov3.weights
echo ''
fi
sleep 1
# start project services
if [ $test_ci = true ]; then
start_project_cmd='npm run start-project:ci'
else
start_project_cmd='npm run start-project'
fi
# start project services
if [ -n "$venvCmd" ]; then
$venvCmd && $start_project_cmd
else
$start_project_cmd
fi