On May 19, 12:04 am, Marc Fournier <marc.fourn...@camptocamp.com> wrote: > Just wondering if you are planning to publish your work once it's > finished ? I've been doing this sort of thing using Exec's of > "xmlstarlet", but a pure puppet type is much smarter !
Allow me to publish it here. The code: * Assumes error-free operation; there is no validation, error checking, etc. * Unforgivingly expects the value of the attributes property to conform to the syntax "key=value,key=value,key=value" .. Obviously, the code will benefit from some T.L.C., but it functions in my use case as-is. BTW, I, too, was using xmlstarlet in shell scripts, whose essence are being migrated into Puppet wholesale. ---- 8< ---- Manifest snippet, for your delectation ---- 8< ---- configxml { '/opt/jira/atlassian-jira/WEB-INF/classes/ entityengine.xml': xpath => "/entity-config/ datasour...@name='defaultDS']", attributes => 'field-type-name=postgres72,schema- name=public', } configxml { '/opt/jira/conf/server.xml': xpath => "/Server/Service/Engine/Host/Context/ resour...@name='jdbc/JiraDS']", attributes => "username=$username,password= $password,driverClassName=org.postgresql.Driver,url=jdbc:postgresql:// $host/$database", } ---- 8< ---- /etc/puppet/modules/common/lib/puppet/type/configxml.rb ---- 8< ---- Puppet::Type.newtype(:configxml) do @doc = "Modify attributes of the first element matching the XPath expression in an XML file." newparam(:name) do desc "Full path of the XML document." end newparam(:xpath) do desc "XPath expression selecting target elements; the first of which whose attributes will be set." end newproperty(:attributes) do desc "An array of strings, each containing 'key=value' expressions setting element attributes." end end ---- 8< ---- /etc/puppet/modules/common/lib/puppet/provider/configxml/ configxml.rb ---- 8< ---- Puppet::Type.type(:configxml).provide(:configxml) do desc "TODO" require 'rexml/document' require 'set' def attributes doc = REXML::Document.new(File.open(resource[:name])) # Take the first element matching the provided XPath expression attrs = doc.elements[resource[:xpath] + "[1]"].attributes # Split up the comma-separated key-value pairs into an array and then isolate the keys keys = resource[:attributes].split(/,/).map { |s| s.split(/=/, 2) [0] } # Build up an array of attributes for the specified keys. Arrange keys in order of appearance # in resource[:attributes] so that Puppet can compare the desired and current values. keys.map { |k| "#{k}=#{attrs[k]}" }.join(',') end def attributes=(value) doc = REXML::Document.new(File.open(resource[:name])) # Take the first element matching the provided XPath expression attrs = doc.elements[resource[:xpath] + "[1]"].attributes # Split up the comma-separated key-value pairs and convert the resulting array to a hash updates = Hash[*resource[:attributes].split(/,/).map { |s| s.split(/ =/, 2) }.flatten] # Update this element's attributes updates.each { |k, v| attrs[k] = v } # Write the changes back doc.write(File.open(resource[:name], 'w')) end end -- You received this message because you are subscribed to the Google Groups "Puppet Users" group. To post to this group, send email to puppet-us...@googlegroups.com. To unsubscribe from this group, send email to puppet-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/puppet-users?hl=en.