- name: a play that runs entirely on the ansible host
  hosts: 127.0.0.1
  connection: local

  vars:
    upload_shacache: "{{ lookup('env','UPLOAD') }}"

  tasks:

### Build instances 

    - shell: mkdir -p log/
    - easy_install:
        name: slapos.libnetworkcache

    - shell: PATH=$PATH:/opt/packer/ packer build -var 'disk_size={{ item.size }}' -var 'name={{ item.name }}' -var 'custom_script={{ item.script }}' debian9.json >> log/debian9-{{ item.name }}.log
      args:
        creates: output-debian9-{{ item.size }}G-{{ item.name }}
      with_items:
        - { name: "vm-bootstrap", size: "200", script: "scripts/vm-bootstrap.sh"}
        - { name: "vm-bootstrap", size: "50", script: "scripts/vm-bootstrap.sh"}
        - { name: "vm-bootstrap", size: "25", script: "scripts/vm-bootstrap.sh"}

    - shell: PATH=$PATH:/opt/packer/ packer build -var 'disk_size={{ item.size }}' -var 'name={{ item.name }}' -var 'custom_script={{ item.script }}' debian10.json >> log/debian10-{{ item.name }}.log
      args:
        creates: output-debian10-{{ item.size }}G-{{ item.name }}
      with_items:
        - { name: "vm-bootstrap", size: "200", script: "scripts/vm-bootstrap.sh"}
        - { name: "vm-bootstrap", size: "50", script: "scripts/vm-bootstrap.sh"}
        - { name: "vm-bootstrap", size: "25", script: "scripts/vm-bootstrap.sh"}


    - shell: PATH=$PATH:/opt/packer/ packer build -var 'disk_size={{ item.size }}' -var 'name={{ item.name }}' -var 'custom_script={{ item.script }}' ubuntu-18.04-server-amd64.json >> log/ubuntu-18.04-server-amd64-{{ item.name }}.log
      args:
        creates: output-ubuntu-18.04-server-{{ item.size }}G-{{ item.name }}
      with_items:
        - { name: "vm-bootstrap", size: "200", script: "scripts/vm-bootstrap.sh"}
        - { name: "vm-bootstrap", size: "50", script: "scripts/vm-bootstrap.sh"}
        - { name: "vm-bootstrap", size: "25", script: "scripts/vm-bootstrap.sh"}


    - shell: PATH=$PATH:/opt/packer/ packer build -var 'disk_size={{ item.size }}' -var 'name={{ item.name }}' -var 'custom_script={{ item.script }}' ubuntu-20.04-server-amd64.json >> log/ubuntu-20.04-server-amd64-{{ item.name }}.log
      args:
        creates: output-ubuntu-20.04-server-{{ item.size }}G-{{ item.name }}
      with_items:
        - { name: "vm-bootstrap", size: "200", script: "scripts/vm-bootstrap.sh"}
        - { name: "vm-bootstrap", size: "50", script: "scripts/vm-bootstrap.sh"}
        - { name: "vm-bootstrap", size: "25", script: "scripts/vm-bootstrap.sh"}


#    - shell: PATH=$PATH:/opt/packer/ packer build -var 'disk_size={{ item.size }}' -var 'name={{ item.name }}' -var 'custom_script={{ item.script }}' centos8.json >> log/centos8-{{ item.name }}.log
#      args:
#        creates: output-centos8-{{ item.size }}G-{{ item.name }}
#      with_items:
#        - { name: "vm-bootstrap", size: "200", script: "scripts/vm-bootstrap.sh"}
#        - { name: "vm-bootstrap", size: "50", script: "scripts/vm-bootstrap.sh"}
#        - { name: "vm-bootstrap", size: "25", script: "scripts/vm-bootstrap.sh"}

    - shell: PATH=$PATH:/opt/packer/ packer build -var 'disk_size={{ item.size }}' -var 'name={{ item.name }}' -var 'custom_script={{ item.script }}' centos7.json >> log/centos7-{{ item.name }}.log
      args:
        creates: output-centos7-{{ item.size }}G-{{ item.name }}
      with_items:
        - { name: "vm-bootstrap", size: "200", script: "scripts/vm-bootstrap.sh"}
        - { name: "vm-bootstrap", size: "50", script: "scripts/vm-bootstrap.sh"}
        - { name: "vm-bootstrap", size: "25", script: "scripts/vm-bootstrap.sh"}

#### Genetation of images are over.
#### Start compress everything


    - shell: gzip output-debian9-{{ item.size }}G-{{ item.name }}/packer-debian9-{{ item.size }}G-{{ item.name }}
      args:
        creates: output-debian9-{{ item.size }}G-{{ item.name }}/packer-debian9-{{ item.size }}G-{{ item.name }}.gz
      with_items:
        - { name: "vm-bootstrap", size: "200"}
        - { name: "vm-bootstrap", size: "50"}
        - { name: "vm-bootstrap", size: "25"}
      ignore_errors: True

    - shell: gzip output-debian10-{{ item.size }}G-{{ item.name }}/packer-debian10-{{ item.size }}G-{{ item.name }}
      args:
        creates: output-debian10-{{ item.size }}G-{{ item.name }}/packer-debian10-{{ item.size }}G-{{ item.name }}.gz
      with_items:
        - { name: "vm-bootstrap", size: "200"}
        - { name: "vm-bootstrap", size: "50"}
        - { name: "vm-bootstrap", size: "25"}
      ignore_errors: True

    - shell: gzip output-ubuntu-18.04-server-{{ item.size }}G-{{ item.name }}/packer-ubuntu-18.04-server-{{ item.size }}G-{{ item.name }}
      args:
        creates: output-ubuntu-18.04-server-{{ item.size }}G-{{ item.name }}/packer-ubuntu-18.04-server-{{ item.size }}G-{{ item.name }}.gz
      with_items:
        - { name: "vm-bootstrap", size: "200"}
        - { name: "vm-bootstrap", size: "50"}
        - { name: "vm-bootstrap", size: "25"}
      ignore_errors: True

    - shell: gzip output-ubuntu-20.04-server-{{ item.size }}G-{{ item.name }}/packer-ubuntu-20.04-server-{{ item.size }}G-{{ item.name }}
      args:
        creates: output-ubuntu-20.04-server-{{ item.size }}G-{{ item.name }}/packer-ubuntu-20.04-server-{{ item.size }}G-{{ item.name }}.gz
      with_items:
        - { name: "vm-bootstrap", size: "200"}
        - { name: "vm-bootstrap", size: "50"}
        - { name: "vm-bootstrap", size: "25"}
      ignore_errors: True

    - shell: gzip output-centos8-{{ item.size }}G-{{ item.name }}/packer-centos8-{{ item.size }}G-{{ item.name }}
      args:
        creates: output-centos8-{{ item.size }}G-{{ item.name }}/packer-centos8-{{ item.size }}G-{{ item.name }}.gz
      with_items:
        - { name: "vm-bootstrap", size: "200"}
        - { name: "vm-bootstrap", size: "50"}
        - { name: "vm-bootstrap", size: "25"}
      ignore_errors: True

    - shell: gzip output-centos7-{{ item.size }}G-{{ item.name }}/packer-centos7-{{ item.size }}G-{{ item.name }}
      args:
        creates: output-centos7-{{ item.size }}G-{{ item.name }}/packer-centos7-{{ item.size }}G-{{ item.name }}.gz
      with_items:
        - { name: "vm-bootstrap", size: "200"}
        - { name: "vm-bootstrap", size: "50"}
        - { name: "vm-bootstrap", size: "25"}
      ignore_errors: True

#### Gzip is over, start to upload to shacache
#### Build SHA512SUM.txt and MD5SUM.txt of every file.

    - shell: rm -f SHA512SUM.txt MD5SUM.txt URL.txt

    - shell: |
        FILE=output-{{ item.distro }}-{{ item.size }}G-{{ item.name }}/packer-{{ item.distro }}-{{ item.size }}G-{{ item.name }}.gz
        sha512sum $FILE >> SHA512SUM.txt
        md5sum $FILE >> MD5SUM.txt
      with_items:
        - { name: "vm-bootstrap", size: "200", distro: "debian9"}
        - { name: "vm-bootstrap", size: "50", distro: "debian9"}
        - { name: "vm-bootstrap", size: "25", distro: "debian9"}
        - { name: "vm-bootstrap", size: "200", distro: "debian10"}
        - { name: "vm-bootstrap", size: "50", distro: "debian10"}
        - { name: "vm-bootstrap", size: "25", distro: "debian10"}
        - { name: "vm-bootstrap", size: "200", distro: "centos7"}
        - { name: "vm-bootstrap", size: "50", distro: "centos7"}
        - { name: "vm-bootstrap", size: "25", distro: "centos7"}
        - { name: "vm-bootstrap", size: "200", distro: "centos8"}
        - { name: "vm-bootstrap", size: "50", distro: "centos8"}
        - { name: "vm-bootstrap", size: "25", distro: "centos8"}
        - { name: "vm-bootstrap", size: "200", distro: "ubuntu-18.04-server"}
        - { name: "vm-bootstrap", size: "50", distro: "ubuntu-18.04-server"}
        - { name: "vm-bootstrap", size: "25", distro: "ubuntu-18.04-server"}
        - { name: "vm-bootstrap", size: "200", distro: "ubuntu-20.04-server"}
        - { name: "vm-bootstrap", size: "50", distro: "ubuntu-20.04-server"}
        - { name: "vm-bootstrap", size: "25", distro: "ubuntu-20.04-server"}

