Class: Koch::Swapfile

Inherits:
Resource show all
Defined in:
lib/koch/swapfile.rb

Overview

Creates a single swap file

Instance Attribute Summary

Attributes inherited from Resource

#changed, #name

Instance Method Summary collapse

Methods inherited from Resource

dsl_writer

Constructor Details

#initialize(name) ⇒ Swapfile

Returns a new instance of Swapfile.



8
9
10
11
# File 'lib/koch/swapfile.rb', line 8

def initialize(name)
  super
  @size = "1G"
end

Instance Method Details

#apply!Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/koch/swapfile.rb', line 13

def apply!
  if File.exist? name
    debug "Swap file #{name} already exists"
    return
  end

  @changed = true

  maybe "fallocate -l #{size} #{name}"
  maybe "Chmod 600 swap file #{name}" do
    File.chmod 0o600, name
  end
  maybe "mkswap #{name}"
  maybe "swapon #{name}"
  maybe "Add swap file #{name} to /etc/fstab" do
    File.write("/etc/fstab", "#{name} none swap sw 0 0\n", mode: "a")
  end
end