From 477b52c516ea06d4a413aa52d31693075e8bd531 Mon Sep 17 00:00:00 2001 From: Haitao Pan Date: Sun, 28 Jun 2026 15:31:54 +0800 Subject: [PATCH] fix(acp_server_opencode): detect opencode CLI at deploy time (portable across Debian/Ubuntu/macOS) (#22) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Stop assuming a fixed opencode path. Probe the real binary with 'command -v' using the role PATH, then feed the resolved path to both the systemd unit and the launchd plist (plist now also passes -opencode-bin). Falls back to the OS-aware default when opencode is not yet installed. Also remove the dead acp-bridge.service.j2 template: it was not deployed by any task and referenced two undefined vars (acp_opencode_bridge_disabled_binary_path, acp_opencode_bridge_opencode_binary_path) — a hardcoding landmine. Co-authored-by: Haitao Pan Co-authored-by: Claude Opus 4.8 --- .../acp_server_opencode/tasks/config.yml | 23 ++++++++++++++++ .../templates/acp-bridge.service.j2 | 27 ------------------- .../templates/opencode.plist.j2 | 1 + 3 files changed, 24 insertions(+), 27 deletions(-) delete mode 100644 roles/vhosts/acp_server_opencode/templates/acp-bridge.service.j2 diff --git a/roles/vhosts/acp_server_opencode/tasks/config.yml b/roles/vhosts/acp_server_opencode/tasks/config.yml index 368f031..d4b992e 100644 --- a/roles/vhosts/acp_server_opencode/tasks/config.yml +++ b/roles/vhosts/acp_server_opencode/tasks/config.yml @@ -79,6 +79,29 @@ - "{{ acp_opencode_home }}/.local" - "{{ acp_opencode_workdir }}" +# Resolve the OpenCode CLI at deploy time instead of assuming a fixed path. +# npm installs it wherever the active node prefix points (NodeSource -> /usr/bin +# on Debian/Ubuntu, ~/.local/bin or Homebrew on macOS), so probe the real path +# with the role PATH and fall back to the OS-aware default when not yet present. +- name: Resolve OpenCode CLI binary path + ansible.builtin.shell: | + set -eu + export PATH="{{ acp_opencode_path }}:${PATH}" + command -v opencode || true + args: + executable: /bin/bash + register: acp_opencode_resolved_bin + changed_when: false + +- name: Use resolved OpenCode CLI binary path when present + ansible.builtin.set_fact: + acp_opencode_binary_path: "{{ acp_opencode_resolved_bin.stdout_lines[0] | trim }}" + when: acp_opencode_resolved_bin.stdout | default('') | trim | length > 0 + +- name: Report effective OpenCode CLI binary path + ansible.builtin.debug: + msg: "OpenCode CLI binary resolved to: {{ acp_opencode_binary_path }}" + - name: Deploy Caddy main file ansible.builtin.template: src: Caddyfile.j2 diff --git a/roles/vhosts/acp_server_opencode/templates/acp-bridge.service.j2 b/roles/vhosts/acp_server_opencode/templates/acp-bridge.service.j2 deleted file mode 100644 index 293a963..0000000 --- a/roles/vhosts/acp_server_opencode/templates/acp-bridge.service.j2 +++ /dev/null @@ -1,27 +0,0 @@ -[Unit] -Description=XWorkmate OpenCode ACP bridge server -After=network-online.target {{ acp_opencode_service_name }}.service -Wants=network-online.target - -[Service] -Type=simple -User={{ acp_opencode_service_user }} -Group={{ acp_opencode_service_group }} -WorkingDirectory={{ acp_opencode_workdir }} -Environment=HOME={{ acp_opencode_home }} -Environment=TERM=xterm-256color -Environment=ACP_LISTEN_ADDR={{ acp_opencode_bridge_listen_host }}:{{ acp_opencode_bridge_listen_port }} -Environment=ACP_ALLOWED_ORIGINS={{ acp_opencode_bridge_allowed_origins | join(',') }} -{% if acp_opencode_auth_token | trim | length > 0 %} -Environment=ACP_AUTH_TOKEN={{ acp_opencode_auth_token }} -{% endif %} -Environment=ACP_CODEX_BIN={{ acp_opencode_bridge_disabled_binary_path }} -Environment=ACP_CLAUDE_BIN={{ acp_opencode_bridge_disabled_binary_path }} -Environment=ACP_GEMINI_BIN={{ acp_opencode_bridge_disabled_binary_path }} -Environment=ACP_OPENCODE_BIN={{ acp_opencode_bridge_opencode_binary_path }} -ExecStart={{ acp_opencode_bridge_binary_path }} serve --listen {{ acp_opencode_bridge_listen_host }}:{{ acp_opencode_bridge_listen_port }} -Restart=always -RestartSec=2 - -[Install] -WantedBy=multi-user.target diff --git a/roles/vhosts/acp_server_opencode/templates/opencode.plist.j2 b/roles/vhosts/acp_server_opencode/templates/opencode.plist.j2 index 716ead6..3a4997f 100644 --- a/roles/vhosts/acp_server_opencode/templates/opencode.plist.j2 +++ b/roles/vhosts/acp_server_opencode/templates/opencode.plist.j2 @@ -17,6 +17,7 @@ exec "{{ acp_opencode_bridge_binary_path }}" adapter opencode \ -listen {{ acp_opencode_listen_host }}:{{ acp_opencode_listen_port }} \ + -opencode-bin "{{ acp_opencode_binary_path }}" \ -cwd "{{ acp_opencode_workdir }}"