# Copyright © 2020 Interplanetary Database Association e.V.,
# BigchainDB and IPDB software contributors.
# SPDX-License-Identifier: (Apache-2.0 AND CC-BY-4.0)
# Code is Apache-2.0 and docs are CC-BY-4.0

---
- name: Install pymongo
  pip:
    name: pymongo
    state: present
  tags: [bigchaindb]

- name: Install BigchainDB
  shell: "python3.6 -m pip install -e /opt/stack/bigchaindb/.[dev] --ignore-installed pyyaml"
  register: install_bdb
  failed_when: "'FAILED' in install_bdb.stderr or install_bdb.rc != 0"
  tags: [bigchaindb]

- name: MongoDB Process Check
  shell: pgrep mongod | wc -l
  register: mdb_pchk
  tags: [bigchaindb]

- name: Tendermint Process Check
  shell: pgrep tendermint | wc -l
  register: tm_pchk
  tags: [bigchaindb]

- name: BigchainDB Process Check
  shell: pgrep bigchaindb | wc -l
  register: bdb_pchk
  tags: [bigchaindb]

- name: Start BigchainDB
  shell: nohup bigchaindb -l DEBUG start > /tmp/bigchaindb_log_$(date +%Y%m%d_%H%M%S) 2>&1 &
  environment:
    BIGCHAINDB_DATABASE_BACKEND: "localmongodb"
    BIGCHAINDB_DATABASE_HOST: "127.0.0.1"
    BIGCHAINDB_DATABASE_PORT: "27017"
    BIGCHAINDB_SERVER_BIND: "0.0.0.0:9984"
    BIGCHAINDB_WSSERVER_HOST: "0.0.0.0"
    BIGCHAINDB_WSSERVER_PORT: "9985"
    BIGCHAINDB_TENDERMINT_HOST: "127.0.0.1"
    BIGCHAINDB_TENDERMINT_PORT: "26657"
  when: mdb_pchk.stdout| int != 0 and bdb_pchk.stdout| int == 0 and tm_pchk.stdout| int != 0
  tags: [bigchaindb]
