-
-
Save egeland/6821885 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Manages swapspace on a node. | |
# | |
# This version is based on https://gist.github.com/hertell/6688708, | |
# which is based on https://gist.github.com/philfreo/6576492 and that | |
# again is based on https://gist.github.com/Yggdrasil/3918632 | |
# | |
# Parameters: | |
# - [*ensure8] - Allows creation or removal of swapfiles. | |
# - [*swapfile*] - Defaults to /mnt which is a fast ephemeral filesystem on EC2 instances. | |
# This keeps performance reasonable while avoiding I/O charges on EBS. | |
# - [*swapsize*] - Size of the swapfile in MB. Defaults to memory size *2 | |
# | |
# Actions: | |
# Creates and mounts a swapfile. | |
# Umounts and removes a swapfile. | |
# | |
# Sample Usage: | |
# common::swapfile { 'swap': | |
# ensure => present, | |
# } | |
# | |
# common::swapfile { 'swap': | |
# ensure => absent, | |
# } | |
# | |
define common::swapfile ( | |
$ensure = 'present', | |
$swapfile = "/mnt/${title}", | |
$swapsize = $::memorysize_mb, | |
) { | |
$swapsizes = split("${swapsize}",'[.]') | |
$swapfilesize = $swapsizes[0] * 2 | |
file_line { "swap_fstab_line_${swapfile}": | |
ensure => $ensure, | |
line => "${swapfile} swap swap defaults 0 0", | |
path => "/etc/fstab", | |
match => "${swapfile}", | |
} | |
if $ensure == 'present' { | |
exec { "Create swap file ${swapfile}": | |
command => "/bin/dd if=/dev/zero of=${swapfile} bs=1M count=${swapfilesize}", | |
creates => $swapfile, | |
} | |
exec { "Attach swap file ${swapfile}": | |
command => "/sbin/mkswap ${swapfile} && /sbin/swapon ${swapfile}", | |
require => Exec["Create swap file ${swapfile}"], | |
unless => "/sbin/swapon -s | grep ${swapfile}", | |
} | |
} elsif $ensure == 'absent' { | |
exec { "Detach swap file ${swapfile}": | |
command => "/sbin/swapoff ${swapfile}", | |
onlyif => "/sbin/swapon -s | grep ${swapfile}", | |
} | |
file { $swapfile: | |
ensure => absent, | |
require => Exec["Detach swap file ${swapfile}"], | |
backup => false | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment