Skip to content

Instantly share code, notes, and snippets.

@cwjohnston
Created January 7, 2011 18:36

Revisions

  1. cwjohnston created this gist Jan 7, 2011.
    109 changes: 109 additions & 0 deletions swapfile.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,109 @@
    #
    # 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