---
- hosts: prod

  vars:
    project_path: /var/www/html/clientes
    pvkey_dest: /var/www/html/clientes_id_rsa


  vars_files:
    - ./vars/deploy_vault.yml
    - ./vars/deploy_vars.yml

  become: yes
  become_method: sudo
  become_user: www-data
  become_flags: '-u sh'

  tasks:
    - name: Send key to remote deploy
      copy:
        src: "{{ playbook_dir }}/id_rsa"
        dest: "{{ pvkey_dest }}"
        mode: '0400'

    - name: Clone the repository
      git:
        repo: "git@github.com:siglodelhombreeditores/clientes.git"
        dest: "{{ project_path }}"
        accept_hostkey: yes
        key_file: "{{ pvkey_dest }}"
        force: yes
        version: master

    - name: Set up env parameters
      template:
        src: "{{ playbook_dir }}/templates/.tpl.env"
        dest: "{{ project_path }}/.env.local"

    - name: Set up htaccess
      template:
        src: "{{ playbook_dir }}/templates/.tpl.htaccess"
        dest: "{{ project_path }}/public/.htaccess"

    - name: Install composer dependencies
      composer:
        command: install
        working_dir: "{{ project_path }}"

    - name: Run migrations
      command: "php bin/console doctrine:migrations:migrate --no-interaction --env=prod"
      args:
        chdir: "{{ project_path }}"

    - name: Setup directory permissions for var/
      file:
        path: "{{ project_path }}/var"
        state: directory
        mode: 0775
        recurse: true
        follow: true

    - name: Clear the cache
      command: "php bin/console cache:clear --env=prod"
      args:
        chdir: "{{ project_path }}"

#    - name: Install node dependencies
#      command: yarn install
#      args:
#        chdir: "{{ project_path }}"
#
#    - name: Build node production
#      command: yarn build
#      args:
#        chdir: "{{ project_path }}"

    - name: Remove key
      file:
        path: "{{ pvkey_dest }}"
        state: absent
