# This playbook imports the cookies given on IRC to the Matrix Zodbot

- name: Import the cookies
  hosts: value02.iad2.fedoraproject.org:value02.stg.iad2.fedoraproject.org
  user: root

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

  tasks:

    - name: add the pgpass file
      copy:
        content: |
          db01{{ env_suffix }}.iad2.fedoraproject.org:*:maubot:maubot:{{ (env == 'production')|ternary(maubot_prod_db_password, maubot_stg_db_password) }}
        dest: /root/.pgpass
        owner: root
        group: root
        mode: 0400

    - name: install the required package
      dnf:
        state: installed
        name: python3-psycopg2

    - name: install the import script
      copy:
        src: "{{ files }}/zodbot/karma-to-cookies-db.py"
        dest: /usr/local/bin/karma-to-cookies-db
        mode: 0755

    - name: run the import script
      command:
        argv:
          - /usr/local/bin/karma-to-cookies-db
          - -i
          - zodbot{% if env == "staging" %}-stg{% endif %}-fedora
          - /var/lib/{{ (env == 'production')|ternary('zodbot', 'ursabot') }}/data/karma.db
