Skip to content

Commit

Permalink
fix: potential problem when having spaces in the job name.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Filip Chabik committed Feb 9, 2022
1 parent 9416b91 commit af50ba4
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 10 deletions.
8 changes: 4 additions & 4 deletions tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 }}"
Expand All @@ -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)) }}"
Expand All @@ -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 }}"
Expand Down
4 changes: 2 additions & 2 deletions templates/restic-exporter.sh.j2
Original file line number Diff line number Diff line change
@@ -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("(?<no_tz>.*)(?<tz_sgn>[-+])(?<tz_hr>\\d{2}):(?<tz_min>\\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("(?<no_tz>.*)(?<tz_sgn>[-+])(?<tz_hr>\\d{2}):(?<tz_min>\\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
2 changes: 1 addition & 1 deletion templates/restic-wrapper.sh.j2
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
Expand Down
6 changes: 3 additions & 3 deletions templates/restic.cron.j2
Original file line number Diff line number Diff line change
Expand Up @@ -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 -%}
Expand All @@ -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 -%}

Expand Down

0 comments on commit af50ba4

Please sign in to comment.