-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrun
executable file
·410 lines (343 loc) · 8.55 KB
/
run
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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
#!/bin/bash
#
# Groups commands for controlling the vagrant environment
source "$(dirname $(realpath $0))/.envrc"
### Configuration
NODE_VERSION=v12.13.0
XDISPLAY=0
XAUTHORITY=~/.Xauthority
SCREEN_WIDTH=1920
SCREEN_HEIGHT=1080
FRAMERATE=30
NOVNC_SOURCE=$PROJECT_ROOT/noVNC
NOVNC_PROXY_PORT=6080
FFMPEG_SOURCE=$PROJECT_ROOT/ffmpeg
FFMPEG_BUILD=$PROJECT_ROOT/build/ffmpeg
FFMPEG_INSTALL=$FFMPEG_BUILD/install
MEDIA_SINK_SOCKET=$PROJECT_ROOT/build/server/share-space-media-sink.sock
INFO_SINK_SOCKET=$PROJECT_ROOT/build/server/share-space-info-sink.sock
# Holds the last time the stack was installed successfully
STACK_INSTALLED=$PROJECT_ROOT/build/stack/installed
STACK_BUILT=$PROJECT_ROOT/build/stack/built
### Commands. All commands are dash-cased, everything else uses_underscores
## Common
quit() {
echo $1
exit 1
}
_kill() {
local PIDS=($(pgrep $1))
for pid in ${PIDS[*]}; do
[ -d "/proc/$pid" ] && echo "Killing $1 with PID $pid"
kill $pid &> /dev/null
timeout 3s tail --pid=$pid -f /dev/null
kill -KILL $pid &> /dev/null
done
}
RUN_TIME=$(date -Iseconds)
LOG_DIR=$PROJECT_ROOT/build/stack/logs-$RUN_TIME
LATEST_LOGS=$PROJECT_ROOT/build/stack/logs-latest
log_cmd() {
mkdir -p $LOG_DIR
rm -f $LATEST_LOGS
ln -fs $LOG_DIR $LATEST_LOGS
$PROJECT_ROOT/run $@ &> $LOG_DIR/$1.log
}
run_cmd() {
$PROJECT_ROOT/run $@ || quit "Error running $1"
}
require_installed() {
[ -a "$STACK_INSTALLED" ] || \
quit "No stack installed - no file at $STACK_INSTALLED"
}
require_built() {
[ -a "$STACK_BUILT" ] || \
quit "No stack built - no file at $STACK_INSTALLED"
}
## Stack control
stack-install() {
if [ -a "$STACK_INSTALLED" ]; then
echo "Not installing - $STACK_INSTALLED indicates an active install"
echo "$(cat $STACK_INSTALLED)"
exit 1
fi
ENV_SETUP="source $PROJECT_ROOT/.envrc"
grep -q "$ENV_SETUP" ~/.bashrc || \
echo $ENV_SETUP >> ~/.bashrc || \
quit "Couldn't update ~/.bashrc"
grep -q "$ENV_SETUP" ~/.profile || \
echo $ENV_SETUP >> ~/.profile || \
quit "Couldn't update ~/.profile"
echo "Installing dependencies"
sudo apt-get -y update || quit "apt update failed"
run_cmd install-node
run_cmd install-ffmpeg-deps
run_cmd install-gui
run_cmd install-nfs
echo "Install time: $(date)" > $STACK_INSTALLED
echo "Finished installing"
}
stack-build() {
require_installed
run_cmd build-ffmpeg
run_cmd build-frontend
echo "Build time: $(date)" > $STACK_BUILT
echo "Finished building"
}
stack-up() {
require_built
log_cmd run-Xvfb &
log_cmd run-x11vnc &
log_cmd run-frontend &
log_cmd run-noVNC-proxy &
echo "Pausing before starting screencap..."
sleep 5
log_cmd run-screencap-stream &
}
stack-vnc-up() {
require_built
log_cmd run-Xvfb &
log_cmd run-x11vnc fb &
log_cmd run-frontend &
log_cmd run-noVNC-proxy &
}
stack-down() {
_kill x11vnc
_kill Xvfb
_kill ffmpeg
_kill node
_kill '-f websockify'
}
stack-status() {
ps -ef | grep -i -E '(x11vnc|xvfb|xorg|xserver|node|ffmpeg|websockify)'
}
## Install
install-node() {
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.0/install.sh | bash || \
quit "Error installing nvm"
# Inline nvm activation
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && source "$NVM_DIR/nvm.sh"
[ -s "$NVM_DIR/bash_completion" ] && source "$NVM_DIR/bash_completion"
nvm install $NODE_VERSION || \
quit "Error installing node $NODE_VERSION"
}
install-gui() {
# Install Cinnamon desktop environment on Xvfb, a headless X server.
# Install x11vnc for remote control
# Install firefox for browsing and libavcodec-extra for video playback.
sudo apt-get -y install \
xvfb \
x11vnc \
cinnamon-core \
xinit \
dbus-x11 \
libavcodec-extra \
firefox || \
quit "Couldn't install GUI"
if [ $(systemctl list-unit-files "display-manager.service" | wc -l) -gt 3 ]; then
sudo systemctl disable display-manager.service || \
quit "Couldn't disable display manager"
fi
}
install-ffmpeg-deps() {
sudo apt-get -y install \
autoconf \
automake \
build-essential \
cmake \
nasm \
yasm \
git-core \
libass-dev \
libfreetype6-dev \
libsdl2-dev \
libtool \
libva-dev \
libvdpau-dev \
libvorbis-dev \
libxcb1-dev \
libxcb-shm0-dev \
libxcb-xfixes0-dev \
pkg-config \
texinfo \
zlib1g-dev \
libx264-dev \
libvpx-dev \
libopus-dev || \
quit "Couldn't install ffmpeg deps"
}
install-nfs() {
if [ $(systemctl is-active nfs-kernel-server) != "active" ]; then
sudo apt-get -y install nfs-kernel-server || \
quit "Couldn't install nfs-kernel-server"
fi
if [ -a /etc/exports -a -n "$(grep "$PROJECT_ROOT" /etc/exports)" ]; then
echo "NFS already configured"
exit
fi
sudo echo "$PROJECT_ROOT *(rw,sync,no_root_squash)" | sudo tee --append /etc/exports > /dev/null || \
quit "Couldn't update /etc/exports"
sudo systemctl restart nfs-kernel-server || \
quit "Couldn't restart NFS"
}
## Build
build-frontend() {
(npm install && \
cd client && \
npm install) || \
quit "Couldn't build frontend"
}
configure-ffmpeg() {
cd $FFMPEG_BUILD
$FFMPEG_SOURCE/configure \
--prefix=$FFMPEG_INSTALL \
--enable-libvpx \
--enable-libopus \
--enable-libvorbis \
--enable-libx264 \
--enable-libpulse \
--enable-gpl \
--enable-shared
}
build-ffmpeg() {
configure-ffmpeg && \
make -j $(nproc) && \
make install
}
install-ci() {
build-frontend
}
test-ci() {
npm test
}
build-site-ci() {
(cd client && \
npm run build && \
cd .. && \
rm -rf project-site/app && \
cp -r client/build project-site/app && \
cp Readme.md project-site) || \
quit "Couldn't build project site"
}
## Run
run-frontend() {
export REACT_APP_DESKTOP_WIDTH=$SCREEN_WIDTH
export REACT_APP_DESKTOP_HEIGHT=$SCREEN_HEIGHT
export REACT_APP_DEFAULT_ROOM_CODE=""
echo $SHARE_SPACE_ENV
if [ "$SHARE_SPACE_ENV" == "dev" ]; then
npm start
else
npm run deployLocal
fi
}
run-Xvfb() {
xvfb-run \
--server-num=$XDISPLAY \
--auth-file=$XAUTHORITY \
--server-args="-screen 0 ${SCREEN_WIDTH}x${SCREEN_HEIGHT}x24" \
/etc/X11/Xsession cinnamon-session
}
run-x11vnc() {
local FB="-nofb"
if [ "$1" == "fb" ]; then
FB=""
fi
x11vnc \
-auth $XAUTHORITY \
-display :$XDISPLAY \
$FB \
-nevershared \
-loop \
-forever
}
run-noVNC-proxy() {
# Proxies websocket traffic to localhost-accessible vnc server
# The script will print a url at which the vnc server can be accessed.
# The URL should NOT be publicly accessible, as the vnc access is not
# secured. This is useful for local access/ssh port forwarding.
# noVNC URL: http://$GUEST_HOSTNAME:$NOVNC_PROXY_PORT/vnc.html?host=$GUEST_HOSTNAME&port=$NOVNC_PROXY_PORT
cd $NOVNC_SOURCE
./utils/launch.sh --listen $NOVNC_PROXY_PORT --vnc localhost:5900
}
run-screencap-stream() {
local INFO_URL=${1:-"unix://$INFO_SINK_SOCKET"}
local MEDIA_URL=${2:-"unix://$MEDIA_SINK_SOCKET"}
# The magic incantation
ffmpeg \
-v info \
-f pulse \
-channels 2 \
-thread_queue_size 500 \
-i default \
-f x11grab \
-probesize 32 \
-video_size ${SCREEN_WIDTH}x${SCREEN_HEIGHT} \
-framerate $FRAMERATE \
-i :0 \
-threads 0 \
-pix_fmt yuv420p \
-c:a libvorbis \
-c:v libvpx-vp9 \
-quality realtime \
-speed 8 \
-qmin 4 -qmax 48 \
-crf 15 \
-b:v 1000k \
-static-thresh 0 \
-max-intra-rate 300 \
-lag-in-frames 0 \
-error-resilient 1 \
-tile-columns 4 \
-frame-parallel 1 \
-row-mt 1 \
-f webm_streaming_chunk \
-info_url $INFO_URL \
$MEDIA_URL
}
### Command Line Interface
help() {
echo "Usage: $prog_name { $(join_by " | " ${CMDS[*]}) } [args ...]"
}
join_by() {
local d=$1;
shift;
echo -n "$1";
shift;
printf "%s" "${@/#/$d}"
}
CMDS=(stack-install \
stack-build \
stack-up \
stack-vnc-up \
stack-down \
stack-status \
install-node \
install-ffmpeg-deps \
install-gui \
install-nfs \
run-Xvfb \
run-x11vnc \
configure-ffmpeg \
build-ffmpeg \
run-screencap-stream \
build-frontend \
run-frontend \
run-noVNC-proxy \
install-ci \
test-ci \
build-site-ci \
help)
prog_name=$0
cmd=$1
shift || true
for option in ${CMDS[*]}; do
if [ "$cmd" == "$option" ]; then
$cmd $@
exit
fi
done
echo "Unrecognized command $cmd"
help
exit 1