From 0a35e6622cf2d7cf996181d1100fbaaf565c2cfb Mon Sep 17 00:00:00 2001 From: to-bar <46519524+to-bar@users.noreply.github.com> Date: Thu, 29 Aug 2019 16:45:54 +0200 Subject: [PATCH] Added download role --- .../common/ansible/playbooks/download.yml | 8 +++ .../tasks/download_all_files_to_cache.yml | 11 ++++ .../download/tasks/download_file_to_cache.yml | 62 +++++++++++++++++++ .../playbooks/roles/download/tasks/main.yml | 3 + .../defaults/configuration/download.yml | 47 ++++++++++++++ 5 files changed, 131 insertions(+) create mode 100644 core/src/epicli/data/common/ansible/playbooks/download.yml create mode 100644 core/src/epicli/data/common/ansible/playbooks/roles/download/tasks/download_all_files_to_cache.yml create mode 100644 core/src/epicli/data/common/ansible/playbooks/roles/download/tasks/download_file_to_cache.yml create mode 100644 core/src/epicli/data/common/ansible/playbooks/roles/download/tasks/main.yml create mode 100644 core/src/epicli/data/common/defaults/configuration/download.yml diff --git a/core/src/epicli/data/common/ansible/playbooks/download.yml b/core/src/epicli/data/common/ansible/playbooks/download.yml new file mode 100644 index 0000000000..3169d7ee0b --- /dev/null +++ b/core/src/epicli/data/common/ansible/playbooks/download.yml @@ -0,0 +1,8 @@ +--- +# Ansible playbook that downloads files and images + +- hosts: 127.0.0.1 + gather_facts: no + connection: local + roles: + - download \ No newline at end of file diff --git a/core/src/epicli/data/common/ansible/playbooks/roles/download/tasks/download_all_files_to_cache.yml b/core/src/epicli/data/common/ansible/playbooks/roles/download/tasks/download_all_files_to_cache.yml new file mode 100644 index 0000000000..70491f1325 --- /dev/null +++ b/core/src/epicli/data/common/ansible/playbooks/roles/download/tasks/download_all_files_to_cache.yml @@ -0,0 +1,11 @@ +--- +- name: "download_all_files_to_cache | Prepare list of all URLs with checksums" + set_fact: + urls: "{{ urls | default([]) + [ item ] }}" + loop: "{{ specification.files | json_query('*.values(@) | [].values(@) | []') }}" + +- name: download_all_files_to_cache | Download files + include_tasks: download_file_to_cache.yml + vars: + download: "{{ item }}" + loop: "{{ urls }}" \ No newline at end of file diff --git a/core/src/epicli/data/common/ansible/playbooks/roles/download/tasks/download_file_to_cache.yml b/core/src/epicli/data/common/ansible/playbooks/roles/download/tasks/download_file_to_cache.yml new file mode 100644 index 0000000000..0766f17c7c --- /dev/null +++ b/core/src/epicli/data/common/ansible/playbooks/roles/download/tasks/download_file_to_cache.yml @@ -0,0 +1,62 @@ +--- +- name: download_file_to_cache | Split URL + set_fact: + splitted_url: "{{ download.url | urlsplit }}" + +- name: download_file_to_cache | Set destination path + set_fact: + cached_file_path: "{{ specification.download_cache_dir }}/files/{{ splitted_url.hostname }}{{ splitted_url.path }}" + +- name: download_file_to_cache | Starting download + debug: + msg: + - "From: {{ download.url }}" + - "To: {{ cached_file_path }}" + +# Checking whether file already exists in cache could be handled by 'get_url' module, but 'stat' + when is faster +- name: download_file_to_cache | Check if file is already in cache + stat: + path: "{{ cached_file_path }}" + checksum_algorithm: sha512 + register: check_file_in_cache + +- name: download_file_to_cache | Set fact (is_file_in_cache) + set_fact: + is_file_in_cache: "{{ check_file_in_cache.stat.exists and check_file_in_cache.stat.checksum == download.sha512 }}" + +- name: download_file_to_cache | Get user + become: false + set_fact: + my_ansible_user: "{{ lookup('pipe','id -u -n') }}" + when: + - my_ansible_user is not defined + +- name: download_file_to_cache | Get group + become: false + set_fact: + my_ansible_group: "{{ lookup('pipe','id -g -n') }}" + when: + - my_ansible_group is not defined + +- name: download_file_to_cache | Create directory + file: + path: "{{ cached_file_path | dirname }}" + state: directory + owner: "{{ my_ansible_user }}" + group: "{{ my_ansible_group }}" + become: true + when: + - not is_file_in_cache + +- name: download_file_to_cache | Download file + get_url: + url: "{{ download.url }}" + dest: "{{ cached_file_path }}" + checksum: "{{ 'sha512:' + download.sha512 }}" + validate_certs: "{{ specification.validate_certs }}" + register: get_url_result + until: "'OK' in get_url_result.msg or 'file already exists' in get_url_result.msg" + retries: "{{ specification.download_retries }}" + delay: "{{ specification.download_retry_delay }}" + when: + - not is_file_in_cache \ No newline at end of file diff --git a/core/src/epicli/data/common/ansible/playbooks/roles/download/tasks/main.yml b/core/src/epicli/data/common/ansible/playbooks/roles/download/tasks/main.yml new file mode 100644 index 0000000000..ae95fb9954 --- /dev/null +++ b/core/src/epicli/data/common/ansible/playbooks/roles/download/tasks/main.yml @@ -0,0 +1,3 @@ +--- +- name: Download all files to cache + include_tasks: download_all_files_to_cache.yml \ No newline at end of file diff --git a/core/src/epicli/data/common/defaults/configuration/download.yml b/core/src/epicli/data/common/defaults/configuration/download.yml new file mode 100644 index 0000000000..7200e526d0 --- /dev/null +++ b/core/src/epicli/data/common/defaults/configuration/download.yml @@ -0,0 +1,47 @@ +kind: configuration/download +version: 0.3.0 +title: "Download settings and inventory" +name: default +specification: + download_cache_dir: /var/cache/epiphany + # If true, download directly from Internet (on remote hosts) without using local cache + direct_download: false + # If true, disable SSL validation of get_url module. Checksum validation will still be performed. + validate_certs: true + download_retries: 3 + download_retry_delay: 5 + + files: + cni-plugins: + 0.7.5: + amd64: + url: "https://github.com/containernetworking/plugins/releases/download/v0.7.5/cni-plugins-amd64-v0.7.5.tgz" + sha512: 1abfb567c13f87aab94efd6f2c6bb17f3581cbcce87bf8c6216510a92486cb44e6b8701fd3a6cb85273a97981feecbd0c4eb624eab0eca5a005fd85a7d95c284 + + kubeadm: + 1.14.4: + amd64: + url: "https://storage.googleapis.com/kubernetes-release/release/v1.14.4/bin/linux/amd64/kubeadm" + sha512: 500ab172e7399917c55d9f83a1b7be28f0e85bf165cb606a8db589231b5bc55a5e4d7c8193270aac90ed1e89d8c30eba97876cd1a3f16ad527c911a9f25e1b75 + 1.13.8: + amd64: + url: "https://storage.googleapis.com/kubernetes-release/release/v1.13.8/bin/linux/amd64/kubeadm" + sha512: e4d876e3ff1f8da2ea434362c8c084d3280d3bdf552cfc18f631a317fa2a989313fb729eceafdadf76c85c9cfa3589a3c9b827e6c3154383ea64a1c3219f63bd + 1.12.10: + amd64: + url: "https://storage.googleapis.com/kubernetes-release/release/v1.12.10/bin/linux/amd64/kubeadm" + sha512: dcb6b06195a093a87f952cbb3f471dd690da9baea4a8b09520900301b0e9cee3bfdb50cb28eb10dfdc5b73bbf6bcd08d5c49711f91cea14679ebb1690a9368d9 + + hyperkube: + 1.14.4: + amd64: + url: "https://storage.googleapis.com/kubernetes-release/release/v1.14.4/bin/linux/amd64/hyperkube" + sha512: f32d4a85f7e1316e7abe2c84d051a3a3bd689645b18d4ae50ed89fa80e980e586727f69c01b247593c4182445b0d54f7f623a223a1baf7be10f81204e1298677 + 1.13.8: + amd64: + url: "https://storage.googleapis.com/kubernetes-release/release/v1.13.8/bin/linux/amd64/hyperkube" + sha512: f189a751bc3922aae40b9ee01e8a81bd15ac87286ddae9be6deefcca631c4b0856e443c4b15557d402674a6e54f08502058b3d7fe5326970d057e9113d2b83c8 + 1.12.10: + amd64: + url: "https://storage.googleapis.com/kubernetes-release/release/v1.12.10/bin/linux/amd64/hyperkube" + sha512: 17555aa5748f09b84db2af386f703e4b6d96fda6b7a4e5855f7a455909ef50b4e5de60b86ab60c10de8e3adf7a494bc444e1840645cc25509cbf80d690f4ad07