From 2600243a965848fab691351812a50758f3e67b57 Mon Sep 17 00:00:00 2001 From: Svante Bengtson Date: Wed, 19 Jun 2024 20:06:55 +0200 Subject: [PATCH] allow setting systemd timer's RandomizedDelaySec property and depend on timers.target --- EXAMPLES.md | 1 + manifests/init.pp | 82 +++++++------ manifests/repository.pp | 186 ++++++++++++++++------------- manifests/service.pp | 3 +- spec/default_params.rb | 85 ++++++------- spec/shared_examples/repository.rb | 6 +- spec/shared_examples/service.rb | 52 +++++--- spec/tests.rb | 31 +++++ templates/restic.timer.epp | 7 ++ types/repositories.pp | 73 +++++------ 10 files changed, 310 insertions(+), 216 deletions(-) diff --git a/EXAMPLES.md b/EXAMPLES.md index e98d426..7c9e601 100644 --- a/EXAMPLES.md +++ b/EXAMPLES.md @@ -48,6 +48,7 @@ restic::enable_backup: true restic::enable_forget: true restic::backup_timer: 'Mon..Sun *:00:00' restic::forget_timer: Mon..Sun 23:00:00 +restic::forget_timer_random_delay: 6h restic::host: 'host.name' restic::id: 's3id' restic::key: 's3key' diff --git a/manifests/init.pp b/manifests/init.pp index 269e42c..edaaed6 100644 --- a/manifests/init.pp +++ b/manifests/init.pp @@ -37,6 +37,9 @@ # @param backup_timer # Default systemd timer for backup see: https://wiki.archlinux.de/title/Systemd/Timers # +# @param backup_timer_random_delay +# Delay the timer by a random amount of time to avoid multiple hosts running at the same time see: https://www.freedesktop.org/software/systemd/man/latest/systemd.timer.html#RandomizedDelaySec= +# # @param backup_exit3_success # Consider restic's exit code 3 as success. https://restic.readthedocs.io/en/latest/040_backup.html#exit-status-codes # @@ -70,6 +73,9 @@ # @param forget_timer # Default systemd timer for forget see: https://wiki.archlinux.de/title/Systemd/Timers # +# @param forget_timer_random_delay +# Delay the timer by a random amount of time to avoid multiple hosts running at the same time see: https://www.freedesktop.org/software/systemd/man/latest/systemd.timer.html#RandomizedDelaySec= +# # @param global_flags # Default global flags for `restic `. See `restic --help` # @@ -121,6 +127,9 @@ # @param restore_timer # Default systemd timer for restore see: https://wiki.archlinux.de/title/Systemd/Timers # +# @param restore_timer_random_delay +# Delay the timer by a random amount of time to avoid multiple hosts running at the same time see: https://www.freedesktop.org/software/systemd/man/latest/systemd.timer.html#RandomizedDelaySec= +# # @param type # Default name for the Restic repository. Only S3 supported # @@ -146,41 +155,44 @@ ## ## default values for restic::repositories ## - Variant[Array[String[1]],String[1]] $backup_flags = [], - Optional[Restic::Path] $backup_path = undef, - Optional[Variant[Array[String[1]],String[1]]] $backup_pre_cmd = undef, - Optional[Variant[Array[String[1]],String[1]]] $backup_post_cmd = undef, - Optional[String[1]] $backup_timer = undef, - Boolean $backup_exit3_success = false, - Stdlib::Absolutepath $binary = '/usr/bin/restic', - Optional[String] $bucket = undef, - Boolean $enable_backup = true, - Boolean $enable_forget = false, - Boolean $enable_restore = false, - Restic::Forget $forget = {}, - Variant[Array[String[1]],String[1]] $forget_flags = [], - Optional[Variant[Array[String[1]],String[1]]] $forget_pre_cmd = undef, - Optional[Variant[Array[String[1]],String[1]]] $forget_post_cmd = undef, - Optional[String[1]] $forget_timer = undef, - Variant[Array[String[1]],String[1]] $global_flags = [], - Optional[Stdlib::Absolutepath] $gcs_credentials_path = undef, - Optional[Variant[Sensitive[String],String]] $gcs_project_id = undef, - Optional[Variant[Sensitive[String],String]] $gcs_repository = undef, - String $group = 'root', - Optional[Variant[Sensitive[String],String]] $host = undef, - Optional[Variant[Sensitive[String],String]] $id = undef, - Boolean $init_repo = true, - Optional[Variant[Sensitive[String],String]] $key = undef, - Optional[Variant[Sensitive[String],String]] $password = undef, - Boolean $prune = false, - Variant[Array[String[1]],String[1]] $restore_flags = [], - Optional[Stdlib::Absolutepath] $restore_path = undef, - Optional[Variant[Array[String[1]],String[1]]] $restore_pre_cmd = undef, - Optional[Variant[Array[String[1]],String[1]]] $restore_post_cmd = undef, - String[1] $restore_snapshot = 'latest', - Optional[String[1]] $restore_timer = undef, - Restic::Repository::Type $type = 's3', - String[1] $user = 'root', + Variant[Array[String[1]],String[1]] $backup_flags = [], + Optional[Restic::Path] $backup_path = undef, + Optional[Variant[Array[String[1]],String[1]]] $backup_pre_cmd = undef, + Optional[Variant[Array[String[1]],String[1]]] $backup_post_cmd = undef, + Optional[String[1]] $backup_timer = undef, + Optional[Variant[String[1], Integer]] $backup_timer_random_delay = undef, + Boolean $backup_exit3_success = false, + Stdlib::Absolutepath $binary = '/usr/bin/restic', + Optional[String] $bucket = undef, + Boolean $enable_backup = true, + Boolean $enable_forget = false, + Boolean $enable_restore = false, + Restic::Forget $forget = {}, + Variant[Array[String[1]],String[1]] $forget_flags = [], + Optional[Variant[Array[String[1]],String[1]]] $forget_pre_cmd = undef, + Optional[Variant[Array[String[1]],String[1]]] $forget_post_cmd = undef, + Optional[String[1]] $forget_timer = undef, + Optional[Variant[String[1], Integer]] $forget_timer_random_delay = undef, + Variant[Array[String[1]],String[1]] $global_flags = [], + Optional[Stdlib::Absolutepath] $gcs_credentials_path = undef, + Optional[Variant[Sensitive[String],String]] $gcs_project_id = undef, + Optional[Variant[Sensitive[String],String]] $gcs_repository = undef, + String $group = 'root', + Optional[Variant[Sensitive[String],String]] $host = undef, + Optional[Variant[Sensitive[String],String]] $id = undef, + Boolean $init_repo = true, + Optional[Variant[Sensitive[String],String]] $key = undef, + Optional[Variant[Sensitive[String],String]] $password = undef, + Boolean $prune = false, + Variant[Array[String[1]],String[1]] $restore_flags = [], + Optional[Stdlib::Absolutepath] $restore_path = undef, + Optional[Variant[Array[String[1]],String[1]]] $restore_pre_cmd = undef, + Optional[Variant[Array[String[1]],String[1]]] $restore_post_cmd = undef, + String[1] $restore_snapshot = 'latest', + Optional[String[1]] $restore_timer = undef, + Optional[Variant[String[1], Integer]] $restore_timer_random_delay = undef, + Restic::Repository::Type $type = 's3', + String[1] $user = 'root', ) { contain restic::package contain restic::reload diff --git a/manifests/repository.pp b/manifests/repository.pp index 732d2a3..7ed39a3 100644 --- a/manifests/repository.pp +++ b/manifests/repository.pp @@ -16,6 +16,9 @@ # @param backup_timer # Default systemd timer for backup see: https://wiki.archlinux.de/title/Systemd/Timers # +# @param backup_timer_random_delay +# Delay the timer by a random amount of time to avoid multiple hosts running at the same time see: https://www.freedesktop.org/software/systemd/man/latest/systemd.timer.html#RandomizedDelaySec= +# # @param backup_exit3_success # Consider restic's exit code 3 as success. https://restic.readthedocs.io/en/latest/040_backup.html#exit-status-codes # @@ -49,6 +52,9 @@ # @param forget_timer # Default systemd timer for forget see: https://wiki.archlinux.de/title/Systemd/Timers # +# @param forget_timer_random_delay +# Delay the timer by a random amount of time to avoid multiple hosts running at the same time see: https://www.freedesktop.org/software/systemd/man/latest/systemd.timer.html#RandomizedDelaySec= +# # @param global_flags # Default global flags for `restic `. See `restic --help` # @@ -100,6 +106,9 @@ # @param restore_timer # Default systemd timer for restore see: https://wiki.archlinux.de/title/Systemd/Timers # +# @param restore_timer_random_delay +# Delay the timer by a random amount of time to avoid multiple hosts running at the same time see: https://www.freedesktop.org/software/systemd/man/latest/systemd.timer.html#RandomizedDelaySec= +# # @param type # Default name for the Restic repository. Only S3 supported # @@ -107,79 +116,85 @@ # Default user for systemd services # define restic::repository ( - Optional[Boolean] $backup_exit3_success = undef, - Optional[Variant[Array[String[1]],String[1]]] $backup_flags = undef, - Optional[Restic::Path] $backup_path = undef, - Optional[Variant[Array[String[1]],String[1]]] $backup_post_cmd = undef, - Optional[Variant[Array[String[1]],String[1]]] $backup_pre_cmd = undef, - Optional[String[1]] $backup_timer = undef, - Optional[Stdlib::Absolutepath] $binary = undef, - Optional[String] $bucket = undef, - Optional[Boolean] $enable_backup = undef, - Optional[Boolean] $enable_forget = undef, - Optional[Boolean] $enable_restore = undef, - Optional[Restic::Forget] $forget = undef, - Optional[Variant[Array[String[1]],String[1]]] $forget_flags = undef, - Optional[Variant[Array[String[1]],String[1]]] $forget_pre_cmd = undef, - Optional[Variant[Array[String[1]],String[1]]] $forget_post_cmd = undef, - Optional[String[1]] $forget_timer = undef, - Optional[Variant[Array[String[1]],String[1]]] $global_flags = undef, - Optional[Stdlib::Absolutepath] $gcs_credentials_path = undef, - Optional[Variant[Sensitive[String],String]] $gcs_repository = undef, - Optional[Variant[Sensitive[String],String]] $gcs_project_id = undef, - Optional[String] $group = undef, - Optional[Variant[Sensitive[String],String]] $host = undef, - Optional[Variant[Sensitive[String],String]] $id = undef, - Optional[Boolean] $init_repo = undef, - Optional[Variant[Sensitive[String],String]] $key = undef, - Optional[Variant[Sensitive[String],String]] $password = undef, - Optional[Boolean] $prune = undef, - Optional[Variant[Array[String[1]],String[1]]] $restore_flags = undef, - Optional[Stdlib::Absolutepath] $restore_path = undef, - Optional[Variant[Array[String[1]],String[1]]] $restore_post_cmd = undef, - Optional[Variant[Array[String[1]],String[1]]] $restore_pre_cmd = undef, - Optional[String[1]] $restore_snapshot = undef, - Optional[String[1]] $restore_timer = undef, - Optional[Restic::Repository::Type] $type = undef, - Optional[String[1]] $user = undef, + Optional[Boolean] $backup_exit3_success = undef, + Optional[Variant[Array[String[1]],String[1]]] $backup_flags = undef, + Optional[Restic::Path] $backup_path = undef, + Optional[Variant[Array[String[1]],String[1]]] $backup_post_cmd = undef, + Optional[Variant[Array[String[1]],String[1]]] $backup_pre_cmd = undef, + Optional[String[1]] $backup_timer = undef, + Optional[Variant[String[1], Integer]] $backup_timer_random_delay = undef, + Optional[Stdlib::Absolutepath] $binary = undef, + Optional[String] $bucket = undef, + Optional[Boolean] $enable_backup = undef, + Optional[Boolean] $enable_forget = undef, + Optional[Boolean] $enable_restore = undef, + Optional[Restic::Forget] $forget = undef, + Optional[Variant[Array[String[1]],String[1]]] $forget_flags = undef, + Optional[Variant[Array[String[1]],String[1]]] $forget_pre_cmd = undef, + Optional[Variant[Array[String[1]],String[1]]] $forget_post_cmd = undef, + Optional[String[1]] $forget_timer = undef, + Optional[Variant[String[1], Integer]] $forget_timer_random_delay = undef, + Optional[Variant[Array[String[1]],String[1]]] $global_flags = undef, + Optional[Stdlib::Absolutepath] $gcs_credentials_path = undef, + Optional[Variant[Sensitive[String],String]] $gcs_repository = undef, + Optional[Variant[Sensitive[String],String]] $gcs_project_id = undef, + Optional[String] $group = undef, + Optional[Variant[Sensitive[String],String]] $host = undef, + Optional[Variant[Sensitive[String],String]] $id = undef, + Optional[Boolean] $init_repo = undef, + Optional[Variant[Sensitive[String],String]] $key = undef, + Optional[Variant[Sensitive[String],String]] $password = undef, + Optional[Boolean] $prune = undef, + Optional[Variant[Array[String[1]],String[1]]] $restore_flags = undef, + Optional[Stdlib::Absolutepath] $restore_path = undef, + Optional[Variant[Array[String[1]],String[1]]] $restore_post_cmd = undef, + Optional[Variant[Array[String[1]],String[1]]] $restore_pre_cmd = undef, + Optional[String[1]] $restore_snapshot = undef, + Optional[String[1]] $restore_timer = undef, + Optional[Variant[String[1], Integer]] $restore_timer_random_delay = undef, + Optional[Restic::Repository::Type] $type = undef, + Optional[String[1]] $user = undef, ) { include restic - $_backup_exit3_success = pick($backup_exit3_success, $restic::backup_exit3_success) - $_backup_flags = pick($backup_flags, $restic::backup_flags) - $_backup_path = $backup_path.lest || { $restic::backup_path } - $_backup_post_cmd = $backup_post_cmd.lest || { $restic::backup_post_cmd } - $_backup_pre_cmd = $backup_pre_cmd.lest || { $restic::backup_pre_cmd } - $_backup_timer = $backup_timer.lest || { $restic::backup_timer } - $_binary = pick($binary, $restic::binary) - $_bucket = $bucket.lest || { $restic::bucket } - $_enable_backup = pick($enable_backup, $restic::enable_backup) - $_enable_forget = pick($enable_forget, $restic::enable_forget) - $_enable_restore = pick($enable_restore, $restic::enable_restore) - $_forget = pick($forget, $restic::forget) - $_forget_flags = pick($forget_flags, $restic::forget_flags) - $_forget_post_cmd = $forget_post_cmd.lest || { $restic::forget_post_cmd } - $_forget_pre_cmd = $forget_pre_cmd.lest || { $restic::forget_pre_cmd } - $_forget_timer = $forget_timer.lest || { $restic::forget_timer } - $_global_flags = pick($global_flags, $restic::global_flags) - $_gcs_credentials_path = $gcs_credentials_path.lest || { $restic::gcs_credentials_path } - $_gcs_project_id = $gcs_project_id.lest || { $restic::gcs_project_id } - $_gcs_repository = $gcs_repository.lest || { $restic::gcs_repository } - $_group = pick($group, $restic::group) - $_host = $host.lest || { $restic::host } - $_id = $id.lest || { $restic::id } - $_init_repo = pick($init_repo, $restic::init_repo) - $_key = $key.lest || { $restic::key } - $_password = pick($password, $restic::password) - $_prune = pick($prune, $restic::prune) - $_restore_flags = pick($restore_flags, $restic::restore_flags) - $_restore_path = $restore_path.lest || { $restic::restore_path } - $_restore_post_cmd = $restore_post_cmd.lest || { $restic::restore_post_cmd } - $_restore_pre_cmd = $restore_pre_cmd.lest || { $restic::restore_pre_cmd } - $_restore_snapshot = pick($restore_snapshot, $restic::restore_snapshot) - $_restore_timer = $restore_timer.lest || { $restic::restore_timer } - $_type = pick($type, $restic::type) - $_user = pick($user, $restic::user) + $_backup_exit3_success = pick($backup_exit3_success, $restic::backup_exit3_success) + $_backup_flags = pick($backup_flags, $restic::backup_flags) + $_backup_path = $backup_path.lest || { $restic::backup_path } + $_backup_post_cmd = $backup_post_cmd.lest || { $restic::backup_post_cmd } + $_backup_pre_cmd = $backup_pre_cmd.lest || { $restic::backup_pre_cmd } + $_backup_timer = $backup_timer.lest || { $restic::backup_timer } + $_backup_timer_random_delay = $backup_timer_random_delay.lest || { $restic::backup_timer_random_delay } + $_binary = pick($binary, $restic::binary) + $_bucket = $bucket.lest || { $restic::bucket } + $_enable_backup = pick($enable_backup, $restic::enable_backup) + $_enable_forget = pick($enable_forget, $restic::enable_forget) + $_enable_restore = pick($enable_restore, $restic::enable_restore) + $_forget = pick($forget, $restic::forget) + $_forget_flags = pick($forget_flags, $restic::forget_flags) + $_forget_post_cmd = $forget_post_cmd.lest || { $restic::forget_post_cmd } + $_forget_pre_cmd = $forget_pre_cmd.lest || { $restic::forget_pre_cmd } + $_forget_timer = $forget_timer.lest || { $restic::forget_timer } + $_forget_timer_random_delay = $forget_timer_random_delay.lest || { $restic::forget_timer_random_delay } + $_global_flags = pick($global_flags, $restic::global_flags) + $_gcs_credentials_path = $gcs_credentials_path.lest || { $restic::gcs_credentials_path } + $_gcs_project_id = $gcs_project_id.lest || { $restic::gcs_project_id } + $_gcs_repository = $gcs_repository.lest || { $restic::gcs_repository } + $_group = pick($group, $restic::group) + $_host = $host.lest || { $restic::host } + $_id = $id.lest || { $restic::id } + $_init_repo = pick($init_repo, $restic::init_repo) + $_key = $key.lest || { $restic::key } + $_password = pick($password, $restic::password) + $_prune = pick($prune, $restic::prune) + $_restore_flags = pick($restore_flags, $restic::restore_flags) + $_restore_path = $restore_path.lest || { $restic::restore_path } + $_restore_post_cmd = $restore_post_cmd.lest || { $restic::restore_post_cmd } + $_restore_pre_cmd = $restore_pre_cmd.lest || { $restic::restore_pre_cmd } + $_restore_snapshot = pick($restore_snapshot, $restic::restore_snapshot) + $_restore_timer = $restore_timer.lest || { $restic::restore_timer } + $_restore_timer_random_delay = $restore_timer_random_delay.lest || { $restic::restore_timer_random_delay } + $_type = pick($type, $restic::type) + $_user = pick($user, $restic::user) if $_enable_backup and $_backup_path == undef { fail("restic::repository[${title}]: You have to set \$backup_path if you enable the backup!") @@ -284,6 +299,7 @@ enable => $_enable_backup, group => $_group, timer => $_backup_timer, + timer_random_delay => $_backup_timer_random_delay, success_exit_status => $success_exit_status, user => $_user, } @@ -304,13 +320,14 @@ } restic::service { "restic_forget_${title}": - commands => $forget_commands.delete_undef_values, - config => $config_file, - configs => $forget_keys, - enable => $_enable_forget, - group => $_group, - timer => $_forget_timer, - user => $_user, + commands => $forget_commands.delete_undef_values, + config => $config_file, + configs => $forget_keys, + enable => $_enable_forget, + group => $_group, + timer => $_forget_timer, + timer_random_delay => $_forget_timer_random_delay, + user => $_user, } ## @@ -327,12 +344,13 @@ } restic::service { "restic_restore_${title}": - commands => $restore_commands.delete_undef_values, - config => $config_file, - configs => $restore_keys, - enable => $_enable_restore, - group => $_group, - timer => $_restore_timer, - user => $_user, + commands => $restore_commands.delete_undef_values, + config => $config_file, + configs => $restore_keys, + enable => $_enable_restore, + group => $_group, + timer => $_restore_timer, + timer_random_delay => $_restore_timer_random_delay, + user => $_user, } } diff --git a/manifests/service.pp b/manifests/service.pp index 07b8bfa..09dcd14 100644 --- a/manifests/service.pp +++ b/manifests/service.pp @@ -11,6 +11,7 @@ $group, $user, $timer, + $timer_random_delay = undef, $success_exit_status = undef, ) { assert_private() @@ -79,7 +80,7 @@ } $timer_content = $timer ? { - String => epp("${module_name}/restic.timer.epp", { timer => $timer, }), + String => epp("${module_name}/restic.timer.epp", { timer => $timer, timer_random_delay => $timer_random_delay }), Undef => undef, } diff --git a/spec/default_params.rb b/spec/default_params.rb index bfebb7e..2ecd858 100644 --- a/spec/default_params.rb +++ b/spec/default_params.rb @@ -1,43 +1,46 @@ DEFAULTS = { - 'package_ensure' => 'present', - 'package_manage' => true, - 'package_name' => 'restic', - 'package_version' => :undef, - 'checksum' => :undef, - 'install_method' => 'package', - 'repositories' => {}, - 'backup_flags' => [], - 'backup_path' => :undef, - 'backup_pre_cmd' => :undef, - 'backup_post_cmd' => :undef, - 'backup_timer' => :undef, - 'binary' => '/usr/bin/restic', - 'bucket' => :undef, - 'enable_backup' => true, - 'enable_forget' => false, - 'enable_restore' => false, - 'forget' => {}, - 'forget_flags' => [], - 'forget_pre_cmd' => :undef, - 'forget_post_cmd' => :undef, - 'forget_timer' => :undef, - 'global_flags' => [], - 'gcs_credentials_path' => :undef, - 'gcs_project_id' => :undef, - 'gcs_repository' => :undef, - 'group' => 'root', - 'host' => :undef, - 'id' => :undef, - 'init_repo' => true, - 'key' => :undef, - 'password' => :undef, - 'prune' => false, - 'restore_flags' => [], - 'restore_path' => :undef, - 'restore_pre_cmd' => :undef, - 'restore_post_cmd' => :undef, - 'restore_snapshot' => 'latest', - 'restore_timer' => :undef, - 'user' => 'root', - 'type' => 's3', + 'package_ensure' => 'present', + 'package_manage' => true, + 'package_name' => 'restic', + 'package_version' => :undef, + 'checksum' => :undef, + 'install_method' => 'package', + 'repositories' => {}, + 'backup_flags' => [], + 'backup_path' => :undef, + 'backup_pre_cmd' => :undef, + 'backup_post_cmd' => :undef, + 'backup_timer' => :undef, + 'backup_timer_random_delay' => :undef, + 'binary' => '/usr/bin/restic', + 'bucket' => :undef, + 'enable_backup' => true, + 'enable_forget' => false, + 'enable_restore' => false, + 'forget' => {}, + 'forget_flags' => [], + 'forget_pre_cmd' => :undef, + 'forget_post_cmd' => :undef, + 'forget_timer' => :undef, + 'forget_timer_random_delay' => :undef, + 'global_flags' => [], + 'gcs_credentials_path' => :undef, + 'gcs_project_id' => :undef, + 'gcs_repository' => :undef, + 'group' => 'root', + 'host' => :undef, + 'id' => :undef, + 'init_repo' => true, + 'key' => :undef, + 'password' => :undef, + 'prune' => false, + 'restore_flags' => [], + 'restore_path' => :undef, + 'restore_pre_cmd' => :undef, + 'restore_post_cmd' => :undef, + 'restore_snapshot' => 'latest', + 'restore_timer' => :undef, + 'restore_timer_random_delay' => :undef, + 'user' => 'root', + 'type' => 's3', }.freeze diff --git a/spec/shared_examples/repository.rb b/spec/shared_examples/repository.rb index cf424b8..73405bf 100644 --- a/spec/shared_examples/repository.rb +++ b/spec/shared_examples/repository.rb @@ -120,7 +120,7 @@ 'BACKUP_FLAGS' => backup_config.join(' '), } - include_examples 'service', "restic_backup_#{title}", backup_commands, config_file, backup_keys, values['enable_backup'], values['group'], values['user'], values['backup_timer'], success_exit_status + include_examples 'service', "restic_backup_#{title}", backup_commands, config_file, backup_keys, values['enable_backup'], values['group'], values['user'], values['backup_timer'], values['backup_timer_random_delay'], success_exit_status ## ## forget service @@ -147,7 +147,7 @@ 'FORGET_FLAGS' => forget_config.join(' '), } - include_examples 'service', "restic_forget_#{title}", forget_commands, config_file, forget_keys, values['enable_forget'], values['group'], values['user'], values['forget_timer'] + include_examples 'service', "restic_forget_#{title}", forget_commands, config_file, forget_keys, values['enable_forget'], values['group'], values['user'], values['forget_timer'], values['forget_timer_random_delay'] ## ## restore service @@ -172,5 +172,5 @@ 'RESTORE_FLAGS' => restore_config.join(' '), } - include_examples 'service', "restic_restore_#{title}", restore_commands, config_file, restore_keys, values['enable_restore'], values['group'], values['user'], values['restore_timer'] + include_examples 'service', "restic_restore_#{title}", restore_commands, config_file, restore_keys, values['enable_restore'], values['group'], values['user'], values['restore_timer'], values['restore_timer_random_delay'] end diff --git a/spec/shared_examples/service.rb b/spec/shared_examples/service.rb index 1fae07c..f52cab4 100644 --- a/spec/shared_examples/service.rb +++ b/spec/shared_examples/service.rb @@ -1,20 +1,25 @@ # frozen_string_literal: true -shared_examples 'service' do |title, commands, config, configs, enable, group, user, timer = :undef, success_exit_status = :undef| +shared_examples 'service' do |title, commands, config, configs, enable, group, user, timer = :undef, timer_random_delay = :undef, success_exit_status = :undef| it { is_expected.to contain_restic__service(title).with( { - 'commands' => commands, - 'config' => config, - 'configs' => configs, - 'enable' => enable, - 'group' => group, - 'user' => user, - 'timer' => if timer == :undef - nil - else - timer - end, + 'commands' => commands, + 'config' => config, + 'configs' => configs, + 'enable' => enable, + 'group' => group, + 'user' => user, + 'timer' => if timer == :undef + nil + else + timer + end, + 'timer_random_delay' => if timer_random_delay == :undef + nil + else + timer_random_delay + end, }, ) } @@ -109,11 +114,24 @@ else timer_ensure = ensure_value timer_enable = enable - timer_content = [ - '[Timer]', - "OnCalendar=#{timer}", - '', - ].join("\n") + if timer_random_delay == :undef + timer_content = <<~HEREDOC + [Timer] + OnCalendar=#{timer} + + [Install] + WantedBy=timers.target + HEREDOC + else + timer_content = <<~HEREDOC + [Timer] + OnCalendar=#{timer} + RandomizedDelaySec=#{timer_random_delay} + + [Install] + WantedBy=timers.target + HEREDOC + end end it { diff --git a/spec/tests.rb b/spec/tests.rb index 40e93d7..0b37a28 100644 --- a/spec/tests.rb +++ b/spec/tests.rb @@ -73,6 +73,21 @@ } } }, + 'backup and backup_timer and backup_timer_random_delay' => { + 'repositories' => { + 'backup1' => { + 'backup_path' => '/home/rspec', + 'backup_timer' => 'Sunday', + 'backup_timer_random_delay' => '6h', + 'bucket' => 'some_bucket_value', + 'enable_backup' => true, + 'host' => 'some_host_value', + 'id' => 'some_id_value', + 'key' => 'some_key_value', + 'password' => 'some_password_value', + } + } + }, 'backup and backup_pre_cmd' => { 'repositories' => { 'backup2' => { @@ -103,6 +118,22 @@ } } }, + 'restore and restore_timer and restore_timer_random_delay' => { + 'repositories' => { + 'restore1' => { + 'bucket' => 'some_bucket_value', + 'enable_backup' => false, + 'enable_restore' => true, + 'host' => 'some_host_value', + 'id' => 'some_id_value', + 'key' => 'some_key_value', + 'password' => 'some_password_value', + 'restore_path' => '/home/rspec', + 'restore_timer' => 'Sunday', + 'restore_timer_random_delay' => 60, + } + } + }, 'backup and restore and forget' => { 'repositories' => { 'backup3' => { diff --git a/templates/restic.timer.epp b/templates/restic.timer.epp index a979e1b..7d458bf 100644 --- a/templates/restic.timer.epp +++ b/templates/restic.timer.epp @@ -1,5 +1,12 @@ <%- | String $timer, + Optional[Variant[String, Integer]] $timer_random_delay = undef, | -%> [Timer] OnCalendar=<%= $timer %> +<% if $timer_random_delay != undef { -%> +RandomizedDelaySec=<%= $timer_random_delay %> +<% } -%> + +[Install] +WantedBy=timers.target diff --git a/types/repositories.pp b/types/repositories.pp index 90e2616..7934123 100644 --- a/types/repositories.pp +++ b/types/repositories.pp @@ -5,41 +5,44 @@ String[1], Struct[ { - backup_exit3_success => Optional[Boolean], - backup_flags => Optional[Variant[Array[String[1]],String[1]]], - backup_path => Optional[Restic::Path], - backup_post_cmd => Optional[Variant[Array[String[1]],String[1]]], - backup_pre_cmd => Optional[Variant[Array[String[1]],String[1]]], - backup_timer => Optional[String[1]], - binary => Optional[Stdlib::Absolutepath], - bucket => Optional[String], - enable_backup => Optional[Boolean], - enable_forget => Optional[Boolean], - enable_restore => Optional[Boolean], - forget => Optional[Restic::Forget], - forget_flags => Optional[Variant[Array[String[1]],String[1]]], - forget_post_cmd => Optional[Variant[Array[String[1]],String[1]]], - forget_pre_cmd => Optional[Variant[Array[String[1]],String[1]]], - forget_timer => Optional[String[1]], - global_flags => Optional[Variant[Array[String[1]],String[1]]], - gcs_credentials_path => Optional[Stdlib::Absolutepath], - gcs_project_id => Optional[Variant[Sensitive[String],String]], - gcs_repository => Optional[Variant[Sensitive[String],String]], - group => Optional[String], - host => Optional[Variant[Sensitive[String],String]], - id => Optional[Variant[Sensitive[String],String]], - init_repo => Optional[Boolean], - key => Optional[Variant[Sensitive[String],String]], - password => Optional[Variant[Sensitive[String],String]], - prune => Optional[Boolean], - restore_flags => Optional[Variant[Array[String[1]],String[1]]], - restore_path => Optional[Stdlib::Absolutepath], - restore_post_cmd => Optional[Variant[Array[String[1]],String[1]]], - restore_pre_cmd => Optional[Variant[Array[String[1]],String[1]]], - restore_snapshot => Optional[String[1]], - restore_timer => Optional[String[1]], - type => Optional[Restic::Repository::Type], - user => Optional[String[1]], + backup_exit3_success => Optional[Boolean], + backup_flags => Optional[Variant[Array[String[1]],String[1]]], + backup_path => Optional[Restic::Path], + backup_post_cmd => Optional[Variant[Array[String[1]],String[1]]], + backup_pre_cmd => Optional[Variant[Array[String[1]],String[1]]], + backup_timer => Optional[String[1]], + backup_timer_random_delay => Optional[Variant[String[1],Integer]], + binary => Optional[Stdlib::Absolutepath], + bucket => Optional[String], + enable_backup => Optional[Boolean], + enable_forget => Optional[Boolean], + enable_restore => Optional[Boolean], + forget => Optional[Restic::Forget], + forget_flags => Optional[Variant[Array[String[1]],String[1]]], + forget_post_cmd => Optional[Variant[Array[String[1]],String[1]]], + forget_pre_cmd => Optional[Variant[Array[String[1]],String[1]]], + forget_timer => Optional[String[1]], + forget_timer_random_delay => Optional[Variant[String[1],Integer]], + global_flags => Optional[Variant[Array[String[1]],String[1]]], + gcs_credentials_path => Optional[Stdlib::Absolutepath], + gcs_project_id => Optional[Variant[Sensitive[String],String]], + gcs_repository => Optional[Variant[Sensitive[String],String]], + group => Optional[String], + host => Optional[Variant[Sensitive[String],String]], + id => Optional[Variant[Sensitive[String],String]], + init_repo => Optional[Boolean], + key => Optional[Variant[Sensitive[String],String]], + password => Optional[Variant[Sensitive[String],String]], + prune => Optional[Boolean], + restore_flags => Optional[Variant[Array[String[1]],String[1]]], + restore_path => Optional[Stdlib::Absolutepath], + restore_post_cmd => Optional[Variant[Array[String[1]],String[1]]], + restore_pre_cmd => Optional[Variant[Array[String[1]],String[1]]], + restore_snapshot => Optional[String[1]], + restore_timer => Optional[String[1]], + restore_timer_random_delay => Optional[Variant[String[1],Integer]], + type => Optional[Restic::Repository::Type], + user => Optional[String[1]], } ] ]