---

- include: clamav.yml
- include: eventsource.yml
- include: gitolite.yml
- include: milter.yml
- include: postgres.yml

- name: Install helpful development packages
  dnf:
    name:
      - git
      - ngrep
      - nmap-ncat
      - python3-rpdb
      - tmux
      - tree
      - vim-enhanced
    state: present

- name: Install Pagure development packages
  dnf:
    name:
      - gcc
      - libgit2-devel
      - libffi-devel
      - libjpeg-devel
      - make
      - python3-alembic
      - python3-arrow
      - python3-binaryornot
      - python3-bleach
      - python3-blinker
      - python3-chardet
      - python3-cryptography
      - python3-docutils
      - python3-eventlet
      - python3-fedora-flask
      - python3-flask
      - python3-flask-wtf
      - python3-jinja2
      - python3-markdown
      - python3-munch
      - python3-openid-cla
      - python3-openid-teams
      - python3-pip
      - python3-psutil
      - python3-pygit2
      - python3-redis
      - python3-sqlalchemy
      - python3-straight-plugin
      - python3-virtualenvwrapper
      - python3-wtforms
      - python3-devel
      - python3-devel
      - redhat-rpm-config
      - python3-bcrypt
      - python3-virtualenv
    state: present

# Add various helpful configuration files
- name: Install a custom bashrc
  become_user: "{{ ansible_env.SUDO_USER }}"
  copy: src=bashrc dest=/home/{{ ansible_env.SUDO_USER }}/.bashrc

- name: Install the message of the day
  copy: src=motd dest=/etc/motd

- name: Remove the motd duplicate
  pamd:
    name: sshd
    type: session
    control: optional
    module_path: pam_motd.so
    state: absent

- name: populate bash history
  become_user: "{{ ansible_env.SUDO_USER }}"
  copy: src=bash_history dest=/home/{{ ansible_env.SUDO_USER }}/.bash_history

- name: register the libgit2 version installed
  shell: rpm -q libgit2|cut -d \- -f 2| cut -d \. -f 1,2
  register: libgit2_version
  args:
    warn: false

# Install Pagure inside a virtualenv and configure it
- name: Install pygit2 in the virtualenv
  become_user: "{{ ansible_env.SUDO_USER }}"
  pip:
    name:
      - "pygit2=={{ libgit2_version.stdout_lines[0] }}.*"
    virtualenv: /home/{{ ansible_env.SUDO_USER }}/.virtualenvs/python3-pagure/
    virtualenv_python: python3
    virtualenv_command: virtualenv

- name: Install Pagure Python dependencies into a virtualenv
  become_user: "{{ ansible_env.SUDO_USER }}"
  pip:
    requirements: /home/{{ ansible_env.SUDO_USER }}/devel/{{ item }}
    virtualenv: /home/{{ ansible_env.SUDO_USER }}/.virtualenvs/python3-pagure/
    virtualenv_command: virtualenv
    virtualenv_python: python3
    virtualenv_site_packages: yes
    extra_args: --no-use-pep517
  with_items:
    - "requirements.txt"
    - "requirements-ev.txt"
    - "requirements-testing.txt"

- name: Hotfix python-fedora to make flask_fas_openid compatible with flask >= 1.0
  become: "{{ ansible_env.SUDO_USER }}"
  lineinfile:
    path: /home/{{ ansible_env.SUDO_USER }}/.virtualenvs/python3-pagure/lib/python3.7/site-packages/flask_fas_openid.py
    regexp: '^        if assume_recent or \(major >'
    line: '        if assume_recent or (major > 0 or minor >= 10):'

- name: Install Pagure package into a virtualenv
  become_user: "{{ ansible_env.SUDO_USER }}"
  pip:
    name: /home/{{ ansible_env.SUDO_USER }}/devel/
    extra_args: '-e'
    virtualenv: /home/{{ ansible_env.SUDO_USER }}/.virtualenvs/python3-pagure/
    virtualenv_command: virtualenv

- name: Install Pagure package into /usr/lib
  pip:
    name: /home/{{ ansible_env.SUDO_USER }}/devel/
    extra_args: '-e'

- name: Install the pagure configuration
  become_user: "{{ ansible_env.SUDO_USER }}"
  copy: src=pagure.cfg dest=/home/{{ ansible_env.SUDO_USER }}/pagure.cfg

- name: Creates pagure data directories
  become_user: "{{ ansible_env.SUDO_USER }}"
  file: path=/home/{{ ansible_env.SUDO_USER }}/pagure_data/{{ item }} state=directory
  with_items:
    - forks
    - docs
    - tickets
    - requests
    - remotes
    - repos

- name: Add a working copy of alembic.ini
  become_user: "{{ ansible_env.SUDO_USER }}"
  copy:
    src: /home/{{ ansible_env.SUDO_USER }}/devel/files/alembic.ini
    dest: /home/{{ ansible_env.SUDO_USER }}/alembic.ini
    remote_src: True

- name: Configure alembic to use our development database
  become_user: "{{ ansible_env.SUDO_USER }}"
  replace:
    dest: /home/{{ ansible_env.SUDO_USER }}/alembic.ini
    regexp: "sqlalchemy.url = sqlite:////var/tmp/pagure_dev.sqlite"
    replace: "sqlalchemy.url = sqlite:////home/{{ ansible_env.SUDO_USER }}/pagure_data/pagure_dev.sqlite"

- name: Configure alembic to point to the pagure migration folder
  become_user: "{{ ansible_env.SUDO_USER }}"
  replace:
    dest: /home/{{ ansible_env.SUDO_USER }}/alembic.ini
    regexp: "script_location = /usr/share/pagure/alembic"
    replace: "script_location = /home/vagrant/devel/alembic/"

- name: Create the Pagure database
  become_user: "{{ ansible_env.SUDO_USER }}"
  command: .virtualenvs/python3-pagure/bin/python devel/createdb.py
  args:
    creates: /home/{{ ansible_env.SUDO_USER }}/pagure_data/pagure_dev.sqlite
    chdir: "/home/{{ ansible_env.SUDO_USER }}/"

- name: Stamp the database with its current migration
  become_user: "{{ ansible_env.SUDO_USER }}"
  shell: /home/{{ ansible_env.SUDO_USER }}/.virtualenvs/python3-pagure/bin/alembic stamp $(/home/{{ ansible_env.SUDO_USER }}/.virtualenvs/python3-pagure/bin/alembic heads | awk '{ print $1 }')
  args:
    chdir: "/home/{{ ansible_env.SUDO_USER }}/"

- name: Create systemd user unit directory
  become_user: "{{ ansible_env.SUDO_USER }}"
  file:
    path: /home/{{ ansible_env.SUDO_USER }}/.config/systemd/user/
    state: directory

- name: Install the Pagure service files for systemd
  become_user: "{{ ansible_env.SUDO_USER }}"
  copy:
    src: "{{ item }}"
    dest: /home/{{ ansible_env.SUDO_USER }}/.config/systemd/user/{{ item }}
  with_items:
    - pagure.service
    - pagure-docs.service
    - pagure_ci.service
    - pagure_ev.service
    - pagure_webhook.service
    - pagure_worker.service

- name: Enable and start the Pagure systemd services
  become: False
  systemd:
    daemon_reload: yes
    name: "{{ item }}"
    enabled: True
    state: started
    scope: user
  with_items:
    - pagure
    - pagure-docs
    - pagure_ci
    - pagure_ev
    - pagure_webhook
    - pagure_worker
