---
- name: Harbor | Synchronize Harbor offline package to remote host
  copy:
    src: >-
      {{ .binary_dir }}/image-registry/harbor/{{ .image_registry.harbor_version }}/{{ .binary_type }}/harbor-offline-installer-{{ .image_registry.harbor_version }}.tgz
    dest: >-
      /opt/harbor/{{ .image_registry.harbor_version }}/harbor-offline-installer-{{ .image_registry.harbor_version }}.tgz

- name: Harbor | Extract Harbor offline package
  command: |
    cd /opt/harbor/{{ .image_registry.harbor_version }}/ && tar -zxvf harbor-offline-installer-{{ .image_registry.harbor_version }}.tgz

- name: Harbor | Synchronize image registry certificate to remote host
  copy:
    src: >-
      {{ .binary_dir }}/pki/image_registry.crt
    dest: >-
      /opt/harbor/{{ .image_registry.harbor_version }}/ssl/server.crt

- name: Harbor | Synchronize image registry private key to remote host
  copy:
    src: >-
      {{ .binary_dir }}/pki/image_registry.key
    dest: >-
      /opt/harbor/{{ .image_registry.harbor_version }}/ssl/server.key

- name: Harbor | Generate Harbor configuration file
  template:
    src: harbor.yml
    dest: >-
      /opt/harbor/{{ .image_registry.harbor_version }}/harbor/harbor.yml

- name: Harbor | Install Harbor registry
  command: |
    cd /opt/harbor/{{ .image_registry.harbor_version }}/harbor && /bin/bash install.sh

- name: Harbor | Register Harbor as a systemd service
  template:
    src: harbor.service
    dest: /etc/systemd/system/harbor.service

- name: Harbor | Start and enable Harbor service
  command: systemctl daemon-reload && systemctl start harbor.service && systemctl enable harbor.service

- name: Harbor | Configure HA and synchronize Harbor images
  when:
    - .image_registry.ha_vip | empty | not
    - .groups.image_registry | len | lt 1
  block:
    - name: Harbor | Add keepalived service to Harbor docker-compose
      command: |
        KEEPALIVED_SERVICE='# keepalived is generated by kubekey.
          keepalived:
            image: osixia/keepalived:{{ .keepalived_version }}
            container_name: keepalived
            command:
              - --copy-service
            network_mode: "host"
            restart: always
            dns_search: .
            cap_drop:
              - ALL
            cap_add:
              - NET_ADMIN
              - NET_BROADCAST
              - NET_RAW
              - CHOWN
              - DAC_OVERRIDE
            depends_on:
              - proxy
            volumes:
              - type: bind
                source: /opt/keepalived/{{ .keepalived_version }}/keepalived.conf
                target: /container/service/keepalived/assets/keepalived.conf
              - type: bind
                source: /opt/keepalived/{{ .keepalived_version }}/healthcheck.sh
                target: /etc/keepalived/healthcheck.sh'
        TARGET_FILE="/opt/harbor/{{ .image_registry.harbor_version }}/harbor/docker-compose.yml"
        TMP_FILE="/opt/harbor/{{ .image_registry.harbor_version }}/harbor/docker-compose.yml.tmp"
        awk -v service="$KEEPALIVED_SERVICE" '
          /^services:/ {
            print
            print service
            next
          }
          { print }
        ' "$TARGET_FILE" > "$TMP_FILE" && mv "$TMP_FILE" "$TARGET_FILE"
        systemctl restart harbor.service
    - name: Harbor | Wait for Harbor service to become ready
      command: |
        if ! timeout 300 bash -c 'while ! nc -zv localhost 443; do sleep 2; done'; then
            echo "ERROR: Harbor did not start within 5 minutes!"
            exit 1
        fi
    - name: Harbor | Synchronize harbor-replications script to remote host
      template:
        src: harbor-replications.sh
        dest: /opt/harbor/scripts/harbor-replications.sh
        mode: 0755
    - name: Harbor | Execute harbor-replications script
      command: bash /opt/harbor/scripts/harbor-replications.sh