---
- name: "STRESS-SETUP: Sync Local Code"
  hosts: all
  gather_facts: no
  vars_files:
    - "group_vars/all/generated_vars.stress.yml"
    - "group_vars/all/vault.yml"
  tasks:
    - name: Define base directory for node
      ansible.builtin.set_fact:
        base_dir: "{{ airflow_master_dir if inventory_hostname in groups['airflow_master'] else airflow_worker_dir }}"

    - name: Ensure base directory exists for code sync
      ansible.builtin.file:
        path: "{{ base_dir }}"
        state: directory
        owner: "{{ ansible_user }}"
        group: "{{ deploy_group }}"
        mode: '0755'
      become: yes

    - name: Sync python packages and directories for stress testing
      ansible.posix.synchronize:
        src: "../ytops_client-source/{{ item }}/"
        dest: "{{ base_dir }}/{{ item }}/"
        rsync_opts:
          - "--delete"
          - "--exclude=.DS_Store"
          - "--exclude=__pycache__"
          - "--exclude='*.pyc'"
        recursive: yes
        perms: yes
      loop:
        - "thrift_model"
        - "pangramia"
        - "ytops_client"
        - "policies"
        - "bin"
        - "yt_ops_services"
      become: yes
      become_user: "{{ ansible_user }}"

    - name: Sync client utility scripts and configs
      ansible.posix.synchronize:
        src: "../ytops_client-source/{{ item }}"
        dest: "{{ base_dir }}/{{ item }}"
        perms: yes
      loop:
        - "cli.auth.config"
        - "cli.download.config"
        - "package_client.py"
        - "setup.py"
        - "VERSION"
        - "VERSION.client"
        - "thrift_exceptions_patch.py"
        - "ytdlp.json"
      become: yes
      become_user: "{{ ansible_user }}"
