0

I need to edit some XML files, I'm OK with removing and setting, but I don't see if and how is it possible to sort XML file using only Augeas?

Has anyone done it, I am trying to avoid other resources than Augeas for now?

I am using puppet, ruby, shell scripts. I can use augeas in puppet, not only augtool.

This is my exact task: sort elements on element name, attributes name, table and column

I have a large XML file which actually contains a lot of tables, this is example of one table:

<table name="validation_rule" numRows="6" remarks="" schema="public" type="TABLE">
  <column autoUpdated="false" digits="0" id="0" name="id" nullable="false" remarks="" size="10" type="int4">
    <child column="validation_rule_id" foreignKey="meta_field_name_validation_rule_id_fkey" implied="false" onDeleteCascade="false" table="meta_field_name"/>
    <child column="validation_rule_id" foreignKey="preference_type_validation_rule_id_fkey" implied="false" onDeleteCascade="false" table="preference_type"/>
    <child column="validation_rule_id" foreignKey="validation_rule_attributes_validation_rule_id_fkey" implied="false" onDeleteCascade="false" table="validation_rule_attributes"/>
  </column>
  <column autoUpdated="false" digits="0" id="1" name="rule_type" nullable="false" remarks="" size="25" type="varchar"/>
  <column autoUpdated="false" digits="0" id="2" name="enabled" nullable="true" remarks="" size="1" type="bool"/>
  <column autoUpdated="false" digits="0" id="3" name="optlock" nullable="false" remarks="" size="10" type="int4"/>
  <primaryKey column="id" sequenceNumberInPK="1"/>
  <index name="validation_rule_pkey" unique="true">
    <column ascending="true" name="id"/>
  </index>
</table>

This is how I delete from same sample file:

augtool> set /augeas/load/xml/lens "Xml.lns" 
augtool> set /augeas/load/xml/incl /home/ESSENT/i.camilovic/test/test.xml
augtool> load
augtool> rm /files/home/ESSENT/i.camilovic/test/test.xml/TopLevel/FooBar
augtool> save

Here is a code sample in puppet that we use for something else:

    augeas { "${name} ReverseBuild Threshold fails":
      lens    => 'Xml.lns',
      incl    => $config_file,
      context => "${context}/triggers/jenkins.triggers.ReverseBuildTrigger/threshold",
      changes => [
        "set name/#text 'FAILURE'",
        "set ordinal/#text '2'",
        "set color/#text 'RED'",
        "set completeBuild/#text 'true'",
      ],
      notify  => Exec['reload-configuration'],
      require => Augeas["${name} Upstream Projects"],
    }

2 Answers2

0

This cannot be achieved with Augeas alone (i.e. augtool), but it could be achieved using the Augeas library together with language bindings. In which context are you using Augeas?

raphink
  • 3,625
  • 1
  • 28
  • 39
  • We have environments on AWS with AmazonLinux2, we use puppet, ruby, shell scripts...we try to avoid python although we have it installed.I need to create Jenkins job which will do this job. This job will use existing files which are generated using schemaspy connection to database, modify them, and push them to some git repository. – Igor Igorito Nov 27 '18 at 14:16
  • If using Puppet for that, can you add to the question that it's about Puppet and provide an example of Puppet syntax you're thinking of using please? – raphink Nov 27 '18 at 15:17
  • I have added syntax for deleting, I do not know how to sort, that is why I am asking. If not with augeas, then I would have to try with ruby – Igor Igorito Nov 28 '18 at 08:27
  • That was not my question though: what Puppet syntax do you expect to be using to achieve sorting? – raphink Nov 28 '18 at 15:10
  • I have added it to post. I have found some ruby gem called chilkat, initial test works, I will see... – Igor Igorito Nov 29 '18 at 10:06
  • Unfortunately, chilkat and nokogiri ruby gems can not read attributes, they can only sort by tag itself, or value, and I have no values and need to sort by attributes. Seems like I will have to use python, I made inital tests and it works: https://docs.python.org/2/library/xml.etree.elementtree.html – Igor Igorito Nov 29 '18 at 11:59
0

Instead of using Augeas to achieve that task in Puppet, I would recommend using an exec, e.g.:

exec { "Sort test.xml":
  path        => $::path,
  command     => 'xmllint -c14n -o /path/to/test.xml /path/to/test.xml',
  refreshonly => true,
}
raphink
  • 3,625
  • 1
  • 28
  • 39