---
# Setup Gitea Actions Runner
# This playbook sets up a Gitea Actions runner to process workflow jobs
# Run with: ansible-playbook -i hosts.ini playbooks/setup_gitea_runner.yml --limit homelab
#
# The Gitea API token is prompted at runtime and never stored in this file.
# Retrieve the token from Vaultwarden (collection: Homelab > Gitea API Tokens).

- name: Setup Gitea Actions Runner
  hosts: homelab
  become: yes
  vars:
    gitea_url: "https://git.vish.gg"
    runner_name: "homelab-runner"
    runner_labels: "ubuntu-latest,linux,x64"
    runner_dir: "/opt/gitea-runner"

  vars_prompt:
    - name: gitea_token
      prompt: "Enter Gitea API token (see Vaultwarden > Homelab > Gitea API Tokens)"
      private: yes

  tasks:
    - name: Create runner directory
      file:
        path: "{{ runner_dir }}"
        state: directory
        owner: root
        group: root
        mode: '0755'

    - name: Check if act_runner binary exists
      stat:
        path: "{{ runner_dir }}/act_runner"
      register: runner_binary

    - name: Download act_runner binary
      get_url:
        url: "https://dl.gitea.com/act_runner/0.2.6/act_runner-0.2.6-linux-amd64"
        dest: "{{ runner_dir }}/act_runner"
        mode: '0755'
        owner: root
        group: root
      when: not runner_binary.stat.exists

    - name: Get registration token from Gitea API
      uri:
        url: "{{ gitea_url }}/api/v1/repos/Vish/homelab-optimized/actions/runners/registration-token"
        method: GET
        headers:
          Authorization: "token {{ gitea_token }}"
        return_content: yes
      register: registration_response
      delegate_to: localhost
      run_once: true

    - name: Extract registration token
      set_fact:
        registration_token: "{{ registration_response.json.token }}"

    - name: Check if runner is already registered
      stat:
        path: "{{ runner_dir }}/.runner"
      register: runner_config

    - name: Register runner with Gitea
      shell: |
        cd {{ runner_dir }}
        echo "{{ gitea_url }}" | {{ runner_dir }}/act_runner register \
          --token {{ registration_token }} \
          --name {{ runner_name }} \
          --labels {{ runner_labels }} \
          --no-interactive
      when: not runner_config.stat.exists

    - name: Create systemd service file
      copy:
        content: |
          [Unit]
          Description=Gitea Actions Runner
          After=network.target

          [Service]
          Type=simple
          User=root
          WorkingDirectory={{ runner_dir }}
          ExecStart={{ runner_dir }}/act_runner daemon
          Restart=always
          RestartSec=5

          [Install]
          WantedBy=multi-user.target
        dest: /etc/systemd/system/gitea-runner.service
        owner: root
        group: root
        mode: '0644'

    - name: Reload systemd daemon
      systemd:
        daemon_reload: yes

    - name: Enable and start gitea-runner service
      systemd:
        name: gitea-runner
        enabled: yes
        state: started

    - name: Check runner status
      systemd:
        name: gitea-runner
      register: runner_status

    - name: Display runner status
      debug:
        msg: |
          Gitea Actions Runner Status:
          - Service: {{ runner_status.status.ActiveState }}
          - Directory: {{ runner_dir }}
          - Name: {{ runner_name }}
          - Labels: {{ runner_labels }}
          - Gitea URL: {{ gitea_url }}

    - name: Verify runner registration
      uri:
        url: "{{ gitea_url }}/api/v1/repos/Vish/homelab-optimized/actions/runners"
        method: GET
        headers:
          Authorization: "token {{ gitea_token }}"
        return_content: yes
      register: runners_list
      delegate_to: localhost
      run_once: true

    - name: Display registered runners
      debug:
        msg: |
          Registered Runners: {{ runners_list.json.total_count }}
          {% for runner in runners_list.json.runners %}
          - {{ runner.name }} ({{ runner.status }})
          {% endfor %}
