Repository navigation
Expand file tree
/
Copy pathdev.sh
More file actions
executable file
·203 lines (174 loc) · 6.2 KB
/
Copy pathdev.sh
File metadata and controls
executable file
·203 lines (174 loc) · 6.2 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
#!/usr/bin/env bash
set -Eeuo pipefail
ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
WEB_DIR="$ROOT_DIR/apps/vds-web"
WEB_PORT="${VDS_WEB_PORT:-4174}"
WEB_HOST="${VDS_WEB_HOST:-0.0.0.0}"
CONTROL_PORT=8080
SPI_CUSE_BUILD_DIR="$ROOT_DIR/build/adapters/spi-cuse"
SPI_CUSE_EXECUTABLE="$SPI_CUSE_BUILD_DIR/vds4e-spi-cuse"
I2C_CUSE_BUILD_DIR="$ROOT_DIR/build/adapters/i2c-cuse"
I2C_CUSE_EXECUTABLE="$I2C_CUSE_BUILD_DIR/vds4e-i2c-cuse"
UART_PTY_BUILD_DIR="$ROOT_DIR/build/adapters/uart-pty"
UART_PTY_EXECUTABLE="$UART_PTY_BUILD_DIR/vds4e-uart-pty"
GPIO_SIM_BUILD_DIR="$ROOT_DIR/build/adapters/gpio-sim"
GPIO_SIM_EXECUTABLE="$GPIO_SIM_BUILD_DIR/vds4e-gpio-sim"
LOCK_FILE="${TMPDIR:-/tmp}/vds4e-dev-${UID}.lock"
SERVER_PID=""
WEB_PID=""
SERVER_OWN_PROCESS_GROUP=false
exec 9>"$LOCK_FILE"
if ! flock -n 9; then
echo "Another VDS4E development workspace is already running." >&2
echo "Stop its dev.sh terminal with Ctrl+C before starting a new one." >&2
exit 1
fi
port_is_open() {
(exec 8<>"/dev/tcp/127.0.0.1/$1") 2>/dev/null
}
prepare_cmake_build_dir() {
local source_dir="$1"
local build_dir="$2"
local cache_file="$build_dir/CMakeCache.txt"
local directory_info="$build_dir/CMakeFiles/CMakeDirectoryInformation.cmake"
local cached_source=""
local cached_binary=""
local generated_binary=""
if [[ ! -f "$cache_file" ]]; then
return
fi
cached_source="$(sed -n 's#^CMAKE_HOME_DIRECTORY:INTERNAL=##p' "$cache_file")"
cached_binary="$(sed -n 's#^CMAKE_CACHEFILE_DIR:INTERNAL=##p' "$cache_file")"
if [[ -f "$directory_info" ]]; then
generated_binary="$(sed -n 's#^set(CMAKE_RELATIVE_PATH_TOP_BINARY "\(.*\)")#\1#p' "$directory_info")"
fi
if [[ "$cached_source" != "$source_dir" ||
"$cached_binary" != "$build_dir" ||
( -n "$generated_binary" && "$generated_binary" != "$build_dir" ) ]]; then
echo "Discarding relocated CMake cache: $build_dir"
cmake -E remove_directory "$build_dir"
fi
}
wait_for_control_api() {
echo "Waiting for the Control API to become ready..."
while ! port_is_open "$CONTROL_PORT"; do
if ! kill -0 "$SERVER_PID" 2>/dev/null; then
set +e
wait "$SERVER_PID"
local status=$?
set -e
echo "vds-server stopped before the Control API became ready (status $status)." >&2
if [[ $status -eq 0 ]]; then
return 1
fi
return "$status"
fi
sleep 0.1
done
}
if port_is_open "$WEB_PORT"; then
echo "Web port $WEB_PORT is already in use." >&2
echo "Stop the existing process or choose VDS_WEB_PORT=<port>." >&2
exit 1
fi
if port_is_open "$CONTROL_PORT"; then
echo "Control API port $CONTROL_PORT is already in use." >&2
echo "Stop the existing vds-server before starting dev.sh." >&2
exit 1
fi
cleanup() {
trap - EXIT
if [[ -n "$WEB_PID" ]]; then
kill -TERM -- "-$WEB_PID" 2>/dev/null || true
fi
if [[ -n "$SERVER_PID" ]]; then
if [[ "$SERVER_OWN_PROCESS_GROUP" == true ]]; then
kill -TERM -- "-$SERVER_PID" 2>/dev/null || true
else
kill -TERM "$SERVER_PID" 2>/dev/null || true
fi
fi
wait "$WEB_PID" "$SERVER_PID" 2>/dev/null || true
}
trap cleanup EXIT
trap 'exit 130' INT TERM
if [[ ! -d "$WEB_DIR/node_modules" ]]; then
echo "Web dependencies are missing; installing them once..."
npm --prefix "$WEB_DIR" install
fi
cd "$ROOT_DIR"
echo "Building shared adapter transport..."
make -C "$ROOT_DIR/adapters/bridge" BUILD_DIR="$ROOT_DIR/build/bridge" all
echo "Configuring and building the SPI CUSE adapter..."
prepare_cmake_build_dir "$ROOT_DIR/adapters/spi-cuse" "$SPI_CUSE_BUILD_DIR"
cmake \
-S "$ROOT_DIR/adapters/spi-cuse" \
-B "$SPI_CUSE_BUILD_DIR" \
-DCMAKE_BUILD_TYPE=Debug
cmake --build "$SPI_CUSE_BUILD_DIR" --parallel
echo "Configuring and building the I2C CUSE adapter..."
prepare_cmake_build_dir "$ROOT_DIR/adapters/i2c-cuse" "$I2C_CUSE_BUILD_DIR"
cmake \
-S "$ROOT_DIR/adapters/i2c-cuse" \
-B "$I2C_CUSE_BUILD_DIR" \
-DCMAKE_BUILD_TYPE=Debug
cmake --build "$I2C_CUSE_BUILD_DIR" --parallel
echo "Configuring and building the UART PTY adapter..."
prepare_cmake_build_dir "$ROOT_DIR/adapters/uart-pty" "$UART_PTY_BUILD_DIR"
cmake \
-S "$ROOT_DIR/adapters/uart-pty" \
-B "$UART_PTY_BUILD_DIR" \
-DCMAKE_BUILD_TYPE=Debug
cmake --build "$UART_PTY_BUILD_DIR" --parallel
echo "Configuring and building the GPIO simulator adapter..."
prepare_cmake_build_dir "$ROOT_DIR/adapters/gpio-sim" "$GPIO_SIM_BUILD_DIR"
cmake \
-S "$ROOT_DIR/adapters/gpio-sim" \
-B "$GPIO_SIM_BUILD_DIR" \
-DCMAKE_BUILD_TYPE=Debug
cmake --build "$GPIO_SIM_BUILD_DIR" --parallel
if [[ "${VDS4E_ADAPTER_AUTH:-pkexec}" == "sudo" ]]; then
# sudo's default credential cache is terminal-scoped. Keep the server in
# this terminal session so adapter helpers can use the one authorization
# acquired by run.sh, while still running the server itself unprivileged.
cargo build -p vds-server
env \
VDS4E_SPI_CUSE_EXECUTABLE="$SPI_CUSE_EXECUTABLE" \
VDS4E_I2C_CUSE_EXECUTABLE="$I2C_CUSE_EXECUTABLE" \
VDS4E_UART_PTY_EXECUTABLE="$UART_PTY_EXECUTABLE" \
VDS4E_GPIO_SIM_EXECUTABLE="$GPIO_SIM_EXECUTABLE" \
"$ROOT_DIR/build/rust/debug/vds-server" --config config/vds-server.yaml &
SERVER_PID=$!
else
setsid env \
VDS4E_SPI_CUSE_EXECUTABLE="$SPI_CUSE_EXECUTABLE" \
VDS4E_I2C_CUSE_EXECUTABLE="$I2C_CUSE_EXECUTABLE" \
VDS4E_UART_PTY_EXECUTABLE="$UART_PTY_EXECUTABLE" \
VDS4E_GPIO_SIM_EXECUTABLE="$GPIO_SIM_EXECUTABLE" \
cargo run -p vds-server -- --config config/vds-server.yaml &
SERVER_PID=$!
SERVER_OWN_PROCESS_GROUP=true
fi
wait_for_control_api
setsid npm --prefix "$WEB_DIR" run dev -- --host "$WEB_HOST" --port "$WEB_PORT" --strictPort &
WEB_PID=$!
echo
echo "VDS4E development workspace is starting:"
echo " Web UI: http://127.0.0.1:$WEB_PORT"
echo " Web bind: $WEB_HOST:$WEB_PORT"
if [[ "$WEB_HOST" == "0.0.0.0" ]]; then
echo " Remote UI: http://<Linux-host-IP>:$WEB_PORT"
fi
echo " Control API: http://127.0.0.1:$CONTROL_PORT/api/v1/health"
echo " Data plane: /tmp/vds4e.sock"
echo
echo "Keep this terminal open. Press Ctrl+C to stop both processes."
echo
set +e
wait -n "$SERVER_PID" "$WEB_PID"
STATUS=$?
set -e
if [[ $STATUS -ne 130 ]]; then
echo "A development process stopped (status $STATUS); shutting down the workspace."
fi
exit "$STATUS"