-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode-server.sh
More file actions
executable file
·68 lines (59 loc) · 2.07 KB
/
code-server.sh
File metadata and controls
executable file
·68 lines (59 loc) · 2.07 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
#!/bin/bash
#
# launch an instance of Microsoft Visual Studio Code for the web
# expects environment variables: IDLE, HOME, USER, PASSWORD
#
# originally authored by Robert Hurst <rhurst@bilh.org> on 6-Mar-2025
#
sudo -v || exit
[ -n "$IDLE" ] || IDLE=3600
let idle=$IDLE
pname=/usr/lib/code-server/lib/node
# if PREPARE requested but a session is already running ...
if pgrep -c -f $pname -U $USER &> /dev/null ; then
PORT=`pgrep -a sudo | grep 'code-server' | grep " ${USER} " | awk -FPORT= '{ print $2 }' | awk '{ print $1 }'`
[ -n "$PORT" ] && sudo pkill -f $pname -U $USER && sleep 2
PORT=
fi
for pool in `seq 4 -1 1`; do
let port=( 6500+pool )
let count=`ss -Hl src :$port | wc -l`
(( count == 0 )) && PORT=$port
done
[ -n "$PORT" ] || exit 1
echo "PORT=$PORT"
CS=${HOME}/.local/share/code-server
HB=${CS}/heartbeat
DEVOPS=${CS}/User/Workspaces/${USER}-devops.code-workspace
# redirect this new developer session to shared space
if [ ! -d "/files/.code-server${CS}" ]; then
sudo su - $USER -c "mkdir -p /files/.code-server${CS}"
ln -sf "/files/.code-server${CS}" "${CS}"
fi
if [ ! -f "${DEVOPS}" ]; then
sudo su - $USER -c "mkdir -p ${CS}/User/Workspaces"
sudo su - $USER -c "cp '/files/.code-server/BILH HCIE DevOps.code-workspace' ${DEVOPS}"
sudo su - $USER -c "cp '/files/.code-server/settings.json' ${CS}/User/"
fi
sudo su - $USER -c "PASSWORD=$PASSWORD PORT=$PORT code-server \
-an 'EDI DevOps' --i18n /files/.code-server/custom.json \
--auth password --disable-telemetry --disable-update-check \
--idle-timeout-seconds $IDLE \
--extensions-dir /files/.code-server/extensions" &
echo "`date` Launched ... "
sleep 3
sudo pgrep -c -f $pname -U $USER &> /dev/null || exit
echo -n "... and waiting until it goes idle after $idle-seconds"
touch "${HB}"
while (( alive < idle )); do
now=$( date +%s )
pulse=$( sudo date +%s -r "${HB}" )
let alive=( now-pulse )
echo -n "."
sleep 200
sudo pgrep -c -f $pname -U $USER &> /dev/null || break
done
echo
echo -n "`date` Shutting down ... "
sudo pkill -f $pname -U $USER && echo "success" || echo "failure!"
reset &> /dev/null