Stephen Brown II
2013-Jul-29 18:38 UTC
[Puppet Users] Verify variable value and set to default param if out of range?
Greetings all, I have a feeling I''m trying to be a bit too clever for my own good, or making the mistake of treating the declarative language as a procedural. But at any rate, here is my issue. I would like to set up a params class ( in the style of http://docs.puppetlabs.com/guides/parameterized_classes<http://docs.puppetlabs.com/guides/parameterized_classes.html#appendix-smart-parameter-defaults> ) for installing mariadb on any operating system that is supported by the official mariadb.org repository configuratior: https://downloads.mariadb.org/mariadb/repositories/ However, I''ve noticed that not all versions of MariaDB are available for all OS releases. For the most part, 5.5 and 10.0 are available, but for older versions of Ubuntu /(hardy|maverick|natty|oneiric)/, only 5.2 and 5.3 are available. Here is the gist of my params.pp as it stands: https://gist.github.com/StephenBrown2/6106113 What I would eventually like to get working is the ability for the module to set a reasonable default, and also allow for overriding in the calling, but if the operating system doesn''t support that MariaDB release version, to use the latest available release instead. For example, this should work fine: class { ''maria'': } This should also work: class { ''maria'': version => ''5.5'', } both resulting in mariadb 5.5 being installed on anything but those older ubuntu''s. in which case, Puppet would throw a warning and set $version instead to ''5.3''. Similarly, if this was specified: class { ''maria'': version => ''5.3'', } on a Debian 7 system, I''d expect it to come back as ''10.0''. But this might be where the cleverness could be a liability. Might I have to simply set the default version for each possible operatingsystem value? I would hope not, as that seems a bit unweildy, but maybe necessary since variables can only be declared once per scope ( http://docs.puppetlabs.com/learning/variables<http://docs.puppetlabs.com/learning/variables.html#variables> : "You can only assign the same variable *once* in a given scope." ) Please let me know if I''ve been unclear, or enlighten me if this is going down the wrong path. Thanks! Stephen B -- You received this message because you are subscribed to the Google Groups "Puppet Users" group. To unsubscribe from this group and stop receiving emails from it, send an email to puppet-users+unsubscribe@googlegroups.com. To post to this group, send email to puppet-users@googlegroups.com. Visit this group at http://groups.google.com/group/puppet-users. For more options, visit https://groups.google.com/groups/opt_out.
Rahul Khengare
2013-Jul-30 12:29 UTC
[Puppet Users] Re: Verify variable value and set to default param if out of range?
Hi Stephen, As per my understanding of your query, you want version parameter value 5.5 as default and on passing parameter value its become 5.3. But in your case its take version = 5.5. Correct me if i am wrong. Solution for your query is u have to construct parameterized class maria and it has to be inheriated by maria::params. # maria class class maria ( $version = maria::params::version ) inherits maria::params { notify { "$version" :} } # params class class maria::params { $version = ''5.5'' } # when we call the class maria with version parameter it set the $version value to passed value (here 5.3) class {"maria": $version => ''5.3'' } # when we call the class maria without version parameter it set the $version value to default value from params class (here 5.5) class {"maria": } I hope this will solve your problem. If i interpreted your query correctly. Thanks and Regards, Rahul Khengare. NTT DATA GTS (OSS centre) pune On Tuesday, July 30, 2013 12:08:17 AM UTC+5:30, Stephen Brown II wrote:> > Greetings all, > > I have a feeling I''m trying to be a bit too clever for my own good, or > making the mistake of treating the declarative language as a procedural. > But at any rate, here is my issue. > > I would like to set up a params class ( in the style of > http://docs.puppetlabs.com/guides/parameterized_classes<http://docs.puppetlabs.com/guides/parameterized_classes.html#appendix-smart-parameter-defaults> ) for > installing mariadb on any operating system that is supported by the > official mariadb.org repository configuratior: > https://downloads.mariadb.org/mariadb/repositories/ > > However, I''ve noticed that not all versions of MariaDB are available for > all OS releases. For the most part, 5.5 and 10.0 are available, but for > older versions of Ubuntu /(hardy|maverick|natty|oneiric)/, only 5.2 and 5.3 > are available. > > Here is the gist of my params.pp as it stands: > https://gist.github.com/StephenBrown2/6106113 > > What I would eventually like to get working is the ability for the module > to set a reasonable default, and also allow for overriding in the calling, > but if the operating system doesn''t support that MariaDB release version, > to use the latest available release instead. > > For example, this should work fine: > > class { ''maria'': } > > This should also work: > > class { ''maria'': > version => ''5.5'', > } > > both resulting in mariadb 5.5 being installed on anything but those older > ubuntu''s. in which case, Puppet would throw a warning and set $version > instead to ''5.3''. > > Similarly, if this was specified: > > class { ''maria'': > version => ''5.3'', > } > > on a Debian 7 system, I''d expect it to come back as ''10.0''. But this might > be where the cleverness could be a liability. > > Might I have to simply set the default version for each possible > operatingsystem value? I would hope not, as that seems a bit unweildy, but > maybe necessary since variables can only be declared once per scope ( > http://docs.puppetlabs.com/learning/variables<http://docs.puppetlabs.com/learning/variables.html#variables> > : "You can only assign the same variable *once* in a given scope." ) > > Please let me know if I''ve been unclear, or enlighten me if this is going > down the wrong path. > > Thanks! > Stephen B >-- You received this message because you are subscribed to the Google Groups "Puppet Users" group. To unsubscribe from this group and stop receiving emails from it, send an email to puppet-users+unsubscribe@googlegroups.com. To post to this group, send email to puppet-users@googlegroups.com. Visit this group at http://groups.google.com/group/puppet-users. For more options, visit https://groups.google.com/groups/opt_out.
Stephen Brown II
2013-Jul-30 13:34 UTC
[Puppet Users] Re: Verify variable value and set to default param if out of range?
Well, I understand that much, but I want to validate that the parameter passed to the called class is valid. Right now I''m doing the logic for setting the valid releases in the params class, like this: case $::osfamily { ''RedHat'': { $available_releases = [''5.5'', ''10.0''] } ''Debian'': { case $os { ''debian'': { case $::lsbmajrelease { ''6'': { $available_releases = [''5.2'', ''5.3'', ''5.5'', ''10.0''] } ''7'': { $available_releases = [''5.5'', ''10.0''] } default: { fail( "MariaDB not supported on ${::operatingsystem} ${::operatingsystemrelease}" ) } } } ''ubuntu'': { case $::lsbdistcodename { /(hardy|maverick|natty|oneiric)/: { if $::operatingsystem == ''LinuxMint'' { fail( "MariaDB not supported on ${::operatingsystem} ${::operatingsystemrelease}" ) } else { $available_releases = [''5.2'', ''5.3''] } } ''lucid'': { $available_releases = [''5.2'', ''5.3'', ''5.5'', ''10.0''] } /(precise|quantal|raring)/: { $available_releases = [''5.5'', ''10.0''] } default: { fail( "MariaDB not supported on ${::operatingsystem} ${::operatingsystemrelease}" ) } } } default: { fail( "MariaDB not supported on ${::operatingsystem} ${::operatingsystemrelease}" ) } } } And then I do the validation of the $version variable as such: if ! ($version in $available_releases) { warning( "MariaDB ${version} not supported on ${::operatingsystem} ${::operatingsystemrelease}, setting release to ${available_releases[-1]}" ) $version = $available_releases[-1] } But maybe I just need to set the default value in params, and do the validation in the maria class itself, to make sure scope is correct. Then I would have to simply fail puppet, and not try to correct the user error, like this: if ! ($version in $maria::params::available_releases) { fail( "MariaDB ${version} not supported on ${::operatingsystem} ${::operatingsystemrelease}, setting release to ${available_releases[-1]}" ) } On Tuesday, July 30, 2013 8:29:40 AM UTC-4, Rahul Khengare wrote:> > Hi Stephen, > As per my understanding of your query, you want version parameter value > 5.5 as default and on passing parameter value its become 5.3. > But in your case its take version = 5.5. Correct me if i am wrong. > > Solution for your query is u have to construct parameterized class maria > and it has to be inheriated by maria::params. > > # maria class > class maria ( > $version = maria::params::version > ) inherits maria::params { > > notify { "$version" :} > } > > # params class > class maria::params { > $version = ''5.5'' > } > > # when we call the class maria with version parameter it set the $version > value to passed value (here 5.3) > class {"maria": > $version => ''5.3'' > } > > # when we call the class maria without version parameter it set the > $version value to default value from params class (here 5.5) > class {"maria": > } > > I hope this will solve your problem. If i interpreted your query correctly. > > Thanks and Regards, > Rahul Khengare. > NTT DATA GTS (OSS centre) > pune > > > On Tuesday, July 30, 2013 12:08:17 AM UTC+5:30, Stephen Brown II wrote: >> >> Greetings all, >> >> I have a feeling I''m trying to be a bit too clever for my own good, or >> making the mistake of treating the declarative language as a procedural. >> But at any rate, here is my issue. >> >> I would like to set up a params class ( in the style of >> http://docs.puppetlabs.com/guides/parameterized_classes<http://docs.puppetlabs.com/guides/parameterized_classes.html#appendix-smart-parameter-defaults> ) for >> installing mariadb on any operating system that is supported by the >> official mariadb.org repository configuratior: >> https://downloads.mariadb.org/mariadb/repositories/ >> >> However, I''ve noticed that not all versions of MariaDB are available for >> all OS releases. For the most part, 5.5 and 10.0 are available, but for >> older versions of Ubuntu /(hardy|maverick|natty|oneiric)/, only 5.2 and 5.3 >> are available. >> >> Here is the gist of my params.pp as it stands: >> https://gist.github.com/StephenBrown2/6106113 >> >> What I would eventually like to get working is the ability for the module >> to set a reasonable default, and also allow for overriding in the calling, >> but if the operating system doesn''t support that MariaDB release version, >> to use the latest available release instead. >> >> For example, this should work fine: >> >> class { ''maria'': } >> >> This should also work: >> >> class { ''maria'': >> version => ''5.5'', >> } >> >> both resulting in mariadb 5.5 being installed on anything but those older >> ubuntu''s. in which case, Puppet would throw a warning and set $version >> instead to ''5.3''. >> >> Similarly, if this was specified: >> >> class { ''maria'': >> version => ''5.3'', >> } >> >> on a Debian 7 system, I''d expect it to come back as ''10.0''. But this >> might be where the cleverness could be a liability. >> >> Might I have to simply set the default version for each possible >> operatingsystem value? I would hope not, as that seems a bit unweildy, but >> maybe necessary since variables can only be declared once per scope ( >> http://docs.puppetlabs.com/learning/variables<http://docs.puppetlabs.com/learning/variables.html#variables> >> : "You can only assign the same variable *once* in a given scope." ) >> >> Please let me know if I''ve been unclear, or enlighten me if this is going >> down the wrong path. >> >> Thanks! >> Stephen B >> >-- You received this message because you are subscribed to the Google Groups "Puppet Users" group. To unsubscribe from this group and stop receiving emails from it, send an email to puppet-users+unsubscribe@googlegroups.com. To post to this group, send email to puppet-users@googlegroups.com. Visit this group at http://groups.google.com/group/puppet-users. For more options, visit https://groups.google.com/groups/opt_out.
jcbollinger
2013-Jul-30 14:13 UTC
[Puppet Users] Re: Verify variable value and set to default param if out of range?
On Monday, July 29, 2013 1:38:17 PM UTC-5, Stephen Brown II wrote:> > Greetings all, > > I have a feeling I''m trying to be a bit too clever for my own good, or > making the mistake of treating the declarative language as a procedural. > But at any rate, here is my issue. > > I would like to set up a params class ( in the style of > http://docs.puppetlabs.com/guides/parameterized_classes<http://docs.puppetlabs.com/guides/parameterized_classes.html#appendix-smart-parameter-defaults> ) for > installing mariadb on any operating system that is supported by the > official mariadb.org repository configuratior: > https://downloads.mariadb.org/mariadb/repositories/ > > However, I''ve noticed that not all versions of MariaDB are available for > all OS releases. For the most part, 5.5 and 10.0 are available, but for > older versions of Ubuntu /(hardy|maverick|natty|oneiric)/, only 5.2 and 5.3 > are available. > > Here is the gist of my params.pp as it stands: > https://gist.github.com/StephenBrown2/6106113 > > What I would eventually like to get working is the ability for the module > to set a reasonable default, and also allow for overriding in the calling, > but if the operating system doesn''t support that MariaDB release version, > to use the latest available release instead. > > For example, this should work fine: > > class { ''maria'': } > > This should also work: > > class { ''maria'': > version => ''5.5'', > } > > both resulting in mariadb 5.5 being installed on anything but those older > ubuntu''s. in which case, Puppet would throw a warning and set $version > instead to ''5.3''. > >I think that''s a lousy idea. It''s very counterintuitive for the class to accept data telling it what version to use, and then to use a different version instead. At minimum, name the parameter so that it better reflects its function (e.g. maybe "preferred_version"), but even then I''m not very fond of the idea. I don''t see a good use case. If you don''t care which version is installed on any given machine, then why is it worthwhile to specify even a preferred version?> Similarly, if this was specified: > > class { ''maria'': > version => ''5.3'', > } > > on a Debian 7 system, I''d expect it to come back as ''10.0''. But this might > be where the cleverness could be a liability. > > Might I have to simply set the default version for each possible > operatingsystem value? I would hope not, as that seems a bit unweildy, but > maybe necessary since variables can only be declared once per scope ( > http://docs.puppetlabs.com/learning/variables<http://docs.puppetlabs.com/learning/variables.html#variables> > : "You can only assign the same variable *once* in a given scope." ) > > Please let me know if I''ve been unclear, or enlighten me if this is going > down the wrong path. > >The values of class parameters are set by the first-parsed declaration of the class, taking into account any explicit data bindings, hiera data bindings, and default values, but not anything in the class body. Parameter values do not thereafter change for the duration of the catalog run. You could, however, have additional class variables. For example, you might have $maria::preferred_version (a parameter) and $maria::selected_version (an ordinary class variable). I''m not convinced that''s a good idea, but at least it would be clear. An alternative would be to compute a default version, perhaps in a ::params class, based on the node facts. Use that version if the user does not specify a different one, but otherwise defer to the user. I''d be inclined to simply allow application of the catalog to fail if an unsupported version is specified (which also lets it succeed without you changing anything if the available versions on for the target platform change). If you want to validate, however, then do so, failing the catalog if an invalid version is specified. John -- You received this message because you are subscribed to the Google Groups "Puppet Users" group. To unsubscribe from this group and stop receiving emails from it, send an email to puppet-users+unsubscribe@googlegroups.com. To post to this group, send email to puppet-users@googlegroups.com. Visit this group at http://groups.google.com/group/puppet-users. For more options, visit https://groups.google.com/groups/opt_out.