-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup-code-server.sh
More file actions
108 lines (92 loc) · 3.62 KB
/
setup-code-server.sh
File metadata and controls
108 lines (92 loc) · 3.62 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
#!/usr/bin/env bash
# --- Self-heal CRLF issues(safe if not present)
[ -n "$(head -c 2048 "$0" | tr -d '\n' | grep $'\r')" ] && sed -i 's/\r$//' "$0"
# --- Auto-elevate to root to install deps; we’ll drop privileges to run code-server
if [ "${EUID:-$(id -u)}" -ne 0 ]; then
exec sudo -E bash "$0" "$@"
fi
set -euo pipefail
# ===== Config =====
USER_PASSWORD="${1:-password}"
REAL_USER="${SUDO_USER:-$(logname 2>/dev/null || echo ec2-user)}"
REAL_HOME="$(getent passwd "$REAL_USER" | cut -d: -f6)"
CONF_DIR="$REAL_HOME/.config/code-server"
PORT=8080
BIND_ADDR="0.0.0.0:${PORT}"
echo "Using user: $REAL_USER"
echo "Home dir : $REAL_HOME"
echo "Password : $USER_PASSWORD"
echo "Port : $PORT"
# ===== Detect package manager - To make things easier =====
PKG=""
if command -v dnf >/dev/null 2>&1; then PKG=dnf
elif command -v yum >/dev/null 2>&1; then PKG=yum
elif command -v apt-get >/dev/null 2>&1; then PKG=apt-get
else
echo "Unsupported system (need dnf/yum/apt-get)"; exit 1
fi
# ===== Base packages =====
if [ "$PKG" = "apt-get" ]; then
apt-get update -y
apt-get install -y curl wget tar git openssl
else
$PKG -y install wget tar git openssl
$PKG -y install --allowerasing curl || true
fi
# ===== Install Python 3 + pip (Additional)=====
echo "Installing Python 3 and pip..."
if [ "$PKG" = "apt-get" ]; then
DEBIAN_FRONTEND=noninteractive apt-get install -y python3 python3-pip python3-venv build-essential python3-dev
else
# Amazon Linux / RHEL-like
$PKG -y install python3 python3-pip || true
fi
# Make 'python' and 'pip' available
if ! command -v python >/dev/null 2>&1 && command -v python3 >/dev/null 2>&1; then
ln -sf "$(command -v python3)" /usr/local/bin/python
fi
if ! command -v pip >/dev/null 2>&1 && command -v pip3 >/dev/null 2>&1; then
ln -sf "$(command -v pip3)" /usr/local/bin/pip
fi
echo "Python version: $(python --version 2>/dev/null || python3 --version || echo 'not found')"
echo "pip version : $(pip --version 2>/dev/null || pip3 --version || echo 'not found')"
# ===== Install code-server =====
if ! command -v code-server >/dev/null 2>&1; then
curl -fsSL https://code-server.dev/install.sh | sh
fi
# ===== Stop/disable any systemd unit (we run foreground) =====
systemctl disable --now "code-server@${REAL_USER}" 2>/dev/null || true
systemctl disable --now code-server 2>/dev/null || true
pkill -f "code-server.*${PORT}" 2>/dev/null || true
# ===== Write config =====
install -o "$REAL_USER" -g "$REAL_USER" -d "$CONF_DIR"
cat >/tmp/config.yaml <<EOF
bind-addr: ${BIND_ADDR}
auth: password
password: ${USER_PASSWORD}
cert: false
EOF
chown "$REAL_USER:$REAL_USER" /tmp/config.yaml
mv /tmp/config.yaml "$CONF_DIR/config.yaml"
# ===== Bump inotify watchers (helpful for large workspaces) =====
SYSCTL_FILE="/etc/sysctl.d/60-code-server-inotify.conf"
echo "fs.inotify.max_user_watches=524288" > "$SYSCTL_FILE"
sysctl -p "$SYSCTL_FILE" >/dev/null || true
# ===== Show connection info =====
PUBLIC_IP="$(curl -s http://checkip.amazonaws.com || true)"
echo ""
echo "================================================================"
[ -n "$PUBLIC_IP" ] && echo "Open: http://${PUBLIC_IP}:${PORT}" || echo "Find your Public IP in EC2 console."
echo "Password: ${USER_PASSWORD}"
echo "Press Ctrl+C here to stop the server."
echo "================================================================"
echo ""
# ===== Run in foreground as the real user; Ctrl+C will stop it =====
cleanup() {
[ -n "${CS_PID:-}" ] && kill -INT "$CS_PID" 2>/dev/null || true
wait "$CS_PID" 2>/dev/null || true
}
trap cleanup INT TERM
sudo -u "$REAL_USER" -E env HOME="$REAL_HOME" /usr/bin/code-server --config "$CONF_DIR/config.yaml" &
CS_PID=$!
wait "$CS_PID"