- name: Setup gitlab runner on the Jumphost
  hosts: jumphost
  become: true
  ignore_errors: "{{ ansible_check_mode }}"
  vars: {}
  tasks:

    - name: ensure libguestfs-tools-c is installed
      dnf:
        name: libguestfs-tools-c
        state: present

    - name: ensure ssh key directory exists
      file:
        path: /root/.ssh
        state: directory
        mode: '0700'

    - name: generate ssh keypair
      openssh_keypair:
        path: /root/.ssh/id_rsa
        type: rsa
        size: 2048
        mode: '0600'
        force: no

    - name: set correct permissions for public key
      file:
        path: /root/.ssh/id_rsa.pub
        mode: '0644'

    - name: check if gitlab runner base image exists
      stat:
        path: /var/lib/libvirt/images/gitlab-runner-base.qcow2
      register: gitlab_runner_image

    - name: build gitlab runner base image with virt-builder
      command: >
        virt-builder debian-11
        --size 8G
        --output /var/lib/libvirt/images/gitlab-runner-base.qcow2
        --format qcow2
        --hostname gitlab-runner-bullseye
        --network
        --install curl
        --run-command 'curl -L "https://packages.gitlab.com/install/repositories/runner/gitlab-runner/script.deb.sh" | bash'
        --run-command 'curl -s "https://packagecloud.io/install/repositories/github/git-lfs/script.deb.sh" | bash'
        --run-command 'useradd -m -p "" gitlab-runner -s /bin/bash'
        --install gitlab-runner,git,git-lfs,openssh-server
        --run-command "git lfs install --skip-repo"
        --ssh-inject gitlab-runner:file:/root/.ssh/id_rsa.pub
        --run-command "echo 'gitlab-runner ALL=(ALL) NOPASSWD: ALL' >> /etc/sudoers"
        --run-command "sed -E 's/GRUB_CMDLINE_LINUX=\"\"/GRUB_CMDLINE_LINUX=\"net.ifnames=0 biosdevname=0\"/' -i /etc/default/grub"
        --run-command "grub-mkconfig -o /boot/grub/grub.cfg"
        --run-command "echo 'auto eth0' >> /etc/network/interfaces"
        --run-command "echo 'allow-hotplug eth0' >> /etc/network/interfaces"
        --run-command "echo 'iface eth0 inet dhcp' >> /etc/network/interfaces"
        --root-password password:{{ gitlab_runner_root_password }}
      when: not gitlab_runner_image.stat.exists
    
    - name: create the /opt/libvirt-driver/ directory
      file:
        path: /opt/libvirt-driver
        state: directory
        owner: root
        group: root
        mode: '0755'

    - name: copy base.sh script
      copy:
        src: base.sh
        dest: /opt/libvirt-driver/base.sh
        mode: '0755'
        owner: root
        group: root

    - name: copy prepare.sh script
      copy:
        src: prepare.sh
        dest: /opt/libvirt-driver/prepare.sh
        mode: '0755'
        owner: root
        group: root

    - name: copy run.sh script
      copy:
        src: run.sh
        dest: /opt/libvirt-driver/run.sh
        mode: '0755'
        owner: root
        group: root

    - name: copy cleanup.sh script
      copy:
        src: cleanup.sh
        dest: /opt/libvirt-driver/cleanup.sh
        mode: '0755'
        owner: root
        group: root

    - name: Download GitLab Runner install script
      get_url:
        url: https://packages.gitlab.com/install/repositories/runner/gitlab-runner/script.rpm.sh
        dest: /tmp/gitlab-runner-install.sh
        mode: '0755'

    - name: Run GitLab Runner install script
      command: /tmp/gitlab-runner-install.sh

    - name: Import GitLab Runner GPG key
      rpm_key:
        key: https://packages.gitlab.com/runner/gitlab-runner/gpgkey/runner-gitlab-runner-49F16C5CC3A0F81F.pub.gpg
        state: present

    - name: Install GitLab Runner
      dnf:
        name: gitlab-runner
        state: present

    - name: Register GitLab Runner non-interactively
      command: >
        gitlab-runner register
        --non-interactive
        --url "https://git.phoenix-systems.ch"
        --registration-token "{{ GITLAB_RUNNER_TOKEN }}" 
        --executor "custom"
        --description "jumphost-runner"
        --tag-list "kvant"
        --run-untagged "true"
        --locked "false"
        --config "/etc/gitlab-runner/config.toml"
        --custom-run-exec "/opt/libvirt-driver/run.sh"
        --custom-prepare-exec "/opt/libvirt-driver/prepare.sh"
        --custom-cleanup-exec "/opt/libvirt-driver/cleanup.sh"
        --builds-dir "/srv/builder/gitlab-runner/builds"
        --cache-dir "/srv/builder/gitlab-runner/cache"

    - name: Update existing gitlab-runner configuration
      blockinfile:
        path: /etc/gitlab-runner/config.toml
        block: |
          builds_dir = "/srv/builder/gitlab-runner/builds"
          [runners.cache]
            MaxUploadedArchiveSize = 0
          [runners.custom]
            prepare_exec = "/opt/libvirt-driver/prepare.sh"
            run_exec = "/opt/libvirt-driver/run.sh"
            cleanup_exec = "/opt/libvirt-driver/cleanup.sh"
        insertafter: 'executor = "custom"'
  
    - name: Start GitLab Runner if config exists
      command: gitlab-runner run --config /etc/gitlab-runner/config.toml
      async: 3600
      poll: 0
      tags: run_gitlab_runner
