CraftyTech
2010-Jun-21 15:07 UTC
[Puppet Users] erb templating support for case statements?
Hello All, Can you guys point out to me, how do I do a case statement within a template? i.g: my.cnf max_allowed_packet=<% case ($memorysize<=4) = 8M, case ($memorysize<=8) = 16M)? I''ve tried different combinations, but so far no luck. The syntax checker coughs up hair balls.... Thanks, Henry -- You received this message because you are subscribed to the Google Groups "Puppet Users" group. To post to this group, send email to puppet-users@googlegroups.com. To unsubscribe from this group, send email to puppet-users+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/puppet-users?hl=en.
CraftyTech
2010-Jun-21 15:31 UTC
[Puppet Users] Re: erb templating support for case statements?
I''m trying this line on my my.cnf.erb file: <% case memorysize when /<=4/ %> max_allowed_packet=8M <% case memorysize when />4<=8/ %> max_allowed_packet=16M <%end%> I got the syntax from a different post. still not working thought. On Jun 21, 11:07 am, CraftyTech <hmmed...@gmail.com> wrote:> Hello All, > > Can you guys point out to me, how do I do a case statement within > a template? i.g: my.cnf > > max_allowed_packet=<% case ($memorysize<=4) = 8M, case > ($memorysize<=8) = 16M)? > > I''ve tried different combinations, but so far no luck. The syntax > checker coughs up hair balls.... > > Thanks, > > Henry-- You received this message because you are subscribed to the Google Groups "Puppet Users" group. To post to this group, send email to puppet-users@googlegroups.com. To unsubscribe from this group, send email to puppet-users+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/puppet-users?hl=en.
Joe McDonagh
2010-Jun-21 15:31 UTC
Re: [Puppet Users] erb templating support for case statements?
On 06/21/2010 11:07 AM, CraftyTech wrote:> Hello All, > > Can you guys point out to me, how do I do a case statement within > a template? i.g: my.cnf > > max_allowed_packet=<% case ($memorysize<=4) = 8M, case > ($memorysize<=8) = 16M)? > > I''ve tried different combinations, but so far no luck. The syntax > checker coughs up hair balls.... > > Thanks, > > Henry > >Anything inside <% %> uses standard ruby coding, however it does *not* put stdout into the file. Take this for example: <% if somevar == 100 -%> variable="this" <% else -%> variable="that" <% end -%> This is how you would print out a setting for a variable. -- -- Joe McDonagh Operations Engineer AIM: YoosingYoonickz IRC: joe-mac on freenode "When the going gets weird, the weird turn pro." -- You received this message because you are subscribed to the Google Groups "Puppet Users" group. To post to this group, send email to puppet-users@googlegroups.com. To unsubscribe from this group, send email to puppet-users+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/puppet-users?hl=en.
Benoit Cattié
2010-Jun-21 15:41 UTC
Re: [Puppet Users] erb templating support for case statements?
CraftyTech a écrit, le 21/06/2010 17:07:> Hello All, >Hey,> Can you guys point out to me, how do I do a case statement within > a template? i.g: my.cnf > > max_allowed_packet=<% case ($memorysize<=4) = 8M, case > ($memorysize<=8) = 16M)? >I think case dont support "order" comparaison. You can do it with if / else. Otherwise case statement is : max_allowed_packet=<% case memorysize when 4 %>8M<% when 8 %>16M<% end %> or max_allowed_packet=<% if memorysize.to_i <= 4 %>8M<% elsif memorysize.to_i <= 8 %>16M<% end %>> I''ve tried different combinations, but so far no luck. The syntax > checker coughs up hair balls.... > > Thanks, > > Henry > >Benoit -- You received this message because you are subscribed to the Google Groups "Puppet Users" group. To post to this group, send email to puppet-users@googlegroups.com. To unsubscribe from this group, send email to puppet-users+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/puppet-users?hl=en.
CraftyTech
2010-Jun-21 19:37 UTC
[Puppet Users] Re: erb templating support for case statements?
Thanks for the response. Right now I have it: max_allowed_packet=<% if $memorysize.to_i <= 4 %>8M<% elseif memorysize.to_i = 4.1..8 %>16M<% elseif memorysize.to_i = 8.1..16 %>32M<% elseif memorysize.to_i > 16 %>32M<%end %> but for some reason ignores everything, and it just show "max_allowed_packet=" with no value afterwards... On Jun 21, 11:41 am, Benoit Cattié <pup...@benoit.cattie.net> wrote:> CraftyTech a écrit, le 21/06/2010 17:07:> Hello All, > > Hey, > > Can you guys point out to me, how do I do a case statement within > > a template? i.g: my.cnf > > > max_allowed_packet=<% case ($memorysize<=4) = 8M, case > > ($memorysize<=8) = 16M)? > > I think case dont support "order" comparaison. You can do it with if / else. > > Otherwise case statement is : > max_allowed_packet=<% case memorysize when 4 %>8M<% when 8 %>16M<% end %> > > or > > max_allowed_packet=<% if memorysize.to_i <= 4 %>8M<% elsif > memorysize.to_i <= 8 %>16M<% end %>> I''ve tried different combinations, but so far no luck. The syntax > > checker coughs up hair balls.... > > > Thanks, > > > Henry > > Benoit-- You received this message because you are subscribed to the Google Groups "Puppet Users" group. To post to this group, send email to puppet-users@googlegroups.com. To unsubscribe from this group, send email to puppet-users+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/puppet-users?hl=en.
CraftyTech
2010-Jun-22 13:20 UTC
[Puppet Users] Re: erb templating support for case statements?
Can anyone help out, and let me know why the line below omits everything after the "=" sign, and just puts out the "max_allowed_packet="? It seems pretty straight forward, but for some reason it doesn''t work. Can anyone spot what I''m missing? Thanks, Henry On Jun 21, 3:37 pm, CraftyTech <hmmed...@gmail.com> wrote:> Thanks for the response. Right now I have it: > > max_allowed_packet=<% if $memorysize.to_i <= 4 %>8M<% elseif > memorysize.to_i = 4.1..8 %>16M<% elseif memorysize.to_i = 8.1..16 > %>32M<% elseif memorysize.to_i > 16 %>32M<%end %> > > but for some reason ignores everything, and it just show > "max_allowed_packet=" with no value afterwards... > > On Jun 21, 11:41 am, Benoit Cattié <pup...@benoit.cattie.net> wrote: > > > CraftyTech a écrit, le 21/06/2010 17:07:> Hello All, > > > Hey, > > > Can you guys point out to me, how do I do a case statement within > > > a template? i.g: my.cnf > > > > max_allowed_packet=<% case ($memorysize<=4) = 8M, case > > > ($memorysize<=8) = 16M)? > > > I think case dont support "order" comparaison. You can do it with if / else. > > > Otherwise case statement is : > > max_allowed_packet=<% case memorysize when 4 %>8M<% when 8 %>16M<% end %> > > > or > > > max_allowed_packet=<% if memorysize.to_i <= 4 %>8M<% elsif > > memorysize.to_i <= 8 %>16M<% end %>> I''ve tried different combinations, but so far no luck. The syntax > > > checker coughs up hair balls.... > > > > Thanks, > > > > Henry > > > Benoit-- You received this message because you are subscribed to the Google Groups "Puppet Users" group. To post to this group, send email to puppet-users@googlegroups.com. To unsubscribe from this group, send email to puppet-users+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/puppet-users?hl=en.
CraftyTech
2010-Jun-22 15:09 UTC
[Puppet Users] Re: erb templating support for case statements?
It works now... The syntax was right all along. I was testing it on a VM which has less than a gig of mem, hence why it''d omit the logic, since it didn''t apply. Sometimes the hurtles are a lot closer than you expect them to be :-) On Jun 22, 9:20 am, CraftyTech <hmmed...@gmail.com> wrote:> Can anyone help out, and let me know why the line below omits > everything after the "=" sign, and just puts out the > "max_allowed_packet="? It seems pretty straight forward, but for some > reason it doesn''t work. Can anyone spot what I''m missing? > > Thanks, > > Henry > > On Jun 21, 3:37 pm, CraftyTech <hmmed...@gmail.com> wrote: > > > Thanks for the response. Right now I have it: > > > max_allowed_packet=<% if $memorysize.to_i <= 4 %>8M<% elseif > > memorysize.to_i = 4.1..8 %>16M<% elseif memorysize.to_i = 8.1..16 > > %>32M<% elseif memorysize.to_i > 16 %>32M<%end %> > > > but for some reason ignores everything, and it just show > > "max_allowed_packet=" with no value afterwards... > > > On Jun 21, 11:41 am, Benoit Cattié <pup...@benoit.cattie.net> wrote: > > > > CraftyTech a écrit, le 21/06/2010 17:07:> Hello All, > > > > Hey, > > > > Can you guys point out to me, how do I do a case statement within > > > > a template? i.g: my.cnf > > > > > max_allowed_packet=<% case ($memorysize<=4) = 8M, case > > > > ($memorysize<=8) = 16M)? > > > > I think case dont support "order" comparaison. You can do it with if / else. > > > > Otherwise case statement is : > > > max_allowed_packet=<% case memorysize when 4 %>8M<% when 8 %>16M<% end %> > > > > or > > > > max_allowed_packet=<% if memorysize.to_i <= 4 %>8M<% elsif > > > memorysize.to_i <= 8 %>16M<% end %>> I''ve tried different combinations, but so far no luck. The syntax > > > > checker coughs up hair balls.... > > > > > Thanks, > > > > > Henry > > > > Benoit-- You received this message because you are subscribed to the Google Groups "Puppet Users" group. To post to this group, send email to puppet-users@googlegroups.com. To unsubscribe from this group, send email to puppet-users+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/puppet-users?hl=en.
Thomas Bellman
2010-Jun-22 16:13 UTC
[Puppet Users] Re: erb templating support for case statements?
On 2010-06-21 21:37, CraftyTech wrote:> Thanks for the response. Right now I have it: > > max_allowed_packet=<% if $memorysize.to_i <= 4 %>8M<% elseif > memorysize.to_i = 4.1..8 %>16M<% elseif memorysize.to_i = 8.1..16 > %>32M<% elseif memorysize.to_i > 16 %>32M<%end %> > > but for some reason ignores everything, and it just show > "max_allowed_packet=" with no value afterwards...When I try that exact code in an ERB template, I get an error: Failed to parse template /tmp/trh/smurf.erb: undefined method `to_i='' for "1.93 GB":String at /tmp/trh/craftytech.pp:5 on node kumiko.nsc.liu.se There are actually several errors in your code. First of all, you have a dollar sign in front of the first ''memorysize''. The dollar signs are only valid syntax in Puppet manifests, not in ERB templates (or other Ruby code, for that matter). Second, in Ruby, it''s not ''elseif'', but ''elsif''. Third, you are trying to assign to memorysize.to_i, by using the = operator. Maybe you were thinking about using the equality comparison operator (==)? But even using the == operator would be wrong. To check if a value lies inside a range, you must use the .include? method, like this: (8.1 .. 16).include?(memorysize.to_i) So, if I change the template to this: max_allowed_packet=<% if memorysize.to_i <= 4 %>8M<% elsif (4.1 .. 8).include?(memorysize.to_i) %>16M<% elsif (8.1 .. 16).include?(memorysize.to_i) %>32M<% elsif memorysize.to_i > 16 %>64M<% end %> then it works. But since you are using .to_i, you might not get quite what you expect slightly above each limit; if memorysize is for example 4.99 GB, then memorysize.to_i will be 4, and you will get "max_allowd_packet=8M" as result. Your use of 4.1 and 8.1 implies to me that you wanted to get "max_allowed_packet=16M" in that case. Replacing .to_i with .to_f would fix that. Also, since we are writing it as a chain of if-elsif-elsif statements, this would be better, IMHO: max_allowed_packet=<% if memorysize.to_f <= 4 %>8M<% elsif memorysize.to_f <= 8 %>16M<% elsif memorysize.to_f <= 16 %>32M<% else %>64M<% end %> However, the memorysize fact is unfortunately in a not very nice format. It is a floating point number followed by a unit ("MB" or "GB" in most common cases today, but can also be "kB" or "TB", or nothing at all to signify bytes). Presumably you expect the memorysize to be expressed in Gbytes, but if you run it on a machine with less than 1 Gbyte memory or more that 1024 Gbyte memory, you will not get what you expect. The correct way to handle this is to parse the unit and take that into account. Here is how I would do this: <% mem,unit = memorysize.split mem = mem.to_f # Normalize mem to Mebibyte case unit when nil: mem /= (1<<20) when ''kB'': mem /= (1<<10) when ''MB'': mem *= (1<<0) when ''GB'': mem *= (1<<10) when ''TB'': mem *= (1<<20) end -%> max_allowed_packet=<% if mem <= 4096 %>8M<% elsif mem <= 8192 %>16M<% elsif mem <= 16384 %>32M<% else %>64M<% end %> /Bellman -- You received this message because you are subscribed to the Google Groups "Puppet Users" group. To post to this group, send email to puppet-users@googlegroups.com. To unsubscribe from this group, send email to puppet-users+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/puppet-users?hl=en.
CraftyTech
2010-Jun-22 19:49 UTC
[Puppet Users] Re: erb templating support for case statements?
Whoa dude... that''s like a little crash course on ruby... not that I''m complaining... :-)) You''re right, that first "$" with the variable wasn''t meant to be there... and I was wondering the same thing regarding the usage of the memorysize variable from facter. Thanks a lot Bellman, I now have a good starting point... Cheers, On Jun 22, 12:13 pm, Thomas Bellman <bell...@nsc.liu.se> wrote:> On 2010-06-21 21:37, CraftyTech wrote: > > > Thanks for the response. Right now I have it: > > > max_allowed_packet=<% if $memorysize.to_i <= 4 %>8M<% elseif > > memorysize.to_i = 4.1..8 %>16M<% elseif memorysize.to_i = 8.1..16 > > %>32M<% elseif memorysize.to_i > 16 %>32M<%end %> > > > but for some reason ignores everything, and it just show > > "max_allowed_packet=" with no value afterwards... > > When I try that exact code in an ERB template, I get an error: > > Failed to parse template /tmp/trh/smurf.erb: undefined method > `to_i='' for "1.93 GB":String at /tmp/trh/craftytech.pp:5 on > node kumiko.nsc.liu.se > > There are actually several errors in your code. First of all, you > have a dollar sign in front of the first ''memorysize''. The dollar > signs are only valid syntax in Puppet manifests, not in ERB templates > (or other Ruby code, for that matter). > > Second, in Ruby, it''s not ''elseif'', but ''elsif''. > > Third, you are trying to assign to memorysize.to_i, by using the > = operator. Maybe you were thinking about using the equality > comparison operator (==)? > > But even using the == operator would be wrong. To check if a value > lies inside a range, you must use the .include? method, like this: > > (8.1 .. 16).include?(memorysize.to_i) > > So, if I change the template to this: > > max_allowed_packet=<% > if memorysize.to_i <= 4 %>8M<% > elsif (4.1 .. 8).include?(memorysize.to_i) %>16M<% > elsif (8.1 .. 16).include?(memorysize.to_i) %>32M<% > elsif memorysize.to_i > 16 %>64M<% > end %> > > then it works. But since you are using .to_i, you might not get > quite what you expect slightly above each limit; if memorysize is > for example 4.99 GB, then memorysize.to_i will be 4, and you will > get "max_allowd_packet=8M" as result. Your use of 4.1 and 8.1 > implies to me that you wanted to get "max_allowed_packet=16M" in > that case. Replacing .to_i with .to_f would fix that. > > Also, since we are writing it as a chain of if-elsif-elsif statements, > this would be better, IMHO: > > max_allowed_packet=<% > if memorysize.to_f <= 4 %>8M<% > elsif memorysize.to_f <= 8 %>16M<% > elsif memorysize.to_f <= 16 %>32M<% > else %>64M<% > end %> > > However, the memorysize fact is unfortunately in a not very nice format. > It is a floating point number followed by a unit ("MB" or "GB" in most > common cases today, but can also be "kB" or "TB", or nothing at all to > signify bytes). Presumably you expect the memorysize to be expressed in > Gbytes, but if you run it on a machine with less than 1 Gbyte memory or > more that 1024 Gbyte memory, you will not get what you expect. > > The correct way to handle this is to parse the unit and take that > into account. Here is how I would do this: > > <% mem,unit = memorysize.split > mem = mem.to_f > # Normalize mem to Mebibyte > case unit > when nil: mem /= (1<<20) > when ''kB'': mem /= (1<<10) > when ''MB'': mem *= (1<<0) > when ''GB'': mem *= (1<<10) > when ''TB'': mem *= (1<<20) > end > -%> > max_allowed_packet=<% > if mem <= 4096 %>8M<% > elsif mem <= 8192 %>16M<% > elsif mem <= 16384 %>32M<% > else %>64M<% > end %> > > /Bellman-- You received this message because you are subscribed to the Google Groups "Puppet Users" group. To post to this group, send email to puppet-users@googlegroups.com. To unsubscribe from this group, send email to puppet-users+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/puppet-users?hl=en.