Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify archive download tasks #9

Merged
merged 1 commit into from
Nov 17, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
189 changes: 80 additions & 109 deletions tasks/install.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
---
- name: "{{ exporter_name }}: Add system group"

Check failure on line 2 in tasks/install.yml

View workflow job for this annotation

GitHub Actions / Ansible Lint

name[template]

Jinja templates should only be at the end of name
ansible.builtin.group:
name: "{{ exporter_group }}"
system: true
state: present
when: exporter_manage_user

- name: "{{ exporter_name }}: Add system user"

Check failure on line 9 in tasks/install.yml

View workflow job for this annotation

GitHub Actions / Ansible Lint

name[template]

Jinja templates should only be at the end of name
ansible.builtin.user:
name: "{{ exporter_user }}"
group: "{{ exporter_group }}"
Expand All @@ -18,7 +18,7 @@
state: present
when: exporter_manage_user

- name: "{{ exporter_name }}: Create directories"

Check failure on line 21 in tasks/install.yml

View workflow job for this annotation

GitHub Actions / Ansible Lint

name[template]

Jinja templates should only be at the end of name
ansible.builtin.file:
path: "{{ item }}"
state: directory
Expand All @@ -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"

Check failure on line 32 in tasks/install.yml

View workflow job for this annotation

GitHub Actions / Ansible Lint

name[template]

Jinja templates should only be at the end of name
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"

Check failure on line 89 in tasks/install.yml

View workflow job for this annotation

GitHub Actions / Ansible Lint

name[template]

Jinja templates should only be at the end of name
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"

Check failure on line 98 in tasks/install.yml

View workflow job for this annotation

GitHub Actions / Ansible Lint

name[template]

Jinja templates should only be at the end of name
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"