---

# Following task gathers facts and caches them so that subsequent tasks would avoid running
# setup task (from ansible) on demand resulting in consistent output.
- name: Gather facts
  hosts: all
  tasks: []
  tags:
   - facts
   - always

- name: Applying dev_directories
  hosts: dev
  roles:
    - { role: dev_directories, tags: [dev_directories,calibration,carta] }

# This task contains a check for unintentional setup script use; should be run as early as possible.
# Depends on dev_directories role to create /etc/zoox.
- name: Applying machine_role
  hosts: dev
  roles:
    - { role: machine_role, tags: [machine_role] }

- name: Remove systemd
  hosts: dev
  roles:
    - { role: systemd, tags: [systemd] }

- name: Check memory size
  hosts: all
  tasks:
    - fail:
        msg:
          - "hostvars check for 'ansible_memtotal_mb' in {{ inventory_hostname }} failed."
          - "hostvars[{{inventory_hostname}}] is {{ hostvars[inventory_hostname] }}"
      when: "'ansible_memtotal_mb' not in hostvars[inventory_hostname] or hostvars[inventory_hostname]['ansible_memtotal_mb'] < min_memory_requirement_in_mb|int"
  ignore_errors: true
  tags:
    - check_memory
    - always

- name: Applying driving_repo
  # See explanation for extra_driving_repo_host in setup_vehicle.py
  hosts: vision:{{ extra_driving_repo_host|default(omit) }}
  roles:
    - { role: driving_repo, tags: [driving_repo] }

- name: Applying apt_sources
  hosts: dev
  roles:
    - { role: apt_sources, tags: [apt_sources] }

- name: Applying autofs
  hosts: dev
  roles:
    - { role: autofs, tags: [autofs] }

- name: Applying utilities
  hosts: dev
  roles:
    - { role: utilities, tags: [utilities] }

- name: Applying python
  hosts: dev
  roles:
    - { role: python, tags: [python] }

- name: Applying python_pip
  hosts: dev
  roles:
    - { role: python_pip, tags: [python_pip] }

- name: Applying python_pip_conf
  hosts: dev
  roles:
    - { role: python_pip_conf, tags: [python_pip_conf] }

- name: Applying file limits
  hosts: dev
  roles:
    - { role: os-file-limits, tags: [file-limit] }

- name: Applying system_tools
  hosts: dev
  roles:
    - { role: system_tools, tags: [system_tools] }

- name: Applying compilers
  hosts: dev
  roles:
    - { role: compilers, tags: [compilers] }

- name: Applying libs
  hosts: dev
  roles:
    - { role: libs, tags: [libs] }

- name: Applying nvidia
  hosts: nvidia
  roles:
    - { role: nvidia, tags: [nvidia] }

- name: Write Chrony Config
  hosts: ai
  roles:
    - { role: chrony_server, tags: [chrony_server] }

- name: Linuxptp
  hosts: ai
  roles:
    - { role: linuxptp_configure, tags: [linuxptp_configure] }

- name: Linuxptp vision
  hosts: vision
  roles:
    - { role: linuxptp_configure_vision, tags: [linuxptp_configure_vision] }

- name: Linuxptp operator
  hosts: operator_group
  roles:
    - { role: linuxptp_configure_operator, tags: [linuxptp_configure_operator] }

- name: Applying video
  hosts: dev
  roles:
    - { role: video, tags: [video] }

- name: Applying utilities_x11
  hosts: dev
  roles:
    - { role: utilities_x11, tags: [utilities_x11] }

- name: Applying iperf3
  hosts: dev
  roles:
    - { role: iperf3, tags: [iperf3] }

- name: Applying python_deps
  hosts: dev
  roles:
    - { role: python_deps, tags: [python_deps] }

- name: Applying python_dev_deps
  hosts: dev
  roles:
    - { role: python_dev_deps, tags: [python_dev_deps] }

- name: Applying python_gdal
  hosts: dev
  roles:
    - { role: python_gdal, tags: [python_gdal] }

- name: Applying ros
  hosts: dev
  roles:
    - { role: ros, tags: [ros] }

- name: Applying vault-client
  hosts: dev
  roles:
    - { role: vault-client, tags: [vault-client] }

- name: Applying docker
  hosts: dev
  roles:
    - { role: docker, tags: [docker] }

- name: Applying nodejs
  hosts: dev
  roles:
    - { role: nodejs, tags: [nodejs] }

- name: Applying nodejs_deps
  hosts: dev
  roles:
    - { role: nodejs_deps, tags: [nodejs_deps] }

- name: Applying bazel
  hosts: dev
  roles:
    - { role: bazel, tags: [bazel] }

- name: Applying aws_credentials
  hosts: dev
  roles:
    - { role: aws_credentials, tags: [aws_credentials] }

- name: Applying labeling
  hosts: dev
  roles:
    - { role: labeling, tags: [labeling] }

- name: Applying clang_config
  hosts: dev
  roles:
    - { role: clang_config, tags: [clang_config] }

- name: Applying buildifier
  hosts: dev
  roles:
    - { role: buildifier, tags: [buildifier] }

- name: Applying buildozer
  hosts: dev
  roles:
    - { role: buildozer, tags: [buildozer] }

- name: Applying file_validators
  hosts: dev
  roles:
    - { role: file_validators, tags: [file_validators] }

- name: Applying rti_dds
  hosts: dev
  roles:
    - { role: rti_dds, tags: [rti_dds] }

- name: Applying can-isotp
  hosts: dev
  roles:
    - { role: can-isotp, tags: [can-isotp] }

- name: Applying docker_compose
  hosts: dev
  roles:
    - { role: docker_compose, tags: [docker_compose] }

- name: Applying hddtemp
  hosts: dev
  roles:
    - { role: hddtemp, tags: [hddtemp] }

- name: Applying terraform
  hosts: dev
  roles:
    - { role: terraform, tags: [terraform] }

- name: Applying packer
  hosts: dev
  roles:
    - { role: packer, tags: [packer] }

# Note: If you change tag for calibration then update `ANSIBLE_CALIBRATION_TAG` in setup_vehicle.py
- name: Applying calibration
  hosts: dev:vision:simulator_group:logplay_group:{{ extra_calibration_repo_host|default(omit) }}
  roles:
    - { role: calibration, tags: [calibration] }

- name: Applying git_lfs
  hosts: dev
  roles:
    - { role: git_lfs, tags: [git_lfs,carta] }

- name: Applying carta
  hosts: []  # We do not need to use the carta repo in production yet
  roles:
    - { role: carta, tags: [carta] }

- name: Applying data_cache
  hosts: dev
  roles:
    - { role: data_cache, tags: [data_cache], when: mount|bool }

- name: Install STLink Udev rules
  hosts: dev
  roles:
    - { role: stlink_rules, tags: [stlink_rules] }

- name: Configure updatedb
  hosts: dev
  roles:
    - { role: updatedb, tags: [updatedb] }

- name: Applying zaccess
  hosts: dev
  roles:
    - { role: zaccess, tags: [zaccess] }

- name: Restart gpsd
  hosts: ai
  roles:
    - { role: gpsd, tags: [gpsd] }

# TEMP https://jira.zooxlabs.com/browse/RSI-1075
- name: Purge logging directories
  hosts: ai:vision:operator_group:simulator_group:logplay_group
  roles:
    - { role: purge_logging_directories, tags: [purge_logging_directories] }

- name: Deploy Swopdash Lite
  hosts: swopdash_lite
  roles:
    - { role: swopdash_lite, tags: [swopdash_lite] }

- name: Generate Universal Config
  hosts: universal_config
  roles:
    - { role: get_hardware_config, tags: [get_hardware_config] }

- name: Active Directory Query
  hosts: built_ai
  roles:
    - { role: active_directory_query, tags: [active_directory_query] }

- name: Install bootstrap toggle
  hosts: proto_zb
  roles:
    - { role: bootstrap_toggle, tags: [bootstrap_toggle] }

# requires /opt and get_hardware_config to be deployed
- name: Configure CGroups
  hosts: built_ai:built_vision
  roles:
    - { role: cgroups, tags: [cgroups] }

- name: Restart zoox system services
  hosts: ai
  roles:
    - { role: restart_zoox_system_services, tags: [never, restart_zoox_system_services] }

# This role depends on the get_hardware_config role
- name: Configure cellular routing rules
  hosts: built_ai
  roles:
    - { role: cellular_routing, tags: [cellular_routing] }
