Skip to content

Commit

Permalink
[sys::setup_swap] move more processing to chef resources. RS standard…
Browse files Browse the repository at this point in the history
… on comments. using mount resource to edit /etc/fstab
  • Loading branch information
Lopaka Delp committed Aug 23, 2011
1 parent 9f1762f commit 076f515
Showing 1 changed file with 32 additions and 60 deletions.
92 changes: 32 additions & 60 deletions recipes/setup_swap.rb
Expand Up @@ -34,52 +34,34 @@

def clean_swap(swap_file)

# turn off swap on swap_file if turned on
if ( File.open('/proc/swaps').grep(/^#{swap_file}\b/).any? )
script 'deactivate swapfile' do
interpreter 'bash'
code <<-eof
swapoff #{swap_file}
eof
end
# Turn off swap on swap_file if turned on
bash 'deactivate swapfile' do
only_if { File.open('/proc/swaps').grep(/^#{swap_file}\b/).any? }
code <<-eof
swapoff #{swap_file}
eof
end

# remove swap from /etc/fstab
if ( File.open('/etc/fstab').grep(/^\s*#{swap_file}\b/).any? )
new_fstab_contents = ""
fstab_contents = File.open('/etc/fstab') { |f| f.read }
fstab_contents.each_line do |line|
if ( line.strip =~ /^#{swap_file}\b/ )
# skipping
else
new_fstab_contents << line
end
end
file "/etc/fstab" do
content new_fstab_contents
owner "root"
group "root"
mode "0644"
action :create
end
# Remove swap from /etc/fstab
mount '/dev/null' do
action :disable
device "#{swap_file}"
end

# delete swap_file if it exists
if ( File.exists?(swap_file) )
file "#{swap_file}" do
backup false
action :delete
end
# Delete swap_file if it exists
file "#{swap_file}" do
only_if {File.exists?(swap_file)}
backup false
action :delete
end

end

def create_swap(swap_file, swap_size)

# create swapfile, set it as swap, and turn swap on
script 'create swapfile' do
not_if {File.exists?(swap_file)}
interpreter 'bash'
# Create swapfile, set it as swap, and turn swap on
bash 'create swapfile' do
not_if { File.exists?(swap_file) }
code <<-eof
dd if=/dev/zero of=#{swap_file} bs=1M count=#{swap_size}
chmod 600 #{swap_file}
Expand All @@ -88,38 +70,29 @@ def create_swap(swap_file, swap_size)
eof
end

# append swap to /etc/fstab if not already there
if ( File.open('/etc/fstab').grep(/^\s*#{swap_file}\b/).any? )
log "#{swapfile} already in /etc/fstab"
else
fstab_contents = File.open('/etc/fstab') { |f| f.read }
fstab_contents << "#{swap_file} swap swap defaults 0 0\n"
file "/etc/fstab" do
content fstab_contents
owner "root"
group "root"
mode "0644"
action :create
end
# add swap to /etc/fstab
mount '/dev/null' do
action :enable
device "#{swap_file}"
fstype 'swap'
end

end

# sanitize user data 'swap_size'
# Sanitize user data 'swap_size'
if ( swap_size !~ /^\d*[.]?\d+$/ )
log "invalid swap size '#{swap_size}' - raising error"
raise "ERROR: invalid swap size."
else
# convert swap_size from GB to MB
# Convert swap_size from GB to MB
swap_size = ((swap_size.to_f)*1024).to_i
end

# sanitize user data 'swap_file'
# Sanitize user data 'swap_file'
if (swap_file !~ /^\/{1}(((\/{1}\.{1})?[a-zA-Z0-9 ]+\/?)+(\.{1}[a-zA-Z0-9]{2,4})?)$/ )
log "invalid swap file name - raising error"
raise "ERROR: invalid swap file name"
end

# skip creating swap or disable swap
# Skip creating swap or disable swap
if (swap_size == 0)
if ( File.exists?(swap_file) && File.open('/proc/swaps').grep(/^#{swap_file}\b/).any? )
clean_swap(swap_file)
Expand All @@ -128,26 +101,25 @@ def create_swap(swap_file, swap_size)
end
else

# for idempotency, check if selected swapfle is in place, correct size, and on
# For idempotency, check if selected swapfle is in place, it's correct size, and it's on
if ( File.exists?(swap_file) && \
File.stat(swap_file).size/1048576 == swap_size && \
File.open('/proc/swaps').grep(/^#{swap_file}\b/).any? )
log "valid current swap config"
else
# check for remnents of swap
# Check for remnents of swap
if ( File.exists?(swap_file) || File.open('/proc/swaps').grep(/^#{swap_file}\b/).any? )
log "swap remnents detected - cleaning"
clean_swap(swap_file)
end

# determine if swapfile is too big for fs that holds it
# Determine if swapfile is too big for fs that holds it
(fs_total,fs_used) = `df --block-size=1M -P #{File.dirname(swap_file)} |tail -1| awk '{print $2":"$3}'`.split(":")
if ( (((fs_used.to_f + swap_size).to_f/fs_total.to_f)*100).to_i >= fs_size_threshold_percent )
log "swap file size would exceed filesystem threshold of #{fs_size_threshold_percent} percent - raising error"
raise "ERROR: swap file size too big - would exceed #{fs_size_threshold_percent} percent of filesystem"
end

# should now setup swap file
# Should now setup swap file
create_swap(swap_file,swap_size)
end
end
Expand Down

0 comments on commit 076f515

Please sign in to comment.