Created
January 7, 2011 18:36
-
-
Save cwjohnston/769888 to your computer and use it in GitHub Desktop.
non-functional attempt at a swapfile provider for chef
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
# | |
# Provider Name:: swapfile | |
# | |
# Copyright 2010, Blue Coat Systems Inc. | |
# | |
require 'chef/mixin/command' | |
include Chef::Mixin::Command | |
action :create do | |
unless @swap.exists | |
execute "/bin/dd if=/dev/zero of= bs=1 count=#{params[:size]}" | |
if File.exist?("#{params[:name]}") | |
if File.size?("#{params[:name]}") == params[:size] | |
Chef::Log.debug "Existing size of #{params[:name]} already matches desired swapfile size of #{params[:size]}" | |
new_resource.updated = false | |
else | |
if system("/bin/swapoff #{params[:name]}") | |
Chef::Log.info "Creating swapfile at #{params[:name]}" | |
if system("/bin/dd if=/dev/zero of=#{params[:name]} bs=1 count=#{params[:size]}") | |
if system("/sbin/mkswap #{params[:name]}") | |
if system("/usr/local/bin/fstab-update") | |
new_resource.updated = true | |
else | |
Chef::Log.info "Failed to update /etc/fstab using fstab-update" | |
end | |
else | |
Chef::Log.info "Failed to execute mkswap against #{params[:name]}" | |
new_resource.updated = false | |
end | |
else | |
Chef::Log.info "Failed to create swapfile of size #{params[:size]} at #{params[:name]}" | |
new_resource.updated = false | |
end | |
else | |
Chef::Log.info "Failed to swapoff #{params[:name]} for swapfile resizing. No action taken." | |
new_resource.updated = false | |
end | |
end | |
else | |
Chef::Log.info "Creating swapfile at #{params[:name]}" | |
system("/bin/dd if=/dev/zero of=#{params[:name]} bs=1M count=#{params[:size]}") | |
new_resource.updated = true | |
end | |
end | |
#action :delete do | |
# if File.exist?("#{params[:name]}") | |
# if system("/bin/swapoff #{params[:name]}") | |
# if File.delete("#{params[:name]}") | |
# Chef::Log.info "Deleting swapfile at #{params[:name]}" | |
# new_resource.updated = true | |
# else | |
# Chef::Log.info "Failed to delete swapfile at #{params[:name]}" | |
# new_resource.updated = false | |
# end | |
# else | |
# Chef::Log.info "You asked me to delete a swapfile at #{params[:name]} but I could not swapoff that file." | |
# new_resource.updated = false | |
# end | |
# else | |
# Chef::Log.info "You asked me to delete a swapfile at #{params[:name]} but the specified file does not exist. No action taken." | |
# new_resource.updated = false | |
# end | |
#end | |
def load_current_resource | |
@swap = Chef::Resource::Swapfile.new(new_resource.swap_name) | |
@swap.swap_name(new_resource.swap_name) | |
Chef::Log.debug("Checking for existing swap file #{new_resource.swap_name}") | |
begin | |
if File.exist?("#{new_resource.swap_name}") | |
@swap.exists(true) | |
end | |
rescue Chef::Exceptions::Exec | |
@swap.exists(false) | |
nil | |
end | |
Chef::Log.debug("Checking size of existing swap file #{new_resource.swap_name}") | |
begin | |
if @swap.exists | |
@swap.size(File.size("#{new_resource.swap_name}")) | |
end | |
rescue Chef::Exceptions::Exec | |
@swap.size(nil) | |
nil | |
end | |
Chef::Log.debug("Checking status of swap file #{new_resource.swap_name}") | |
begin | |
if @swap.exists | |
if system("/bin/swapon -s | grep #{new_resource.swap_name}") | |
@swap.enabled(true) | |
end | |
end | |
rescue Chef::Exceptions::Exec | |
@swap.enabled(false) | |
nil | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment