Amos Shapira
2012-Oct-02 01:28 UTC
[Puppet Users] Removing intermediate variables in calculation
Hello, I have a small Puppet 2.7 module to configure Sonatype Nexus Professional. The module takes, among other things, a baseurl in the form of "http://example.com/path" and I''d like it to extract the "/path" from that variable into a separate variable IF an optional "path" variable haven''t been supplied. Here is an extract: class nexus::config( ... $baseurl, $webapp_context_path = ''/'' ) { if ($webapp_context_path != '''') { $int_webapp_context_path = $webapp_context_path notify{"using webapp_context_path \"${webapp_context_path}\"":} } else { $extracted_url_path = regsubst($baseurl, ''^https?://[^/]+(/.*)'', ''\1'') if ($extracted_url_path) { $int_webapp_context_path = $extracted_url_path } else { # in case we were given a $baseurl without the tailing "/" $int_webapp_context_path = ''/'' } notify{"extracted int_webapp_context_path \"${int_webapp_context_path}\" from url \"${baseurl}\"": } } # use $int_webapp_context_path in the .erb template file My question - this use of $int_webapp_context_path and $extracted_url_path looks a bit shabby. But I didn''t find a way to use conditional assignments to remove these intermediate variables and either: 1. Assign the value I want to $webapp_context_path if it''s not set yet. 2. Or at least get rid of the $extracted_url_path Is there a nicer way to achieve the above? Thanks. -- You received this message because you are subscribed to the Google Groups "Puppet Users" group. To view this discussion on the web visit https://groups.google.com/d/msg/puppet-users/-/rNRGRX2LrzkJ. 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.
Guzman Braso
2012-Oct-02 15:05 UTC
Re: [Puppet Users] Removing intermediate variables in calculation
I''m in no way a puppet guru.... but rewritting it didnt work? from a logic point of view this should work: class .... $baseurl, $webapp_context_path = '''' ) { if ($webapp_context_path == '''') { # Try to get value from baseurl $webapp_context_path = regsubst($baseurl, ''^https?://[^/]+(/.*)'', ''\1'') if ($webapp_context_path == '''') { #Set default $webapp_context_path = ''/'' } } notify{"rewritted webapp_context_path=''${webapp_context_path}'' from url=''${baseurl}''": } } But as I said I''m fairly new with puppet, did not tried above code. On Mon, Oct 1, 2012 at 10:28 PM, Amos Shapira <amos.shapira@gmail.com>wrote:> Hello, > > I have a small Puppet 2.7 module to configure Sonatype Nexus Professional. > The module takes, among other things, a baseurl in the form of " > http://example.com/path" and I''d like it to extract the "/path" from that > variable into a separate variable IF an optional "path" variable haven''t > been supplied. > > Here is an extract: > class nexus::config( > ... > $baseurl, > $webapp_context_path = '''' > ) { > if ($webapp_context_path == '''') > { > $webapp_context_path = regsubst($baseurl, ''^https?://[^/]+(/.*)'', ''\1'') >$webapp_context_path = regsubst($baseurl, ''^https?://[^/]+(/.*)'', ''\1'') if ($extracted_url_path)> { > $int_webapp_context_path = $extracted_url_path > } > else > { > # in case we were given a $baseurl without the tailing "/" > $int_webapp_context_path = ''/'' > } > notify{"extracted int_webapp_context_path > \"${int_webapp_context_path}\" from url \"${baseurl}\"": } > } > > # use $int_webapp_context_path in the .erb template file > > My question - this use of $int_webapp_context_path and $extracted_url_path > looks a bit shabby. But I didn''t find a way to use conditional assignments > to remove these intermediate variables and either: > 1. Assign the value I want to $webapp_context_path if it''s not set yet. > 2. Or at least get rid of the $extracted_url_path > > Is there a nicer way to achieve the above? > > Thanks. > > -- > You received this message because you are subscribed to the Google Groups > "Puppet Users" group. > To view this discussion on the web visit > https://groups.google.com/d/msg/puppet-users/-/rNRGRX2LrzkJ. > 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. >-- GuruHub - Guzmán Brasó http://www.guruhub.com.uy - +59898674020 Rivera 3565 - CP11400 - Montevideo, Uruguay -- 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.
jcbollinger
2012-Oct-03 15:02 UTC
Re: [Puppet Users] Removing intermediate variables in calculation
On Tuesday, October 2, 2012 10:10:13 AM UTC-5, Guzmán Brasó wrote:> > I''m in no way a puppet guru.... but rewritting it didnt work? from a logic > point of view this should work: > > > class .... > $baseurl, > $webapp_context_path = '''' > ) { > if ($webapp_context_path == '''') { > # Try to get value from baseurl > $webapp_context_path = regsubst($baseurl, ''^https?://[^/]+(/.*)'', ''\1'') > if ($webapp_context_path == '''') { > #Set default > $webapp_context_path = ''/'' > } > } > notify{"rewritted webapp_context_path=''${webapp_context_path}'' from > url=''${baseurl}''": } > } > >No, that won''t work, because Puppet variables, including class and definition parameters, can be assigned a value at most once. There are good reasons for that, but they''re not really relevant to the present question. Anyway, for that very reason, constructs involving internal variables such as $int_webapp_context_path are fairly standard practice in Puppet. Among the alternatives could be to redesign your class parameterization or to drop the adaptive behavior. Or you could write a custom function implementing the logic for context path selection, or you could put it into your template instead of your class. (Note, by the way, that because you assign a default value of ''/'' to $webapp_context_path in your parameter list, in the class body that parameter will be observed to have an empty value only if such a value is explicitly set when the class is declared.) John -- You received this message because you are subscribed to the Google Groups "Puppet Users" group. To view this discussion on the web visit https://groups.google.com/d/msg/puppet-users/-/sF_qog4IzWQJ. 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.
Amos Shapira
2012-Oct-04 01:01 UTC
Re: [Puppet Users] Removing intermediate variables in calculation
Thanks Guzman, as John pointed out that won''t work (I think this is because Puppet''s language is declarative, even if it resembles procedural language in many ways). On Wednesday, October 3, 2012 1:10:13 AM UTC+10, Guzmán Brasó wrote:> > I''m in no way a puppet guru.... but rewritting it didnt work? from a logic > point of view this should work: > > > class .... > $baseurl, > $webapp_context_path = '''' > ) { > if ($webapp_context_path == '''') { > # Try to get value from baseurl > $webapp_context_path = regsubst($baseurl, ''^https?://[^/]+(/.*)'', ''\1'') > if ($webapp_context_path == '''') { > #Set default > $webapp_context_path = ''/'' > } > } > notify{"rewritted webapp_context_path=''${webapp_context_path}'' from > url=''${baseurl}''": } > } > > But as I said I''m fairly new with puppet, did not tried above code. > > On Mon, Oct 1, 2012 at 10:28 PM, Amos Shapira <amos.s...@gmail.com<javascript:> > > wrote: > >> Hello, >> >> I have a small Puppet 2.7 module to configure Sonatype Nexus >> Professional. The module takes, among other things, a baseurl in the form >> of "http://example.com/path" and I''d like it to extract the "/path" from >> that variable into a separate variable IF an optional "path" variable >> haven''t been supplied. >> >> Here is an extract: >> class nexus::config( >> ... >> $baseurl, >> $webapp_context_path = '''' >> ) { >> if ($webapp_context_path == '''') >> { >> $webapp_context_path = regsubst($baseurl, ''^https?://[^/]+(/.*)'', >> ''\1'') >> > > $webapp_context_path = regsubst($baseurl, ''^https?://[^/]+(/.*)'', ''\1'') > > if ($extracted_url_path) >> { >> $int_webapp_context_path = $extracted_url_path >> } >> else >> { >> # in case we were given a $baseurl without the tailing "/" >> $int_webapp_context_path = ''/'' >> } >> notify{"extracted int_webapp_context_path >> \"${int_webapp_context_path}\" from url \"${baseurl}\"": } >> } >> >> # use $int_webapp_context_path in the .erb template file >> >> My question - this use of $int_webapp_context_path and >> $extracted_url_path looks a bit shabby. But I didn''t find a way to use >> conditional assignments to remove these intermediate variables and either: >> 1. Assign the value I want to $webapp_context_path if it''s not set yet. >> 2. Or at least get rid of the $extracted_url_path >> >> Is there a nicer way to achieve the above? >> >> Thanks. >> >> -- >> You received this message because you are subscribed to the Google Groups >> "Puppet Users" group. >> To view this discussion on the web visit >> https://groups.google.com/d/msg/puppet-users/-/rNRGRX2LrzkJ. >> To post to this group, send email to puppet...@googlegroups.com<javascript:> >> . >> To unsubscribe from this group, send email to >> puppet-users...@googlegroups.com <javascript:>. >> For more options, visit this group at >> http://groups.google.com/group/puppet-users?hl=en. >> > > > > -- > GuruHub - Guzmán Brasó > http://www.guruhub.com.uy - +59898674020 > Rivera 3565 - CP11400 - Montevideo, Uruguay > >-- You received this message because you are subscribed to the Google Groups "Puppet Users" group. To view this discussion on the web visit https://groups.google.com/d/msg/puppet-users/-/sM14FzeDd3AJ. 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.
Amos Shapira
2012-Oct-04 01:02 UTC
Re: [Puppet Users] Removing intermediate variables in calculation
Thanks John. At least I know there is no way to improve my code. I prefer to try to keep logic in the .pp files and out of the templates, just to make it easier to find. On Thursday, October 4, 2012 1:02:35 AM UTC+10, jcbollinger wrote:> > > > On Tuesday, October 2, 2012 10:10:13 AM UTC-5, Guzmán Brasó wrote: >> >> I''m in no way a puppet guru.... but rewritting it didnt work? from a >> logic point of view this should work: >> >> >> class .... >> $baseurl, >> $webapp_context_path = '''' >> ) { >> if ($webapp_context_path == '''') { >> # Try to get value from baseurl >> $webapp_context_path = regsubst($baseurl, ''^https?://[^/]+(/.*)'', >> ''\1'') >> if ($webapp_context_path == '''') { >> #Set default >> $webapp_context_path = ''/'' >> } >> } >> notify{"rewritted webapp_context_path=''${webapp_context_path}'' from >> url=''${baseurl}''": } >> } >> >> > > No, that won''t work, because Puppet variables, including class and > definition parameters, can be assigned a value at most once. There are > good reasons for that, but they''re not really relevant to the present > question. > > Anyway, for that very reason, constructs involving internal variables such > as $int_webapp_context_path are fairly standard practice in Puppet. > Among the alternatives could be to redesign your class parameterization > or to drop the adaptive behavior. Or you could write a custom function > implementing the logic for context path selection, or you could put it into > your template instead of your class. > > (Note, by the way, that because you assign a default value of ''/'' to > $webapp_context_path in your parameter list, in the class body that > parameter will be observed to have an empty value only if such a value is > explicitly set when the class is declared.) > > > John > >-- You received this message because you are subscribed to the Google Groups "Puppet Users" group. To view this discussion on the web visit https://groups.google.com/d/msg/puppet-users/-/Bmju_PjXasEJ. 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.