11 Oct 2010, 11:01
Tags: , , , , ,
2 comments

Puppet Tips&Tricks: Custom type to manipulate XML config (activemq.xml)

Last week I worked in my first ever Ruby code. It’s a custom type for Puppet that allows one to edit the /etc/activemq/activemq.xml file and add (or remove) transportConnectors. It’s a fairly simple thing, not very elaborate, but it’s a start. I’ll try to expand it to make editing other xml files possible too, because I think that shouldn’t be too hard. Now for the code. Keep in mind this is my first ever Ruby code (well, second, technically, because I worked out the REXML lib in a separate script first). It’s probably very ugly and inefficient, so if you can do better, do let me know!

xml_manipulator.rb type:

Puppet::Type.newtype(:xml_manipulator) do
    @doc = "Manage TransportConnectors for ActiveMQ"
 
    ensurable
 
    newparam(:uri) do
        desc "The uri on which the connector should listen"
    end
 
    newparam(:type) do
        desc "The connector type."
 
	isnamevar
    end
end

And the provider activemq_transportconnector.rb:

require 'rexml/document'
include REXML
 
Puppet::Type.type(:xml_manipulator).provide :activemq_transportconnector do
    desc "The actual ActiveMQ config manipulator"
 
    defaultfor :operatingsystem => :debian
 
    def create
        file = File.open("/etc/activemq/activemq.xml", "r+")
        doc = REXML::Document.new file
	connectors = doc.elements["//transportConnectors"]
	connectors.add_element 'transportConnector', {'name' => resource[:type], 'uri' => resource[:uri]}
	file.rewind()
	doc.write( file, 4 )
    end
 
    def destroy
        file = File.open("/etc/activemq/activemq.xml", "r+")
        doc = REXML::Document.new file
	doc.root.elements.delete("transportConnect[@name=" + resource[:type] + "]")
	file.rewind()
	doc.write( file, 4 )
    end
 
    def exists?
        file = File.open("/etc/activemq/activemq.xml", "r+")
        doc = REXML::Document.new file
        XPath.match( doc, "//amq:transportConnector[@name='" + resource[:type] + "' and @uri='" + resource[:uri] + "']", {"amq" => "http://activemq.apache.org/schema/core"}).length() > 0
    end
end

Tags: , , , , ,

 
  • Calender

    October 2010
    M T W T F S S
    « Sep   Nov »
     123
    45678910
    11121314151617
    18192021222324
    25262728293031
  • Twitter

    Powered by Twitter Tools

  • RSS Delicious feed

  • Archives