Skip to content

Commit 17b8244

Browse files
authored
Merge pull request #1589 from puppetlabs/CAT-1147-conversion-erb-to-epp
CAT-1147 erb to epp conversion
2 parents fae6549 + dd9c4b2 commit 17b8244

15 files changed

+356
-234
lines changed

Diff for: .rubocop_todo.yml

+1
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

Diff for: lib/puppet/functions/mysql/innobackupex_args.rb

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# frozen_string_literal: true
2+
3+
# @summary this function populates and returns the string of arguments which later gets injected in template.
4+
# arguments that return string holds is conditional and decided by the the input given to function.
5+
6+
Puppet::Functions.create_function(:'mysql::innobackupex_args') do
7+
# @param args
8+
# String backupuser
9+
# Boolean backupcompress
10+
# String backuppassword_unsensitive
11+
# Array backupdatabases
12+
# Array optional_args
13+
#
14+
# @return String
15+
# Generated on the basis of provided values.
16+
#
17+
dispatch :innobackupex_args do
18+
required_param 'Optional[String]', :backupuser
19+
required_param 'Boolean', :backupcompress
20+
required_param 'Optional[Variant[String, Sensitive[String]]]', :backuppassword_unsensitive
21+
required_param 'Array[String[1]]', :backupdatabases
22+
required_param 'Array[String[1]]', :optional_args
23+
return_type 'Variant[String]'
24+
end
25+
26+
def innobackupex_args(backupuser, backupcompress, backuppassword_unsensitive, backupdatabases, optional_args)
27+
innobackupex_args = ''
28+
innobackupex_args = "--user=\"#{backupuser}\" --password=\"#{backuppassword_unsensitive}\"" if backupuser && backuppassword_unsensitive
29+
30+
innobackupex_args = "#{innobackupex_args} --compress" if backupcompress
31+
32+
innobackupex_args = "#{innobackupex_args} --databases=\"#{backupdatabases.join(' ')}\"" if backupdatabases.is_a?(Array) && !backupdatabases.empty?
33+
34+
if optional_args.is_a?(Array)
35+
optional_args.each do |arg|
36+
innobackupex_args = "#{innobackupex_args} #{arg}"
37+
end
38+
end
39+
innobackupex_args
40+
end
41+
end

Diff for: manifests/backup/mysqldump.pp

+25-1
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 {

Diff for: manifests/backup/xtrabackup.pp

+13-2
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
}

Diff for: manifests/server/config.pp

+6-1
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,

Diff for: manifests/server/root_password.pp

+7-1
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
}

Diff for: spec/functions/mysql_innobackupex_args_spec.rb

+58
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

Diff for: templates/my.cnf.epp

+25
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+
<% } -%>

Diff for: templates/my.cnf.erb

-24
This file was deleted.

Diff for: templates/my.cnf.pass.epp

+9
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+
<% } %>

Diff for: templates/my.cnf.pass.erb

-11
This file was deleted.

0 commit comments

Comments
 (0)