# Finalize ingest kiosk account setup.
# Runs after installation of bazel and checkout of driving

- set_fact:
    ingest_style_defaulted: "{{ ingest_style | default('cron') }}"
  tags:
    - kiosk_build

- name: create role file
  shell: echo -n kiosk > /etc/zoox/role
  tags:
    - kiosk_account
    - kiosk_build

# prevent run of ingest scripts while we're redeploying
- name: Temporarily remove the crontab for ingest
  become: yes
  cron:
    cron_file: "{{INGEST_CRON_FILE}}"
    user: "{{INGEST_USER}}"
    state: absent
  tags:
    - kiosk_build

- name: Add zooxrc to bash
  lineinfile:
    dest: "/home/{{INGEST_USER}}/.bashrc"
    state: present
    insertafter: EOF
    line: "source {{REPO_DIR}}/scripts/shell/zooxrc.sh"
  tags:
    - kiosk_account

- name: add useful bash aliases to shell
  template:
    src: bash_aliases.j2
    dest: "/home/{{INGEST_USER}}/.bash_aliases"
  tags:
    - kiosk_account

- name: Add pip packages needed for ingest
  become: yes
  pip:
    requirements: "{{REPO_DIR}}/data/vehicle_data_sync/requirements.py2"
  tags:
    - kiosk_build

- name: Install Zoox PKI packages
  become: yes
  apt:
    name:
      - zoox-cachain
      - chrome-zooxca
      - firefox-zooxca
    state: latest
    update_cache: yes
    cache_valid_time: 3600

- name: remove any previous cron registration of push main and previous file locks
  file:
    path: "{{ item }}"
    state: absent
  with_items:
    - "{{ INGEST_CRON_FILE }}"
    - /var/lock/kiosk_sync_d
    - /var/lock/kiosk_disk_clear_d
  become: yes

- name: Build everything under vehicle_data_sync
  shell: "source {{REPO_DIR}}/scripts/shell/zooxrc.sh && bazel build //data/vehicle_data_sync:all"
  args:
    chdir: "{{ REPO_DIR }}"
    executable: /bin/bash
  tags:
    - kiosk_build

# Create the crontab for running ingest
- block:
    - name: set cron variable -- DATA_CATALOG_ENV
      cron:
        user: "{{INGEST_USER}}"
        env: yes
        name: DATA_CATALOG_ENV
        value: "{{DATA_CATALOG_ENV}}"
        cron_file: "{{INGEST_CRON_FILE}}"
      tags:
        - kiosk_build

    - name: set cron variable -- MAILTO
      cron:
        user: "{{INGEST_USER}}"
        env: yes
        name: MAILTO
        value: "rdp@zoox.com"
        cron_file: "{{INGEST_CRON_FILE}}"
      tags:
        - kiosk_build

    - name: set cron variable -- SHELL
      cron:
        user: "{{INGEST_USER}}"
        env: yes
        name: SHELL
        value: /bin/bash
        cron_file: "{{INGEST_CRON_FILE}}"
      tags:
        - kiosk_build

    - name: create sync cron job non-zimbeast
      cron:
        user: "{{INGEST_USER}}"
        name: "{{INGEST_SYNC_CRON_NAME}}"
        job: "{{INGEST_WRAPPER}} >> {{INGEST_LOGDIR}}/{{INGEST_SYNC_LOG_NAME}} 2>&1"
        minute: "{{INGEST_SYNC_CRON_CADENCE}}"
        cron_file: "{{INGEST_CRON_FILE}}"
      tags:
        - kiosk_build

  become: yes
  when: "ingest_style_defaulted == 'cron'"

- name: Install sshd config (require zaccess)
  import_tasks: secure_sshd.yml
  tags:
    - kiosk_build

- name: Configure zoox user
  import_tasks: zoox_user.yml
  tags:
    - kiosk_build

