1 module Puppet::Parser::Functions
2 newfunction(:memcached_max_memory, :type => :rvalue, :doc => <<-EOS
3 Calculate max_memory size from fact 'memsize' and passed argument.
7 raise(Puppet::ParseError, "memcached_max_memory(): " +
8 "Wrong number of arguments given " +
9 "(#{arguments.size} for 1)") if arguments.size != 1
12 memsize = lookupvar('memorysize')
14 if arg and !arg.to_s.end_with?('%')
15 result_in_mb = arg.to_i
17 max_memory_percent = arg ? arg : '95%'
19 # Taken from puppetlabs-stdlib to_bytes() function
20 value,prefix = */([0-9.e+-]*)\s*([^bB]?)/.match(memsize)[1,2]
24 when '' then value = value
25 when 'k' then value *= (1<<10)
26 when 'M' then value *= (1<<20)
27 when 'G' then value *= (1<<30)
28 when 'T' then value *= (1<<40)
29 when 'E' then value *= (1<<50)
30 else raise Puppet::ParseError, "memcached_max_memory(): Unknown prefix #{prefix}"
33 result_in_mb = ( (value / (1 << 20) ) * (max_memory_percent.to_f / 100.0) ).floor