---

- block:

  # RSI-1973: Set up a backwards compatibility path as the operator
  # machine migrates to PTP on compute 3.2 and 3.5 by disabling ptp4l and
  # phc2sys, and enabling chrony.
  - name: Compute 3.2 and 3.5 configuration
    block:
      - name: Disable ptp4l
        service:
          name: ptp4l
          enabled: false
          state: stopped

      # Sanity check for an installed chrony service.
      - name: Stat chrony service
        stat:
          path: "/etc/chrony/chrony.conf"
        register: chrony_conf

      - name: Check chrony service
        fail:
          msg: "Chrony service is not installed."
        when: not chrony_conf.stat.exists

      - name: Start chrony service
        service:
          name: chrony
          enabled: yes
          state: started

    when: zoox_compute_version is version("3.5", "<=")

  - name: Compute 3.6 configuration
    block:
    - name: Disable chrony
      service:
        name: chrony
        enabled: false
        state: stopped

    - name: Configure linuxptp
      copy:
        src: "ptp"
        dest: "/etc/default/ptp"
        owner: root
        group: root
        mode: '644'

    - name: Configure ptp4l
      block:
        - copy:
            src: "ptp4l_1588v2.conf"
            dest: "/etc/linuxptp/ptp4l.conf"
            owner: root
            group: root
            mode: '644'
          notify:
            - "Restart ptp4l"
        - copy:
            src: "ptp4l.init"
            dest: "/etc/init/ptp4l.conf"
            owner: root
            group: root
            mode: '644'
          notify:
            - "Restart ptp4l"
        - service:
            name: "ptp4l"
            enabled: yes

    when: zoox_compute_version is version("3.6", ">=")

  # phc2sys gets created when the ptp4l package is installed
  # Since we use software timestamping on operator, we remove this to prevent
  # this from conflicting with ptp4l
  - name: Remove phc2sys
    block:
      - name: Stat phc2sys service
        stat:
          path: "/etc/init/phc2sys.conf"
        register: phc2sys_stat
      - name: Stop phc2sys service
        service:
          name: "phc2sys"
          state: stopped
        when: phc2sys_stat.stat.exists
      - name: Remove phc2sys.conf
        file:
          dest: "/etc/init/phc2sys.conf"
          state: absent

  become: yes
