Custom type obligatory field?

56 views
Skip to first unread message

Jakov Sosic

unread,
Sep 5, 2012, 2:07:21 PM9/5/12
to Puppet Users
Hi.

I've been trying to develop a module for managing Cobbler from puppet.
So, I need a custom type - cobblerdistro, which will fetch ISO from
http, unpack it at desired destination and register distro with Cobbler.

I have one issue with ensure => absent situation.

First of all, here is my type:

<code>

# cat type/cobblerdistro.rb
Puppet::Type.newtype(:cobblerdistro) do
@doc = "Manages the Cobbler distros.

A typical rule will look like this:

distro {'CentOS-6.3-x86_64':
ensure => present,
arch => 'x86_64',
isolink =>
'http://mi.mirror.garr.it/mirrors/CentOS/6.3/isos/x86_64/CentOS-6.3-x86_64-bin-DVD1.iso',
}

This rule would ensure that the kernel swappiness setting be set to '20'"

desc "The cobbler distro type"

ensurable

newparam(:name) do
isnamevar
desc "The name of the distro, that will create subdir in $distro"
end

newparam(:arch) do
desc "The architecture of distro (x86_64 or i386)."
newvalues(:x86_64, :i386)
munge do |value| # fix values
case value
when :amd64
:x86_64
when :i86pc
:i386
else
super
end
end
end

newparam(:isolink) do
desc "The link of the distro ISO image."
validate do |value|
unless value =~ /^http:.*iso/
raise ArgumentError, "%s is not a valid link to ISO image." % value
end
end
end

newparam(:destdir) do
desc "The link of the distro ISO image."
validate do |value|
unless Pathname.new(value).absolute?
raise ArgumentError, "Full pathname must be set"
end
end
end
end

</code>

Now, how can I ensure that destdir param is obligatory? I don't want my
provider to run at all, if destdir is not specified in the resource.

So, for example, I want to spew error if user writes this code:
cobblerdistro {'CentOS-6.3-x86_64':
ensure => absent,
}

and want this to be a minimal code snippet

cobblerdistro {'CentOS-6.3-x86_64':
ensure => absent,
destdir => '/distro',
}

Otherwise, I have to hardcode the destination directory for distribution
releases and I don't want to do that :(

Jakov Sosic

unread,
Sep 7, 2012, 9:04:49 AM9/7/12
to puppet...@googlegroups.com
On 09/05/2012 08:07 PM, Jakov Sosic wrote:
> newparam(:destdir) do
> desc "The link of the distro ISO image."
> validate do |value|
> unless Pathname.new(value).absolute?
> raise ArgumentError, "Full pathname must be set"
> end
> end
> end


> cobblerdistro {'CentOS-6.3-x86_64':
> ensure => absent,
> destdir => '/distro',
> }



Maybe I didn't make my self clear enough.... On a puppet run, if the
resource doesn't have destdir defined, i get this error:

err:
/Stage[main]//Node[sunce.srce.hr]/Cobbler::Add_distro[CentOS-6.3-x86_64]/Cobblerdistro[CentOS-6.3-x86_64]:
Could not evaluate: undefined method `+' for nil:NilClass

I would like to inform user what the problem is rather then print out
some obscure message like this one.

So, how can I do this from type definition ruby code? Thank you.

jcbollinger

unread,
Sep 10, 2012, 10:59:17 AM9/10/12
to puppet...@googlegroups.com


You cannot use the parameter validation hook for this because it is invoked only when a value is set.  The case you want to trap is exactly the case to which that hook does not apply.

As far as I know or can tell, there is no hook for what you want -- basically, whole-resource validation.  You can put the validation code into your provider(s), at the point where you (first) use the value.  That's not ideal, but it can get you a better error message (albeit issued on the client side, not the master).


John

Trevor Vaughan

unread,
Sep 10, 2012, 11:45:40 AM9/10/12
to puppet...@googlegroups.com
There's a 'validate' define that can cover global cases for each type.
This will probably do what you want it to.

Trevor
> --
> 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/-/UDMxli5gn7QJ.
>
> To post to this group, send email to puppet...@googlegroups.com.
> To unsubscribe from this group, send email to
> puppet-users...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/puppet-users?hl=en.



--
Trevor Vaughan
Vice President, Onyx Point, Inc
(410) 541-6699
tvau...@onyxpoint.com

-- This account not approved for unencrypted proprietary information --

Jakov Sosic

unread,
Sep 10, 2012, 9:37:33 PM9/10/12
to puppet...@googlegroups.com
On 09/10/2012 04:59 PM, jcbollinger wrote:
>
>
> On Friday, September 7, 2012 8:04:57 AM UTC-5, Jakov Sosic wrote:
>
> On 09/05/2012 08:07 PM, Jakov Sosic wrote:
> > newparam(:destdir) do
> > desc "The link of the distro ISO image."
> > validate do |value|
> > unless Pathname.new(value).absolute?
> > raise ArgumentError, "Full pathname must be set"
> > end
> > end
> > end
>
>
> > cobblerdistro {'CentOS-6.3-x86_64':
> > ensure => absent,
> > destdir => '/distro',
> > }
>
>
>
> Maybe I didn't make my self clear enough.... On a puppet run, if the
> resource doesn't have destdir defined, i get this error:
>
> err:
> /Stage[main]//Node[sunce.srce.hr
> <http://sunce.srce.hr>]/Cobbler::Add_distro[CentOS-6.3-x86_64]/Cobblerdistro[CentOS-6.3-x86_64]:
>
> Could not evaluate: undefined method `+' for nil:NilClass
>
> I would like to inform user what the problem is rather then print out
> some obscure message like this one.
>
> So, how can I do this from type definition ruby code? Thank you.
>
>
>
> You cannot use the parameter validation hook for this because it is
> invoked only when a value is set. The case you want to trap is exactly
> the case to which that hook does not apply.

Hmm, now that explains it!


> As far as I know or can tell, there is no hook for what you want --
> basically, whole-resource validation. You can put the validation code
> into your provider(s), at the point where you (first) use the value.
> That's not ideal, but it can get you a better error message (albeit
> issued on the client side, not the master).

This is how I circuvmented it in my provider, by providing custom method
check_params and calling it from create/destroy/exits? methods.

def check_params
if @resource[:destdir].nil?
raise ArgumentError, "destdir must be specified in cobblerdistro
resource!"
end
end

def create
check_params
# other create code
...
...
end

"exists?" and "destroy" look the same as "create".


Now, if I don't have destdir specified in resource definition, here is
what the puppet clients poops out:

err: /Stage[main]//Node[somenode]/Cobblerdistro[CentOS-6.3-x86_64]:
Could not evaluate: destdir must be specified in cobblerdistro resource!



Now, I think this is much better. Hope you like it :) And thank you for
assistance.


--
Jakov Sosic
www.srce.unizg.hr
Reply all
Reply to author
Forward
0 new messages