Skip to content

Commit 26c3eff

Browse files
Dinesh GujarDinesh Gujar
Dinesh Gujar
authored and
Dinesh Gujar
committed
(CAT-1147)-erb to epp conversion
1 parent fae6549 commit 26c3eff

15 files changed

+343
-234
lines changed

.rubocop_todo.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ RSpec/NamedSubject:
9595
- 'spec/classes/mysql_server_spec.rb'
9696
- 'spec/defines/mysql_db_spec.rb'
9797
- 'spec/functions/mysql_normalise_and_deepmerge_spec.rb'
98+
- 'spec/functions/mysql_innobackupex_args_spec.rb'
9899
- 'spec/functions/mysql_strip_hash_spec.rb'
99100

100101
# Offense count: 45
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# frozen_string_literal: true
2+
3+
Puppet::Functions.create_function(:'mysql::innobackupex_args') do
4+
dispatch :innobackupex_args do
5+
required_param 'Optional[String]', :backupuser
6+
required_param 'Boolean', :backupcompress
7+
required_param 'Optional[Variant[String, Sensitive[String]]]', :backuppassword_unsensitive
8+
required_param 'Array[String[1]]', :backupdatabases
9+
required_param 'Array[String[1]]', :optional_args
10+
return_type 'Variant[String]'
11+
end
12+
13+
def innobackupex_args(backupuser, backupcompress, backuppassword_unsensitive, backupdatabases, optional_args)
14+
innobackupex_args = ''
15+
innobackupex_args = "--user=\"#{backupuser}\" --password=\"#{backuppassword_unsensitive}\"" if backupuser && backuppassword_unsensitive
16+
17+
innobackupex_args = "#{innobackupex_args} --compress" if backupcompress
18+
19+
innobackupex_args = "#{innobackupex_args} --databases=\"#{backupdatabases.join(' ')}\"" if backupdatabases.is_a?(Array) && !backupdatabases.empty?
20+
21+
if optional_args.is_a?(Array)
22+
optional_args.each do |arg|
23+
innobackupex_args = "#{innobackupex_args} #{arg}"
24+
end
25+
end
26+
innobackupex_args
27+
end
28+
end

manifests/backup/mysqldump.pp

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,14 +88,38 @@
8888
require => File['mysqlbackup.sh'],
8989
}
9090

91+
$parameters = {
92+
'backupuser'=> $backupuser,
93+
'backuppassword_unsensitive'=> $backuppassword_unsensitive,
94+
'maxallowedpacket'=> $maxallowedpacket,
95+
'backupdir'=> $backupdir,
96+
'backuprotate'=> $backuprotate,
97+
'prescript'=> $prescript,
98+
'ignore_events'=> $ignore_events,
99+
'backupdatabases'=> $backupdatabases,
100+
'include_triggers'=> $include_triggers,
101+
'optional_args'=> $optional_args,
102+
'execpath'=> $execpath,
103+
'delete_before_dump'=> $delete_before_dump,
104+
'excludedatabases'=> $excludedatabases,
105+
'backupmethod'=> $backupmethod,
106+
'backupcompress'=> $backupcompress,
107+
'compression_command'=> $compression_command,
108+
'compression_extension'=> $compression_extension,
109+
'backup_success_file_path'=> $backup_success_file_path,
110+
'postscript'=> $postscript,
111+
'file_per_database'=> $file_per_database,
112+
'include_routines' => $include_routines,
113+
}
114+
91115
# TODO: use EPP instead of ERB, as EPP can handle Data of Type Sensitive without further ado
92116
file { 'mysqlbackup.sh':
93117
ensure => $ensure,
94118
path => '/usr/local/sbin/mysqlbackup.sh',
95119
mode => '0700',
96120
owner => 'root',
97121
group => $mysql::params::root_group,
98-
content => template('mysql/mysqlbackup.sh.erb'),
122+
content => epp('mysql/mysqlbackup.sh.epp',$parameters),
99123
}
100124

101125
if $mysqlbackupdir_target {

manifests/backup/xtrabackup.pp

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
String[1] $backupdirgroup = $mysql::params::root_group,
1414
Boolean $backupcompress = true,
1515
Variant[Integer, String[1]] $backuprotate = 30,
16-
String[1] $backupscript_template = 'mysql/xtrabackup.sh.erb',
16+
String[1] $backupscript_template = 'mysql/xtrabackup.sh.epp',
1717
Optional[String[1]] $backup_success_file_path = undef,
1818
Boolean $ignore_events = true,
1919
Boolean $delete_before_dump = false,
@@ -176,12 +176,23 @@
176176
}
177177

178178
# TODO: use EPP instead of ERB, as EPP can handle Data of Type Sensitive without further ado
179+
$parameters = {
180+
'innobackupex_args' => mysql::innobackupex_args($backupuser, $backupcompress, $backuppassword_unsensitive, $backupdatabases, $optional_args),
181+
'backuprotate' => $backuprotate,
182+
'backupdir' => $backupdir,
183+
'backupmethod' => $backupmethod,
184+
'delete_before_dump' => $delete_before_dump,
185+
'prescript' => $prescript,
186+
'backup_success_file_path'=> $backup_success_file_path,
187+
'postscript'=> $postscript,
188+
}
189+
179190
file { 'xtrabackup.sh':
180191
ensure => $ensure,
181192
path => '/usr/local/sbin/xtrabackup.sh',
182193
mode => '0700',
183194
owner => 'root',
184195
group => $mysql::params::root_group,
185-
content => template($backupscript_template),
196+
content => epp($backupscript_template,$parameters),
186197
}
187198
}

manifests/server/config.pp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,15 @@
6161
default: {}
6262
}
6363

64+
$parameters= {
65+
'options' => $options,
66+
'includedir' => $includedir,
67+
}
68+
6469
if $mysql::server::manage_config_file {
6570
file { 'mysql-config-file':
6671
path => $mysql::server::config_file,
67-
content => template('mysql/my.cnf.erb'),
72+
content => epp('mysql/my.cnf.epp', $parameters),
6873
mode => $mysql::server::config_file_mode,
6974
owner => $mysql::server::mycnf_owner,
7075
group => $mysql::server::mycnf_group,

manifests/server/root_password.pp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,16 @@
3737
}
3838
}
3939

40+
$parameters = {
41+
'root_password_set' => $root_password_set,
42+
'root_password' => $root_password,
43+
'options' => $options,
44+
}
45+
4046
if $mysql::server::create_root_my_cnf and $root_password_set {
4147
# TODO: use EPP instead of ERB, as EPP can handle Data of Type Sensitive without further ado
4248
file { "${facts['root_home']}/.my.cnf":
43-
content => template('mysql/my.cnf.pass.erb'),
49+
content => epp('mysql/my.cnf.pass.epp',$parameters),
4450
owner => 'root',
4551
mode => '0600',
4652
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# frozen_string_literal: true
2+
3+
require 'spec_helper'
4+
5+
describe 'mysql::innobackupex_args' do
6+
it 'exists' do
7+
expect(subject).not_to be_nil
8+
end
9+
10+
it 'accepts empty strings as puppet undef' do
11+
expect(subject).to run.with_params('', true, '', [], [])
12+
end
13+
14+
context 'should work with username and password' do
15+
it 'returns args with username and password' do
16+
expect(subject).to run.with_params('root', false, '12345', [], []).and_return('--user="root" --password="12345"')
17+
end
18+
19+
it 'returns args with database lists' do
20+
expect(subject).to run.with_params('root', false, '12345', ['db1', 'db2'], []).and_return('--user="root" --password="12345" --databases="db1 db2"')
21+
end
22+
23+
it 'returns args with backup compress only' do
24+
expected_results = '--user="root" --password="12345" --compress'
25+
expect(subject).to run.with_params('root', true, '12345', [], []).and_return(expected_results)
26+
end
27+
28+
it 'returns args with backup compress, database list and optional_args' do
29+
expected_results = '--user="root" --password="12345" --compress --databases="db1 db2" tst_arg_1 tst_arg_2'
30+
expect(subject).to run.with_params('root', true, '12345', ['db1', 'db2'], ['tst_arg_1', 'tst_arg_2']).and_return(expected_results)
31+
end
32+
end
33+
34+
context 'should work without database args' do
35+
it 'returns args without database list' do
36+
expect(subject).to run.with_params('root', false, '12345', [], []).and_return('--user="root" --password="12345"')
37+
end
38+
end
39+
40+
it 'returns args without backup compress' do
41+
expect(subject).to run.with_params('root', false, '12345', [], []).and_return('--user="root" --password="12345"')
42+
end
43+
44+
it 'returns args with backup compress and database list' do
45+
expected_results = '--user="root" --password="12345" --compress --databases="db1 db2"'
46+
expect(subject).to run.with_params('root', true, '12345', ['db1', 'db2'], []).and_return(expected_results)
47+
end
48+
49+
it 'returns args without backup compress database list and optional_args' do
50+
expected_results = '--user="root" --password="12345" --databases="db1 db2" tst_arg_1 tst_arg_2'
51+
expect(subject).to run.with_params('root', false, '12345', ['db1', 'db2'], ['tst_arg_1', 'tst_arg_2']).and_return(expected_results)
52+
end
53+
54+
it 'returns args without backup compress database list and with optional_args' do
55+
expected_results = '--user="root" --password="12345" tst_arg_1 tst_arg_2'
56+
expect(subject).to run.with_params('root', false, '12345', [], ['tst_arg_1', 'tst_arg_2']).and_return(expected_results)
57+
end
58+
end

templates/my.cnf.epp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
### MANAGED BY PUPPET ###
2+
3+
<% sort($options.map |$key, $value| { [$key, $value] }).map |$v| { -%>
4+
<% if type($v[1]) =~ Type[Hash] { -%>
5+
[<%= $v[0] %>]
6+
<%sort($v[1].map |$key, $value| { [$key, $value] }).map |$vi| { -%>
7+
<%- if ($vi[0] == 'ssl-disable') or ($vi[0] =~ /^ssl/ and $v[1]['ssl-disable'] == true) or ($vi[0] =~ /^ssl-/ and $v[1]['ssl'] == false) { -%>
8+
<%- next -%>
9+
<%- } elsif $vi[1] == true or $vi[1] == '' { -%>
10+
<%= $vi[0] -%>
11+
<%- } elsif type($vi[1]) =~ Type[Array] { -%>
12+
<%- $vi[1].each |$vii| { -%>
13+
<%-$base = $vi[0]-%>
14+
<%= $base %> = <%= $vii %>
15+
<%- } -%>
16+
<%- } elsif !($vi[1] ==nil or $vi[1]=='' or $vi[1]==undef) { -%>
17+
<%-$base = $vi[0]-%>
18+
<%= $base %> = <%= $vi[1] -%>
19+
<% } %>
20+
<% } %>
21+
<% } %>
22+
<% } %>
23+
<% if $includedir and $includedir != '' { -%>
24+
!includedir <%= $includedir %>
25+
<% } -%>

templates/my.cnf.erb

Lines changed: 0 additions & 24 deletions
This file was deleted.

templates/my.cnf.pass.epp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<%['mysql', 'client', 'mysqldump', 'mysqladmin', 'mysqlcheck'].each |$section| { %>
2+
[<%= $section -%>]
3+
user=root
4+
host=localhost
5+
<% if $root_password_set { -%>
6+
password='<%= $root_password %>'
7+
<% } -%>
8+
socket=<%= $options['client']['socket'] %>
9+
<% } %>

templates/my.cnf.pass.erb

Lines changed: 0 additions & 11 deletions
This file was deleted.

templates/mysqlbackup.sh.epp

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
<%- if $kernel == 'Linux' { -%>
2+
#!/bin/bash
3+
<%- } else { -%>
4+
#!/bin/sh
5+
<%- } -%>
6+
#
7+
# MySQL Backup Script
8+
# Dumps mysql databases to a file for another backup tool to pick up.
9+
#
10+
# MySQL code:
11+
# GRANT SELECT, RELOAD, LOCK TABLES ON *.* TO 'user'@'localhost'
12+
# IDENTIFIED BY 'password';
13+
# FLUSH PRIVILEGES;
14+
#
15+
##### START CONFIG ###################################################
16+
17+
USER=<%= $backupuser %>
18+
PASS='<%= $backuppassword_unsensitive %>'
19+
MAX_ALLOWED_PACKET=<%= $maxallowedpacket %>
20+
DIR=<%= $backupdir %>
21+
ROTATE=<%= [ Integer($backuprotate) - 1, 0 ].max %>
22+
23+
# Create temporary mysql cnf file.
24+
TMPFILE=`mktemp /tmp/backup.XXXXXX` || exit 1
25+
echo -e "[client]\npassword=$PASS\nuser=$USER\nmax_allowed_packet=$MAX_ALLOWED_PACKET" > $TMPFILE
26+
27+
<% if $prescript { -%>
28+
<%- [$prescript].flatten().filter |$value| {$value}.each |$script| { %>
29+
<%= $script %>
30+
<%- } -%>
31+
<% } -%>
32+
33+
# Ensure backup directory exist.
34+
mkdir -p $DIR
35+
36+
PREFIX=mysql_backup_
37+
<% if $ignore_events { %>
38+
ADDITIONAL_OPTIONS="--ignore-table=mysql.event"
39+
<% } else { %>
40+
ADDITIONAL_OPTIONS="--events"
41+
<% } %>
42+
43+
<%# Only include routines or triggers if we're doing a file per database -%>
44+
<%# backup. This happens if we named databases, or if we explicitly set -%>
45+
<%# file per database mode -%>
46+
<% if !$backupdatabases.empty or $file_per_database { -%>
47+
<% if $include_triggers { -%>
48+
ADDITIONAL_OPTIONS="$ADDITIONAL_OPTIONS --triggers"
49+
<% } else { -%>
50+
ADDITIONAL_OPTIONS="$ADDITIONAL_OPTIONS --skip-triggers"
51+
<% } -%>
52+
<% if $include_routines { -%>
53+
ADDITIONAL_OPTIONS="$ADDITIONAL_OPTIONS --routines"
54+
<% } else { -%>
55+
ADDITIONAL_OPTIONS="$ADDITIONAL_OPTIONS --skip-routines"
56+
<% } -%>
57+
<% } -%>
58+
59+
<%- if $optional_args and type($optional_args) =~ Type(Array) { -%>
60+
<% $optional_args.each |$arg| { -%>
61+
ADDITIONAL_OPTIONS="$ADDITIONAL_OPTIONS <%= $arg %>"
62+
<%- } -%>
63+
<%- } -%>
64+
##### STOP CONFIG ####################################################
65+
PATH=<%= $execpath %>
66+
67+
<%- if $kernel == 'Linux' { -%>
68+
set -o pipefail
69+
<%- } -%>
70+
71+
72+
73+
cleanup()
74+
{
75+
find "${DIR}/" -maxdepth 1 -type f -name "${PREFIX}*.sql*" -mtime +${ROTATE} -print0 | xargs -0 -r rm -f
76+
}
77+
78+
<% if $delete_before_dump { -%>
79+
cleanup
80+
81+
<% } -%>
82+
<% if $backupdatabases.empty { -%>
83+
<% if $file_per_database { -%>
84+
<% if $excludedatabases.empty { -%>
85+
mysql --defaults-extra-file=$TMPFILE -s -r -N -e 'SHOW DATABASES' | while read dbname
86+
<%} else {-%>
87+
mysql --defaults-extra-file=$TMPFILE -s -r -N -e 'SHOW DATABASES' | grep -v '^\(<%= $excludedatabases.join('|') %>\)$' | while read dbname
88+
<% } -%>
89+
do
90+
<%= $backupmethod %> --defaults-extra-file=$TMPFILE --opt --flush-logs --single-transaction \
91+
${ADDITIONAL_OPTIONS} \
92+
${dbname} <% if $backupcompress { %>| <%= $compression_command %> <% } %>> ${DIR}/${PREFIX}${dbname}_`date +%Y%m%d-%H%M%S`.sql<% if $backupcompress { %><%= $compression_extension %><% } %>
93+
done
94+
<% } else { -%>
95+
<%= $backupmethod %> --defaults-extra-file=$TMPFILE --opt --flush-logs --single-transaction \
96+
${ADDITIONAL_OPTIONS} \
97+
--all-databases <% if $backupcompress { %>| <%= $compression_command %> <% } %>> ${DIR}/${PREFIX}`date +%Y%m%d-%H%M%S`.sql<% if $backupcompress { %><%= $compression_extension %><% } %>
98+
<% } -%>
99+
<% } else { -%>
100+
<% $backupdatabases.each |$db| { -%>
101+
<%= $backupmethod %> --defaults-extra-file=$TMPFILE --opt --flush-logs --single-transaction \
102+
${ADDITIONAL_OPTIONS} \
103+
<%= $db %><% if $backupcompress { %>| <%= $compression_command %> <% } %>> ${DIR}/${PREFIX}<%= $db %>_`date +%Y%m%d-%H%M%S`.sql<% if $backupcompress { %><%= $compression_extension %><% } %>
104+
<% } -%>
105+
<% } -%>
106+
107+
<% unless $delete_before_dump { -%>
108+
if [ $? -eq 0 ] ; then
109+
cleanup
110+
touch <%= $backup_success_file_path %>
111+
fi
112+
<% } -%>
113+
114+
<% if $postscript { -%>
115+
<%- [$postscript].flatten().filter |$value| { $value }.each |$script| { %>
116+
<%= $script %>
117+
<%- } -%>
118+
<% } -%>
119+
120+
# Remove temporary file
121+
rm -f $TMPFILE

0 commit comments

Comments
 (0)