calmodovar23 commited on
Commit
739bd50
·
verified ·
1 Parent(s): 63e753b

Upload 2 files

Browse files
scripts/10-init-persistent-data.sh CHANGED
@@ -1,6 +1,25 @@
1
  #!/bin/sh
2
  set -eu
3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
  uid="$(id -u)"
5
  gid="$(id -g)"
6
 
@@ -14,74 +33,139 @@ ensure_dir() {
14
  if [ ! -w "${directory}" ]; then
15
  sudo chown "${uid}:${gid}" "${directory}"
16
  fi
 
 
 
 
 
17
  }
18
 
19
  for directory in \
20
- /data/workspace \
21
- /data/code-server \
22
- /data/code-server/user-data \
23
- /data/code-server/extensions \
24
- /data/claude \
25
- /data/ngrok \
26
- /data/cache/uv \
27
- /data/cache/huggingface
28
  do
29
  ensure_dir "${directory}"
30
  done
31
 
32
- # Initialize a brand-new persistent workspace exactly once.
33
- if [ -z "$(find /data/workspace -mindepth 1 -maxdepth 1 -print -quit)" ]; then
34
- echo "Initializing persistent workspace at /data/workspace"
35
- tar -C /data/workspace -xzf /opt/bootstrap/workspace-seed.tar.gz
 
 
 
 
 
36
  fi
37
 
38
- # Restore the prebuilt Python 3.12 environment when it is absent or broken.
39
- if ! /data/workspace/.venv/bin/python -c \
 
 
40
  'import sys; raise SystemExit(sys.version_info[:2] != (3, 12))' \
41
  >/dev/null 2>&1
42
  then
43
- echo "Initializing Python 3.12 environment at /data/workspace/.venv"
44
- rm -rf /data/workspace/.venv
45
- tar -C /data/workspace -xzf /opt/bootstrap/venv.tar.gz
46
  fi
47
 
48
- # Add image-provided extensions without deleting extensions already installed.
49
- cp -an /opt/bootstrap/extensions/. /data/code-server/extensions/
 
 
 
 
 
 
 
 
 
 
50
 
51
- # Keep the requested IDE path while storing the actual files on /data.
52
- if [ -e /home/coder/workspace ] && [ ! -L /home/coder/workspace ]; then
53
- rm -rf /home/coder/workspace
54
  fi
55
 
56
- if [ -L /home/coder/workspace ]; then
57
- current_target="$(readlink /home/coder/workspace || true)"
58
- if [ "${current_target}" != "/data/workspace" ]; then
59
- rm -f /home/coder/workspace
60
- fi
 
61
  fi
62
 
63
- if [ ! -L /home/coder/workspace ]; then
64
- ln -s /data/workspace /home/coder/workspace
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
65
  fi
66
 
67
- # ngrok uses ~/.config/ngrok/ngrok.yml on Linux. Redirect that directory to
68
- # persistent storage so `ngrok config add-authtoken ...` survives restarts.
69
- mkdir -p /home/coder/.config
 
70
 
71
- if [ -e /home/coder/.config/ngrok ] && [ ! -L /home/coder/.config/ngrok ]; then
72
- if [ -d /home/coder/.config/ngrok ]; then
73
- cp -an /home/coder/.config/ngrok/. /data/ngrok/
74
- fi
75
- rm -rf /home/coder/.config/ngrok
76
  fi
77
 
78
- if [ -L /home/coder/.config/ngrok ]; then
79
- current_target="$(readlink /home/coder/.config/ngrok || true)"
80
- if [ "${current_target}" != "/data/ngrok" ]; then
81
- rm -f /home/coder/.config/ngrok
 
 
 
82
  fi
 
 
 
 
 
83
  fi
84
 
85
- if [ ! -L /home/coder/.config/ngrok ]; then
86
- ln -s /data/ngrok /home/coder/.config/ngrok
87
  fi
 
 
 
 
 
 
 
 
 
 
1
  #!/bin/sh
2
  set -eu
3
 
4
+ log() {
5
+ printf '%s\n' "[hf-code-server-init] $*"
6
+ }
7
+
8
+ DATA_ROOT="${DATA_ROOT:-/data}"
9
+ CODER_HOME="${CODER_HOME:-/home/coder}"
10
+ BOOTSTRAP_ROOT="${BOOTSTRAP_ROOT:-/opt/bootstrap}"
11
+ CODE_SERVER_BIN="${CODE_SERVER_BIN:-/usr/bin/code-server}"
12
+
13
+ WORKSPACE_DATA="${DATA_ROOT}/workspace"
14
+ WORKSPACE_LINK="${WORKSPACE:-${CODER_HOME}/workspace}"
15
+ CODE_SERVER_ROOT="${DATA_ROOT}/code-server"
16
+ USER_DATA_DIR="${CODE_SERVER_USER_DATA:-${CODE_SERVER_ROOT}/user-data}"
17
+ EXTENSIONS_DIR="${CODE_SERVER_EXTENSIONS:-${CODE_SERVER_ROOT}/extensions}"
18
+ CLAUDE_DIR="${DATA_ROOT}/claude"
19
+ NGROK_DIR="${DATA_ROOT}/ngrok"
20
+ UV_CACHE_DIR_RUNTIME="${DATA_ROOT}/cache/uv"
21
+ HF_CACHE_DIR="${DATA_ROOT}/cache/huggingface"
22
+
23
  uid="$(id -u)"
24
  gid="$(id -g)"
25
 
 
33
  if [ ! -w "${directory}" ]; then
34
  sudo chown "${uid}:${gid}" "${directory}"
35
  fi
36
+
37
+ if [ ! -w "${directory}" ]; then
38
+ log "ERROR: ${directory} is not writable by uid ${uid}"
39
+ exit 1
40
+ fi
41
  }
42
 
43
  for directory in \
44
+ "${WORKSPACE_DATA}" \
45
+ "${CODE_SERVER_ROOT}" \
46
+ "${USER_DATA_DIR}" \
47
+ "${EXTENSIONS_DIR}" \
48
+ "${CLAUDE_DIR}" \
49
+ "${NGROK_DIR}" \
50
+ "${UV_CACHE_DIR_RUNTIME}" \
51
+ "${HF_CACHE_DIR}"
52
  do
53
  ensure_dir "${directory}"
54
  done
55
 
56
+ # Add image-seeded files that are missing, but never overwrite work already on
57
+ # the persistent disk. The marker avoids repeating this on normal restarts.
58
+ seed_marker="${WORKSPACE_DATA}/.hf-code-server-seed-v4"
59
+ if [ ! -f "${seed_marker}" ]; then
60
+ log "Seeding persistent workspace at ${WORKSPACE_DATA}"
61
+ tar --skip-old-files \
62
+ -C "${WORKSPACE_DATA}" \
63
+ -xzf "${BOOTSTRAP_ROOT}/workspace-seed.tar.gz"
64
+ touch "${seed_marker}"
65
  fi
66
 
67
+ # Restore the prebuilt Python 3.12 virtual environment when it is absent or
68
+ # invalid. It was built at /home/coder/workspace/.venv, so script shebangs keep
69
+ # working through the runtime workspace symlink.
70
+ if ! "${WORKSPACE_DATA}/.venv/bin/python" -c \
71
  'import sys; raise SystemExit(sys.version_info[:2] != (3, 12))' \
72
  >/dev/null 2>&1
73
  then
74
+ log "Restoring Python 3.12 virtual environment"
75
+ rm -rf "${WORKSPACE_DATA}/.venv"
76
+ tar -C "${WORKSPACE_DATA}" -xzf "${BOOTSTRAP_ROOT}/venv.tar.gz"
77
  fi
78
 
79
+ # Make the requested IDE-visible path point to persistent storage.
80
+ if [ -L "${WORKSPACE_LINK}" ]; then
81
+ current_target="$(readlink "${WORKSPACE_LINK}" || true)"
82
+ if [ "${current_target}" != "${WORKSPACE_DATA}" ]; then
83
+ rm -f "${WORKSPACE_LINK}"
84
+ fi
85
+ elif [ -e "${WORKSPACE_LINK}" ]; then
86
+ if [ -d "${WORKSPACE_LINK}" ]; then
87
+ cp -a -n "${WORKSPACE_LINK}/." "${WORKSPACE_DATA}/" 2>/dev/null || true
88
+ fi
89
+ rm -rf "${WORKSPACE_LINK}"
90
+ fi
91
 
92
+ if [ ! -L "${WORKSPACE_LINK}" ]; then
93
+ ln -s "${WORKSPACE_DATA}" "${WORKSPACE_LINK}"
 
94
  fi
95
 
96
+ # Seed both the extension payload directory and any user-data metadata created
97
+ # by code-server's installer. Existing persistent files are left untouched.
98
+ if [ -d "${BOOTSTRAP_ROOT}/code-server-user-data" ] \
99
+ && [ -z "$(find "${USER_DATA_DIR}" -mindepth 1 -maxdepth 1 -print -quit)" ]
100
+ then
101
+ cp -a "${BOOTSTRAP_ROOT}/code-server-user-data/." "${USER_DATA_DIR}/"
102
  fi
103
 
104
+ cp -a -n "${BOOTSTRAP_ROOT}/extensions/." "${EXTENSIONS_DIR}/"
105
+
106
+ list_extensions() {
107
+ "${CODE_SERVER_BIN}" \
108
+ --user-data-dir "${USER_DATA_DIR}" \
109
+ --extensions-dir "${EXTENSIONS_DIR}" \
110
+ --list-extensions 2>/dev/null || true
111
+ }
112
+
113
+ has_extension() {
114
+ extension_id="$1"
115
+ printf '%s\n' "${installed_extensions}" \
116
+ | grep -Fxi "${extension_id}" >/dev/null 2>&1
117
+ }
118
+
119
+ installed_extensions="$(list_extensions)"
120
+
121
+ # Copying extension directories is usually enough, but an old persistent
122
+ # extension registry can hide them. Let code-server repair its own registry if
123
+ # either required extension is not visible.
124
+ if ! has_extension 'Anthropic.claude-code' || ! has_extension 'ms-python.python'; then
125
+ log "Repairing required extensions in ${EXTENSIONS_DIR}"
126
+ "${CODE_SERVER_BIN}" \
127
+ --user-data-dir "${USER_DATA_DIR}" \
128
+ --extensions-dir "${EXTENSIONS_DIR}" \
129
+ --force \
130
+ --install-extension Anthropic.claude-code \
131
+ --install-extension ms-python.python
132
+ installed_extensions="$(list_extensions)"
133
  fi
134
 
135
+ if ! has_extension 'Anthropic.claude-code'; then
136
+ log "ERROR: Anthropic.claude-code is unavailable after initialization"
137
+ exit 1
138
+ fi
139
 
140
+ if ! has_extension 'ms-python.python'; then
141
+ log "ERROR: ms-python.python is unavailable after initialization"
142
+ exit 1
 
 
143
  fi
144
 
145
+ # Persist ngrok's default Linux configuration directory.
146
+ mkdir -p "${CODER_HOME}/.config"
147
+
148
+ if [ -L "${CODER_HOME}/.config/ngrok" ]; then
149
+ current_target="$(readlink "${CODER_HOME}/.config/ngrok" || true)"
150
+ if [ "${current_target}" != "${NGROK_DIR}" ]; then
151
+ rm -f "${CODER_HOME}/.config/ngrok"
152
  fi
153
+ elif [ -e "${CODER_HOME}/.config/ngrok" ]; then
154
+ if [ -d "${CODER_HOME}/.config/ngrok" ]; then
155
+ cp -a -n "${CODER_HOME}/.config/ngrok/." "${NGROK_DIR}/" 2>/dev/null || true
156
+ fi
157
+ rm -rf "${CODER_HOME}/.config/ngrok"
158
  fi
159
 
160
+ if [ ! -L "${CODER_HOME}/.config/ngrok" ]; then
161
+ ln -s "${NGROK_DIR}" "${CODER_HOME}/.config/ngrok"
162
  fi
163
+
164
+ # These checks are fatal because the startup wrapper calls this script directly.
165
+ test -d "${WORKSPACE_LINK}"
166
+ test -x "${WORKSPACE_LINK}/.venv/bin/python"
167
+ test -f "${WORKSPACE_LINK}/pyproject.toml"
168
+
169
+ log "Workspace ready: ${WORKSPACE_LINK} -> $(readlink -f "${WORKSPACE_LINK}")"
170
+ log "Python ready: $("${WORKSPACE_LINK}/.venv/bin/python" --version 2>&1)"
171
+ log "Extensions ready: $(printf '%s' "${installed_extensions}" | tr '\n' ' ')"
scripts/start-code-server.sh CHANGED
@@ -1,18 +1,55 @@
1
  #!/bin/sh
2
  set -eu
3
 
4
- # Runtime-only paths: Hugging Face mounts /data after the image is built.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
  export PORT="${PORT:-7860}"
 
 
 
 
 
 
 
 
6
  export UV_CACHE_DIR="${UV_CACHE_DIR:-/data/cache/uv}"
7
  export HF_HOME="${HF_HOME:-/data/cache/huggingface}"
8
  export CLAUDE_CONFIG_DIR="${CLAUDE_CONFIG_DIR:-/data/claude}"
9
- export CODE_SERVER_CONFIG="${CODE_SERVER_CONFIG:-/data/code-server/config.yaml}"
10
 
11
- exec /usr/bin/entrypoint.sh \
 
 
 
 
 
 
 
 
12
  --bind-addr "0.0.0.0:${PORT}" \
13
  --auth password \
14
  --config "${CODE_SERVER_CONFIG}" \
15
- --user-data-dir /data/code-server/user-data \
16
- --extensions-dir /data/code-server/extensions \
17
  --disable-telemetry \
18
- /home/coder/workspace
 
 
1
  #!/bin/sh
2
  set -eu
3
 
4
+ FIXUID_BIN="${FIXUID_BIN:-fixuid}"
5
+ INIT_BIN="${INIT_BIN:-/usr/local/bin/init-persistent-data}"
6
+ DUMB_INIT_BIN="${DUMB_INIT_BIN:-/usr/bin/dumb-init}"
7
+ CODE_SERVER_BIN="${CODE_SERVER_BIN:-/usr/bin/code-server}"
8
+
9
+ # Keep the UID-repair behavior used by the official image.
10
+ eval "$("${FIXUID_BIN}" -q)"
11
+
12
+ # Keep the official image's optional user-renaming behavior.
13
+ if [ "${DOCKER_USER-}" ]; then
14
+ USER="${DOCKER_USER}"
15
+ export USER
16
+
17
+ if [ -z "$(id -u "${DOCKER_USER}" 2>/dev/null)" ]; then
18
+ echo "${DOCKER_USER} ALL=(ALL) NOPASSWD:ALL" \
19
+ | sudo tee -a /etc/sudoers.d/nopasswd >/dev/null
20
+ sudo usermod --login "${DOCKER_USER}" coder
21
+ sudo groupmod -n "${DOCKER_USER}" coder
22
+ sudo sed -i '/coder/d' /etc/sudoers.d/nopasswd
23
+ fi
24
+ fi
25
+
26
  export PORT="${PORT:-7860}"
27
+ export WORKSPACE="${WORKSPACE:-/home/coder/workspace}"
28
+ export DATA_ROOT="${DATA_ROOT:-/data}"
29
+ export CODER_HOME="${CODER_HOME:-/home/coder}"
30
+ export BOOTSTRAP_ROOT="${BOOTSTRAP_ROOT:-/opt/bootstrap}"
31
+ export CODE_SERVER_BIN
32
+ export CODE_SERVER_CONFIG="${CODE_SERVER_CONFIG:-/data/code-server/config.yaml}"
33
+ export CODE_SERVER_USER_DATA="${CODE_SERVER_USER_DATA:-/data/code-server/user-data}"
34
+ export CODE_SERVER_EXTENSIONS="${CODE_SERVER_EXTENSIONS:-/data/code-server/extensions}"
35
  export UV_CACHE_DIR="${UV_CACHE_DIR:-/data/cache/uv}"
36
  export HF_HOME="${HF_HOME:-/data/cache/huggingface}"
37
  export CLAUDE_CONFIG_DIR="${CLAUDE_CONFIG_DIR:-/data/claude}"
 
38
 
39
+ # Run initialization directly. A broken workspace, virtual environment, or
40
+ # extension install now prevents code-server from starting in a partial state.
41
+ "${INIT_BIN}"
42
+
43
+ # This makes the code-server process and integrated terminals start from the
44
+ # requested workspace, in addition to passing it as the folder to open.
45
+ cd "${WORKSPACE}"
46
+
47
+ exec "${DUMB_INIT_BIN}" "${CODE_SERVER_BIN}" \
48
  --bind-addr "0.0.0.0:${PORT}" \
49
  --auth password \
50
  --config "${CODE_SERVER_CONFIG}" \
51
+ --user-data-dir "${CODE_SERVER_USER_DATA}" \
52
+ --extensions-dir "${CODE_SERVER_EXTENSIONS}" \
53
  --disable-telemetry \
54
+ --disable-update-check \
55
+ "${WORKSPACE}"