Convert Floating point to integer

131 views
Skip to first unread message

Bob Vergeer

unread,
May 13, 2015, 3:55:03 PM5/13/15
to puppet...@googlegroups.com
Two issues, removing the decimal places and appending a character.
Trying to generate a  /dev/shm line in /etc/fstab that is set to 1G less than available RAM. 

The line should appear as follows in /etc/fstab:
...
none     /dev/shm     tmpfs    rw,exec,size=2048M    0   0
...

1) How do I convert something like this to integer?  The floating point fails
    $shm_size = $memorysize_mb - 1024 

2) Used file_line to insert the string in /etc/fstab.  How do I append the M to the $shm_size variable without a space between?
    line  => 'none    /dev/shm    tmpfs    rw,exec,size=$shm_sizeM       0     0'


Thanks,
Bob

jcbollinger

unread,
May 14, 2015, 10:13:13 AM5/14/15
to puppet...@googlegroups.com


On Wednesday, May 13, 2015 at 2:55:03 PM UTC-5, Bob Vergeer wrote:
Two issues, removing the decimal places and appending a character.
Trying to generate a  /dev/shm line in /etc/fstab that is set to 1G less than available RAM. 

The line should appear as follows in /etc/fstab:
...
none     /dev/shm     tmpfs    rw,exec,size=2048M    0   0
...

1) How do I convert something like this to integer?  The floating point fails
    $shm_size = $memorysize_mb - 1024 



Puppet 3's DSL does not have a mechanism for explicitly specifying data types or converting between them (but such a system is introduced in Puppet 4).  Nevertheless, you have posed the wrong question: the issue is not so much about the type of $shm_size's value, but about how that value is used in assembling some string.  That could be addressed either by assigning a value whose interpolation yields the desired result (which value doesn't have to be of integer type specifically), or by assembling the target string by some means other than interpolating the variable's value.  I guess your current approach is presented in the next question.

 
2) Used file_line to insert the string in /etc/fstab.  How do I append the M to the $shm_size variable without a space between?
    line  => 'none    /dev/shm    tmpfs    rw,exec,size=$shm_sizeM       0     0'



In direct answer to the question, this should work:

line  => "none    /dev/shm    tmpfs     rw,exec,size=${shm_size}M      0     0"

Note the double quotes instead of single, and especially the curly braces around the variable name.

That begs the question, however, of why you are managing /etc/fstab via File_line resources, when you could instead do it more reliably via the standard "Mount" resource type.  Switching to a Mount won't in itself solve your problem with expressing the mount options, but it is nevertheless far superior.  For example, if you don't need $shm_size for anything else, then this Mount resource would probably serve your whole need:

mount { '/dev/shm':
 
ensure => 'present',
  device
=> 'none',
  fstype
=> 'tmpfs',
  options
=> inline_template('rw,exec,size=<%= @memorysize_mb.to_i - 1024 %>M'),
 
dump => '0',
 
pass => '0'
}


John

Bob Vergeer

unread,
May 14, 2015, 11:33:33 AM5/14/15
to puppet...@googlegroups.com
Thanks, John.   I didn't have another purpose for the shm_size variable. Wasn't using the mount because I'm a rookie and still learning. 

Bob
Reply all
Reply to author
Forward
0 new messages