#
# Copyright (c) 2020 Bubble, Inc.  All rights reserved. For personal (non-commercial) use, see license: https://getbubblenow.com/bubble-license/
#
- name: Install OpenJDK 11 JRE (headless), redis, uuid, jq, and zip
  apt:
    name: [ 'openjdk-11-jre-headless', 'redis', 'uuid', 'jq', 'zip' ]
    state: present
    update_cache: yes

- import_tasks: postgresql.yml

- name: Creates bubble API dir
  file:
    path: /home/bubble/api
    owner: bubble
    group: bubble
    mode: 0555
    state: directory

- name: Install bubble jar
  copy:
    src: bubble.jar
    dest: /home/bubble/api/bubble.jar
    owner: bubble
    group: bubble
    mode: 0444

- name: Unpack site files
  shell: cd /home/bubble && unzip -u /home/bubble/api/bubble.jar 'site/*' && chown -R bubble site

- name: Install helper scripts
  copy:
    src: "{{ item }}"
    dest: "/usr/local/bin/{{ item }}"
    owner: root
    group: root
    mode: 0555
  with_items:
    - "bsql.sh"
    - "random_password.sh"

- name: Install restore helper script for postgres group
  copy:
    src: '{{ item }}'
    dest: "/usr/local/bin/{{ item }}"
    owner: root
    group: postgres
    mode: 0550
  with_items:
    - "bubble_restore_monitor.sh"

- name: Install standard bubble scripts
  copy:
    src: "{{ item }}"
    dest: "/usr/local/bin/"
    owner: root
    group: root
    mode: 0555
  with_fileglob:
    - "scripts/*"

- name: Link current version to the one we just installed
  file:
    src: /home/bubble/api
    dest: /home/bubble/current
    owner: bubble
    group: bubble
    state: link

- name: Creates bubble SQL dir
  file:
    path: /home/bubble/sql
    owner: bubble
    group: postgres
    mode: 0550
    state: directory

- name: Install DB initializer
  copy:
    src: "{{ item }}"
    dest: "/usr/local/bin/{{ item }}"
    owner: root
    group: postgres
    mode: 0550
  with_items:
    - init_bubble_db.sh

- name: Install sbin monitors and scripts
  copy:
    src: "{{ item }}"
    dest: "/usr/local/sbin/{{ item }}"
    owner: root
    group: root
    mode: 0500
  with_items:
    - refresh_bubble_ssh_keys_monitor.sh
    - refresh_bubble_ssh_keys.sh
    - refresh_flex_keys_monitor.sh
    - bubble_upgrade_monitor.sh
    - bubble_upgrade.sh
    - log_manager.sh

- name: Install refresh_bubble_ssh_keys_monitor supervisor conf file
  copy:
    src: supervisor_refresh_bubble_ssh_keys_monitor.conf
    dest: /etc/supervisor/conf.d/refresh_bubble_ssh_keys_monitor.conf

- name: Install refresh_flex_keys_monitor supervisor conf file
  copy:
    src: supervisor_refresh_flex_keys_monitor.conf
    dest: /etc/supervisor/conf.d/refresh_flex_keys_monitor.conf

- name: Install bubble_upgrade_monitor supervisor conf file
  copy:
    src: supervisor_bubble_upgrade_monitor.conf
    dest: /etc/supervisor/conf.d/bubble_upgrade_monitor.conf

- name: Install log_namager monitor cron
  cron:
    name: "Log flag check and manager"
    minute: "*/5"
    user: "root"
    job: "bash -c \"/usr/local/sbin/log_manager.sh >>/var/log/bubble/log_manager.log 2>&1\""

- name: Install packer for sage node
  shell: su - bubble bash -c install_packer.sh
  when: install_type == 'sage'

- name: Install tmp folders' cleaner cron
  cron:
    name: "Cleaning tmp folders"
    special_time: "hourly"
    user: "root"
    job: "find /tmp ~bubble/tmp /var/lib/postgresql/tmp -mtime +1 -type f -delete && find /tmp ~bubble/tmp /var/lib/postgresql/tmp -mindepth 1 -mtime +1 -type d -empty -delete"

- name: Record software versions
  template:
    src: bubble_versions.properties.j2
    dest: /home/bubble/bubble_versions.properties
    owner: bubble
    group: bubble
    mode: 0400
