# Hosts:
# * build_host uses a local shell and is run for all build variants
# * chroot uses chroot into the target rootfs for all build variants
# * ai, vision, and operator also use chroot but only run for their particular build variants

---
- name: Unarchive base OS
  hosts: build_host:&os
  tags:
    - create_base
  vars_files:
    - ../vars/ubuntu_14_04.yml
    - ../vars/robot_os.yml
  roles:
    - { role: create_loop_device, tags: create_loop_device }
    - { role: mount_loop, tags: mount_loop }
    - { role: unarchive_base, tags: unarchive_base }
    - { role: setup_chroot, tags: setup_chroot }

- name: Configuration common to all Zoox machine roles
  hosts: chroot:live:&os
  vars_files:
    - ../vars/ubuntu_14_04.yml
    - ../vars/robot_os.yml
  roles:
    - { role: apt_sources, tags: apt_sources }
    - { role: zoox_user, tags: zoox_user }
    - { role: ubuntu_desktop, tags: ubuntu_desktop }
    - { role: auditd, tags: auditd }
    - { role: ssh_dpkg_reconfigure, tags: ssh_dpkg_reconfigure, when: not is_live_update } # no live updates because this removes ssh host keys, which should be present in config layer
    - { role: secure_sshd, tags: secure_sshd }
    - { role: zoox_directories, tags: zoox_directories }
    - { role: buildifier, tags: buildifier }
    - { role: hddtemp, tags: hddtemp }
    - { role: compilers, tags: compilers }
    - { role: bazel, tags: bazel }
    - { role: system_tools, tags: system_tools }
    - { role: zoox_kernel, tags: zoox_kernel, when: not is_live_update }  # No live updates because the kernel is only loaded from the OS layer
    - { role: grub, tags: grub, when: not is_live_update }  # No live updates because grub is only loaded from the OS layer
    - { role: configure_logrotate, tags: configure_logrotate }
    - { role: fail_safe, tags: fail_safe }
    - { role: file_validators, tags: file_validators }
    - { role: fsck, tags: fsck }
    - { role: ftrace, tags: ftrace }
    - { role: heartbeat, tags: heartbeat }
    - { role: libs, tags: libs }
    - { role: linuxptp_install, tags: linuxptp_install }
    - { role: machine_role, tags: machine_role }
    - { role: nvidia, tags: nvidia, when: not is_live_update } # No live updates because upgrades require a reboot
    - { role: xdma_driver, tags: xdma_driver, when: not is_live_update } # MUST come after nvidia. See roles/xdma_driver/README.md. No live updates for now to reduce risk if Nvidia driver is not upgraded yet.
    - { role: robotnik, tags: robotnik }
    - { role: python, tags: python }
    - { role: python_pip, tags: python_pip }
    - { role: python_pip_conf, tags: python_pip_conf }
    - { role: python_deps, tags: python_deps }
    - { role: python_gdal, tags: python_gdal }
    - { role: rc, tags: rc }
    - { role: rc_local, tags: rc_local }
    - { role: ros, tags: ros }
    - { role: sudo, tags: sudo }
    - { role: timezone, tags: timezone }
    - { role: tpm_tools, tags: tpm_tools } # MUST come after rc and apt_sources.
    - { role: utilities, tags: utilities }
    - { role: utilities_diffmerge, tags: utilities_diffmerge }
    - { role: utilities_x11, tags: utilities_x11 }
    - { role: vault_client, tags: vault_client }
    - { role: video, tags: video }
    - { role: zoox_pki, tags: zoox_pki }
    - { role: overlayfs, tags: overlayfs, when: not is_live_update }  # No live updates because this is not compatible with KI-imaged vehicles.
    - { role: os_file_limits, tags: os_file_limits }
    - { role: os_memlock_limits, tags: os_memlock_limits }
    - { role: firefox_preferences, tags: firefox_preferences }
    - { role: xorg_conf, tags: xorg_conf }
    - { role: os, tags: os }  # TODO: modifies things under /sys
    - { role: core_dumps, tags: core_dumps }
    - { role: networking_conf_ifdown, tags: networking_conf_ifdown }
    - { role: configure_desktop, tags: configure_desktop }
    - { role: install_os_reset_script, tags: install_os_reset_script }
    - { role: zoox_os_build_metadata, asset_type: os, tags: zoox_os_build_metadata, when: not is_live_update }  # no live updates because this file represents the state of the OS layer.
    - { role: zoox_ipmi, tags: zoox_ipmi }

- name: AI configuration
  hosts: chroot:live:&os:&ai
  vars_files:
    - ../vars/ubuntu_14_04.yml
    - ../vars/robot_os.yml
  tags:
    - ai_configuration
  roles:
    - { role: network_ai, tags: network_ai, when: not is_live_update } # No live updates because this is not compatible with KI-imaged vehicles.
    - { role: disk_ai_vision, tags: disk_ai_vision, when: not is_live_update } # No live updates because this is not compatible with KI-imaged vehicles.
    - { role: framegrabber_udev, tags: framegrabber_udev }
    - { role: chrony_server, tags: chrony_server, when: not is_live_update }  # Changed in deploy based on universal config
    - { role: ai_hardware, tags: ai_hardware }
    - { role: vpnc, tags: vpnc }
    - { role: chum_config, tags: chum_config }
    - { role: disk_ops, tags: disk_ops }
    - { role: ethtool_nochecksum, tags: ethtool_nochecksum }
    - { role: iperf3, tags: iperf3 }
    - { role: vm, tags: vm }
    - { role: git_lfs, tags: git_lfs }
    - { role: can_isotp, tags: can_isotp }
    - { role: disable_lightdm, tags: disable_lightdm }
    - { role: install_zoox_system_services, tags: install_zoox_system_services }
    - { role: zstart, tags: zstart, when: not is_live_update }

- name: Vision configuration
  hosts: chroot:live:&os:&vision
  vars_files:
    - ../vars/ubuntu_14_04.yml
    - ../vars/robot_os.yml
  tags:
    - vision_configuration
  roles:
    - { role: network_vision, tags: network_vision, when: not is_live_update } # No live updates because this isn't compatible with KI-imaged vehicles.
    - { role: disk_ai_vision, tags: disk_ai_vision, when: not is_live_update }  # No live updates because this is not compatible with KI-imaged vehicles.
    - { role: framegrabber_udev, tags: framegrabber_udev }
    - { role: chrony_client, tags: chrony_client, when: not is_live_update }  # No live updates because the setup script handles chrony configuration
    - { role: disk_ops, tags: disk_ops }
    - { role: vm, tags: vm }
    - { role: disable_lightdm, tags: disable_lightdm }

- name: Operator configuration
  hosts: chroot:live:&os:&operator
  vars_files:
    - ../vars/ubuntu_14_04.yml
    - ../vars/robot_os.yml
  tags:
    - operator_configuration
  roles:
    - { role: disk_operator, tags: disk_operator, when: not is_live_update }  # No live updates because this is not compatible with KI-imaged vehicles.
    - { role: network_operator, tags: network_operator, when: not is_live_update } # No live updates because this isn't compatible with KI-imaged vehicles.
    - { role: chrony_client, tags: chrony_client, when: not is_live_update }  # No live updates because the setup script handles chrony configuration
    - { role: puffin, tags: puffin }
    - { role: zoom_client, tags: zoom_client }
    - { role: zstart, tags: zstart, when: not is_live_update }  # No live updates because we should use the version of zstart hosted at zstart.zooxlabs.com

- name: Simulator configuration
  hosts: chroot:live:&os:&simulator
  vars_files:
    - ../vars/ubuntu_14_04.yml
    - ../vars/robot_os.yml
  tags:
    - simulator_configuration
  roles:
    - { role: disk_simulator, tags: disk_simulator }
    - { role: network_simulator, tags: network_simulator }
    - { role: chrony_sim, tags: chrony_sim }

- name: Logplay configuration
  hosts: chroot:live:&os:&logplay
  vars_files:
    - ../vars/ubuntu_14_04.yml
    - ../vars/robot_os.yml
  tags:
    - logplay_configuration
  roles:
    - { role: disk_logplay, tags: disk_logplay }
    - { role: network_logplay, tags: network_logplay }
    - { role: chrony_client, tags: chrony_client, when: not is_live_update }  # No live updates because the setup script handles chrony configuration

- name: Teardown chroot
  hosts: build_host:&os
  tags:
    - teardown_mounts
  vars_files:
    - ../vars/ubuntu_14_04.yml
    - ../vars/robot_os.yml
  roles:
    - { role: teardown_chroot, tags: teardown_chroot }
    - { role: unmount_loop, tags: unmount_loop }

- name: Finalize image
  hosts: build_host:&os
  vars_files:
    - ../vars/ubuntu_14_04.yml
    - ../vars/robot_os.yml
  roles:
    - { role: gzip_image, tags: gzip_image }
