Skip to content

Commit

Permalink
CAT-1147 epp conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
Dinesh Gujar authored and Dinesh Gujar committed Jul 31, 2023
1 parent fae6549 commit 5c1202b
Show file tree
Hide file tree
Showing 6 changed files with 194 additions and 3 deletions.
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
8 changes: 7 additions & 1 deletion manifests/server/config.pp
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,16 @@
default: {}
}

$parameters={

Check failure on line 64 in manifests/server/config.pp

View workflow job for this annotation

GitHub Actions / Spec / Spec tests (Puppet: ~> 7.24, Ruby Ver: 2.7)

there should be a single space before an opening brace (check: manifest_whitespace_opening_brace_before)

Check failure on line 64 in manifests/server/config.pp

View workflow job for this annotation

GitHub Actions / Spec / Spec tests (Puppet: ~> 8.0, Ruby Ver: 3.2)

there should be a single space before an opening brace (check: manifest_whitespace_opening_brace_before)
'options' => $options,
'includedir' => $includedir,
}


Check failure on line 69 in manifests/server/config.pp

View workflow job for this annotation

GitHub Actions / Spec / Spec tests (Puppet: ~> 7.24, Ruby Ver: 2.7)

there should be no two consecutive empty lines (check: manifest_whitespace_two_empty_lines)

Check failure on line 69 in manifests/server/config.pp

View workflow job for this annotation

GitHub Actions / Spec / Spec tests (Puppet: ~> 8.0, Ruby Ver: 3.2)

there should be no two consecutive empty lines (check: manifest_whitespace_two_empty_lines)
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
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 %>
<% } -%>
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'] %>
<% } %>
121 changes: 121 additions & 0 deletions templates/mysqlbackup.sh.epp
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
<%- if $kernel == 'Linux' { -%>
#!/bin/bash
<%- } else { -%>
#!/bin/sh
<%- } -%>
#
# MySQL Backup Script
# Dumps mysql databases to a file for another backup tool to pick up.
#
# MySQL code:
# GRANT SELECT, RELOAD, LOCK TABLES ON *.* TO 'user'@'localhost'
# IDENTIFIED BY 'password';
# FLUSH PRIVILEGES;
#
##### START CONFIG ###################################################

USER=<%= $backupuser %>
PASS='<%= $backuppassword_unsensitive %>'
MAX_ALLOWED_PACKET=<%= $maxallowedpacket %>
DIR=<%= $backupdir %>
ROTATE=<%= [ Integer($backuprotate) - 1, 0 ].max %> #need to check

# Create temporary mysql cnf file.
TMPFILE='mktemp /tmp/backup.XXXXXX' or exit 1
echo -e "[client]\npassword=$PASS\nuser=$USER\nmax_allowed_packet=$MAX_ALLOWED_PACKET" > $TMPFILE

<% if $prescript { -%>
<%- [$prescript].flatten().filter |$value| {$value}.each |$script| { %>
<%= $script %>
<%- } -%>
<% } -%>

# Ensure backup directory exist.
mkdir -p $DIR

PREFIX=mysql_backup_
<% if $ignore_events { %>
ADDITIONAL_OPTIONS="--ignore-table=mysql.event"
<% } else { %>
ADDITIONAL_OPTIONS="--events"
<% } %>

<%# Only include routines or triggers if we're doing a file per database -%>
<%# backup. This happens if we named databases, or if we explicitly set -%>
<%# file per database mode -%>
<% if !$backupdatabases.empty or $file_per_database { -%>
<% if $include_triggers { -%>
ADDITIONAL_OPTIONS="$ADDITIONAL_OPTIONS --triggers"
<% } else { -%>
ADDITIONAL_OPTIONS="$ADDITIONAL_OPTIONS --skip-triggers"
<% } -%>
<% if $include_routines { -%>
ADDITIONAL_OPTIONS="$ADDITIONAL_OPTIONS --routines"
<% } else { -%>
ADDITIONAL_OPTIONS="$ADDITIONAL_OPTIONS --skip-routines"
<% } -%>
<% } -%>

<%- if $optional_args and type($optional_args) =~ Type(Array) { -%>
<% $optional_args.each |$arg| { -%>
ADDITIONAL_OPTIONS="$ADDITIONAL_OPTIONS <%= $arg %>"
<%- } -%>
<%- } -%>
##### STOP CONFIG ####################################################
PATH=<%= $execpath %>

<%- if $kernel == 'Linux' { -%>
set -o pipefail
<%- } -%>



cleanup()
{
find "${DIR}/" -maxdepth 1 -type f -name "${PREFIX}*.sql*" -mtime +${ROTATE} -print0 | xargs -0 -r rm -f
}

<% if $delete_before_dump { -%>
cleanup

<% } -%>
<% if $backupdatabases.empty { -%>
<% if $file_per_database { -%>
<% if $excludedatabases.empty { -%>
mysql --defaults-extra-file=$TMPFILE -s -r -N -e 'SHOW DATABASES' | while read dbname
<%} else {-%>
mysql --defaults-extra-file=$TMPFILE -s -r -N -e 'SHOW DATABASES' | grep -v '^\(<%= $excludedatabases.join('|') %>\)$' | while read dbname
<% } -%>
do
<%= $backupmethod -%> --defaults-extra-file=$TMPFILE --opt --flush-logs --single-transaction \
${ADDITIONAL_OPTIONS} \
${dbname} <% if $backupcompress { %>| <%= $compression_command %> <% } %>> ${DIR}/${PREFIX}${dbname}_`date +%Y%m%d-%H%M%S`.sql<% if $backupcompress { %><%= $compression_extension %><% } %>
done
<% } else { -%>
<%= $backupmethod -%> --defaults-extra-file=$TMPFILE --opt --flush-logs --single-transaction \
${ADDITIONAL_OPTIONS} \
--all-databases <% if $backupcompress { %>| <%= $compression_command %> <% } %>> ${DIR}/${PREFIX}`date +%Y%m%d-%H%M%S`.sql<% if $backupcompress { %><%= $compression_extension %><% } %>
<% } -%>
<% } else { -%>
<% $backupdatabases.each |$db| { -%>
<%= $backupmethod -%> --defaults-extra-file=$TMPFILE --opt --flush-logs --single-transaction \
${ADDITIONAL_OPTIONS} \
<%= $db %><% if $backupcompress { %>| <%= $compression_command %> <% } %>> ${DIR}/${PREFIX}<%= $db %>_`date +%Y%m%d-%H%M%S`.sql<% if $backupcompress { %><%= $compression_extension %><% } %>
<% } -%>
<% } -%>

<% unless $delete_before_dump { -%>
if [ $? -eq 0 ] ; then
cleanup
touch <%= $backup_success_file_path %>
fi
<% } -%>

<% if $postscript { -%>
<%- [$postscript].flatten().filter |$value| { $value }.each |$script| { %>
<%= $script %>
<%- } -%>
<% } -%>

# Remove temporary file
rm -f $TMPFILE

0 comments on commit 5c1202b

Please sign in to comment.