tenfyzhong commited on
Commit
8bcf0e1
·
unverified ·
1 Parent(s): 0e229f4

chore: replace root runtime usage with ubuntu user (#5)

Browse files
Dockerfile CHANGED
@@ -23,15 +23,29 @@ RUN apt-get update && \
23
  tar && \
24
  rm -rf /var/lib/apt/lists/*
25
 
26
- RUN curl -fsSL https://openclaw.ai/install.sh | bash -s -- --no-onboard --version "${OPENCLAW_VERSION}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27
 
28
  RUN /bin/bash -lc 'set -euo pipefail; \
29
  OPENCLAW_BIN="$(command -v openclaw || true)"; \
30
- if [[ -z "$OPENCLAW_BIN" && -x /root/.npm-global/bin/openclaw ]]; then \
31
- OPENCLAW_BIN=/root/.npm-global/bin/openclaw; \
32
  fi; \
33
- if [[ -z "$OPENCLAW_BIN" && -x /root/.local/bin/openclaw ]]; then \
34
- OPENCLAW_BIN=/root/.local/bin/openclaw; \
35
  fi; \
36
  if [[ -z "$OPENCLAW_BIN" ]]; then \
37
  echo "openclaw command not found after installer" >&2; \
@@ -42,21 +56,10 @@ RUN /bin/bash -lc 'set -euo pipefail; \
42
 
43
  RUN python3 -m pip install --no-cache-dir --break-system-packages "huggingface_hub[cli]>=0.31.1"
44
 
45
- RUN curl -sSf https://sshx.io/get | sh && \
46
- if [ -x /root/.local/bin/sshx ]; then ln -sf /root/.local/bin/sshx /usr/local/bin/sshx; fi && \
47
  sshx --help >/dev/null
48
 
49
- RUN if ! getent group ubuntu >/dev/null; then \
50
- groupadd --system ubuntu || groupadd ubuntu; \
51
- fi && \
52
- if ! id -u ubuntu >/dev/null 2>&1; then \
53
- useradd --system --create-home --gid ubuntu --shell /bin/bash ubuntu || \
54
- useradd --create-home --gid ubuntu --shell /bin/bash ubuntu; \
55
- fi && \
56
- mkdir -p /etc/sudoers.d && \
57
- printf '%s\n' 'ubuntu ALL=(ALL) NOPASSWD:ALL' > /etc/sudoers.d/90-ubuntu-nopasswd && \
58
- chmod 0440 /etc/sudoers.d/90-ubuntu-nopasswd
59
-
60
  WORKDIR /opt/openclaw-hf
61
 
62
  COPY openclaw_hf /opt/openclaw-hf/openclaw_hf
 
23
  tar && \
24
  rm -rf /var/lib/apt/lists/*
25
 
26
+ RUN if ! getent group ubuntu >/dev/null; then \
27
+ groupadd --system ubuntu || groupadd ubuntu; \
28
+ fi && \
29
+ if ! id -u ubuntu >/dev/null 2>&1; then \
30
+ useradd --system --create-home --gid ubuntu --shell /bin/bash ubuntu || \
31
+ useradd --create-home --gid ubuntu --shell /bin/bash ubuntu; \
32
+ fi && \
33
+ mkdir -p /etc/sudoers.d && \
34
+ printf '%s\n' 'ubuntu ALL=(ALL) NOPASSWD:ALL' > /etc/sudoers.d/90-ubuntu-nopasswd && \
35
+ chmod 0440 /etc/sudoers.d/90-ubuntu-nopasswd
36
+
37
+ RUN HOME=/home/ubuntu gosu ubuntu /bin/bash -lc \
38
+ "curl -fsSL https://openclaw.ai/install.sh | bash -s -- --no-onboard --version \"${OPENCLAW_VERSION}\""
39
+
40
+ USER ubuntu
41
 
42
  RUN /bin/bash -lc 'set -euo pipefail; \
43
  OPENCLAW_BIN="$(command -v openclaw || true)"; \
44
+ if [[ -z "$OPENCLAW_BIN" && -x /home/ubuntu/.npm-global/bin/openclaw ]]; then \
45
+ OPENCLAW_BIN=/home/ubuntu/.npm-global/bin/openclaw; \
46
  fi; \
47
+ if [[ -z "$OPENCLAW_BIN" && -x /home/ubuntu/.local/bin/openclaw ]]; then \
48
+ OPENCLAW_BIN=/home/ubuntu/.local/bin/openclaw; \
49
  fi; \
50
  if [[ -z "$OPENCLAW_BIN" ]]; then \
51
  echo "openclaw command not found after installer" >&2; \
 
56
 
57
  RUN python3 -m pip install --no-cache-dir --break-system-packages "huggingface_hub[cli]>=0.31.1"
58
 
59
+ RUN HOME=/home/ubuntu gosu ubuntu /bin/bash -lc "curl -sSf https://sshx.io/get | sh" && \
60
+ if [ -x /home/ubuntu/.local/bin/sshx ]; then ln -sf /home/ubuntu/.local/bin/sshx /usr/local/bin/sshx; fi && \
61
  sshx --help >/dev/null
62
 
 
 
 
 
 
 
 
 
 
 
 
63
  WORKDIR /opt/openclaw-hf
64
 
65
  COPY openclaw_hf /opt/openclaw-hf/openclaw_hf
README.md CHANGED
@@ -17,7 +17,7 @@ This setup is designed to provide the following:
17
  - Use third-party OpenAI-compatible `base_url + api_key` by default (injected via environment variables)
18
  - Store OpenClaw config/workspace under `/home/ubuntu/.openclaw`
19
  - Restore state automatically from a Hugging Face Dataset on startup
20
- - Run scheduled backups of OpenClaw data to a Hugging Face Dataset via `cron`
21
  - Preinstall `sshx` in the image so you can open a shareable terminal session from inside the container
22
 
23
  ## Repository Layout
@@ -57,10 +57,12 @@ In that case, you can still configure from inside the container (for example via
57
  - `OPENCLAW_LLM_PROVIDER` (default: `thirdparty`)
58
  - `OPENCLAW_LLM_API` (default: `openai-completions`)
59
  - `OPENCLAW_STATE_DIR` (default: `/home/ubuntu/.openclaw`)
 
 
60
  - `OPENCLAW_CONFIG_PATH` (default: `/home/ubuntu/.openclaw/openclaw.json`)
61
  - `OPENCLAW_WORKSPACE_DIR` (default: `/home/ubuntu/.openclaw/workspace`)
62
  - `OPENCLAW_BACKUP_CRON` (default: `*/30 * * * *`, backup every 30 minutes)
63
- - `OPENCLAW_BACKUP_SOURCE_DIR` (default: `/home/ubuntu`, backup/restore root directory)
64
  - `OPENCLAW_BACKUP_PATH_PREFIX` (default: `backups`)
65
  - `OPENCLAW_BACKUP_LATEST_NAME` (default: `latest-backup.tar.gz`)
66
  - `OPENCLAW_SSHX_AUTO_START` (default: `false`; set `true` to auto-run `sshx` in background on startup)
 
17
  - Use third-party OpenAI-compatible `base_url + api_key` by default (injected via environment variables)
18
  - Store OpenClaw config/workspace under `/home/ubuntu/.openclaw`
19
  - Restore state automatically from a Hugging Face Dataset on startup
20
+ - Run scheduled backups of OpenClaw data to a Hugging Face Dataset via `cron` (as `ubuntu` user)
21
  - Preinstall `sshx` in the image so you can open a shareable terminal session from inside the container
22
 
23
  ## Repository Layout
 
57
  - `OPENCLAW_LLM_PROVIDER` (default: `thirdparty`)
58
  - `OPENCLAW_LLM_API` (default: `openai-completions`)
59
  - `OPENCLAW_STATE_DIR` (default: `/home/ubuntu/.openclaw`)
60
+ - `OPENCLAW_USER` (default: `ubuntu`, runtime user for gateway and cron jobs)
61
+ - `OPENCLAW_GROUP` (default: `ubuntu`, runtime group for gateway and cron jobs)
62
  - `OPENCLAW_CONFIG_PATH` (default: `/home/ubuntu/.openclaw/openclaw.json`)
63
  - `OPENCLAW_WORKSPACE_DIR` (default: `/home/ubuntu/.openclaw/workspace`)
64
  - `OPENCLAW_BACKUP_CRON` (default: `*/30 * * * *`, backup every 30 minutes)
65
+ - `OPENCLAW_BACKUP_SOURCE_DIR` (default: `/home/ubuntu`, backup/restore base directory)
66
  - `OPENCLAW_BACKUP_PATH_PREFIX` (default: `backups`)
67
  - `OPENCLAW_BACKUP_LATEST_NAME` (default: `latest-backup.tar.gz`)
68
  - `OPENCLAW_SSHX_AUTO_START` (default: `false`; set `true` to auto-run `sshx` in background on startup)
scripts/openclaw-entrypoint.sh CHANGED
@@ -304,7 +304,8 @@ write_backup_env_file() {
304
  for key in "${keys[@]}"; do
305
  printf '%s=%q\n' "$key" "${!key:-}" >> /etc/openclaw-backup.env
306
  done
307
- chmod 600 /etc/openclaw-backup.env
 
308
  }
309
 
310
  setup_backup_cron() {
@@ -322,10 +323,11 @@ setup_backup_cron() {
322
  cat > /etc/cron.d/openclaw-backup <<EOFCRON
323
  SHELL=/bin/bash
324
  PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
325
- ${OPENCLAW_BACKUP_CRON} root /usr/local/bin/openclaw-backup-cron.sh >> /var/log/openclaw/backup.log 2>&1
326
  EOFCRON
327
  chmod 0644 /etc/cron.d/openclaw-backup
328
  touch /var/log/openclaw/backup.log
 
329
 
330
  if pgrep -x cron >/dev/null 2>&1; then
331
  return 0
 
304
  for key in "${keys[@]}"; do
305
  printf '%s=%q\n' "$key" "${!key:-}" >> /etc/openclaw-backup.env
306
  done
307
+ chown "$OPENCLAW_USER:$OPENCLAW_GROUP" /etc/openclaw-backup.env
308
+ chmod 640 /etc/openclaw-backup.env
309
  }
310
 
311
  setup_backup_cron() {
 
323
  cat > /etc/cron.d/openclaw-backup <<EOFCRON
324
  SHELL=/bin/bash
325
  PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
326
+ ${OPENCLAW_BACKUP_CRON} ${OPENCLAW_USER} /usr/local/bin/openclaw-backup-cron.sh >> /var/log/openclaw/backup.log 2>&1
327
  EOFCRON
328
  chmod 0644 /etc/cron.d/openclaw-backup
329
  touch /var/log/openclaw/backup.log
330
+ chown "$OPENCLAW_USER:$OPENCLAW_GROUP" /var/log/openclaw/backup.log
331
 
332
  if pgrep -x cron >/dev/null 2>&1; then
333
  return 0
tests/test_runtime_user_defaults.py ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import unittest
2
+ from pathlib import Path
3
+
4
+
5
+ class RuntimeUserDefaultsTests(unittest.TestCase):
6
+ @staticmethod
7
+ def _repo_root() -> Path:
8
+ return Path(__file__).resolve().parents[1]
9
+
10
+ @classmethod
11
+ def _dockerfile_path(cls) -> Path:
12
+ return cls._repo_root() / "Dockerfile"
13
+
14
+ @classmethod
15
+ def _entrypoint_path(cls) -> Path:
16
+ return cls._repo_root() / "scripts" / "openclaw-entrypoint.sh"
17
+
18
+ def test_dockerfile_does_not_reference_root_home(self):
19
+ dockerfile = self._dockerfile_path().read_text(encoding="utf-8")
20
+ self.assertNotIn("/root/", dockerfile)
21
+
22
+ def test_backup_cron_uses_openclaw_user(self):
23
+ entrypoint = self._entrypoint_path().read_text(encoding="utf-8")
24
+ self.assertIn(
25
+ '${OPENCLAW_BACKUP_CRON} ${OPENCLAW_USER} /usr/local/bin/openclaw-backup-cron.sh',
26
+ entrypoint,
27
+ )
28
+ self.assertNotIn(
29
+ "${OPENCLAW_BACKUP_CRON} root /usr/local/bin/openclaw-backup-cron.sh",
30
+ entrypoint,
31
+ )
32
+
33
+
34
+ if __name__ == "__main__":
35
+ unittest.main()