Skip to content

Instantly share code, notes, and snippets.

@jordansissel
Created October 30, 2010 10:06

Revisions

  1. jordansissel revised this gist Oct 30, 2010. 2 changed files with 22 additions and 3 deletions.
    17 changes: 16 additions & 1 deletion puppet-parser-elsewhere.rb
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,3 @@

    #!/usr/bin/env ruby
    #

    @@ -27,6 +26,22 @@
    [ "/var/log/apache2/access.log" ]:
    tags => ["apache-access"];
    }
    # even custom defines work.
    define fancypants() {
    input {
    "/tmp/$name":
    tags => ["fancypants"];
    "/tmp/$name.1":
    tags => ["fancypants"];
    "/tmp/$name.2":
    tags => ["fancypants"];
    }
    }
    fancypants {
    "hello": ;
    }
    EOF

    # Now compile a catalog from our code.
    8 changes: 6 additions & 2 deletions running it
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,8 @@
    % ruby puppet-parser-elsewhere.rb
    Found resource 'Input[/var/log/apache2/access.log]' with tags: "apache-access"
    Found resource 'Input[/var/log/messages]' with tags: "linux-syslog"
    Found resource 'Input[/tmp/hello.2]' with tags: "fancypants"
    Found resource 'Input[/var/log/auth.log]' with tags: "linux-syslog"
    Found resource 'Input[/tmp/hello.1]' with tags: "fancypants"
    Found resource 'Input[/tmp/hello]' with tags: "fancypants"
    Found resource 'Fancypants[hello]' with tags: nil
    Found resource 'Input[/var/log/apache2/access.log]' with tags: "apache-access"
    Found resource 'Input[/var/log/messages]' with tags: "linux-syslog"
  2. jordansissel created this gist Oct 30, 2010.
    41 changes: 41 additions & 0 deletions puppet-parser-elsewhere.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,41 @@

    #!/usr/bin/env ruby
    #

    require "rubygems"
    require "puppet" # gem puppet
    require "ap" # gem awesome_print

    # Need to define a type for each type we want to have.
    # Otherwise, unknown types are errors in puppet.
    Puppet::Type.newtype(:input) do
    newproperty(:name) do
    desc "Input URL"
    end # property :name

    newproperty(:tags) do
    desc "Tags for an input"
    end # property 'tags'
    end # type 'input'

    # This is where we put the puppet manifest code. You could just use a file
    # here, but I'm inlining it for easy reading.
    Puppet[:code] = <<EOF
    input {
    [ "/var/log/messages", "/var/log/auth.log" ]:
    tags => ["linux-syslog"];
    [ "/var/log/apache2/access.log" ]:
    tags => ["apache-access"];
    }
    EOF

    # Now compile a catalog from our code.
    node = Puppet::Node.find("default")
    catalog = Puppet::Resource::Catalog.find("default", :use_node => node)

    # Each vertex is a resource.
    catalog.vertices.each do |resource|
    next if ["Class", "Stage"].include?(resource.type )
    puts "Found resource '#{resource.to_s}' with tags: #{resource[:tags].inspect}"

    end
    4 changes: 4 additions & 0 deletions running it
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,4 @@
    % ruby puppet-parser-elsewhere.rb
    Found resource 'Input[/var/log/apache2/access.log]' with tags: "apache-access"
    Found resource 'Input[/var/log/messages]' with tags: "linux-syslog"
    Found resource 'Input[/var/log/auth.log]' with tags: "linux-syslog"