From e79df44578f4bf1b70168bd8fa9cb3a9620463a0 Mon Sep 17 00:00:00 2001
From: Peter Souter
Date: Thu, 27 Feb 2014 10:14:46 +0000
Subject: [PATCH] Removing custom fact for memory size in bytes
There is a stdlib method for this (to_bytes) so we might as well use that instead of having to maintain our own custom fact! :+1:
---
CHANGELOG.md | 7 ++++--
lib/facter/meminbytes.rb | 39 ----------------------------------
manifests/init.pp | 2 +-
spec/classes/swap_file_spec.rb | 6 ++++--
4 files changed, 10 insertions(+), 44 deletions(-)
delete mode 100644 lib/facter/meminbytes.rb
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 2cfe466..0c3b830 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,5 +1,8 @@
## Change Log
-### upcoming (2014/02/26 19:31 +00:00)
+### upcoming (2014/02/26 19:35 +00:00)
- [c8cf711](https://github.com/petems/puppet-swap_file/commit/c8cf711bb10f100c0b9ad66a16e7e54a54f776d3) Initial Commit (@petems)
-- [02e1a1a](https://github.com/petems/puppet-swap_file/commit/02e1a1ab01886028bf79e7bc870df110a53cf5b7) Rename spec file (@petems)
\ No newline at end of file
+- [02e1a1a](https://github.com/petems/puppet-swap_file/commit/02e1a1ab01886028bf79e7bc870df110a53cf5b7) Rename spec file (@petems)
+- [fbaa2e4](https://github.com/petems/puppet-swap_file/commit/fbaa2e46f632142f4dc63c4928c8e9e206920a2c) Update changelog with `github-changes` (@petems)
+- [3952796](https://github.com/petems/puppet-swap_file/commit/39527966fac72219a1b938af38b8f3a71e3e428e) Removed old CHANGELOG (@petems)
+- [43da89d](https://github.com/petems/puppet-swap_file/commit/43da89d4e4d49b4a08c16d6fa2a38774f59910fc) Update module file with details (@petems)
\ No newline at end of file
diff --git a/lib/facter/meminbytes.rb b/lib/facter/meminbytes.rb
deleted file mode 100644
index 60c212e..0000000
--- a/lib/facter/meminbytes.rb
+++ /dev/null
@@ -1,39 +0,0 @@
-require 'thread'
-
-{ :MemorySizeInBytes => "MemTotal",
- :MemoryFreeInBytes => "MemFree",
- :SwapSizeInBytes => "SwapTotal",
- :SwapFreeInBytes => "SwapFree"
-}.each do |fact, name|
- Facter.add(fact) do
- confine :kernel => :linux
- setcode do
- meminfo_number(name)
- end
- end
-end
-
-def meminfo_number(tag)
- memsize = ""
- Thread::exclusive do
- size, scale = [0, ""]
- File.readlines("/proc/meminfo").each do |l|
- size, scale = [$1.to_f, $2] if l =~ /^#{tag}:\s+(\d+)\s+(\S+)/
- # MemoryFree == memfree + cached + buffers
- # (assume scales are all the same as memfree)
- if tag == "MemFree" &&
- l =~ /^(?:Buffers|Cached):\s+(\d+)\s+(?:\S+)/
- size += $1.to_f
- end
- end
- memsize = scale_number(size, scale)
- end
-
- memsize
-end
-
-def scale_number(size, multiplier)
- suffixes = ['', 'kB', 'MB', 'GB', 'TB']
- size *= 1024 ** suffixes.index(multiplier)
- return "%.0f" % size
-end
diff --git a/manifests/init.pp b/manifests/init.pp
index 78e3fd5..1f9e9fa 100644
--- a/manifests/init.pp
+++ b/manifests/init.pp
@@ -2,7 +2,7 @@
class swap_file (
$ensure = 'present',
$swapfile = '/mnt/swap.1',
- $swapfilesize = $::memorysizeinbytes/1000000
+ $swapfilesize = to_bytes($::memorysize) / 1000000
) inherits swap_file::params {
if $ensure == 'present' {
exec { 'Create swap file':
diff --git a/spec/classes/swap_file_spec.rb b/spec/classes/swap_file_spec.rb
index af507c9..003e0f4 100644
--- a/spec/classes/swap_file_spec.rb
+++ b/spec/classes/swap_file_spec.rb
@@ -7,7 +7,7 @@
describe "swap_file class without any parameters on #{osfamily}" do
let(:params) {{ }}
let(:facts) {{
- :osfamily => osfamily, :memorysizeinbytes => 1073741824,
+ :osfamily => osfamily, :memorysize => '992.65 MB',
}}
it { should compile.with_all_deps }
@@ -15,7 +15,9 @@
it { should contain_class('Swap_file::Params') }
it { should contain_class('Swap_file') }
- it { should contain_exec('Create swap file') }
+ it { should contain_exec('Create swap file').
+ with_command('/bin/dd if=/dev/zero of=/mnt/swap.1 bs=1M count=1040')
+ }
it { should contain_exec('Attach swap file') }
end
end