From af50ba4d009a1c1dce2644862fe543ed9741417c Mon Sep 17 00:00:00 2001 From: Filip Chabik Date: Wed, 9 Feb 2022 16:16:29 +0100 Subject: [PATCH] fix: potential problem when having spaces in the job name. This fix allows for having spaces in the jobs names, they are going to be replaced with hyphens when translated into file names or for the wrapper commands. --- tasks/main.yml | 8 ++++---- templates/restic-exporter.sh.j2 | 4 ++-- templates/restic-wrapper.sh.j2 | 2 +- templates/restic.cron.j2 | 6 +++--- 4 files changed, 10 insertions(+), 10 deletions(-) diff --git a/tasks/main.yml b/tasks/main.yml index 8a30de1..1f42297 100644 --- a/tasks/main.yml +++ b/tasks/main.yml @@ -80,7 +80,7 @@ - name: deploy password files lineinfile: - path: "{{ restic_password_file_path }}/.restic-{{ item.name }}" + path: "{{ restic_password_file_path }}/.restic-{{ item.name | trim | replace(' ', '-') }}" line: "{{ item.password }}" mode: "0440" owner: "root" @@ -93,7 +93,7 @@ - name: deploy wrapper scripts template: src: "restic-wrapper.sh.j2" - dest: "{{ restic_install_path }}/restic-{{ item.name }}" + dest: "{{ restic_install_path }}/restic-{{ item.name | trim | replace(' ', '-') }}" mode: "0770" owner: "root" group: "{{ restic_group }}" @@ -104,7 +104,7 @@ - name: deploy cron script template: src: "restic.cron.j2" - dest: "/etc/cron.d/restic-{{ item.name }}" + dest: "/etc/cron.d/restic-{{ item.name | trim | replace(' ', '-') }}" mode: "0640" with_items: "{{ restic_repos }}" no_log: "{{ restic_ansible_nolog|default(item.no_log | default(true)) }}" @@ -113,7 +113,7 @@ - name: deploy prometheus exporter script template: src: "restic-exporter.sh.j2" - dest: "{{ restic_install_path }}/restic-{{ item.name }}-exporter" + dest: "{{ restic_install_path }}/restic-{{ item.name | trim | replace(' ', '-') }}-exporter" mode: "0770" owner: "root" group: "{{ restic_group }}" diff --git a/templates/restic-exporter.sh.j2 b/templates/restic-exporter.sh.j2 index 7c509ba..fc0e46c 100644 --- a/templates/restic-exporter.sh.j2 +++ b/templates/restic-exporter.sh.j2 @@ -1,5 +1,5 @@ #!/usr/bin/env sh # {{ ansible_managed }} -{{ restic_install_path }}/restic-{{ item.name | trim | quote }} stats latest --json | /usr/bin/jq -r '"restic_stats_total_size_bytes \(.total_size)\nrestic_stats_total_file_count \(.total_file_count)"' | /usr/bin/sponge {{ restic_prometheus_output_path | default('/var/lib/node_exporter/textfile_collector') }}/restic-{{ item.name | trim | quote }}.prom -{{ restic_install_path }}/restic-{{ item.name | trim | quote }} snapshots latest --json | /usr/bin/jq -r 'max_by(.time) | .time | sub("[.][0-9]+"; "") | sub("Z"; "+00:00") | def parseDate(date): date | capture("(?.*)(?[-+])(?\\d{2}):(?\\d{2})$") | (.no_tz + "Z" | fromdateiso8601) - (.tz_sgn + "60" | tonumber) * ((.tz_hr | tonumber) * 60 + (.tz_min | tonumber)); parseDate(.) | "restic_last_snapshot_timestamp \(.)"' | /usr/bin/sponge -a {{ restic_prometheus_output_path | default('/var/lib/node_exporter/textfile_collector') }}/restic-{{ item.name | trim | quote }}.prom +{{ restic_install_path }}/restic-{{ item.name | trim | replace(' ', '-') }} stats latest --json | /usr/bin/jq -r '"restic_stats_total_size_bytes \(.total_size)\nrestic_stats_total_file_count \(.total_file_count)"' | /usr/bin/sponge {{ restic_prometheus_output_path | default('/var/lib/node_exporter/textfile_collector') }}/restic-{{ item.name | trim | replace(' ', '-') }}.prom +{{ restic_install_path }}/restic-{{ item.name | trim | replace(' ', '-') }} snapshots latest --json | /usr/bin/jq -r 'max_by(.time) | .time | sub("[.][0-9]+"; "") | sub("Z"; "+00:00") | def parseDate(date): date | capture("(?.*)(?[-+])(?\\d{2}):(?\\d{2})$") | (.no_tz + "Z" | fromdateiso8601) - (.tz_sgn + "60" | tonumber) * ((.tz_hr | tonumber) * 60 + (.tz_min | tonumber)); parseDate(.) | "restic_last_snapshot_timestamp \(.)"' | /usr/bin/sponge -a {{ restic_prometheus_output_path | default('/var/lib/node_exporter/textfile_collector') }}/restic-{{ item.name | trim | replace(' ', '-') }}.prom diff --git a/templates/restic-wrapper.sh.j2 b/templates/restic-wrapper.sh.j2 index 054a4ed..6a27621 100644 --- a/templates/restic-wrapper.sh.j2 +++ b/templates/restic-wrapper.sh.j2 @@ -2,7 +2,7 @@ # {{ ansible_managed }} export RESTIC_REPOSITORY={{ item.url | trim | quote }} -export RESTIC_PASSWORD_FILE={{ restic_password_file_path }}/.restic-{{ item.name | trim | quote }} +export RESTIC_PASSWORD_FILE={{ restic_password_file_path }}/.restic-{{ item.name | trim | replace(' ', '-') }} {% if item.remote_credentials is defined %} {% for k,v in item.remote_credentials.items() %} export {{ k | upper }}={{ v | trim | quote }} diff --git a/templates/restic.cron.j2 b/templates/restic.cron.j2 index f8f2e45..c821973 100644 --- a/templates/restic.cron.j2 +++ b/templates/restic.cron.j2 @@ -6,7 +6,7 @@ PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:{{ restic_inst {% if item.retention is defined -%} # Apply retention policy -{{ item.retention_time | default('17 3 * * *') }} {{ restic_user }} restic-{{ item.name }} forget --prune +{{ item.retention_time | default('17 3 * * *') }} {{ restic_user }} restic-{{ item.name | trim | replace(' ', '-') }} forget --prune {%- if item.retention.last is defined %} --keep-last {{ item.retention.last }}{% endif -%} {% if item.retention.hourly is defined %} --keep-hourly {{ item.retention.hourly }}{% endif -%} {% if item.retention.daily is defined %} --keep-daily {{ item.retention.daily }}{% endif -%} @@ -18,12 +18,12 @@ PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:{{ restic_inst {% if item.check | default(true) -%} # Check repository -{{ item.check_time | default('17 4 * * *') }} {{ restic_user }} restic-{{ item.name }} check{% if restic_discard_cron_stdout %} > /dev/null{% endif %} +{{ item.check_time | default('17 4 * * *') }} {{ restic_user }} restic-{{ item.name | trim | replace(' ', '-') }} check{% if restic_discard_cron_stdout %} > /dev/null{% endif %} {%- endif %} {% if restic_prometheus_exporter | default(false) -%} # Dump stats for Prometheus -{{ restic_prometheus_time | default('0 0 * * *') }} {{ restic_user }} restic-{{ item.name }}-exporter{% if restic_discard_cron_stdout %} > /dev/null{% endif %} +{{ restic_prometheus_time | default('0 0 * * *') }} {{ restic_user }} restic-{{ item.name | trim | replace(' ', '-') }}-exporter{% if restic_discard_cron_stdout %} > /dev/null{% endif %} {% endif -%}