---
- hosts: kittsim
  remote_user: zoox
  tasks:
  - name: Add zimbot AWS access key to credentials
    ini_file:
      dest: "~/.aws/credentials"
      section: "zimbot"
      option: aws_access_key_id
      value: "{{ lookup('hashi_vault', 'secret=secret/aws/s3_zimbot:access_key') }}"
      owner: zoox
      group: zoox
      mode: 0644
      state: present
    tags:
      - aws

  - name: Add zimbot AWS secret key to credentials
    ini_file:
      dest: "~/.aws/credentials"
      section: "zimbot"
      option: aws_secret_access_key
      value: "{{ lookup('hashi_vault', 'secret=secret/aws/s3_zimbot:secret_access_key') }}"
      owner: zoox
      group: zoox
      mode: 0644
      state: present
    tags:
      - aws

  - name: Install ZimBot / LogBot Integration Test Script
    template:
      src: templates/integration_test_TALOS.sh
      dest: /home/zoox/integration_test_TALOS.sh
      owner: zoox
      group: zoox
      mode: 0755
    tags:
      - integration_test

  - name: Install ZimBot / LogBot Integration Test Script (Legacy)
    template:
      src: templates/integration_test_LEGACY_SHIM.sh
      dest: /home/zoox/integration_test_LEGACY_SHIM.sh
      owner: zoox
      group: zoox
      mode: 0755
    tags:
      - integration_test

  - name: Give python executables right to bind to low ports
    command: setcap CAP_NET_BIND_SERVICE+epi {{item}}
    with_items:
      - /usr/bin/python2.7
      - /usr/bin/python3.4
      - /usr/bin/python3.6
    become: yes
    tags:
    - net_bind_perms

  - name: Copy Py3 requirements file
    copy:
      src: "templates/zimbot_requirements3.6.txt"
      dest: /tmp
    tags:
      - python3_deps
  - name: Install Python 3 dependencies
    pip:
      executable: pip3.6
      requirements: /tmp/zimbot_requirements3.6.txt
    become: yes
    tags:
      - python3_deps

- hosts: imaging_v2
  remote_user: zoox
  tasks:
  - name: Prepare overlay for customization
    command: mount -o remount,rw /overlay/customization
      warn=false
    become: yes
  - name: Prepare aws directory
    file:
      path=/overlay/customization/overlay/home/zoox/.aws/
      state=directory
  - name: Persist aws credentials in overlay
    copy:
      remote_src=yes
      mode=preserve
      src=/home/zoox/.aws/credentials
      dest=/overlay/customization/overlay/home/zoox/.aws/
  - name: Prepare bamboo directory
    file:
      path=/overlay/customization/overlay/home/zoox/bamboo-agent-home/
      state=directory
  - name: Persist bamboo configuration in overlay
    copy:
      remote_src=yes
      mode=preserve
      src=/home/zoox/bamboo-agent-home/bamboo-agent.cfg.xml
      dest=/overlay/customization/overlay/home/zoox/bamboo-agent-home/
  - name: Persist TALOS Integration Test Script in overlay
    copy:
      remote_src=yes
      mode=preserve
      src=/home/zoox/integration_test_TALOS.sh
      dest=/overlay/customization/overlay/home/zoox/
  - name: Persist SHIM Integration Test Script in overlay
    copy:
      remote_src=yes
      mode=preserve
      src=/home/zoox/integration_test_LEGACY_SHIM.sh
      dest=/overlay/customization/overlay/home/zoox/
  - name: Remove SIFT integration script
    file:
      state=absent
      path=/overlay/customization/overlay/home/zoox/integration_test_TEST_SIFT.sh
  - name: Remove old integration script
    file:
      state=absent
      path=/overlay/customization/overlay/home/zoox/integration_test.sh
  - name: Persist boot script in overlay
    template:
      src=templates/setup_v2_boot.sh
      dest=/overlay/customization/overlay/home/zoox/
      owner=zoox
      group=zoox
      mode=0755
  - name: Enable boot script in rc.local
    lineinfile:
      path=/etc/rc.local
      line='sudo -H -u zoox /home/zoox/setup_v2_boot.sh'
      insertbefore='^exit 0$'
    become: yes
  - name: Persist rc.local changes in overlay
    copy:
      remote_src=yes
      mode=preserve
      src=/etc/rc.local
      dest=/overlay/customization/overlay/etc/
    become: yes
  - name: Update limits.conf memlock hard limit to unlimited
    lineinfile:
      path=/etc/security/limits.conf
      line="zoox   hard       memlock unlimited"
    become: yes
  - name: Update limits.conf memlock soft limit to unlimited
    lineinfile:
      path=/etc/security/limits.conf
      line="zoox   soft       memlock unlimited"
    become: yes
  - name: Prepare etc/security directory
    file:
      path=/overlay/customization/overlay/etc/security/
      state=directory
    become: yes
  - name: Persist limits.conf in customization layer
    copy:
      remote_src=yes
      mode=preserve
      src=/etc/security/limits.conf
      dest=/overlay/customization/overlay/etc/security/limits.conf
    become: yes
  - name: Re-lock customization overlay
    command: mount -o remount,ro /overlay/customization
      warn=false
    become: yes
  - name: Clear lost+found directory
    # The driving repository is inside an lvm partition.
    # git clone will fail if the target directory isn't empty.
    file:
      path: /home/zoox/driving/lost+found
      state: absent
    become: yes
    tags:
      - setup_repos
  - name: Clone driving repo
    git:
      repo: git@git.zooxlabs.com:zooxco/driving.git
      dest: /home/zoox/driving
      version: "master"
      clone: yes
      update: yes
      force: no
    tags:
      - setup_repos
  - name: Clone calibration repo
    git:
      repo: git@git.zooxlabs.com:zooxco/calibration.git
      dest: /etc/zoox/calibration
      version: "master"
      clone: yes
      update: yes
      force: no
    tags:
      - setup_repos
