#####################################
### someone"s ansible provisioner ###
#####################################
# Part of: https://git.somenet.org/root/pub/somesible.git
# 2017-2025 by someone <someone@somenet.org>
#
# its not supported to run multiple mattestmost instances on one host.
#
---
- name: create mattermost user
  user:
    name: "mattermost"
    home: "{{mattermost_homedir}}"
    shell: "/bin/zsh"
    system: yes
    state: present


- name: create mattermost homedir
  file:
    path: "{{mattermost_homedir}}"
    state: directory
    mode: 0710
    owner: "mattermost"
    group: "mattermost"


- name: create mattermost data-dir
  file:
    path: "{{mattermost_homedir}}/data"
    state: directory
    mode: 0750
    owner: "mattermost"
    group: "mattermost"


#- name: create postgres-db and user
#  include_role:
#    name: util/postgres-db-usr
#  vars:
#    pg_data:
#      dbname: "{{mattermost_db_name}}"
#      pw: "{{mattermost_db_pw}}"
#  when: mattermost_db_create | default('True') | bool


- name: include vars_nginx_vhost_custom
  include_vars:
    file: "{{item}}"
    name: vars_nginx_vhost_custom
  with_first_found:
    - "{{lookup('env','PWD')}}/host_files/{{inventory_hostname}}/{{role_name}}/vars_nginx_vhost_custom.yml"
    - "{{lookup('env','PWD')}}/group_files/{{group_files_group}}/{{role_name}}/vars_nginx_vhost_custom.yml"
    - "{{lookup('env','PWD')}}/group_files/all/{{role_name}}/vars_nginx_vhost_custom.yml"
    - "default/vars_nginx_vhost_custom.yml"


- name: configure mattermost vhost
  include_role:
    name: server/nginx/vhost-unified
  vars:
    vhost_type: "custom"
    vhost_name: "{{mattermost_domain}}"
    vhost_awstats_valid_http_codes: "200 304 101"
    vhost_custom:
      vhost_custom_pre_server: "{{vars_nginx_vhost_custom.vhost_custom_pre_server}}"
      vhost_custom: "{{vars_nginx_vhost_custom.vhost_custom}}"


- name: copy mattermost.service to /etc/systemd/system/
  copy:
    src: "{{item}}"
    dest: "/etc/systemd/system/mattermost.service"
    mode: 0644
    owner: "root"
    group: "root"
  with_first_found:
    - "{{lookup('env','PWD')}}/host_files/{{inventory_hostname}}/{{role_name}}/mattermost.service"
    - "{{lookup('env','PWD')}}/group_files/{{group_files_group}}/{{role_name}}/mattermost.service"
    - "{{lookup('env','PWD')}}/group_files/all/{{role_name}}/mattermost.service"
    - "default/mattermost.service"


- name: download mattermost release and check checksums
  get_url:
    url: "{{mattermost_download_url}}"
    dest: "{{mattermost_homedir}}/server.tar.gz"
    mode: 0640
    owner: "mattermost"
    group: "mattermost"
    checksum: "{{mattermost_download_checksum}}"
    timeout: 30
  tags: "online"
  register: download


- name: extract and update/install mattermost
  block:
  - name: stop mattermost.service
    systemd:
      name: mattermost.service
      daemon_reload: yes
      state: stopped


  - name: remove old server files
    file:
      path: "{{mattermost_homedir}}/server.old"
      state: absent


  - name: limit access to current server files
    file:
      path: "{{mattermost_homedir}}/server"
      state: directory
      mode: 0700
      owner: "mattermost"
      group: "mattermost"


  - name: move current server files away
    command: "mv {{mattermost_homedir}}/server {{mattermost_homedir}}/server.old"


  - name: create new server files dir, because unarchive fails to do so
    file:
      path: "{{mattermost_homedir}}/server"
      state: directory
      mode: 0750
      owner: "mattermost"
      group: "mattermost"


  - name: extract mattermost files
    unarchive:
      src: "{{mattermost_homedir}}/server.tar.gz"
      dest: "{{mattermost_homedir}}/server"
      remote_src: yes
      mode: "u=rwX,g=rX,o-rwx"
      owner: "mattermost"
      group: "mattermost"
      extra_opts:
        - '--strip-components=1'
        - '--show-stored-names'


  - name: reuse existing config file
    command: "mv {{mattermost_homedir}}/server.old/config/config.json {{mattermost_homedir}}/server/config/config.json"
    register: config_copy
    ignore_errors: yes


  - name: template new config.json
    template:
      src: "{{item}}"
      dest: "{{mattermost_homedir}}/server/config/config.json"
      mode: 0640
      owner: "mattermost"
      group: "mattermost"
    with_first_found:
      - "{{lookup('env','PWD')}}/host_files/{{inventory_hostname}}/{{role_name}}/config.json.j2"
      - "{{lookup('env','PWD')}}/group_files/{{group_files_group}}/{{role_name}}/config.json.j2"
      - "{{lookup('env','PWD')}}/group_files/all/{{role_name}}/config.json.j2"
      - "default/config.json.j2"
    when: config_copy.failed


  - name: enable and restart mattermost.service
    include_role: name="base/systemd/enable-and-start"
    vars:
      service_name: mattermost.service
      service_state: restarted


  - name: create mattermost admin user
    become: true
    become_user: "mattermost"
    shell: "bin/mattermost user create --system_admin --username {{mattermost_admin_user | quote}} --email {{mattermost_admin_user_email | quote}} --password {{mattermost_admin_user_pw | quote}} || true"
    args:
      chdir: "{{mattermost_homedir}}/server"
    register: script_res
    changed_when: "'An account with that email already exists' not in script_res.stdout"

  when: download.changed
