- hosts: test_systems
  name: "Setup and run a test system for the 'WikibaseManifest' extension"
  become: yes
  vars:
    VECTOR_SUBPATH: skins/Vector
    ULS_SUBPATH: extensions/UniversalLanguageSelector
    CLDR_SUBPATH: extensions/cldr
    WIKIBASE_SUBPATH: extensions/Wikibase
    WIKIBASEMANIFEST_SUBPATH: extensions/WikibaseManifest
    OAUTH_SUBPATH: extensions/OAuth
  roles:
    - role: git_updater
      vars:
        git_directories:
          - "{{ MW_PATH }}"
          - "{{ MW_PATH }}/{{ VECTOR_SUBPATH }}"
          - "{{ MW_PATH }}/{{ ULS_SUBPATH }}"
          - "{{ MW_PATH }}/{{ CLDR_SUBPATH }}"
          - "{{ MW_PATH }}/{{ WIKIBASE_SUBPATH }}"
          - "{{ MW_PATH }}/{{ WIKIBASEMANIFEST_SUBPATH }}"
          - "{{ MW_PATH }}/{{ OAUTH_SUBPATH }}"
        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
        - patch
        - 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
        mode: 0755
      loop:
        - { name: "{{ PROJECT_DIR }}", owner: root }
        - { name: "{{ PROJECT_DIR }}/docker", owner: root }
        - { name: "{{ SCRIPTS_DIR }}", owner: mediawiki }
        - { name: "{{ LOG_DIR }}", owner: mediawiki }
        - { name: "{{ MW_PATH }}", owner: mediawiki }
        - { name: "{{ MW_PATH }}/LocalSettings.d", 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: Clone mediawiki master
      git:
        repo: "https://gerrit.wikimedia.org/r/mediawiki/core"
        dest: "{{ MW_PATH }}"
        depth: 1
        version: master

    - name: Clone Vector skin and extensions
      git:
        repo: "https://gerrit.wikimedia.org/r/mediawiki/{{ item }}"
        dest: "{{ MW_PATH }}/{{ item }}"
        depth: 1
        version: master
      loop:
        - "{{ VECTOR_SUBPATH }}"
        - "{{ ULS_SUBPATH }}"
        - "{{ CLDR_SUBPATH }}"
        - "{{ WIKIBASE_SUBPATH }}"
        - "{{ WIKIBASEMANIFEST_SUBPATH }}"
        - "{{ OAUTH_SUBPATH }}"

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

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

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

    - name: Ensure docker build files are in place
      copy:
        src: "files/{{ item }}"
        dest: "{{ PROJECT_DIR }}/docker/{{ item }}"
        mode: 0644
      loop:
        - Dockerfile
        - entrypoint.sh
        - wait-for-it.sh
        - extraSettings.php.fragment

    - name: Build wikibase image
      docker_image:
        name: wikibase-manifest-webserver
        build:
          nocache: yes
          path: "{{ PROJECT_DIR }}/docker"
          pull: yes
        source: build
        force_source: yes

    - 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: Recursively change ownership of MediaWiki directory to allow update
      file:
        path: "{{ MW_PATH }}"
        recurse: yes
        owner: mediawiki
        group: mediawiki

    - name: Clean LocalSettings
      file:
        state: absent
        path: "{{ MW_PATH }}/LocalSettings.php"

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

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

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