Guest User

Untitled

a guest
Jul 19th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.21 KB | None | 0 0
  1. #
  2. # Provider Name:: swapfile
  3. #
  4. # Copyright 2010, Blue Coat Systems Inc.
  5. #
  6.  
  7. require 'chef/mixin/command'
  8. include Chef::Mixin::Command
  9.  
  10. action :create do
  11. unless @swap.exists
  12. execute "/bin/dd if=/dev/zero of= bs=1 count=#{params[:size]}"
  13.  
  14.  
  15.  
  16.  
  17. if File.exist?("#{params[:name]}")
  18. if File.size?("#{params[:name]}") == params[:size]
  19. Chef::Log.debug "Existing size of #{params[:name]} already matches desired swapfile size of #{params[:size]}"
  20. new_resource.updated = false
  21. else
  22. if system("/bin/swapoff #{params[:name]}")
  23. Chef::Log.info "Creating swapfile at #{params[:name]}"
  24. if system("/bin/dd if=/dev/zero of=#{params[:name]} bs=1 count=#{params[:size]}")
  25. if system("/sbin/mkswap #{params[:name]}")
  26. if system("/usr/local/bin/fstab-update")
  27. new_resource.updated = true
  28. else
  29. Chef::Log.info "Failed to update /etc/fstab using fstab-update"
  30. end
  31. else
  32. Chef::Log.info "Failed to execute mkswap against #{params[:name]}"
  33. new_resource.updated = false
  34. end
  35. else
  36. Chef::Log.info "Failed to create swapfile of size #{params[:size]} at #{params[:name]}"
  37. new_resource.updated = false
  38. end
  39. else
  40. Chef::Log.info "Failed to swapoff #{params[:name]} for swapfile resizing. No action taken."
  41. new_resource.updated = false
  42. end
  43. end
  44. else
  45. Chef::Log.info "Creating swapfile at #{params[:name]}"
  46. system("/bin/dd if=/dev/zero of=#{params[:name]} bs=1M count=#{params[:size]}")
  47. new_resource.updated = true
  48. end
  49. end
  50.  
  51. #action :delete do
  52. # if File.exist?("#{params[:name]}")
  53. # if system("/bin/swapoff #{params[:name]}")
  54. # if File.delete("#{params[:name]}")
  55. # Chef::Log.info "Deleting swapfile at #{params[:name]}"
  56. # new_resource.updated = true
  57. # else
  58. # Chef::Log.info "Failed to delete swapfile at #{params[:name]}"
  59. # new_resource.updated = false
  60. # end
  61. # else
  62. # Chef::Log.info "You asked me to delete a swapfile at #{params[:name]} but I could not swapoff that file."
  63. # new_resource.updated = false
  64. # end
  65. # else
  66. # Chef::Log.info "You asked me to delete a swapfile at #{params[:name]} but the specified file does not exist. No action taken."
  67. # new_resource.updated = false
  68. # end
  69. #end
  70.  
  71. def load_current_resource
  72. @swap = Chef::Resource::Swapfile.new(new_resource.swap_name)
  73. @swap.swap_name(new_resource.swap_name)
  74.  
  75. Chef::Log.debug("Checking for existing swap file #{new_resource.swap_name}")
  76.  
  77. begin
  78. if File.exist?("#{new_resource.swap_name}")
  79. @swap.exists(true)
  80. end
  81. rescue Chef::Exceptions::Exec
  82. @swap.exists(false)
  83. nil
  84. end
  85.  
  86. Chef::Log.debug("Checking size of existing swap file #{new_resource.swap_name}")
  87.  
  88. begin
  89. if @swap.exists
  90. @swap.size(File.size("#{new_resource.swap_name}"))
  91. end
  92. rescue Chef::Exceptions::Exec
  93. @swap.size(nil)
  94. nil
  95. end
  96.  
  97. Chef::Log.debug("Checking status of swap file #{new_resource.swap_name}")
  98.  
  99. begin
  100. if @swap.exists
  101. if system("/bin/swapon -s | grep #{new_resource.swap_name}")
  102. @swap.enabled(true)
  103. end
  104. end
  105. rescue Chef::Exceptions::Exec
  106. @swap.enabled(false)
  107. nil
  108. end
  109. end
Add Comment
Please, Sign In to add comment