From 1a6871bd6cd4903ee26b4406f9ffb28ec8d979ed Mon Sep 17 00:00:00 2001 From: kris Date: Mon, 23 May 2022 11:35:11 +0200 Subject: [PATCH 1/3] backupninja/mysql,pgsql: Accept databases only as arrays and simplify code accordingly --- manifests/entry/mysql.pp | 50 ++++++++++++++++------------------------ manifests/entry/pgsql.pp | 16 +++---------- 2 files changed, 23 insertions(+), 43 deletions(-) diff --git a/manifests/entry/mysql.pp b/manifests/entry/mysql.pp index 8299e93..9935671 100644 --- a/manifests/entry/mysql.pp +++ b/manifests/entry/mysql.pp @@ -3,24 +3,24 @@ # This defined type handles the mysql backupninja task entries # define backupninja::entry::mysql ( - Enum['present', 'absent'] $ensure = $backupninja::ensure, - Optional[Integer] $weight = 20, - Variant[Array[String], String] $when = '', - Boolean $hotcopy = false, - Boolean $sqldump = true, - String $sqldumpoptions = '--lock-tables --complete-insert --add-drop-table --quick --quote-names', - Boolean $compress = false, - String $dbhost = '', - Stdlib::Absolutepath $backupdir = "${backupninja::params::backupdir}/mysql", - Variant[Array[String], String] $databases = 'all', - String $user = '', - String $dbusername = '', - String $dbpassword = '', - Stdlib::Absolutepath $configfile = $backupninja::params::mysql_configfile, - String $nodata = '', - Array[String] $nodata_any = [], - String $vsname = '', - String $handler = 'mysql', + Enum['present', 'absent'] $ensure = $backupninja::ensure, + Optional[Integer] $weight = 20, + Variant[Array[String], String] $when = '', + Boolean $hotcopy = false, + Boolean $sqldump = true, + String $sqldumpoptions = '--lock-tables --complete-insert --add-drop-table --quick --quote-names', + Boolean $compress = false, + String $dbhost = '', + Stdlib::Absolutepath $backupdir = "${backupninja::params::backupdir}/mysql", + Array[String] $databases = ['all'], + String $user = '', + String $dbusername = '', + String $dbpassword = '', + Stdlib::Absolutepath $configfile = $backupninja::params::mysql_configfile, + String $nodata = '', + Array[String] $nodata_any = [], + String $vsname = '', + String $handler = 'mysql', ) { require backupninja::params @@ -32,21 +32,11 @@ $_when_real = [] << $when } - if empty($databases) { - $db_list = ['all'] - } - elsif is_string($databases) { - $db_list = split($databases, ' ') - } - else { - $db_list = $databases - } - - if $db_list.size > 1 and 'all' in $db_list { + if $databases.size > 1 and 'all' in $databases { $db_list_real = ['all'] } else { - $db_list_real = $db_list.unique + $db_list_real = $databases.unique } diff --git a/manifests/entry/pgsql.pp b/manifests/entry/pgsql.pp index 043327b..741cbb6 100644 --- a/manifests/entry/pgsql.pp +++ b/manifests/entry/pgsql.pp @@ -8,7 +8,7 @@ Variant[Array[String], String] $when = '', Boolean $compress = false, Stdlib::Absolutepath $backupdir = "${backupninja::params::backupdir}/postgres", - String $databases = 'all', + Array[String] $databases = ['all'], String $format = 'custom', String $handler = 'pgsql', ) { @@ -22,21 +22,11 @@ $_when_real = [] << $when } - if empty($databases) { - $db_list = ['all'] - } - elsif is_string($databases) { - $db_list = split($databases, ' ') - } - else { - $db_list = $databases - } - - if $db_list.size > 1 and 'all' in $db_list { + if $databases.size > 1 and 'all' in $databases { $db_list_real = ['all'] } else { - $db_list_real = $db_list.unique + $db_list_real = $databases.unique } $formats = [ 'plain', 'tar', 'custom' ] From b00eef12c46c1d91416cc39480c7b629a0264580 Mon Sep 17 00:00:00 2001 From: kris Date: Mon, 23 May 2022 12:38:27 +0200 Subject: [PATCH 2/3] backupninja/entry: Refactorize resource declaration --- manifests/entry.pp | 98 ++++------------------------------------------ 1 file changed, 7 insertions(+), 91 deletions(-) diff --git a/manifests/entry.pp b/manifests/entry.pp index 58781e5..6672f95 100644 --- a/manifests/entry.pp +++ b/manifests/entry.pp @@ -19,97 +19,13 @@ default => $when } - case $type { - 'duplicity': { - backupninja::entry::duplicity { $name: - ensure => $ensure, - weight => $weight, - when => $real_when, - # Duplicity - options => $options[options], - nicelevel => $options[nicelevel], - testconnect => $options[testconnect], - sign => $options[sign], - encryptkey => $options[encryptkey], - signkey => $options[signkey], - password => $options[password], - include => $options[include], - exclude => $options[exclude], - incremental => $options[incremental], - increments => $options[increments], - keep => $options[keep], - desturl => $options[desturl], - awsaccesskeyid => $options[awsaccesskeyid], - awssecretaccesskey => $options[awssecretaccesskey], - ftp_password => $options[ftp_password], - bandwidthlimit => $options[bandwidthlimit], - sshoptions => $options[sshoptions], - destdir => $options[destdir], - desthost => $options[desthost], - destuser => $options[destuser], - } - } - 'ldap': { - backupninja::entry::ldap { $name: - ensure => $ensure, - weight => $weight, - when => $real_when, - # Ldap - backupdir => $options[backupdir], - suffixes => $options[suffixes], - compress => $options[compress], - ldif => $options[ldif], - restart => $options[restart], - } - } - 'mysql': { - backupninja::entry::mysql { $name: - ensure => $ensure, - weight => $weight, - when => $real_when, - # MySQL - hotcopy => $options[hotcopy], - sqldump => $options[sqldump], - sqldumpoptions => $options[sqldumpoptions], - compress => $options[compress], - dbhost => $options[dbhost], - backupdir => $options[backupdir], - databases => $options[databases], - user => $options[user], - dbusername => $options[dbusername], - dbpassword => $options[dbpassword], - configfile => $options[configfile], - nodata => $options[nodata], - nodata_any => $options[nodata_any], - vsname => $options[vsname], - handler => $options[handler], - } - } - 'pgsql': { - backupninja::entry::pgsql { $name: - ensure => $ensure, - weight => $weight, - when => $real_when, - # pgsql - compress => $options[compress], - backupdir => $options[backupdir], - databases => $options[databases], - format => $options[format], - handler => $options[handler], - } - } - 'sh': { - backupninja::entry::sh { $name: - ensure => $ensure, - weight => $weight, - when => $real_when, - #sh - commands => $options[commands], - } - } - default: { - fail "Uknown type ${type} for backupninja::entry" - } + $defaults = { + 'ensure' => $ensure, + 'weight' => $weight, + 'when' => $real_when, } + $params = merge($defaults, $options) + ensure_resource("backupninja::entry::${type}", $name, $params) + } From a52e8797d9cc34436b7b43d9a4e796c4d0579c2a Mon Sep 17 00:00:00 2001 From: kris Date: Mon, 23 May 2022 13:06:53 +0200 Subject: [PATCH 3/3] Generalize _when_real obtainment --- manifests/entry.pp | 8 +++++++- manifests/entry/duplicity.pp | 6 ------ manifests/entry/ldap.pp | 6 ------ manifests/entry/mysql.pp | 6 ------ manifests/entry/params.pp | 10 +++++----- manifests/entry/pgsql.pp | 6 ------ manifests/entry/sh.pp | 6 ------ templates/entry/duplicity.erb | 2 +- templates/entry/ldap.erb | 2 +- templates/entry/mysql.erb | 2 +- templates/entry/pgsql.erb | 2 +- templates/entry/sh.erb | 2 +- 12 files changed, 17 insertions(+), 41 deletions(-) diff --git a/manifests/entry.pp b/manifests/entry.pp index 6672f95..d7dc8e4 100644 --- a/manifests/entry.pp +++ b/manifests/entry.pp @@ -19,10 +19,16 @@ default => $when } + if $real_when =~ Array[String] { + $_when_real = $real_when + } else { + $_when_real = [] << $real_when + } + $defaults = { 'ensure' => $ensure, 'weight' => $weight, - 'when' => $real_when, + 'when' => $_when_real, } $params = merge($defaults, $options) diff --git a/manifests/entry/duplicity.pp b/manifests/entry/duplicity.pp index 2aa83d3..9d89a06 100644 --- a/manifests/entry/duplicity.pp +++ b/manifests/entry/duplicity.pp @@ -49,12 +49,6 @@ require backupninja::entry::params - if $when =~ Array[String] { - $_when_real = $when - } else { - $_when_real = [] << $when - } - if ! defined(Package[$backupninja::entry::params::duplicity_package_name]) { package { $backupninja::entry::params::duplicity_package_name: ensure => $backupninja::ensure, diff --git a/manifests/entry/ldap.pp b/manifests/entry/ldap.pp index 4e468ed..211eda7 100644 --- a/manifests/entry/ldap.pp +++ b/manifests/entry/ldap.pp @@ -17,12 +17,6 @@ require backupninja::params require backupninja::entry::params - if $when =~ Array[String] { - $_when_real = $when - } else { - $_when_real = [] << $when - } - file { "${backupninja::params::config_dir}/${weight}_${name}.${handler}" : ensure => $ensure, owner => 'root', diff --git a/manifests/entry/mysql.pp b/manifests/entry/mysql.pp index 9935671..6447f22 100644 --- a/manifests/entry/mysql.pp +++ b/manifests/entry/mysql.pp @@ -26,12 +26,6 @@ require backupninja::params require backupninja::entry::params - if $when =~ Array[String] { - $_when_real = $when - } else { - $_when_real = [] << $when - } - if $databases.size > 1 and 'all' in $databases { $db_list_real = ['all'] } diff --git a/manifests/entry/params.pp b/manifests/entry/params.pp index 5bc30a8..d6a842c 100644 --- a/manifests/entry/params.pp +++ b/manifests/entry/params.pp @@ -6,11 +6,11 @@ require backupninja::params - if $backupninja::params::when =~ Array[String] { - $_when_real = $backupninja::params::when - } else { - $_when_real = [] << $backupninja::params::when - } +# if $backupninja::params::when =~ Array[String] { +# $_when_real = $backupninja::params::when +# } else { +# $_when_real = [] << $backupninja::params::when +# } case $::osfamily { 'Debian': { diff --git a/manifests/entry/pgsql.pp b/manifests/entry/pgsql.pp index 741cbb6..e94557c 100644 --- a/manifests/entry/pgsql.pp +++ b/manifests/entry/pgsql.pp @@ -16,12 +16,6 @@ require backupninja::params require backupninja::entry::params - if $when =~ Array[String] { - $_when_real = $when - } else { - $_when_real = [] << $when - } - if $databases.size > 1 and 'all' in $databases { $db_list_real = ['all'] } diff --git a/manifests/entry/sh.pp b/manifests/entry/sh.pp index 94ff4b5..27a8831 100644 --- a/manifests/entry/sh.pp +++ b/manifests/entry/sh.pp @@ -11,12 +11,6 @@ require backupninja::params - if $when =~ Array[String] { - $_when_real = $when - } else { - $_when_real = [] << $when - } - file { "${backupninja::params::config_dir}/${weight}_${name}.sh" : ensure => $ensure, owner => 'root', diff --git a/templates/entry/duplicity.erb b/templates/entry/duplicity.erb index 54f2195..fc40d87 100644 --- a/templates/entry/duplicity.erb +++ b/templates/entry/duplicity.erb @@ -1,6 +1,6 @@ # MANAGED BY PUPPET! -<%- @_when_real.each do |time| -%> +<%- @when.each do |time| -%> when = <%= time %> <%- end -%> diff --git a/templates/entry/ldap.erb b/templates/entry/ldap.erb index 69c0898..e9a7e51 100644 --- a/templates/entry/ldap.erb +++ b/templates/entry/ldap.erb @@ -1,6 +1,6 @@ # MANAGED BY PUPPET! -<%- @_when_real.each do |time| -%> +<%- @when.each do |time| -%> when = <%= time %> <%- end -%> diff --git a/templates/entry/mysql.erb b/templates/entry/mysql.erb index a341036..e54391c 100644 --- a/templates/entry/mysql.erb +++ b/templates/entry/mysql.erb @@ -1,6 +1,6 @@ # MANAGED BY PUPPET! -<%- @_when_real.each do |time| -%> +<%- @when.each do |time| -%> when = <%= time %> <%- end -%> diff --git a/templates/entry/pgsql.erb b/templates/entry/pgsql.erb index 92445aa..b76be90 100644 --- a/templates/entry/pgsql.erb +++ b/templates/entry/pgsql.erb @@ -1,6 +1,6 @@ # MANAGED BY PUPPET! -<%- @_when_real.each do |time| -%> +<%- @when.each do |time| -%> when = <%= time %> <%- end -%> diff --git a/templates/entry/sh.erb b/templates/entry/sh.erb index afad5a0..eea0bf5 100644 --- a/templates/entry/sh.erb +++ b/templates/entry/sh.erb @@ -1,6 +1,6 @@ # MANAGED BY PUPPET! -<%- @_when_real.each do |time| -%> +<%- @when.each do |time| -%> when = <%= time %> <%- end -%>