Skip to content
This repository has been archived by the owner on Apr 19, 2024. It is now read-only.

Commit

Permalink
REBASE ME LAETER
Browse files Browse the repository at this point in the history
  • Loading branch information
petems committed May 2, 2016
1 parent d3945d7 commit c403a66
Show file tree
Hide file tree
Showing 5 changed files with 201 additions and 147 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,6 @@ group :system_tests do
gem 'signet', git: "https://github.com/google/signet.git"
gem 'serverspec'
gem 'specinfra'
gem 'vagrant-wrapper'
end

24 changes: 18 additions & 6 deletions manifests/files.pp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
$cmd = 'dd',
$resize_existing = false,
$resize_margin = '50MB',
$resize_verbose = false,
)
{
# Parameter validation
Expand All @@ -52,12 +53,23 @@

if $ensure == 'present' {

if ($resize_existing) {
::swap_file::resize { $swapfile:
swapfile_path => $swapfile,
margin => $resize_margin,
expected_swapfile_size => $swapfilesize,
before => Exec["Create swap file ${swapfile}"],
if ($resize_existing and $::swapfile_sizes) {

if (is_hash($::swapfile_sizes)) {

if (has_key($::swapfile_sizes,$swapfile)) {
::swap_file::resize { $swapfile:
swapfile_path => $swapfile,
margin => $resize_margin,
expected_swapfile_size => $swapfilesize,
actual_swapfile_size => $::swapfile_sizes[$swapfile],
verbose => $resize_verbose,
before => Exec["Create swap file ${swapfile}"],
}
}

} else {
fail('$resize_existing was set to true and stringify_facts was not false. This currently does not work, but will in the future. See https://github.com/petems/petems-swap_file/issues/57')
}
}

Expand Down
47 changes: 21 additions & 26 deletions manifests/resize.pp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#
# @param [String] swapfile_path Path to the swapfile
# @param [String] expected_swapfile_size Expected size of the swapfile
# @param [String] actual_swapfile_size Actual size of the swapfile
# @param [String] margin Margin that is checked before resizing the swapfile
# @param [Boolean] verbose Adds a notify to explain why the change was made
#
Expand All @@ -17,39 +18,33 @@
define swap_file::resize (
$swapfile_path,
$expected_swapfile_size,
$actual_swapfile_size,
$margin = '50MB',
$verbose = false,
)
{
if ($::swapfile_sizes and is_hash($::swapfile_sizes)) {
$margin_bytes = to_bytes($margin)
$existing_swapfile_bytes = to_bytes("${actual_swapfile_size}kb")
$expected_swapfile_size_bytes = to_bytes($expected_swapfile_size)

$margin_bytes = to_bytes($margin)
$existing_swapfile_bytes = to_bytes("${::swapfile_sizes[$swapfile_path]}kb")
$expected_swapfile_size_bytes = to_bytes($expected_swapfile_size)

if has_key($::swapfile_sizes,$swapfile_path) {
if !($expected_swapfile_size_bytes == $existing_swapfile_bytes) {
if !(difference_within_margin([$existing_swapfile_bytes, $expected_swapfile_size_bytes],$margin_bytes)) {

if ($verbose) {
$alert_message = "Existing : ${existing_swapfile_bytes}B\nExpected: ${expected_swapfile_size_bytes}B\nMargin: ${margin_bytes}B"
notify{'Resizing Swapfile Alert':
name => $alert_message,
}
}

exec { "Detach swap file ${swapfile_path} for resize":
command => "/sbin/swapoff ${swapfile_path}",
onlyif => "/sbin/swapon -s | grep ${swapfile_path}",
}
->
exec { "Purge ${swapfile_path} for resize":
command => "/bin/rm -f ${swapfile_path}",
onlyif => "test -f ${swapfile_path}",
path => [ '/bin/', '/sbin/' , '/usr/bin/', '/usr/sbin/' ],
}
if !($expected_swapfile_size_bytes == $existing_swapfile_bytes) {
if !(difference_within_margin([$existing_swapfile_bytes, $expected_swapfile_size_bytes],$margin_bytes)) {
if ($verbose) {
$alert_message = "Existing : ${existing_swapfile_bytes}B\nExpected: ${expected_swapfile_size_bytes}B\nMargin: ${margin_bytes}B"
notify{"Resizing Swapfile Alert ${swapfile_path}":
name => $alert_message,
}
}
exec { "Detach swap file ${swapfile_path} for resize":
command => "/sbin/swapoff ${swapfile_path}",
onlyif => "/sbin/swapon -s | grep ${swapfile_path}",
}
->
exec { "Purge ${swapfile_path} for resize":
command => "/bin/rm -f ${swapfile_path}",
onlyif => "test -f ${swapfile_path}",
path => [ '/bin/', '/sbin/' , '/usr/bin/', '/usr/sbin/' ],
}
}
}

Expand Down
Loading

0 comments on commit c403a66

Please sign in to comment.