- name: check/create instance
  hosts: lists-dev.cloud.fedoraproject.org
  user: root
  gather_facts: False

  vars_files: 
  - /srv/web/infra/ansible/vars/global.yml
  - "/srv/private/ansible/vars.yml"

  tasks:
  - include: "{{ tasks }}/persistent_cloud.yml"

- name: provisions basics onto system/setup paths
  hosts: lists-dev.cloud.fedoraproject.org
  user: root
  gather_facts: True

  vars_files: 
  - /srv/web/infra/ansible/vars/global.yml
  - "/srv/private/ansible/vars.yml"
  - /srv/web/infra/ansible/vars/{{ ansible_distribution }}.yml
  vars:
  - mailman_vardir: /srv/persist/mailman
  - tcp_ports: [22, 25, 80, 443]
  - udp_ports: []
  - postfix_maincf: "{{ roles }}/base/files/postfix/main.cf/main.cf.lists-dev.cloud.fedoraproject.org"

  roles:
  - sudo
  - hosts
  - apache

  tasks:
  - include: "{{ tasks }}/cloud_setup_basic.yml"
  - include: "{{ tasks }}/postfix_basic.yml"
  - include: "{{ tasks }}/yumrepos.yml"
  - include: "{{ tasks }}/motd.yml"
  - include: "{{ tasks }}/mod_wsgi.yml"

  - name: mount up disk of persistent storage
    action: mount name=/srv/persist src='LABEL=lists-dev' fstype=ext4 state=mounted

  - name: selinux status
    selinux: policy=targeted state=enforcing

  # /srv/persist
  - name: mount up bind mount for postgres
    action: mount src=/srv/persist/pgsqldb name=/var/lib/pgsql fstype=auto opts=bind state=mounted
  - name: mount up bind mount for mailman
    action: mount src=/srv/persist/mailman name=/var/lib/mailman3 fstype=auto opts=bind state=mounted

  - name: get the repo file
    get_url: url=https://repos.fedorapeople.org/repos/abompard/hyperkitty/hyperkitty.repo
             dest=/etc/yum.repos.d/hyperkitty.repo mode=0444

  # Database
  - name: install postgresql server packages
    yum: name={{ item }}  state=present
    with_items:
    - postgresql-server
    - postgresql-contrib
    - python-psycopg2

  - name: initialize postgresql
    command: /usr/bin/postgresql-setup initdb
             creates=/var/lib/pgsql/data/postgresql.conf

  - name: copy pg_hba.conf
    copy: src="{{ files }}/lists-dev/pg_hba.conf" dest=/var/lib/pgsql/data/pg_hba.conf
          owner=postgres group=postgres
    notify:
    - restart postgresql

  - name: start postgresql
    service: state=started enabled=yes name=postgresql

  - name: allow running sudo commands as postgresql for ansible
    copy: src="{{ files }}/lists-dev/sudoers-norequiretty-postgres" dest=/etc/sudoers.d/norequiretty-postgres
          owner=root group=root mode=0440

  handlers:
  - include: "{{ handlers }}/restart_services.yml"
  - name: restart postgresql
    service: name=postgresql state=restarted



- name: setup db users/passwords for hyperkitty
  hosts: hyperkitty-stg
  gather_facts: no
  sudo: yes
  sudo_user: postgres
  vars_files: 
  - /srv/web/infra/ansible/vars/global.yml
  - "/srv/private/ansible/vars.yml"
  - "{{ vars_path }}/{{ ansible_distribution }}.yml"
  vars:
  - mailman_vardir: /srv/persist/mailman


  tasks:
  # mailman auto-updates its schema, there can only be one admin user
  - name: mailman DB user
    postgresql_user: name=mailmanadmin password={{ lists_dev_mm_db_pass }}
  - name: hyperkitty DB admin user
    postgresql_user: name=hyperkittyadmin password={{ lists_dev_hk_db_pass }}
  - name: hyperkitty DB user
    postgresql_user: name=hyperkittyapp password={{ lists_dev_hk_db_pass }}
  - name: databases creation
    postgresql_db: name={{ item }} owner="{{ item }}admin" encoding=UTF-8
    with_items:
    - mailman
    - hyperkitty

- name: setup mailman and hyperkitty
  hosts: hyperkitty-stg
  gather_facts: no
  vars_files: 
  - /srv/web/infra/ansible/vars/global.yml
  - "/srv/private/ansible/vars.yml"
  - "{{ vars_path }}/{{ ansible_distribution }}.yml"

  roles:
  - role: mailman
    mailman_db_server: localhost
    mailman_mailman_db_pass: "{{ lists_dev_mm_db_pass }}"
    mailman_hyperkitty_admin_db_pass: "{{ lists_dev_hk_db_pass }}"
    mailman_hyperkitty_db_pass: "{{ lists_dev_hk_db_pass }}"
    mailman_hyperkitty_cookie_key: "randomstringusedasacookiesecurekey-yesthisshouldbeinaprivaterepo_butidonthaveaccesstoit"
    mailman_url: "{{ ansible_fqdn }}"
  - collectd/base

  tasks:

  - name: install more needed packages
    yum: pkg={{ item }} state=present
    with_items:
    - tar
    - vim
    - tmux
    - patch
    tags:
    - packages

  #- name: easy access to the postgresql databases
  #  template: src="{{ files }}/lists-dev/pgpass.j2" dest=/root/.pgpass
  #            owner=root group=root mode=0600

  - name: send root mail to abompard
    lineinfile: dest=/etc/aliases regexp='^root:' line="root:abompard@fedoraproject.org"
    notify:
    - reload aliases

  # Mailman is installed from source, not from the RPM
  - name: systemd service file for mailman3
    template: src="{{ files }}mailman3.service.j2" dest=/etc/systemd/system/multi-user.target.wants/mailman3.service
    tags:
    - config

  - name: mailman logging -- rotation
    template: src=mailman.logrotate.j2 dest=/etc/logrotate.d/mailman3


  handlers:
  - include: "{{ handlers }}/restart_services.yml"
  - name: reload aliases
    command: newaliases 
