Created
March 16, 2015 11:18
-
-
Save petems/26c1d1bfe9746337b8ee to your computer and use it in GitHub Desktop.
Braindump of potential swap_file provider
This file contains hidden or 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
def get_swap_files(file_path) | |
begin | |
output = swapon(['-s']) | |
rescue Puppet::ExecutionFailure => e | |
Puppet.debug("#get_swap_files had an error -> #{e.inspect}") | |
return nil | |
end | |
swapfiles = output.split("\n").sort | |
swapfiles.include?(file_path) | |
end | |
def destroy_swap_file(file_path) | |
swap_off(file_path) | |
end | |
def create_swap_file(file_path, size, mount, mount_options) | |
if Puppet::FileSystem::exist?(file_path) | |
raise Puppet::Error.new("File specified for swap file already exists! (#{file_path}") | |
end | |
create_partition(file_path, size) | |
mk_swap(file_path) | |
swap_on(file_path) | |
end | |
def mk_swap(file_path) | |
begin | |
output = mkswap([file_path]) | |
rescue Puppet::ExecutionFailure => e | |
Puppet.debug("#mk_swap had an error -> #{e.inspect}") | |
return nil | |
end | |
end | |
def swap_on(file_path) | |
begin | |
output = swapon([file_path]) | |
rescue Puppet::ExecutionFailure => e | |
Puppet.debug("#swap_on had an error -> #{e.inspect}") | |
return nil | |
end | |
end | |
def swap_off(file_path) | |
begin | |
output = swapoff([file_path]) | |
rescue Puppet::ExecutionFailure => e | |
Puppet.debug("#swap_off had an error -> #{e.inspect}") | |
return nil | |
end | |
end | |
def create_partition(file_path, size) | |
begin | |
output = dd(['if=/dev/zero'], ["#of=#{size}"], ["bs=1M"], ["count=#{size}"]) | |
rescue Puppet::ExecutionFailure => e | |
Puppet.debug("#create_partition had an error -> #{e.inspect}") | |
return nil | |
end | |
end | |
def create | |
create_swap_file(resource[:name], resource[:size], resource[:mount], resource[:mount_options]) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment