class Chef::Resource::SwapFile

Public Instance Methods

block_size() click to toggle source

The block size (1MB)

# File lib/chef/resource/swap_file.rb, line 149
def block_size
  1_048_576
end
compatible_filesystem?() click to toggle source
# File lib/chef/resource/swap_file.rb, line 178
def compatible_filesystem?
  compatible_filesystems = %w{xfs ext4}
  parent_directory = ::File.dirname(new_resource.path)
  # Get FS info, get second line as first is column headings
  command = "df -PT #{parent_directory} | awk 'NR==2 {print $2}'"
  result = shell_out(command).stdout
  Chef::Log.debug("#{new_resource} filesystem listing is '#{result}'")
  compatible_filesystems.any? { |fs| result.include? fs }
end
compatible_kernel() click to toggle source
# File lib/chef/resource/swap_file.rb, line 172
def compatible_kernel
  fallocate_location = shell_out("which fallocate").stdout
  Chef::Log.debug("#{new_resource} fallocate location is '#{fallocate_location}'")
  ::File.exist?(fallocate_location.chomp)
end
create_swapfile(command) click to toggle source
# File lib/chef/resource/swap_file.rb, line 85
def create_swapfile(command)
  converge_by "create empty swapfile at #{new_resource.path}" do # ~FC054
    shell_out!(command, timeout: new_resource.timeout)
  end
end
dd_command() click to toggle source
# File lib/chef/resource/swap_file.rb, line 166
def dd_command
  command = "dd if=/dev/zero of=#{new_resource.path} bs=#{block_size} count=#{new_resource.size}"
  Chef::Log.debug("#{new_resource} dd command is '#{command}'")
  command
end
do_create(command) click to toggle source
# File lib/chef/resource/swap_file.rb, line 77
def do_create(command)
  create_swapfile(command)
  set_permissions
  mkswap
  swapon
  persist if persist?
end
fallback_swap_creation_command() click to toggle source
# File lib/chef/resource/swap_file.rb, line 142
def fallback_swap_creation_command
  command = dd_command
  Chef::Log.debug("#{new_resource} fallback swap creation command is '#{command}'")
  command
end
fallocate_command() click to toggle source
# File lib/chef/resource/swap_file.rb, line 159
def fallocate_command
  size = fallocate_size
  command = "fallocate -l #{size} #{new_resource.path}"
  Chef::Log.debug("#{new_resource} fallocate command is '#{command}'")
  command
end
fallocate_size() click to toggle source
# File lib/chef/resource/swap_file.rb, line 153
def fallocate_size
  size = block_size * new_resource.size
  Chef::Log.debug("#{new_resource} fallocate size is #{size}")
  size
end
mkswap() click to toggle source
# File lib/chef/resource/swap_file.rb, line 98
def mkswap
  converge_by "make #{new_resource.path} swappable" do
    shell_out!("mkswap -f #{new_resource.path}")
  end
end
persist() click to toggle source
# File lib/chef/resource/swap_file.rb, line 192
def persist
  fstab = "/etc/fstab"
  contents = ::File.readlines(fstab)
  addition = "#{new_resource.path} swap swap defaults 0 0"

  if contents.any? { |line| line.strip == addition }
    Chef::Log.debug("#{new_resource} already added to /etc/fstab - skipping")
  else
    Chef::Log.info("#{new_resource} adding entry to #{fstab} for #{new_resource.path}")

    contents << "#{addition}\n"
    ::File.open(fstab, "w") { |f| f.write(contents.join("")) }
  end
end
persist?() click to toggle source
# File lib/chef/resource/swap_file.rb, line 188
def persist?
  !!new_resource.persist
end
remove_swapfile() click to toggle source
# File lib/chef/resource/swap_file.rb, line 116
def remove_swapfile
  converge_by "remove swap file #{new_resource.path}" do
    ::FileUtils.rm(new_resource.path)
  end
end
set_permissions() click to toggle source
# File lib/chef/resource/swap_file.rb, line 91
def set_permissions
  permissions = "600"
  converge_by "set permissions on #{new_resource.path} to #{permissions}" do
    shell_out!("chmod #{permissions} #{new_resource.path}")
  end
end
swap_creation_command() click to toggle source
# File lib/chef/resource/swap_file.rb, line 132
def swap_creation_command
  command = if compatible_filesystem? && compatible_kernel && !docker?
              fallocate_command
            else
              dd_command
            end
  Chef::Log.debug("#{new_resource} swap creation command is '#{command}'")
  command
end
swap_enabled?() click to toggle source
# File lib/chef/resource/swap_file.rb, line 122
def swap_enabled?
  enabled_swapfiles = shell_out("swapon --summary").stdout
  # Regex for our resource path and only our resource path
  # It will terminate on whitespace after the path it match
  # /testswapfile would match
  # /testswapfiledir/someotherfile will not
  swapfile_regex = Regexp.new("^#{new_resource.path}[\\s\\t\\n\\f]+")
  !swapfile_regex.match(enabled_swapfiles).nil?
end
swapoff() click to toggle source
# File lib/chef/resource/swap_file.rb, line 110
def swapoff
  converge_by "turn off swap for #{new_resource.path}" do
    shell_out!("swapoff #{new_resource.path}")
  end
end
swapon() click to toggle source
# File lib/chef/resource/swap_file.rb, line 104
def swapon
  converge_by "enable swap for #{new_resource.path}" do
    shell_out!("swapon #{new_resource.path}")
  end
end