- hosts: test_systems
  name: "Setup and run a test system with a 'Federated Properties' enabled instance of Wikibase"
  become: yes
  vars:
    VECTOR_PATH: "skins/Vector"
    ULS_PATH: "extensions/UniversalLanguageSelector"
    CLDR_PATH: "extensions/cldr"
    WIKIBASE_PATH: "extensions/Wikibase"
    WIKIBASE_CIRRUS_SEARCH_PATH: "extensions/WikibaseCirrusSearch"
    ELASTICA_PATH: "extensions/Elastica"
    CIRRUS_SEARCH_PATH: "extensions/CirrusSearch"
    CONFIRM_EDIT_PATH: "extensions/ConfirmEdit"
  roles:
    - role: git_updater
      vars:
        git_directories:
          - "{{ MW_PATH }}"
          - "{{ MW_PATH }}/{{ VECTOR_PATH }}"
          - "{{ MW_PATH }}/{{ ULS_PATH }}"
          - "{{ MW_PATH }}/{{ CLDR_PATH }}"
          - "{{ MW_PATH }}/{{ WIKIBASE_PATH }}"
          - "{{ MW_PATH }}/{{ WIKIBASE_CIRRUS_SEARCH_PATH }}"
          - "{{ MW_PATH }}/{{ ELASTICA_PATH }}"
          - "{{ MW_PATH }}/{{ CIRRUS_SEARCH_PATH }}"
          - "{{ MW_PATH }}/{{ CONFIRM_EDIT_PATH }}"
        USERNAME: mediawiki
  tasks:
    - name: Ensure essential system packages are installed
      package:
        name: "{{ item }}"
        state: present
      loop:
        - "zip"
        - "unzip"
        - "php-ast"
        - "php-curl"
        - "php-intl"
        - "php-mbstring"
        - "php-mysql"
        - "php-xml"
        - "python3"
        - "python3-pip"
        - "composer"
        - "git"
        - "docker.io"

    - name: Ensure essential python packages are installed
      pip:
        name: "{{ item }}"
        state: present
      loop:
        - "setuptools"
        - "docker-compose"

    - name: Ensure user 'mediawiki' exists and belongs to the 'docker' group
      user:
        name: mediawiki
        groups: docker
        state: present

    - name: Ensure group 'mediawiki' exists
      group:
        name: mediawiki
        state: present

    - name: Ensure project directories exists
      file:
        path: "{{ item.name }}"
        owner: "{{ item.owner }}"
        group: "{{ item.owner }}"
        state: directory
      loop:
        - { name: "{{ PROJECT_DIR }}", owner: "root" }
        - { name: "{{ SCRIPTS_DIR }}", owner: "mediawiki" }
        - { name: "{{ LOG_DIR }}", owner: "mediawiki" }
        - { name: "{{ MW_PATH }}", owner: "mediawiki" }

    - name: Ensure update and logrotate scripts are in place
      template:
        src: "templates/{{ item }}.j2"
        dest: "{{ SCRIPTS_DIR }}/{{ item }}"
        owner: "mediawiki"
        group: "mediawiki"
        mode: 0774
      loop:
        - "update.sh"
        - "logrotate.sh"

    - name: Checkout mediawiki master branch
      git:
        repo: "https://gerrit.wikimedia.org/r/mediawiki/core"
        dest: "{{ MW_PATH }}"
        version: "master"

    - name: Ensure .htaccess is in place
      copy:
        src: "files/htaccess"
        dest: "{{ MW_PATH }}/.htaccess"
        mode: 0644
      tags:
        - source

    - name: Ensure symbolic link for script dir is in place
      file:
        src: "/var/www/html"
        dest: "{{ MW_PATH }}{{ MW_SCRIPT_PATH }}"
        state: link
        force: yes
        follow: no

    - name: Checkout skin and extensions
      git:
        repo: "https://gerrit.wikimedia.org/r/mediawiki/{{ item }}"
        dest: "{{ MW_PATH }}/{{ item }}"
        version: "master"
      loop:
        - "{{ VECTOR_PATH }}"
        - "{{ ULS_PATH }}"
        - "{{ CLDR_PATH }}"
        - "{{ WIKIBASE_PATH }}"
        - "{{ WIKIBASE_CIRRUS_SEARCH_PATH }}"
        - "{{ ELASTICA_PATH }}"
        - "{{ CIRRUS_SEARCH_PATH }}"
        - "{{ CONFIRM_EDIT_PATH }}"

    - name: Ensure composer.local.json is in place
      copy:
        src: "files/composer.local.json"
        dest: "{{ MW_PATH }}"
        mode: 0644

    - name: Run 'composer install' on mediawiki core
      composer:
        command: install
        working_dir: "{{ MW_PATH }}"

    - name: Run 'composer update' on mediawiki core
      composer:
        command: update
        working_dir: "{{ MW_PATH }}"

    - name: Ensure docker is started
      systemd:
        state: started
        name: docker

    - name: Ensure .env is in place
      template:
        src: templates/.env.j2
        dest: "{{ PROJECT_DIR }}/.env"
        owner: "root"
        group: "root"
        mode: 0644
        force: no # don't override if it exists

    - name: Ensure docker-compose.yml is in place
      template:
        src: templates/docker-compose.yml.j2
        dest: "{{ PROJECT_DIR }}/docker-compose.yml"
        owner: "root"
        group: "root"
        mode: 0644

    - name: Run `docker-compose up`
      docker_compose:
        project_src: "{{ PROJECT_DIR }}"
        build: no
        state: present

    # On first start, maintenance/install.php will run, which creates the database tables
    # and finishes by writing a default version of LocalSettings.php. However, the script
    # will not run, if a LocalSettings.php file is in place already.
    # Therefore, we must wait for the default LocalSettings.php file to show up, before we
    # can overwrite it with our own one.
    - name: Wait for wikibase install script to finish
      wait_for:
        path: "{{ MW_PATH }}/LocalSettings.php"

    - name: Recursively change ownership of mediawiki directory to 'mediawiki'
      file:
        path: "{{ MW_PATH }}"
        recurse: yes
        owner: "mediawiki"
        group: "mediawiki"

    - name: Make image directory writeable to Apache
      file:
        path: "{{ MW_PATH }}/image"
        recurse: yes
        group: "www-data"
        mode: 0775

    - name: Ensure LocalSettings.php is in place and readable but not writeable to Apache
      template:
        src: "templates/LocalSettings.php.j2"
        dest: "{{ MW_PATH }}/LocalSettings.php"
        owner: "mediawiki"
        group: "www-data"
        mode: 0640

    - name: Execute update.php inside wikibase container
      command: "docker exec wikibase php maintenance/update.php"
      changed_when: false

    - name: Update ElasticSearch index config
      command: "docker exec wikibase php extensions/CirrusSearch/maintenance/UpdateSearchIndexConfig.php --reindexAndRemoveOk --indexIdentifier=now"

    - name: Update ElasticSearch index
      command: "docker exec wikibase php extensions/CirrusSearch/maintenance/ForceSearchIndex.php"

    - name: Run mediawiki jobs for elastic
      command: "docker exec wikibase php maintenance/runJobs.php"

    - name: Ensure update script runs every five minutes, from 6am to 9pm
      cron:
        name: "update"
        minute: "*/5"
        hour: "6-21"
        job: "{{ SCRIPTS_DIR }}/update.sh"
        user: "mediawiki"

    - name: Ensure update logs are rotated once per day
      cron:
        name: "logrotate"
        minute: "59"
        hour: "23"
        job: "{{ SCRIPTS_DIR }}/logrotate.sh"
        user: "mediawiki"
