FAQ
I'm trying to create a function that I can call in a manifest like this:

$doc = doc('apache::service')

In the function, I want it to actually find the filename for the
'apache::service' class... from there, I'm going to run some code that will
generate the puppet-doc in text format, and then return it in a variable.
Ultimately this is so that I can have nodes self-document themselves.

Right now I'm stuck on figuring out the filename from the classname though.
Anyone have any tips on this? Is there a function in the Puppet ruby code
somewhere that will translate a class-name into a file path?

--
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?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.

Search Discussions

  • Ken Barber at Feb 22, 2013 at 6:15 pm
    I do this kind of thing here:

    https://github.com/puppetlabs/puppetlabs-kwalify/blob/master/lib/puppet/parser/functions/validate_resource.rb#L24

    ken.
    On Fri, Feb 22, 2013 at 6:05 PM, Matt W wrote:
    I'm trying to create a function that I can call in a manifest like this:

    $doc = doc('apache::service')

    In the function, I want it to actually find the filename for the
    'apache::service' class... from there, I'm going to run some code that will
    generate the puppet-doc in text format, and then return it in a variable.
    Ultimately this is so that I can have nodes self-document themselves.

    Right now I'm stuck on figuring out the filename from the classname though.
    Anyone have any tips on this? Is there a function in the Puppet ruby code
    somewhere that will translate a class-name into a file path?

    --
    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?hl=en.
    For more options, visit https://groups.google.com/groups/opt_out.
    --
    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?hl=en.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Matt Wise at Feb 22, 2013 at 8:27 pm

    Thanks... I ended up with this:

    #
    # my_puppet_doc.rb
    #

    require 'puppet/util/rdoc'

    module Puppet::Parser::Functions
    newfunction(:my_puppet_doc, :type => :rvalue, :doc => <<-EOS

    This function returns the 'puppet doc' header from the module
    that called it. Usage:

    $doc = my_puppet_doc()

    EOS
    ) do |args|
    # Get the filename of the manifest that called us
    f = source.file

    # Create a Puppet file parser object
    parser = Puppet::Parser::Parser.new(Puppet::Node::Environment.new(Puppet[:environment]))
    parser.file = f
    ast = parser.parse

    # Now, for the manifest walk through it line by line and parse it.
    # (taken from Puppet::Util::RDoc.output)
    astobj = []
    ast.instantiate('').each do |resource_type|
    astobj << resource_type if resource_type.file == f
    end

    # Now, grab the @doc object and return it
    # TODO: Implement some clean parsing to remove the puppet doc formatting
    return astobj[0].doc
    end
    end
    Its not perfect ... there are two big issues.

    1. We manually walk through the puppet doc parsing process because the Puppet::Utils::RDoc class *always* outputs to console or file, rather than giving you an ability to just return the string.
    2. I probably want to run the doc object through a parser to strip out the various control characters.

    Other than that, it allows us to add docs to our node MOTD files like this:

    motd::add { $name: content => my_puppet_doc() }

    --Matt
    On Feb 22, 2013, at 10:15 AM, Ken Barber wrote:

    I do this kind of thing here:

    https://github.com/puppetlabs/puppetlabs-kwalify/blob/master/lib/puppet/parser/functions/validate_resource.rb#L24

    ken.
    On Fri, Feb 22, 2013 at 6:05 PM, Matt W wrote:
    I'm trying to create a function that I can call in a manifest like this:

    $doc = doc('apache::service')

    In the function, I want it to actually find the filename for the
    'apache::service' class... from there, I'm going to run some code that will
    generate the puppet-doc in text format, and then return it in a variable.
    Ultimately this is so that I can have nodes self-document themselves.

    Right now I'm stuck on figuring out the filename from the classname though.
    Anyone have any tips on this? Is there a function in the Puppet ruby code
    somewhere that will translate a class-name into a file path?

    --
    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?hl=en.
    For more options, visit https://groups.google.com/groups/opt_out.
    --
    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?hl=en.
    For more options, visit https://groups.google.com/groups/opt_out.
    --
    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?hl=en.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Matt W at Feb 23, 2013 at 4:33 pm
    Ok I've run into one more issue I could use help with on this code. It
    works fine when I'm running Puppet through Unicorn.. but when I run puppet
    locally on our puppet servers via 'puppet apply', it fails with an import
    error:

    Error: Import loop detected at
    /mnt/puppet/master/puppet-base/modules/nodes/node_puppet/manifests/test.pp:15
    on node test-puppet-uswest2
    Wrapped exception:
    Import loop detected
    Error: Import loop detected at
    /mnt/puppet/master/puppet-base/modules/nodes/node_puppet/manifests/test.pp:15
    on node test-puppet-uswest2

    The line that is erroring out is this:
    parser =
    Puppet::Parser::Parser.new(Puppet::Node::Environment.new(Puppet[:environment]))

    I've confirmed that its really the Puppet::Parser::Parser.new() function
    thats causing the issue. So obviously that probably means there is already
    a parser object somewhere... and that it does not like to be created
    multiple times. The question I've got is how can I change this line to
    reference an already existing Parser object?
    On Friday, February 22, 2013 12:26:59 PM UTC-8, Matt W wrote:

    Thanks... I ended up with this:
    #
    # my_puppet_doc.rb
    #

    require 'puppet/util/rdoc'

    module Puppet::Parser::Functions
    newfunction(:my_puppet_doc, :type => :rvalue, :doc => <<-EOS

    This function returns the 'puppet doc' header from the module
    that called it. Usage:

    $doc = my_puppet_doc()

    EOS
    ) do |args|
    # Get the filename of the manifest that called us
    f = source.file

    # Create a Puppet file parser object
    parser =
    Puppet::Parser::Parser.new(Puppet::Node::Environment.new(Puppet[:environment]))
    parser.file = f
    ast = parser.parse

    # Now, for the manifest walk through it line by line and parse it.
    # (taken from Puppet::Util::RDoc.output)
    astobj = []
    ast.instantiate('').each do |resource_type|
    astobj << resource_type if resource_type.file == f
    end

    # Now, grab the @doc object and return it
    # TODO: Implement some clean parsing to remove the puppet doc
    formatting
    return astobj[0].doc
    end
    end
    Its not perfect ... there are two big issues.

    1. We manually walk through the puppet doc parsing process because the
    Puppet::Utils::RDoc class *always* outputs to console or file, rather than
    giving you an ability to just return the string.
    2. I probably want to run the doc object through a parser to strip out the
    various control characters.

    Other than that, it allows us to add docs to our node MOTD files like
    this:

    motd::add { $name: content => my_puppet_doc() }

    --Matt
    On Feb 22, 2013, at 10:15 AM, Ken Barber wrote:

    I do this kind of thing here:

    https://github.com/puppetlabs/puppetlabs-kwalify/blob/master/lib/puppet/parser/functions/validate_resource.rb#L24
    ken.
    On Fri, Feb 22, 2013 at 6:05 PM, Matt W wrote:
    I'm trying to create a function that I can call in a manifest like
    this:
    $doc = doc('apache::service')

    In the function, I want it to actually find the filename for the
    'apache::service' class... from there, I'm going to run some code that
    will
    generate the puppet-doc in text format, and then return it in a
    variable.
    Ultimately this is so that I can have nodes self-document themselves.

    Right now I'm stuck on figuring out the filename from the classname
    though.
    Anyone have any tips on this? Is there a function in the Puppet ruby
    code
    somewhere that will translate a class-name into a file path?

    --
    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?hl=en.
    For more options, visit https://groups.google.com/groups/opt_out.
    --
    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?hl=en.
    For more options, visit https://groups.google.com/groups/opt_out.
    --
    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?hl=en.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Eric Sorenson at Feb 22, 2013 at 6:24 pm
    Jordan Sissel wrote up a little thing to do this:
    https://github.com/jordansissel/puppet-examples/tree/master/where-art-thou/

    -=Eric
    On Friday, February 22, 2013 10:05:18 AM UTC-8, Matt W wrote:

    I'm trying to create a function that I can call in a manifest like this:

    $doc = doc('apache::service')

    In the function, I want it to actually find the filename for the
    'apache::service' class... from there, I'm going to run some code that will
    generate the puppet-doc in text format, and then return it in a variable.
    Ultimately this is so that I can have nodes self-document themselves.

    Right now I'm stuck on figuring out the filename from the classname
    though. Anyone have any tips on this? Is there a function in the Puppet
    ruby code somewhere that will translate a class-name into a file path?
    --
    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?hl=en.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Matt Wise at Feb 22, 2013 at 6:36 pm
    Yeah, this is interesting... it will essentially report where the template file came from. It doesn't get me the path name to the manifest that called it, which is what i'm looking for.
    On Feb 22, 2013, at 10:24 AM, Eric Sorenson wrote:

    Jordan Sissel wrote up a little thing to do this: https://github.com/jordansissel/puppet-examples/tree/master/where-art-thou/

    -=Eric

    On Friday, February 22, 2013 10:05:18 AM UTC-8, Matt W wrote:
    I'm trying to create a function that I can call in a manifest like this:

    $doc = doc('apache::service')

    In the function, I want it to actually find the filename for the 'apache::service' class... from there, I'm going to run some code that will generate the puppet-doc in text format, and then return it in a variable. Ultimately this is so that I can have nodes self-document themselves.

    Right now I'm stuck on figuring out the filename from the classname though. Anyone have any tips on this? Is there a function in the Puppet ruby code somewhere that will translate a class-name into a file path?

    --
    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?hl=en.
    For more options, visit https://groups.google.com/groups/opt_out.
    --
    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?hl=en.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Matt Wise at Feb 22, 2013 at 6:43 pm
    Another way this could work, by the way, is if I can somehow reference the "calling class filename". Ie, if

    /foo/class.pp:

    class foo {
    document_me()
    }

    If I can somehow reference "/foo/class.pp:2", like some of the error log messages do, that would work as well.

    --Matt
    On Feb 22, 2013, at 10:35 AM, Matt Wise wrote:

    Yeah, this is interesting... it will essentially report where the template file came from. It doesn't get me the path name to the manifest that called it, which is what i'm looking for.
    On Feb 22, 2013, at 10:24 AM, Eric Sorenson wrote:

    Jordan Sissel wrote up a little thing to do this: https://github.com/jordansissel/puppet-examples/tree/master/where-art-thou/

    -=Eric

    On Friday, February 22, 2013 10:05:18 AM UTC-8, Matt W wrote:
    I'm trying to create a function that I can call in a manifest like this:

    $doc = doc('apache::service')

    In the function, I want it to actually find the filename for the 'apache::service' class... from there, I'm going to run some code that will generate the puppet-doc in text format, and then return it in a variable. Ultimately this is so that I can have nodes self-document themselves.

    Right now I'm stuck on figuring out the filename from the classname though. Anyone have any tips on this? Is there a function in the Puppet ruby code somewhere that will translate a class-name into a file path?

    --
    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?hl=en.
    For more options, visit https://groups.google.com/groups/opt_out.

    --
    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?hl=en.
    For more options, visit https://groups.google.com/groups/opt_out.
    --
    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?hl=en.
    For more options, visit https://groups.google.com/groups/opt_out.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppuppet-users @
categoriespuppet
postedFeb 22, '13 at 6:12p
activeFeb 23, '13 at 4:33p
posts7
users3
websitepuppetlabs.com

People

Translate

site design / logo © 2022 Grokbase