playbooks/roles/vhosts/acp_server_opencode/tasks/config.yml
Haitao Pan 477b52c516
fix(acp_server_opencode): detect opencode CLI at deploy time (portable across Debian/Ubuntu/macOS) (#22)
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 <manbuzhe2009@qq.com>
Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-28 15:31:54 +08:00

188 lines
6.1 KiB
YAML

---
- name: Ensure local OpenCode ACP build directory exists
ansible.builtin.file:
path: "{{ acp_opencode_bridge_local_build_dir }}"
state: directory
mode: "0755"
delegate_to: localhost
become: false
when: not (acp_opencode_bridge_use_prebuilt | bool)
- name: Build XWorkmate Go ACP server locally for OpenCode
ansible.builtin.command:
cmd: go build -o "{{ acp_opencode_bridge_local_binary_path }}" .
chdir: "{{ acp_opencode_bridge_local_source_dir }}"
environment:
GOOS: "{{ acp_opencode_bridge_build_goos }}"
GOARCH: "{{ acp_opencode_bridge_build_goarch }}"
CGO_ENABLED: "0"
delegate_to: localhost
become: false
check_mode: false
when: not (acp_opencode_bridge_use_prebuilt | bool)
- name: Inspect OpenCode bridge binary attributes
ansible.builtin.command:
cmd: lsattr "{{ acp_opencode_bridge_binary_path }}"
register: acp_opencode_bridge_binary_attrs
changed_when: false
failed_when: false
- name: Remove immutable flag from OpenCode bridge binary when present
ansible.builtin.command:
cmd: chattr -i "{{ acp_opencode_bridge_binary_path }}"
when:
- ansible_os_family != 'Darwin'
- "'i' in (acp_opencode_bridge_binary_attrs.stdout | default(''))"
changed_when: true
become: true
- name: Download XWorkmate Bridge release archive for OpenCode
ansible.builtin.get_url:
url: "https://github.com/ai-workspace-lab/xworkmate-bridge/releases/latest/download/xworkmate-bridge-{{ ansible_system | lower }}-{{ 'amd64' if ansible_architecture in ['x86_64', 'amd64'] else 'arm64' }}.tar.gz"
dest: "/tmp/xworkmate-bridge-opencode.tar.gz"
mode: "0644"
when: not (acp_opencode_bridge_use_prebuilt | bool)
- name: Extract XWorkmate Go ACP server binary for OpenCode
ansible.builtin.unarchive:
src: "/tmp/xworkmate-bridge-opencode.tar.gz"
dest: "{{ acp_opencode_bridge_binary_path | dirname }}"
remote_src: true
extra_opts:
- "--strip-components=2"
- "xworkmate-bridge/bin/xworkmate-go-core"
owner: "{{ omit if ansible_os_family == 'Darwin' else 'root' }}"
group: "{{ omit if ansible_os_family == 'Darwin' else 'root' }}"
mode: "0755"
become: true
when: not (acp_opencode_bridge_use_prebuilt | bool)
- name: Restore immutable flag on OpenCode bridge binary
ansible.builtin.command:
cmd: chattr +i "{{ acp_opencode_bridge_binary_path }}"
when:
- ansible_os_family != 'Darwin'
- "'i' in (acp_opencode_bridge_binary_attrs.stdout | default(''))"
changed_when: true
become: true
- name: Ensure OpenCode runtime directories exist
ansible.builtin.file:
path: "{{ item }}"
state: directory
owner: "{{ acp_opencode_service_user }}"
group: "{{ acp_opencode_service_group }}"
mode: "0755"
loop:
- "{{ acp_opencode_home }}"
- "{{ 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
dest: "{{ acp_opencode_caddyfile_path }}"
owner: root
group: root
mode: "0644"
notify: Reload caddy
when:
- acp_opencode_manage_caddy | bool
become: true
- name: Remove deprecated standalone OpenCode Caddy fragments
ansible.builtin.file:
path: "{{ item }}"
state: absent
loop: "{{ acp_opencode_obsolete_caddy_fragment_paths }}"
notify: Reload caddy
become: true
- name: Deploy OpenCode ACP systemd service
ansible.builtin.command:
cmd: lsattr "/etc/systemd/system/{{ acp_opencode_service_name }}.service"
register: acp_opencode_service_attrs
changed_when: false
failed_when: false
when: ansible_os_family != 'Darwin'
- name: Remove immutable flag from OpenCode ACP systemd service when present
ansible.builtin.command:
cmd: chattr -i "/etc/systemd/system/{{ acp_opencode_service_name }}.service"
when:
- ansible_os_family != 'Darwin'
- "'i' in (acp_opencode_service_attrs.stdout | default(''))"
changed_when: true
become: true
- name: Deploy OpenCode ACP systemd service
ansible.builtin.template:
src: opencode-acp.service.j2
dest: "/etc/systemd/system/{{ acp_opencode_service_name }}.service"
owner: root
group: root
mode: "0644"
notify: Restart acp opencode
when: ansible_os_family != 'Darwin'
- name: Restore immutable flag on OpenCode ACP systemd service
ansible.builtin.command:
cmd: chattr +i "/etc/systemd/system/{{ acp_opencode_service_name }}.service"
when:
- ansible_os_family != 'Darwin'
- "'i' in (acp_opencode_service_attrs.stdout | default(''))"
changed_when: true
become: true
- name: Reload systemd manager configuration
ansible.builtin.systemd:
daemon_reload: true
when: ansible_os_family != 'Darwin'
- name: Ensure Caddy is enabled and running
ansible.builtin.systemd:
name: caddy
enabled: true
state: started
when:
- ansible_os_family != 'Darwin'
- acp_opencode_manage_caddy | bool
become: true
- name: Ensure OpenCode ACP service is enabled and running
ansible.builtin.systemd:
name: "{{ acp_opencode_service_name }}"
enabled: true
state: started
when:
- not ansible_check_mode
- ansible_os_family != 'Darwin'
- name: Import macOS specific OpenCode ACP tasks
ansible.builtin.import_tasks: macos.yml
when: ansible_os_family == 'Darwin'