# 1. INSTALL MongoDB PACKAGES OR BINARIES

# 2019-02-02: Sugarizer with Node.js 10.x requires MongoDB 2.6+ so
# https://andyfelong.com/2017/08/mongodb-3-0-14-for-raspbian-stretch/ is
# being used on Raspbian, all I found! (Raspbian's apt pkg is MongoDB 2.4.14)
#
# mongodb_stretch_3_0_14_core.zip (20M) & mongodb_stretch_3_0_14_tools.zip (15M)
# were backed up from andyfelong.com to http://download.iiab.io/packages/
#
# CLARIF: mongodb_stretch_3_0_14_core.zip IS IN FACT 3.0.14 (core) BUT...
#         mongodb_stretch_3_0_14_tools.zip IS REALLY 3.0.15 (tools)

- block:
  - name: Create dir /tmp/mongodb-3.0.1x (aarch32)
    file:
      path: /tmp/mongodb-3.0.1x
      state: directory

  - name: Download & unzip 20MB http://download.iiab.io/packages/mongodb_stretch_3_0_14_core.zip to /tmp/mongodb-3.0.1x (aarch32)
    unarchive:
      remote_src: yes
      src: "{{ iiab_download_url }}/mongodb_stretch_3_0_14_core.zip"    # http://download.iiab.io/packages
      dest: /tmp/mongodb-3.0.1x

  - name: Install (move) its 3 CORE binaries from /tmp/mongodb-3.0.1x/core to /usr/bin (aarch32)
    shell: mv /tmp/mongodb-3.0.1x/core/* /usr/bin

  - name: Download & unzip 15MB http://download.iiab.io/packages/mongodb_stretch_3_0_14_tools.zip [IN FACT THIS ONE'S 3.0.15] to /tmp/mongodb-3.0.1x (aarch32)
    unarchive:
      remote_src: yes
      src: "{{ iiab_download_url }}/mongodb_stretch_3_0_14_tools.zip"
      dest: /tmp/mongodb-3.0.1x

  - name: Install (move) its 9 TOOLS binaries from /opt/iiab/downloads/mongodb-3.0.1x/tools to /usr/bin (aarch32)
    shell: mv /tmp/mongodb-3.0.1x/tools/* /usr/bin

  - name: Create Linux group mongodb (aarch32)
    group:
      name: mongodb
      state: present

  - name: Create Linux user mongodb (aarch32)
    user:
      name: mongodb
      group: mongodb    # primary group
      groups: mongodb
      home: /var/lib/mongodb
      shell: /usr/sbin/nologin

  - name: Install {{ mongodb_conf }} from template (aarch32)
    template:
      src: mongod.conf.j2
      dest: "{{ mongodb_conf }}"    # /etc/mongod.conf
      #owner: root
      #group: root
      #mode: 0644

  # end block
  when: not (ansible_architecture == "x86_64" or ansible_architecture == "aarch64")

# 32-bit OS's are handled above: this should handle aarch32 including 32-bit Ubuntu
# from https://ubuntu.com/download/raspberry-pi but Ubuntu 20.04 32-bit might fail
# untested, and 32-bit Intel might puke as this was orginally deployed for Raspbian.
# (Haven't seen bootable 32-bit Intel installers for a while now.)
# 64-bit OS's proceed below.

- block:
  - name: Add mongodb.org signing key (only 64-bit support available)
    shell: wget -qO - https://www.mongodb.org/static/pgp/server-4.4.asc | apt-key add -
    args:
      warn: false

  - name: Use mongodb-org's Debian repo for Debian (only amd64 support available)
    apt_repository:
      # 2020-10-28: http://repo.mongodb.org/apt/debian/dists/ supports only
      # {buster 10, stretch 9, jessie 8, wheezy 7}
      # so Debian 11 "Bullseye" (testing branch) can revert to buster for now:
      repo: deb http://repo.mongodb.org/apt/debian buster/mongodb-org/4.4 main
      #repo: deb http://repo.mongodb.org/apt/debian {{ ansible_distribution_release }}/mongodb-org/4.4 main
      state: present
      filename: mongodb-org
    when: is_debian and (ansible_architecture == "x86_64")

  # Debian 10 aarch64 might work below but is blocked in main.yml
  - name: Use mongodb-org's Ubuntu bionic repo for RaspOS-aarch64
    apt_repository:
      repo: deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu bionic/mongodb-org/4.4 multiverse
      state: present
      filename: mongodb-org
    when: is_raspbian and (ansible_architecture == "aarch64")

  - name: Use mongodb-org's Ubuntu focal repo for Linux Mint - 64bit only
    apt_repository:
      repo: deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/4.4 multiverse
      state: present
      filename: mongodb-org
    when: is_linuxmint

  - name: Use mongodb-org's Ubuntu repo for all non-Mint Ubuntu - 64bit only
    apt_repository:
      # 2020-10-27: https://repo.mongodb.org/apt/ubuntu/dists/ supports only
      # {focal 20.04, bionic 18.04, xenial 16.04, trusty 14.04, precise 12.04}
      # so other Ubuntu's like groovy 20.10 need to revert to recent LTS repo:
      repo: deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/4.4 multiverse
      #repo: deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu {{ ansible_distribution_release }}/mongodb-org/4.4 multiverse
      state: present
      filename: mongodb-org
    when: is_ubuntu and not is_linuxmint

  - name: "Install packages: mongodb-org, mongodb-org-server"
    package:
      name:
        - mongodb-org
        - mongodb-org-server
      state: present

  - name: Establish {{ mongodb_conf }} port {{ mongodb_port }} (mongodb_conf) -- takes effect on next (re)start of the service -- via enable-or-disable.yml or via sugarizer.service auto-starting MongoDB on demand
    lineinfile:
      path: "{{ mongodb_conf }}"
      regexp: '^\s*port:' # \s = any whitespace char. stackoverflow.com/a/38491899
      #backrefs: yes
      line: "  port: {{ mongodb_port }}"    # 27017

  # end block
  when: (ansible_architecture == "aarch64") or (ansible_architecture == "x86_64")


# 2. CONFIGURE MongoDB FOR IIAB

- name: 'Create 3 dirs for MongoDB: /var/lib/mongodb, /var/log/mongodb, {{ mongodb_db_path }}'
  file:
    state: directory
    path: "{{ item }}"
    owner: mongodb
    group: mongodb
  with_items:
    #- { path: '/var/run/mongodb' }
    - /var/lib/mongodb
    - /var/log/mongodb
    - "{{ mongodb_db_path }}"    # /library/dbdata/mongodb

- name: Install mongodb.service, /usr/bin/iiab-mongodb-repair-if-no-lock from templates
  template:
    src: "{{ item.src }}"
    dest: "{{ item.dest }}"
    owner: root
    group: root
    mode: "{{ item.mode }}"
  with_items:
    - { src: 'mongodb.service.j2', dest: '/etc/systemd/system/mongodb.service', mode: '0644' }
    - { src: 'iiab-mongodb-repair-if-no-lock.j2', dest: '/usr/bin/iiab-mongodb-repair-if-no-lock', mode: '0755' }

- name: systemctl daemon_reload, so systemd (re)reads mongodb.service
  systemd:
    daemon_reload: yes


# 3. RECORD MongoDB AS INSTALLED

- name: "Set 'mongodb_installed: True'"
  set_fact:
    mongodb_installed: True

- name: "Add 'mongodb_installed: True' to {{ iiab_state_file }}"
  lineinfile:
    path: "{{ iiab_state_file }}"    # /etc/iiab/iiab_state.yml
    regexp: '^mongodb_installed'
    line: 'mongodb_installed: True'
