Skip to content

Commit 4a3121c

Browse files
chapterjasonclaude
andcommitted
Run web-shell as a systemd service instead of a coder_script
Bakes web-shell.service + web-shell-launch into the base image; Terraform uploads /etc/default/web-shell with the WEB_SHELL_CWD parameter so the unit picks up the chosen working directory. Gains auto-restart on crash and consistent logging to /var/log/web-shell.log. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent 94ba9de commit 4a3121c

4 files changed

Lines changed: 48 additions & 20 deletions

File tree

main.tf

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -194,25 +194,6 @@ resource "coder_app" "web-shell" {
194194
}
195195
}
196196

197-
# Start web-shell on agent start. web-shell lives in the user's nvm default
198-
# node bin, so load nvm and activate the default alias to put it on PATH.
199-
resource "coder_script" "web_shell" {
200-
count = data.coder_workspace.me.start_count
201-
agent_id = coder_agent.main.id
202-
display_name = "web-shell"
203-
icon = "/icon/terminal.svg"
204-
run_on_start = true
205-
script = <<-EOT
206-
#!/usr/bin/env bash
207-
set -e
208-
export NVM_DIR="$HOME/.nvm"
209-
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh" && nvm use default >/dev/null 2>&1 || true
210-
HOST=127.0.0.1 PORT=4000 WEB_SHELL_CWD="${data.coder_parameter.directory.value}" \
211-
nohup web-shell > /tmp/web-shell.log 2>&1 &
212-
disown >/dev/null 2>&1 || true
213-
EOT
214-
}
215-
216197
# See https://registry.coder.com/modules/coder/jetbrains
217198
module "jetbrains" {
218199
count = data.coder_workspace.me.start_count
@@ -453,6 +434,14 @@ resource "docker_container" "workspace" {
453434
content = replace(coder_agent.main.init_script, "/localhost|127\\.0\\.0\\.1/", "host.docker.internal")
454435
}
455436

437+
# Config for web-shell.service (baked into the image). The unit reads this
438+
# via EnvironmentFile; changing the `directory` parameter re-renders it and
439+
# the unit picks it up on next start.
440+
upload {
441+
file = "/etc/default/web-shell"
442+
content = "WEB_SHELL_CWD=${data.coder_parameter.directory.value}\n"
443+
}
444+
456445
host {
457446
host = "host.docker.internal"
458447
ip = "host-gateway"

src/base/Dockerfile

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,11 @@ USER root
9898
# time by the Terraform template (kreuzwerker/docker `upload` block).
9999
RUN mkdir -p /etc/coder
100100
COPY src/base/coder-agent.service /etc/systemd/system/coder-agent.service
101-
RUN systemctl enable coder-agent.service
101+
COPY src/base/web-shell.service /etc/systemd/system/web-shell.service
102+
COPY --chmod=0755 src/base/web-shell-launch.sh /usr/local/bin/web-shell-launch
103+
RUN install -m 0644 /dev/null /var/log/web-shell.log && \
104+
chown coder:coder /var/log/web-shell.log && \
105+
systemctl enable coder-agent.service web-shell.service
102106

103107
# Entrypoint claims fresh-volume mountpoints for the workspace user before
104108
# systemd starts. See entrypoint.sh for rationale.

src/base/web-shell-launch.sh

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/usr/bin/env bash
2+
# web-shell launcher. web-shell is installed under the workspace user's nvm
3+
# default, so nvm must be sourced before the binary is on PATH. Runs as the
4+
# workspace user under web-shell.service.
5+
set -e
6+
7+
export NVM_DIR="$HOME/.nvm"
8+
if [ -s "$NVM_DIR/nvm.sh" ]; then
9+
# shellcheck disable=SC1091
10+
. "$NVM_DIR/nvm.sh"
11+
nvm use default >/dev/null 2>&1 || true
12+
fi
13+
14+
exec web-shell

src/base/web-shell.service

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
[Unit]
2+
Description=web-shell
3+
After=network-online.target
4+
Wants=network-online.target
5+
6+
[Service]
7+
Type=simple
8+
User=coder
9+
Group=coder
10+
WorkingDirectory=/home/coder
11+
EnvironmentFile=-/etc/default/web-shell
12+
Environment=HOST=127.0.0.1
13+
Environment=PORT=4000
14+
ExecStart=/usr/local/bin/web-shell-launch
15+
Restart=on-failure
16+
RestartSec=5
17+
StandardOutput=append:/var/log/web-shell.log
18+
StandardError=append:/var/log/web-shell.log
19+
20+
[Install]
21+
WantedBy=multi-user.target

0 commit comments

Comments
 (0)