-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathNerlDesigner.sh
More file actions
executable file
·298 lines (260 loc) · 7.88 KB
/
NerlDesigner.sh
File metadata and controls
executable file
·298 lines (260 loc) · 7.88 KB
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
#!/bin/bash
NERLNET_LIB_DIR="/usr/local/lib/nerlnet-lib"
NERLNET_DIR=$NERLNET_LIB_DIR/NErlNet
NERLNET_PREFIX="[NERLNET_SCRIPT]"
NERLDESIGNER_PREFIX="[NerlDesigner] "
NERLDESIGNER_ENV_NAME=".nerldesigner_env"
NERLDESIGNER_ENV_PATH="$PWD/$NERLDESIGNER_ENV_NAME"
NERLDESIGNER_REQUIREMENTS_PATH="$PWD/src_py/nerlDesigner/requirements.txt"
# arguments parsing
# Thanks to https://github.com/matejak/argbash
Port=8082
Host="0.0.0.0"
Install=false
Clean=false
help()
{
echo "-------------------------------------" && echo "NerlDesigner Launcher" && echo "-------------------------------------"
echo "Usage:"
echo "--i or --install: Install/reinstall the Python environment"
echo "--c or --clean: Clean existing environment and reinstall"
echo "--p or --port: Set the port for the web interface (default: 8082)"
echo "--h or --host: Set the host for the web interface (default: 0.0.0.0)"
echo "--help: Show this help message"
exit 2
}
print()
{
echo "$NERLDESIGNER_PREFIX $1"
}
die()
{
local _ret="${2:-1}"
test "${_PRINT_HELP:-no}" = yes && print_help >&2
echo "$1" >&2
exit "${_ret}"
}
begins_with_short_option()
{
local first_option all_short_options='hicph'
first_option="${1:0:1}"
test "$all_short_options" = "${all_short_options/$first_option/}" && return 1 || return 0
}
print_help()
{
printf 'Usage: %s [--help] [--install] [--clean] [--port <arg>] [--host <arg>]\n' "$0"
printf '\t%s\n' "--install, -i: Install/reinstall the Python environment"
printf '\t%s\n' "--clean, -c: Clean existing environment and reinstall"
printf '\t%s\n' "--port, -p: Set the port for the web interface (default: 8082)"
printf '\t%s\n' "--host, -h: Set the host for the web interface (default: 0.0.0.0)"
printf '\t%s\n' "--help: Show this help message"
}
parse_commandline()
{
while test $# -gt 0
do
_key="$1"
case "$_key" in
--help)
help
exit 0
;;
-i|--install)
Install=true
;;
-c|--clean)
Clean=true
Install=true
;;
-p|--port)
test $# -lt 2 && die "Missing value for the optional argument '$_key'." 1
Port="$2"
shift
;;
--port=*)
Port="${_key##--port=}"
;;
-p*)
Port="${_key##-p}"
;;
-h|--host)
test $# -lt 2 && die "Missing value for the optional argument '$_key'." 1
Host="$2"
shift
;;
--host=*)
Host="${_key##--host=}"
;;
-h*)
Host="${_key##-h}"
;;
*)
_PRINT_HELP=yes die "FATAL ERROR: Got an unexpected argument '$1'" 1
;;
esac
shift
done
}
check_python3()
{
if command -v python3 >/dev/null 2>&1; then
print "Python 3 is available"
python3 --version
return 0
else
print "ERROR: Python 3 is not installed"
print "Please install Python 3.8 or higher"
return 1
fi
}
check_venv_module()
{
if python3 -m venv --help >/dev/null 2>&1; then
print "Python venv module is available"
return 0
else
print "ERROR: Python venv module is not available"
print "Please install python3-venv package"
return 1
fi
}
clean_environment()
{
if [ -d "$NERLDESIGNER_ENV_PATH" ]; then
print "Removing existing environment: $NERLDESIGNER_ENV_PATH"
rm -rf "$NERLDESIGNER_ENV_PATH"
fi
}
create_environment()
{
print "Creating Python virtual environment: $NERLDESIGNER_ENV_NAME"
python3 -m venv "$NERLDESIGNER_ENV_PATH"
if [ ! -d "$NERLDESIGNER_ENV_PATH" ]; then
print "ERROR: Failed to create virtual environment"
return 1
fi
print "Virtual environment created successfully"
return 0
}
activate_environment()
{
if [ ! -f "$NERLDESIGNER_ENV_PATH/bin/activate" ]; then
print "ERROR: Virtual environment activation script not found"
return 1
fi
print "Activating virtual environment"
source "$NERLDESIGNER_ENV_PATH/bin/activate"
# Verify activation
if [ "$VIRTUAL_ENV" = "$NERLDESIGNER_ENV_PATH" ]; then
print "Virtual environment activated: $VIRTUAL_ENV"
return 0
else
print "ERROR: Failed to activate virtual environment"
return 1
fi
}
install_dependencies()
{
print "Installing NerlDesigner dependencies"
# Upgrade pip first
print "Upgrading pip..."
python -m pip install --upgrade pip
# Install requirements
if [ -f "$NERLDESIGNER_REQUIREMENTS_PATH" ]; then
print "Installing requirements from: $NERLDESIGNER_REQUIREMENTS_PATH"
pip install -r "$NERLDESIGNER_REQUIREMENTS_PATH"
else
print "Requirements file not found, installing basic dependencies"
pip install nicegui>=1.4.0 pydantic>=2.0.0 networkx>=3.0 plotly>=5.0.0 pandas>=1.5.0 fastapi>=0.100.0
fi
print "Dependencies installed successfully"
}
check_environment_exists()
{
if [ -d "$NERLDESIGNER_ENV_PATH" ] && [ -f "$NERLDESIGNER_ENV_PATH/bin/activate" ]; then
return 0
else
return 1
fi
}
setup_environment()
{
print "Setting up NerlDesigner Python environment"
# Check if we need to clean
if [ "$Clean" = true ]; then
clean_environment
fi
# Create environment if it doesn't exist
if ! check_environment_exists; then
if ! create_environment; then
return 1
fi
fi
# Activate environment
if ! activate_environment; then
return 1
fi
# Install dependencies if requested or if environment is new
if [ "$Install" = true ] || [ ! -f "$NERLDESIGNER_ENV_PATH/.dependencies_installed" ]; then
install_dependencies
touch "$NERLDESIGNER_ENV_PATH/.dependencies_installed"
fi
return 0
}
launch_nerldesigner()
{
print "Launching NerlDesigner Web Application"
print "Host: $Host"
print "Port: $Port"
print ""
print "🌐 To access the server:"
if [ "$Host" = "0.0.0.0" ]; then
print " Local access:"
print " http://localhost:$Port"
print " or http://127.0.0.1:$Port"
print ""
print " 📡 SSH Tunnel (if connecting remotely):"
print " On your local machine, run:"
print " ssh -L $Port:localhost:$Port <username>@<server-address>"
print " Then open: http://localhost:$Port"
print ""
else
print " Direct access: http://$Host:$Port"
fi
print "Press Ctrl+C to stop the application"
print "=================================================="
# Set environment variables for the application
export NERLDESIGNER_HOST="$Host"
export NERLDESIGNER_PORT="$Port"
# Launch the application
cd src_py/nerlDesigner
python main.py --host "$Host" --port "$Port"
}
main()
{
print "NerlDesigner Environment Manager and Launcher"
print "============================================="
# Parse command line arguments
parse_commandline "$@"
# Check system requirements
if ! check_python3; then
exit 1
fi
if ! check_venv_module; then
exit 1
fi
# Setup environment
if ! setup_environment; then
print "ERROR: Failed to setup environment"
exit 1
fi
# Launch application if not just installing
if [ "$Install" = false ] || [ "$Clean" = false ]; then
launch_nerldesigner
else
print "Environment setup completed successfully"
print "Run this script without --install to launch NerlDesigner"
fi
}
# Run main function with all arguments
main "$@"