source: other-projects/hathitrust/vagrant-hadoop-cluster/trunk/modules/stdlib/lib/puppet/parser/functions/to_bytes.rb@ 30903

Last change on this file since 30903 was 30903, checked in by davidb, 7 years ago

Vagrant provisioning files for a 4-node Hadoop cluster. See README.txt for more details

File size: 1.1 KB
Line 
1module Puppet::Parser::Functions
2 newfunction(:to_bytes, :type => :rvalue, :doc => <<-EOS
3 Converts the argument into bytes, for example 4 kB becomes 4096.
4 Takes a single string value as an argument.
5 These conversions reflect a layperson's understanding of
6 1 MB = 1024 KB, when in fact 1 MB = 1000 KB, and 1 MiB = 1024 KiB.
7 EOS
8 ) do |arguments|
9
10 raise(Puppet::ParseError, "to_bytes(): Wrong number of arguments " +
11 "given (#{arguments.size} for 1)") if arguments.size != 1
12
13 arg = arguments[0]
14
15 return arg if arg.is_a? Numeric
16
17 value,prefix = */([0-9.e+-]*)\s*([^bB]?)/.match(arg)[1,2]
18
19 value = value.to_f
20 case prefix
21 when '' then return value.to_i
22 when 'k' then return (value*(1<<10)).to_i
23 when 'M' then return (value*(1<<20)).to_i
24 when 'G' then return (value*(1<<30)).to_i
25 when 'T' then return (value*(1<<40)).to_i
26 when 'P' then return (value*(1<<50)).to_i
27 when 'E' then return (value*(1<<60)).to_i
28 else raise Puppet::ParseError, "to_bytes(): Unknown prefix #{prefix}"
29 end
30 end
31end
Note: See TracBrowser for help on using the repository browser.