Skip to content

Instantly share code, notes, and snippets.

@petems
Created March 16, 2015 11:18
Show Gist options
  • Save petems/26c1d1bfe9746337b8ee to your computer and use it in GitHub Desktop.
Save petems/26c1d1bfe9746337b8ee to your computer and use it in GitHub Desktop.
Braindump of potential swap_file provider
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