---
# This networking configuration is common between all 20.04 operator os platforms,
# like Operator Laptop and Ingest Kiosk.
- block:

  # /etc/resolv.conf is configured to use DNS configuration on the build host
  # while an image is being built. When the chroot environment is torn down,
  # /etc/resolv.conf.bak will be restored as the target OS's resolv.conf.
  # With this symlink, we're configuring systemd-networkd to be the source
  # of DNS resolution configuration.

  # We are also using systemd-resolved's stub resolv.conf, which will point to
  # DNS servers registered with systemd-resolved. This is because systemd-resolved
  # does not allow you to specify the order for DNS servers in
  # /run/systemd/resolve/resolv.conf, so for services that just use /etc/resolv.conf,
  # DNS resolution while on the VPN can cause issues.
  - name: Symlink /etc/resolv.conf to systemd-networkd
    file:
      state: link
      path: /etc/resolv.conf.bak
      src: /run/systemd/resolve/stub-resolv.conf
      owner: root
      group: root
      mode: '644'
      force: yes
      follow: no
    when: not is_live_update

  - name: Enable network-manager services
    zoox_systemctl:
      name: "{{ item }}"
      enabled: yes
      live: "{{ is_live_update }}"
    with_items:
      - network-manager.service

  - name: Configure nsswitch.conf
    copy:
      src: "nsswitch.conf"
      dest: "/etc/nsswitch.conf"
      owner: root
      group: root
      mode: '644'

  become: yes

# This configuration is specific to Ingest Kiosks
- block:

  - name: Install IK.1 systemd-networking configuration
    template:
      src: "10-eth-IK.1.network"
      dest: "/etc/systemd/network/10-eth.network"
      owner: root
      group: root
      mode: '644'
    notify: Restart systemd-networkd

  when: zoox_compute_version|default('') == "IK.1"
  become: yes

# This configuration is specific to Operator Laptops
- block:

  - name: Install OL.1 systemd-networking configuration
    template:
      src: "{{ item }}.network"
      dest: "/etc/systemd/network/{{ item }}.network"
      owner: root
      group: root
      mode: '644'
    with_items:
      - .10-robotnet-C3.2
      - .10-robotnet-C3.5
      - 10-imagingnet
      - 10-eth-OL.1
    notify: Restart systemd-networkd

  - name: Install networking README to desktop
    copy:
      src: README_networking.md
      dest: /home/zoox/Desktop/README_networking.md
      owner: zoox
      group: zoox
      mode: '444'

  - name: Install network dongle udev rules
    copy:
      src: 10-robot-networking.rules
      dest: /etc/udev/rules.d/10-robot-networking.rules
      owner: root
      group: root
      mode: '644'
    notify: Reload udev rules

  - name: Install DNS server
    apt:
      name:
        - bind9
      cache_valid_time: "{{ apt_cache_valid_time }}"

  - name: Apply C3.5 network config
    file:
      state: link
      path: /etc/systemd/network/10-robotnet.network
      src: /etc/systemd/network/.10-robotnet-C3.5.network
      owner: root
      group: root
      mode: '644'

  - name: Apply C3.5 hosts file
    file:
      state: link
      path: /etc/hosts
      src: /etc/hosts_C3.5
      owner: root
      group: root
      mode: '644'
      force: yes # The file we're linking to will not exist in the OS layer; it comes from the customization layer.

  - name: Install scripts to change between C3.2 and C3.5 networking config
    template:
      src: configure_for_compute_version.sh
      dest: "/usr/local/bin/configure_for_compute_{{ item }}"
      owner: root
      group: root
      mode: '755'
    with_items:
      - "3.2"
      - "3.5"

  - name: Configure DNS server
    template:
      src: bind-named.conf.options
      dest: /etc/bind/named.conf.options
      owner: root
      group: root
      mode: '644'
    notify: Restart DNS server

  - name: Configure DNS systemd service
    copy:
      src: named.service
      dest: /lib/systemd/system/named.service
      owner: root
      group: root
      mode: '755'
    notify:
      - Restart DNS server

  - name: Install hook to update DNS server configuration when upstream DNS changes
    copy:
      src: bind9
      dest: /etc/networkd-dispatcher/bind9
      owner: root
      group: root
      mode: '755'
    notify:
      - Restart DNS server
      - Restart networkd-dispatcher

  - name: Symlink DNS server update hook into place
    file:
      state: link
      path: "/etc/networkd-dispatcher/{{ item }}/bind9"
      src: /etc/networkd-dispatcher/bind9
      owner: root
      group: root
      mode: '755'
    with_items:
      - routable.d
      - off.d
    notify:
      - Restart DNS server
      - Restart networkd-dispatcher

  - name: Enable DNS service
    zoox_systemctl:
      name: "{{ item }}"
      enabled: yes
      live: "{{ is_live_update }}"
    with_items:
      - named.service

  - name: Configure Z.EXTERNAL Wi-Fi network
    copy:
      src: Z.EXTERNAL.nmconnection
      dest: /etc/NetworkManager/system-connections/Z.EXTERNAL.nmconnection
      owner: root
      group: root
      mode: '600'

  when: zoox_compute_version|default('') == "OL.1"
  become: yes
