---
# - hosts: all
#   tasks:
#     - name: Print some debug information 
#       vars: 
#         msg: |
#             Module Variables ("vars"):
#             --------------------------------
#             {{ vars | to_nice_json }} 

#             Environment Variables ("environment"):
#             --------------------------------
#             {{ environment | to_nice_json }} 

#             GROUP NAMES Variables ("group_names"):
#             --------------------------------
#             {{ group_names | to_nice_json }}

#             GROUPS Variables ("groups"):
#             --------------------------------
#             {{ groups | to_nice_json }}

#             HOST Variables ("hostvars"):
#             --------------------------------
#             {{ hostvars | to_nice_json }} 

#       debug: 
#         msg: "{{ msg.split('\n') }}"       
#       tags: debug_info
#
# Localhost actions
#
- hosts: localhost
  connection: local
  vars:
    isso_build_dir: "{{playbook_dir}}/build/tmp/isso"
    dashboard_build_dir: "{{playbook_dir}}/build/tmp/dashboard"
    clientjs_dir: "{{playbook_dir}}/build/tmp/dashboard/clientjs"
  tasks:

    - name: mkdir build folder
      file:
        path: "{{playbook_dir}}/build/tmp"
        state: directory

    - name: clear dashboard checkout dir if exists
      file:
        path: "{{dashboard_build_dir}}"
        state: absent

    - name: checkout dashboard
      hg:
        repo: "{{dashboard_repo}}"
        version: "{{dashboard_version}}"
        force: yes
        dest: "{{dashboard_build_dir}}"

    - name: clear isso checkout dir if exists
      file:
        path: "{{isso_build_dir}}"
        state: absent

    - name: set discussions repo url
      set_fact:
        discussion_repo_url: "{{lookup('file', dashboard_build_dir+'/server/isso/requirements.txt').split('\n')[1]}}"

    - name: check discussions repo url
      fail: msg="discussions repo specification in wrong format."
      when: discussion_repo_url is not match("^\#-e git\+.+@[\w\-]*\#egg\=isso$")

    - name: get discussion chechout url and version
      set_fact:
        discussion_repo: "{{discussion_repo_url | regex_replace('^\\#-e git\\+(.+)@[\\w\\-]*\\#egg\\=isso$', '\\1')}}"
        discussion_version: "{{discussion_repo_url | regex_replace('^\\#-e git\\+.+@([\\w\\-]*)\\#egg\\=isso$', '\\1')}}"

    - name: checkout isso
      git:
        repo: "{{discussion_repo}}"
        version: "{{discussion_version}}"
        force: yes
        dest: "{{isso_build_dir}}"

    - name: call npm install
      command: npm install chdir="{{isso_build_dir}}"

    - name: call make init
      make:
        chdir: "{{isso_build_dir}}"
        target: init

    - name: call make js
      make:
        chdir: "{{isso_build_dir}}"
        target: js

    - name: make isso package
      command: python setup.py sdist chdir="{{isso_build_dir}}"

#
# all actions
#
- hosts: remote_discussions
  vars:
    isso_build_dir: "{{playbook_dir}}/build/tmp/isso"
    dashboard_build_dir: "{{playbook_dir}}/build/tmp/dashboard"
    clientjs_dir: "{{playbook_dir}}/build/tmp/dashboard/clientjs"
  tasks:

    - name: get isso version
      local_action: command python setup.py -V chdir="{{isso_build_dir}}"
      register: isso_version

    - name: get discussion package version
      set_fact:
        discussions_pkg_version: "{{isso_version.stdout}}"

#
# Localhost actions
#
- hosts: localhost
  connection: local
  vars:
    isso_build_dir: "{{playbook_dir}}/build/tmp/isso"
    dashboard_build_dir: "{{playbook_dir}}/build/tmp/dashboard"
    clientjs_dir: "{{playbook_dir}}/build/tmp/dashboard/clientjs"
  tasks:

# build client js
    - name: remove build
      file: path="{{clientjs_dir}}/packages/{{item}}" state=absent
      with_items:
        - "{{dashboard_name}}/build"
        - dashboard-components/lib

    - name: clientjs npm install
      command: npm install
      args:
        chdir: "{{clientjs_dir}}"

    - name: clientjs lerna bootstrap
      command: npx lerna bootstrap
      args:
        chdir: "{{clientjs_dir}}"

    - name: clientjs dashboard-components build
      command: npm run build
      args:
        chdir: "{{clientjs_dir}}/packages/dashboard-components"

    - name: clientjs dashboard build
      command: npm run build
      args:
        chdir: "{{clientjs_dir}}/packages/{{dashboard_name}}"
      environment:
        REACT_APP_API_URL: "{{annotations_api_url}}/annotations"
        REACT_APP_VIA_BASE_URL: "{{via_base_url}}"
        REACT_APP_DISCUSSION_URL: "{{discussions_url}}/"


#
# Static server
#
- hosts: remote_static
  become: yes
  vars:
    isso_build_dir: "{{playbook_dir}}/build/tmp/isso"
    dashboard_build_dir: "{{playbook_dir}}/build/tmp/dashboard"
    clientjs_dir: "{{playbook_dir}}/build/tmp/dashboard/clientjs"
  tasks:

    - name: create dest static directory content
      file:
        path: "{{remote_static_path}}"
        state: directory
        owner: "{{static_http_user}}"
        group: "{{static_http_group}}"

    - name: remove dest static directory content
      command: /bin/rm -fr "{{remote_static_path}}/*" warn=False
      when: remote_static_path != ""

    # TODO : set dashboard name in setting
    - name: transfert static dist to remote
      copy:
        src: "{{clientjs_dir}}/packages/{{dashboard_name}}/build/"
        dest: "{{remote_static_path}}/"
        owner: "{{static_http_user}}"
        group: "{{static_http_group}}"
      notify:
        - restart static nginx

    - name: create refresh nginx config
      template:
        src: "{{static_nginx_use_ssl | ternary('nginx.static.ssl.conf.j2', 'nginx.static.conf.j2')}}"
        dest: "{{static_nginx_config | default('/etc/nginx/site-available/'+static_server_name, true)}}"
      notify:
        - restart static nginx

  handlers:
    - name: restart static nginx
      service:
        name: "{{static_http_service}}"
        state: restarted
      ignore_errors: yes

#
# Annotations and discussion servers
#
- hosts: remote_annotations_api:remote_discussions
  become: yes
  vars:
    isso_build_dir: "{{playbook_dir}}/build/tmp/isso"
    clientjs_dir: "{{playbook_dir}}/../clientjs"
    annotation_api_dir: "{{playbook_dir}}/../server/sbin"
  tasks:

    - name: add iri group
      group:
        name: iri

    - name: add uwsgi user
      user:
        name: uwsgi
        group: iri
        home: /var/www
        create_home: no
        system: yes

    - name: create log dir
      file:
        path: "{{log_base_path}}"
        state: directory
        owner: uwsgi
        group: iri
        mode: 0755

#
# Annotations server
#
- hosts: remote_annotations_api
  become: yes
  vars:
    isso_build_dir: "{{playbook_dir}}/build/tmp/isso"
    clientjs_dir: "{{playbook_dir}}/../clientjs"
    annotation_api_dir: "{{playbook_dir}}/../server/sbin"
  tasks:

    - name: create config dir
      file:
        path: "{{annotations_api_config_base}}"
        state: directory
        owner: uwsgi
        group: iri
        mode: 0755

    - name: register annotation api virtualenv stats
      stat: path="{{annotations_api_venv}}"
      register: annotations_api_venv_stats

    - name: copy annotation api requirement file
      copy:
        src: "{{annotation_api_dir}}/requirements.txt"
        dest: "{{annotations_api_config_base}}/annotation_api_requirements.txt"
      register: copy_annotation_api_requirements

    - name: remove annotation api venv
      file:
        path: "{{annotations_api_venv}}"
        state: absent
      when: copy_annotation_api_requirements.changed

    - name: create annotations api virtualenv
      pip:
        virtualenv_command: /usr/bin/python3 -m venv
        virtualenv: "{{annotations_api_venv}}"
        requirements: "{{annotations_api_config_base}}/annotation_api_requirements.txt"
      when: copy_annotation_api_requirements.changed or (not annotations_api_venv_stats.stat.exists)
      notify: restart annotations api service

    - name: copy annotations api module
      copy:
        src: "{{annotation_api_dir}}/hypothesis_proxy.py"
        dest: "{{annotations_api_venv}}/bin/"
      notify: restart annotations api service

    - name: copy annotation api config
      template:
        src: "annotation_api_configuration.ini.j2"
        dest: "{{annotations_api_config_base}}/annotation_api_configuration.ini"
      notify: restart annotations api service

    - name: copy annotations api uwsgi config
      template:
        src: "dashboard-nextleap-annotations.yml.j2"
        dest: "{{annotations_api_config_base}}/uwsgi-dashboard-annotations.yml"
      notify: restart annotations api service

    - name: copy annotations api supervisor config
      template:
        src: "dashboard-nextleap-annotations.ini.j2"
        dest: "{{annotations_api_supervisor_conf_path}}/{{annotations_api_service}}.ini"
      notify: restart annotations api service

  handlers:
    - name: reload annotations api service supervisorctl
      listen: "restart annotations api service"
      supervisorctl:
        name: "{{annotations_api_service}}"
        state: present
      ignore_errors: yes

    - name: restart annotations api service supervisorctl
      listen: "restart annotations api service"
      supervisorctl:
        name: "{{annotations_api_service}}"
        state: restarted
      ignore_errors: yes

#
# Discussion servers
#
- hosts: remote_discussions

  become: yes

  vars:
    isso_build_dir: "{{playbook_dir}}/build/tmp/isso"
    dashboard_build_dir: "{{playbook_dir}}/build/tmp/dashboard"
    clientjs_dir: "{{playbook_dir}}/build/tmp/dashboard/clientjs"
    dicussions_pkg_archive: "isso-{{discussions_pkg_version}}.tar.gz"

  tasks:

    - name: create discussion build tmp dir
      tempfile:
        state: directory
      register: discussions_build_tmp_dir

    - name: create config dir
      file:
        path: "{{discussions_config_base}}"
        state: directory
        owner: uwsgi
        group: iri
        mode: 0755

    - name: register discussion virtualenv stats
      stat: path="{{discussions_venv}}"
      register: discussions_venv_stats

    - name: copy discussion requirement file
      copy:
        src: "{{dashboard_build_dir}}/server/isso/requirements.txt"
        dest: "{{discussions_config_base}}/discussions_requirements.txt"
      register: copy_discussions_requirements

    - name: remove annotation api venv
      file:
        path: "{{discussions_venv}}"
        state: absent
      when: copy_discussions_requirements.changed

    - name: copy discussions build
      copy:
        src: "{{isso_build_dir}}/dist/{{dicussions_pkg_archive}}"
        dest: "{{discussions_build_tmp_dir.path}}"
      when: copy_discussions_requirements.changed or (not discussions_venv_stats.stat.exists)

    - name: create discussions virtualenv
      pip:
        virtualenv_command: /usr/bin/python3 -m venv
        virtualenv: "{{discussions_venv}}"
        requirements: "{{discussions_config_base}}/discussions_requirements.txt"
      when: copy_discussions_requirements.changed or (not discussions_venv_stats.stat.exists)
      notify: restart discussions service

    - name: install discussions pkg in venv
      pip:
        virtualenv: "{{discussions_venv}}"
        name: "file://{{discussions_build_tmp_dir.path}}/{{dicussions_pkg_archive}}"
      when: copy_discussions_requirements.changed or (not discussions_venv_stats.stat.exists)
      notify: restart discussions service

    - name: copy discussions uwsgi config
      template:
        src: "dashboard-nextleap-discussions.yml.j2"
        dest: "{{annotations_api_config_base}}/uwsgi-dashboard-discussions.yml"
      notify: restart discussions service

    - name: copy discussions isso config
      template:
        src: "dashboard-nextleap-discussions-isso.cfg.j2"
        dest: "{{annotations_api_config_base}}/dashboard-nextleap-discussions-isso.cfg"
      notify: restart discussions service

    - name: copy discussions supervisor config
      template:
        src: "dashboard-nextleap-discussions.ini.j2"
        dest: "{{discussions_supervisor_conf_path}}/{{discussions_service}}.ini"
      notify: restart discussions service

    - name: create discussions data folder
      file:
        path: "{{discussions_db_dir}}"
        state: directory
        owner: uwsgi
        group: iri
        mode: 0755

  post_tasks:
    - name: clean discussions build temp dir
      file:
        path: "{{discussions_build_tmp_dir.path}}"
        state: absent

  handlers:

    - name: reload discussions service supervisorctl
      listen: "restart discussions service"
      supervisorctl:
        name: "{{discussions_service}}"
        state: present
      ignore_errors: yes

    - name: restart discussions service supervisorctl
      listen: "restart discussions service"
      supervisorctl:
        name: "{{discussions_service}}"
        state: restarted
      ignore_errors: yes
