Skip to content

CAT-1147 erb to epp conversion #1589

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ RSpec/NamedSubject:
- 'spec/classes/mysql_server_spec.rb'
- 'spec/defines/mysql_db_spec.rb'
- 'spec/functions/mysql_normalise_and_deepmerge_spec.rb'
- 'spec/functions/mysql_innobackupex_args_spec.rb'
- 'spec/functions/mysql_strip_hash_spec.rb'

# Offense count: 45
Expand Down
41 changes: 41 additions & 0 deletions lib/puppet/functions/mysql/innobackupex_args.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# frozen_string_literal: true

# @summary this function populates and returns the string of arguments which later gets injected in template.
# arguments that return string holds is conditional and decided by the the input given to function.

Puppet::Functions.create_function(:'mysql::innobackupex_args') do
# @param args
# String backupuser
# Boolean backupcompress
# String backuppassword_unsensitive
# Array backupdatabases
# Array optional_args
#
# @return String
# Generated on the basis of provided values.
#
dispatch :innobackupex_args do
required_param 'Optional[String]', :backupuser
required_param 'Boolean', :backupcompress
required_param 'Optional[Variant[String, Sensitive[String]]]', :backuppassword_unsensitive
required_param 'Array[String[1]]', :backupdatabases
required_param 'Array[String[1]]', :optional_args
return_type 'Variant[String]'
end

def innobackupex_args(backupuser, backupcompress, backuppassword_unsensitive, backupdatabases, optional_args)
innobackupex_args = ''
innobackupex_args = "--user=\"#{backupuser}\" --password=\"#{backuppassword_unsensitive}\"" if backupuser && backuppassword_unsensitive

innobackupex_args = "#{innobackupex_args} --compress" if backupcompress

innobackupex_args = "#{innobackupex_args} --databases=\"#{backupdatabases.join(' ')}\"" if backupdatabases.is_a?(Array) && !backupdatabases.empty?

if optional_args.is_a?(Array)
optional_args.each do |arg|
innobackupex_args = "#{innobackupex_args} #{arg}"
end
end
innobackupex_args
end
end
26 changes: 25 additions & 1 deletion manifests/backup/mysqldump.pp
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,38 @@
require => File['mysqlbackup.sh'],
}

$parameters = {
'backupuser'=> $backupuser,
'backuppassword_unsensitive'=> $backuppassword_unsensitive,
'maxallowedpacket'=> $maxallowedpacket,
'backupdir'=> $backupdir,
'backuprotate'=> $backuprotate,
'prescript'=> $prescript,
'ignore_events'=> $ignore_events,
'backupdatabases'=> $backupdatabases,
'include_triggers'=> $include_triggers,
'optional_args'=> $optional_args,
'execpath'=> $execpath,
'delete_before_dump'=> $delete_before_dump,
'excludedatabases'=> $excludedatabases,
'backupmethod'=> $backupmethod,
'backupcompress'=> $backupcompress,
'compression_command'=> $compression_command,
'compression_extension'=> $compression_extension,
'backup_success_file_path'=> $backup_success_file_path,
'postscript'=> $postscript,
'file_per_database'=> $file_per_database,
'include_routines' => $include_routines,
}

# TODO: use EPP instead of ERB, as EPP can handle Data of Type Sensitive without further ado
file { 'mysqlbackup.sh':
ensure => $ensure,
path => '/usr/local/sbin/mysqlbackup.sh',
mode => '0700',
owner => 'root',
group => $mysql::params::root_group,
content => template('mysql/mysqlbackup.sh.erb'),
content => epp('mysql/mysqlbackup.sh.epp',$parameters),
}

if $mysqlbackupdir_target {
Expand Down
15 changes: 13 additions & 2 deletions manifests/backup/xtrabackup.pp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
String[1] $backupdirgroup = $mysql::params::root_group,
Boolean $backupcompress = true,
Variant[Integer, String[1]] $backuprotate = 30,
String[1] $backupscript_template = 'mysql/xtrabackup.sh.erb',
String[1] $backupscript_template = 'mysql/xtrabackup.sh.epp',
Optional[String[1]] $backup_success_file_path = undef,
Boolean $ignore_events = true,
Boolean $delete_before_dump = false,
Expand Down Expand Up @@ -176,12 +176,23 @@
}

# TODO: use EPP instead of ERB, as EPP can handle Data of Type Sensitive without further ado
$parameters = {
'innobackupex_args' => mysql::innobackupex_args($backupuser, $backupcompress, $backuppassword_unsensitive, $backupdatabases, $optional_args),
'backuprotate' => $backuprotate,
'backupdir' => $backupdir,
'backupmethod' => $backupmethod,
'delete_before_dump' => $delete_before_dump,
'prescript' => $prescript,
'backup_success_file_path'=> $backup_success_file_path,
'postscript'=> $postscript,
}

file { 'xtrabackup.sh':
ensure => $ensure,
path => '/usr/local/sbin/xtrabackup.sh',
mode => '0700',
owner => 'root',
group => $mysql::params::root_group,
content => template($backupscript_template),
content => epp($backupscript_template,$parameters),
}
}
7 changes: 6 additions & 1 deletion manifests/server/config.pp
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,15 @@
default: {}
}

$parameters= {
'options' => $options,
'includedir' => $includedir,
}

if $mysql::server::manage_config_file {
file { 'mysql-config-file':
path => $mysql::server::config_file,
content => template('mysql/my.cnf.erb'),
content => epp('mysql/my.cnf.epp', $parameters),
mode => $mysql::server::config_file_mode,
owner => $mysql::server::mycnf_owner,
group => $mysql::server::mycnf_group,
Expand Down
8 changes: 7 additions & 1 deletion manifests/server/root_password.pp
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,16 @@
}
}

$parameters = {
'root_password_set' => $root_password_set,
'root_password' => $root_password,
'options' => $options,
}

if $mysql::server::create_root_my_cnf and $root_password_set {
# TODO: use EPP instead of ERB, as EPP can handle Data of Type Sensitive without further ado
file { "${facts['root_home']}/.my.cnf":
content => template('mysql/my.cnf.pass.erb'),
content => epp('mysql/my.cnf.pass.epp',$parameters),
owner => 'root',
mode => '0600',
}
Expand Down
58 changes: 58 additions & 0 deletions spec/functions/mysql_innobackupex_args_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# frozen_string_literal: true

require 'spec_helper'

describe 'mysql::innobackupex_args' do
it 'exists' do
expect(subject).not_to be_nil
end

it 'accepts empty strings as puppet undef' do
expect(subject).to run.with_params('', true, '', [], [])
end

context 'should work with username and password' do
it 'returns args with username and password' do
expect(subject).to run.with_params('root', false, '12345', [], []).and_return('--user="root" --password="12345"')
end

it 'returns args with database lists' do
expect(subject).to run.with_params('root', false, '12345', ['db1', 'db2'], []).and_return('--user="root" --password="12345" --databases="db1 db2"')
end

it 'returns args with backup compress only' do
expected_results = '--user="root" --password="12345" --compress'
expect(subject).to run.with_params('root', true, '12345', [], []).and_return(expected_results)
end

it 'returns args with backup compress, database list and optional_args' do
expected_results = '--user="root" --password="12345" --compress --databases="db1 db2" tst_arg_1 tst_arg_2'
expect(subject).to run.with_params('root', true, '12345', ['db1', 'db2'], ['tst_arg_1', 'tst_arg_2']).and_return(expected_results)
end
end

context 'should work without database args' do
it 'returns args without database list' do
expect(subject).to run.with_params('root', false, '12345', [], []).and_return('--user="root" --password="12345"')
end
end

it 'returns args without backup compress' do
expect(subject).to run.with_params('root', false, '12345', [], []).and_return('--user="root" --password="12345"')
end

it 'returns args with backup compress and database list' do
expected_results = '--user="root" --password="12345" --compress --databases="db1 db2"'
expect(subject).to run.with_params('root', true, '12345', ['db1', 'db2'], []).and_return(expected_results)
end

it 'returns args without backup compress database list and optional_args' do
expected_results = '--user="root" --password="12345" --databases="db1 db2" tst_arg_1 tst_arg_2'
expect(subject).to run.with_params('root', false, '12345', ['db1', 'db2'], ['tst_arg_1', 'tst_arg_2']).and_return(expected_results)
end

it 'returns args without backup compress database list and with optional_args' do
expected_results = '--user="root" --password="12345" tst_arg_1 tst_arg_2'
expect(subject).to run.with_params('root', false, '12345', [], ['tst_arg_1', 'tst_arg_2']).and_return(expected_results)
end
end
25 changes: 25 additions & 0 deletions templates/my.cnf.epp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
### MANAGED BY PUPPET ###

<% sort($options.map |$key, $value| { [$key, $value] }).map |$v| { -%>
<% if type($v[1]) =~ Type[Hash] { -%>
[<%= $v[0] %>]
<%sort($v[1].map |$key, $value| { [$key, $value] }).map |$vi| { -%>
<%- if ($vi[0] == 'ssl-disable') or ($vi[0] =~ /^ssl/ and $v[1]['ssl-disable'] == true) or ($vi[0] =~ /^ssl-/ and $v[1]['ssl'] == false) { -%>
<%- next -%>
<%- } elsif $vi[1] == true or $vi[1] == '' { -%>
<%= $vi[0] -%>
<%- } elsif type($vi[1]) =~ Type[Array] { -%>
<%- $vi[1].each |$vii| { -%>
<%-$base = $vi[0]-%>
<%= $base %> = <%= $vii %>
<%- } -%>
<%- } elsif !($vi[1] ==nil or $vi[1]=='' or $vi[1]==undef) { -%>
<%-$base = $vi[0]-%>
<%= $base %> = <%= $vi[1] -%>
<% } %>
<% } %>
<% } %>
<% } %>
<% if $includedir and $includedir != '' { -%>
!includedir <%= $includedir %>
<% } -%>
24 changes: 0 additions & 24 deletions templates/my.cnf.erb

This file was deleted.

9 changes: 9 additions & 0 deletions templates/my.cnf.pass.epp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<%['mysql', 'client', 'mysqldump', 'mysqladmin', 'mysqlcheck'].each |$section| { %>
[<%= $section -%>]
user=root
host=localhost
<% if $root_password_set { -%>
password='<%= $root_password %>'
<% } -%>
socket=<%= $options['client']['socket'] %>
<% } %>
11 changes: 0 additions & 11 deletions templates/my.cnf.pass.erb

This file was deleted.

Loading