From 5466126e3130e2519fd200d03327eb71928e1491 Mon Sep 17 00:00:00 2001 From: Brad Cowie Date: Sun, 17 Nov 2024 16:44:44 +1300 Subject: [PATCH] Simplify archive download tasks --- tasks/install.yml | 189 ++++++++++++++++++++-------------------------- 1 file changed, 80 insertions(+), 109 deletions(-) diff --git a/tasks/install.yml b/tasks/install.yml index a698831..e76c20c 100644 --- a/tasks/install.yml +++ b/tasks/install.yml @@ -29,128 +29,99 @@ - "{{ exporter_bin_dir }}" - "{{ exporter_src_dir }}" -- name: "{{ exporter_name }}: Get exporter URLs from GitHub release" +- name: "{{ exporter_name }}: Get GitHub release information" + ansible.builtin.uri: + url: >- + {{ "https://api.github.com/repos/" + exporter_github_org + "/" + exporter_github_repo + + "/releases" + + ("/tags" if exporter_version != "latest" else "") + "/" + exporter_version }} + method: GET + return_content: true + status_code: 200 + body_format: json + register: _release_info when: - exporter_github_org != "" - exporter_github_repo != "" - block: - - name: "{{ exporter_name }}: Get GitHub release information" - ansible.builtin.uri: - url: >- - {{ "https://api.github.com/repos/" + exporter_github_org + "/" + exporter_github_repo - + "/releases" - + ("/tags" if exporter_version != "latest" else "") + "/" + exporter_version }} - method: GET - return_content: true - status_code: 200 - body_format: json - register: _release_info - - - name: "{{ exporter_name }}: Get archive URL from GitHub release" - ansible.builtin.set_fact: - _exporter_archive_url: >- - {{ _release_info.json.assets - | selectattr("name", "search", exporter_binary, ignorecase=true) - | selectattr("name", "search", "linux", ignorecase=true) - | selectattr("name", "search", exporter_arch_map[ansible_architecture], ignorecase=true) - | map(attribute='browser_download_url') - | first - | default(None) }} - when: exporter_archive_urls | length == 0 - - - name: "{{ exporter_name }}: Get checksum URL from GitHub release" - ansible.builtin.set_fact: - _exporter_checksum_url: >- - {{ _release_info.json.assets - | selectattr("name", "equalto", exporter_github_checksum_filename) - | map(attribute='browser_download_url') - | first - | default(None) }} - when: - - exporter_checksum_url == "" - - exporter_checksums == "" -- name: "{{ exporter_name }}: Get archive URL from variable" - ansible.builtin.set_fact: +- name: Download release archive + vars: _exporter_archive_url: >- + {% if exporter_archive_urls | length >= 1 -%} {{ exporter_archive_urls | select("search", exporter_binary, ignorecase=true) | select("search", "linux", ignorecase=true) | select("search", exporter_arch_map[ansible_architecture], ignorecase=true) | first | default(None) }} - when: exporter_archive_urls | length >= 1 - -- name: "{{ exporter_name }}: Set archive filename" - ansible.builtin.set_fact: + {%- else -%} + {{ _release_info.json.assets + | selectattr("name", "search", exporter_binary, ignorecase=true) + | selectattr("name", "search", "linux", ignorecase=true) + | selectattr("name", "search", exporter_arch_map[ansible_architecture], ignorecase=true) + | map(attribute="browser_download_url") + | first + | default(None) }} + {%- endif %} + _exporter_checksum_url: >- + {% if exporter_checksum_url == "" and exporter_checksums == "" -%} + {{ _release_info.json.assets + | selectattr("name", "equalto", exporter_github_checksum_filename) + | map(attribute="browser_download_url") + | first + | default(None) }} + {%- elif exporter_checksum_url != "" -%} + {{ exporter_checksum_url }} + {%- endif %} _exporter_archive_filename: "{{ _exporter_archive_url | split('/') | last }}" - -- name: "{{ exporter_name }}: Set archive checksum URL from variable" - ansible.builtin.set_fact: - _exporter_checksum_url: "{{ exporter_checksum_url }}" - when: - - exporter_checksum_url != "" - -- name: "{{ exporter_name }}: Get archive checksum from URL" - ansible.builtin.set_fact: _exporter_archive_checksum: >- - {{ lookup('url', _exporter_checksum_url, wantlist=True) - | select('search', _exporter_archive_filename) - | first | default('') | split(' ') | first | default(None) }} - when: - - _exporter_checksum_url | default("") != "" - - exporter_checksums == "" - -- name: "{{ exporter_name }}: Get archive checksum from variable" - ansible.builtin.set_fact: - _exporter_archive_checksum: "{{ exporter_checksums | split('\n') | - select('search', _exporter_archive_filename) - | first | default('') | split(' ') | first | default(None) }}" - when: exporter_checksums != "" - -- name: "{{ exporter_name }}: Download archive" - ansible.builtin.get_url: - url: "{{ _exporter_archive_url }}" - dest: "{{ exporter_src_dir }}/{{ _exporter_archive_filename }}" - checksum: "{{ exporter_checksum_type }}:{{ _exporter_archive_checksum }}" - owner: root - group: root - mode: u=rw,g=r,o=r - -- name: "{{ exporter_name }}: Extract binary from archive" - ansible.builtin.unarchive: - src: "{{ exporter_src_dir }}/{{ _exporter_archive_filename }}" - remote_src: true - dest: "{{ exporter_bin_dir }}" - extra_opts: - - "--strip-components={{ exporter_strip_components }}" - - "--wildcards" - - "--no-anchored" - - "{{ exporter_binary }}" - owner: root - group: root - notify: "{{ exporter_handler }}" + {% if _exporter_checksum_url | default("") != "" and exporter_checksums == "" -%} + {{ lookup("url", _exporter_checksum_url, wantlist=True) + | select("search", _exporter_archive_filename) + | first | default("") | split(" ") | first | default(None) }} + {%- elif exporter_checksums != "" -%} + {% set _exporter_checksum_list = exporter_checksums | split('\n') -%} + {{ _exporter_checksum_list + | select("search", _exporter_archive_filename) + | first | default("") | split(" ") | first | default(None) }} + {%- endif %} + block: + - name: "{{ exporter_name }}: Download archive" + ansible.builtin.get_url: + url: "{{ _exporter_archive_url }}" + dest: "{{ exporter_src_dir }}/{{ _exporter_archive_filename }}" + checksum: "{{ exporter_checksum_type }}:{{ _exporter_archive_checksum }}" + owner: root + group: root + mode: u=rw,g=r,o=r -- name: Get downloaded archive files - ansible.builtin.find: - paths: "{{ exporter_src_dir }}" - patterns: "{{ '*' + _exporter_archive_filename | splitext | last }}" - register: _exporter_src_archives + - name: "{{ exporter_name }}: Extract binary from archive" + ansible.builtin.unarchive: + src: "{{ exporter_src_dir }}/{{ _exporter_archive_filename }}" + remote_src: true + dest: "{{ exporter_bin_dir }}" + extra_opts: + - "--strip-components={{ exporter_strip_components }}" + - "--wildcards" + - "--no-anchored" + - "{{ exporter_binary }}" + owner: root + group: root + notify: "{{ exporter_handler }}" -- name: Remove older downloaded archive files - ansible.builtin.file: - path: "{{ item['path'] }}" - state: absent - loop: "{{ _exporter_src_archives['files'] }}" - loop_control: - label: "{{ item['path'] }}" - when: - - "item['path'] | basename != _exporter_archive_filename" - - "exporter_clean_src_dir" + - name: Get downloaded archive files + ansible.builtin.find: + paths: "{{ exporter_src_dir }}" + patterns: "{{ '*' + _exporter_archive_filename | splitext | last }}" + register: _exporter_src_archives -- name: Unset local variables - ansible.builtin.set_fact: - _exporter_archive_url: "" - _exporter_checksum_url: "" - _exporter_archive_filename: "" - _exporter_archive_checksum: "" + - name: Remove older downloaded archive files + ansible.builtin.file: + path: "{{ item['path'] }}" + state: absent + loop: "{{ _exporter_src_archives['files'] }}" + loop_control: + label: "{{ item['path'] }}" + when: + - "item['path'] | basename != _exporter_archive_filename" + - "exporter_clean_src_dir"