---
- name: Deploy new archives to static.x-cart.com/xc5kit
  hosts: static
  gather_facts: false
  remote_user: deploy
  become: no
  vars:
    deploy_version: "5.4.1.5"
    distr_type: "platform"

  tasks:

    - name: "Put all archives to the static.x-cart.com"
      copy:
        src: "/home/jenkins/agent/workspace/xch-cloud-trial-archive/next/_deploy/files/x-cart-{{ deploy_version }}-{{ distr_type }}-{{ distr_lang }}.tgz"
        dest: /u/deploy/xc5kit/alpha/x-cart-{{ deploy_version }}-{{ distr_type }}-{{ distr_lang }}.tgz
      tags:
        - staging

- name: Deploy new cloud base image
  hosts: staging:production
  gather_facts: yes
  remote_user: root
  become: no
  vars:
    base_image_dir: /root/personal-demos/x-cart-src/basedirs
    deploy_version: "5.4.1.5"
    distr_type: "platform"
    remote_archive_name: "https://static.x-cart.com/xc5kit/alpha/x-cart-{{ deploy_version }}-{{ distr_type }}-{{ distr_lang }}.tgz"

  tasks:
      
    - name: Set datetime fact
      ansible.builtin.set_fact:
        fact_date: "{{ ansible_date_time.year }}{{ ansible_date_time.month }}{{ ansible_date_time.day }}"
      tags:
        - staging
        - production

    - name: "Make sure directory is created on trials"
      file:
        state: directory
        path: "{{ item }}"
        owner: root
        group: root
        mode: 0755
      loop:
        - "{{ base_image_dir }}"
        - "{{ base_image_dir }}/{{ distr_type }}"
        - "{{ base_image_dir }}/{{ distr_type }}/{{ deploy_version }}-{{ distr_lang }}"
        - "{{ base_image_dir }}/_tmp"
      tags:
        - staging
        - production


    - name: "Unarchive new Cloud Version to the tmp dir"
      unarchive:
        src: "{{ remote_archive_name }}"
        dest: "{{ base_image_dir }}/_tmp"
        owner: www-data
        group: www-data
        remote_src: yes
      tags:
        - staging
        - production

    - name: "Prepare basedir"
      shell: "rsync -az {{ base_image_dir }}/_tmp/xcart/ {{ base_image_dir }}/{{ distr_type }}/{{ deploy_version }}-{{ distr_lang }}-{{ fact_date }}/ && rm -rf {{ base_image_dir }}/_tmp/"
      tags:
        - staging
        - production

    - name: "Create link to the new cloud base image"
      file:
        state: link
        src: "{{ deploy_version }}-{{ distr_lang }}-{{ fact_date }}"
        dest: "{{ base_image_dir }}/{{ distr_type }}/latest-{{ distr_lang }}"
        owner: www-data
        group: www-data
      tags:
        - staging
        - production

    - name: "Put Build version to the .version file"
      copy:
        content: "{{ distr_type }}-{{ deploy_version }}-{{ distr_lang }}-{{ fact_date }}"
        dest: "{{ base_image_dir }}/{{ distr_type }}/{{ deploy_version }}-{{ distr_lang }}-{{ fact_date }}/.version"
      tags:
        - staging
        - production

- name: Create production archives
  hosts: static
  gather_facts: false
  remote_user: deploy
  become: no
  vars:
    deploy_version: "5.4.1.5"
    distr_type: "platform"

  tasks:
      
    - name: Move archive to production
      ansible.builtin.copy:
        src: "/u/deploy/xc5kit/alpha/x-cart-{{ deploy_version }}-{{ distr_type }}-{{ distr_lang }}.tgz"
        dest: "/u/deploy/xc5kit/x-cart-{{ deploy_version }}-{{ distr_type }}-{{ distr_lang }}.tgz"
        remote_src: yes
      tags:
        - production

    - name: Copy Platform archive to NFR folder
      ansible.builtin.copy:
        src: "/u/deploy/xc5kit/alpha/x-cart-{{ deploy_version }}-{{ distr_type }}-{{ distr_lang }}.tgz"
        dest: "/u/deploy/nfr_distributives/x-cart-{{ deploy_version }}-{{ distr_lang }}.tgz"
        remote_src: yes
      tags:
        - production
      when:
        distr_type == 'platform'

    - name: Make sure old archive is removed
      ansible.builtin.file:
        path: "/u/deploy/xc5kit/alpha/x-cart-{{ deploy_version }}-{{ distr_type }}-{{ distr_lang }}.tgz"
        state: absent
      tags:
        - production

    - name: "Create link to the new archive"
      file:
        state: link
        src: "x-cart-{{ deploy_version }}-{{ distr_type }}-{{ distr_lang }}.tgz"
        dest: "/u/deploy/xc5kit/latest-{{ distr_type }}-{{ distr_lang }}.tgz"
      tags:
        - production
