- block:

  - name: Disable cgroups daemon
    # Requires a reboot to take effect
    file:
      path: "/etc/init/cgred.conf"
      state: absent

  - name: Source /etc/network/interfaces.d/ahmo_interfaces
    # Note: don't source all of interfaces.d/, there are some default files we don't want
    lineinfile:
      path: "/etc/network/interfaces"
      line: "source /etc/network/interfaces.d/ahmo_interfaces"

  - name: Install ahmo interfaces file
    copy:
      src: ahmo_interfaces
      dest: "/etc/network/interfaces.d/"

  - name: Add sbates PPA
    apt_repository:
      repo: "deb [arch=amd64] http://apt-mirror.zooxlabs.com/mirror/ppa.launchpad.net/sbates/ppa/ubuntu trusty main"
      state: present
      filename: "sbates"

  - name: Install utilities
    apt:
      name:
        - ipmitool
        - nvme-cli  # From sbates ppa
        - stress-ng
        - snmp
      cache_valid_time: "{{ apt_cache_valid_time }}"
      allow_unauthenticated: yes  # For nvme-cli

  - name: Copy python requirements files
    copy:
      src: "{{ item }}"
      dest: /tmp
    with_items:
      - ahmo_requirements2.7.txt
      - ahmo_requirements3.6.txt

  - name: Install python packages
    pip:
      executable: "pip{{ item }}"
      requirements: "/tmp/ahmo_requirements{{ item }}.txt"
    with_items:
      - 2.7
      - 3.6

  become: yes

# This task relies on the build host's ssh-agent
- name: Clone zhat, framegrabber, driving, and calibration repositories
  git:
    repo: "{{ item.repo }}"
    dest: "{{ item.dest }}"
    version: "{{ item.version }}"
    accept_hostkey: yes
  with_items:
    - { repo: "git@git.zooxlabs.com:zooxco/zhat.git", dest: "/home/zoox/zhat", version: "user/jbouchard/compute_3_6" }
    - { repo: "git@git.zooxlabs.com:zooxco/framegrabber.git", dest: "/home/zoox/framegrabber", version: "master" }
    - { repo: "git@git.zooxlabs.com:zooxco/driving.git", dest: "/home/zoox/driving", version: "user/jbouchard/vh6b101_eol_{{ 'vision' if zoox_machine_role == 'vision' else 'main_ai' }}_config" }
    - { repo: "git@git.zooxlabs.com:zooxco/calibration.git", dest: "/etc/zoox/calibration", version: "master" }
  environment:
    SSH_AUTH_SOCK: "{{ lookup('env', 'SSH_AUTH_SOCK') }}"
    SSH_AGENT_PID: "{{ lookup('env', 'SSH_AGENT_PID') }}"
  # If running a live update, we're probably running from /home/zoox/driving
  when: not is_live_update or item.dest != "/home/zoox/driving"
  become_user: zoox
  become: yes

- name: Disable driving lvm mount
  # Otherwise, would have to make an lvm driving asset
  # Also, we want the ahmo driving checkout to be wiped by OS reset
  lineinfile:
    path: /etc/fstab
    regexp: /home/zoox/driving
    state: absent

- name: Create /etc/zoox/current_config/
  file:
    state: directory
    path: "/etc/zoox/current_config"
    owner: zoox
    group: zoox

- name: Populate /etc/zoox/current_config/vehicle.json
  command:
    cmd: "cp /home/zoox/driving/production/cameras/framegrabber_bringup/cammux_jig/config.json /etc/zoox/current_config/vehicle.json"
    creates: "/etc/zoox/current_config/vehicle.json"
  become_user: zoox
  become: yes

- name: Check out driving master
  git:
    repo: "git@git.zooxlabs.com:zooxco/driving.git"
    dest: "/home/zoox/driving"
    version: "master"
    accept_hostkey: yes
  environment:
    SSH_AUTH_SOCK: "{{ lookup('env', 'SSH_AUTH_SOCK') }}"
    SSH_AGENT_PID: "{{ lookup('env', 'SSH_AGENT_PID') }}"
  when: not is_live_update
  become_user: zoox
  become: yes

- name: Build gpu_power_test_sgemm
  command:
    cmd: "bazel --batch build experimental/paulius/gpu_power_test_sgemm --config=cuda"
    chdir: "/home/zoox/driving"
  become_user: zoox
  become: yes

- name: Remove iperf3
  apt:
    name: iperf3
    state: absent
    purge: yes
  become: yes

- name: Download iperf3.6 source
  get_url:
    url: "https://artifactory.zooxlabs.com/artifactory/RSI/imaging/iperf-3.6.tar.gz"
    dest: "/home/zoox/"
    checksum: "sha1:c2d3743d4db8728d29c61643c4338d6e5470a46b"
    owner: zoox
    group: zoox

- name: Unarchive iperf3.6 source
  unarchive:
    src: "/home/zoox/iperf-3.6.tar.gz"
    dest: "/home/zoox/"
    remote_src: yes
    owner: zoox
    group: zoox

- name: Build and install iperf3.6
  command:
    cmd: "{{ item }}"
    chdir: "/home/zoox/iperf-3.6/"
  with_items:
    - "./configure"
    - "make"
    - "sudo cp src/iperf3 /usr/local/bin/"
  become_user: zoox
  become: yes

- name: Download IPMICFG
  get_url:
    url: "https://artifactory.zooxlabs.com/artifactory/RSI/imaging/IPMICFG_1.31.1_build.200623.zip"
    dest: "/home/zoox/"
    checksum: "sha1:d9adbd210d6315b01d4174977afe90ffc88381aa"
    owner: zoox
    group: zoox

- name: Unarchive IPMICFG
  unarchive:
    src: "/home/zoox/IPMICFG_1.31.1_build.200623.zip"
    dest: "/home/zoox/"
    remote_src: yes
    owner: zoox
    group: zoox

- name: Copy OS metadata from OS layer
  command:
    cmd: "cp /etc/zoox/os_metadata.yml /etc/zoox/build_os_metadata.yml"
  become: yes
