| 1 | # | ||
| 2 | # (c) Nathan Abu <aloha2004@gmail.com> | ||
| 3 | # | ||
| 4 | |||
| 5 | =head1 NAME | ||
| 6 | |||
| 7 | Rex::Group::Lookup::XML - read hostnames and groups from a XML file | ||
| 8 | |||
| 9 | =head1 DESCRIPTION | ||
| 10 | |||
| 11 | With this module you can define hostgroups out of an xml file. | ||
| 12 | |||
| 13 | =head1 SYNOPSIS | ||
| 14 | |||
| 15 | use Rex::Group::Lookup::XML; | ||
| 16 | groups_xml "file.xml"; | ||
| 17 | |||
| 18 | |||
| 19 | =head1 EXPORTED FUNCTIONS | ||
| 20 | |||
| 21 | =cut | ||
| 22 | |||
| 23 | package Rex::Group::Lookup::XML; | ||
| 24 | |||
| 25 | use v5.14.4; | ||
| 26 | use warnings; | ||
| 27 | use Rex -base; | ||
| 28 | |||
| 29 | our $VERSION = '1.16.1'; # VERSION | ||
| 30 | |||
| 31 | require Exporter; | ||
| 32 | use base qw(Exporter); | ||
| 33 | use vars qw(@EXPORT); | ||
| 34 | XML::LibXML->require; | ||
| 35 | |||
| 36 | @EXPORT = qw(groups_xml); | ||
| 37 | |||
| 38 | =head2 groups_xml($file) | ||
| 39 | |||
| 40 | With this function you can read groups from xml files. | ||
| 41 | |||
| 42 | File example: | ||
| 43 | |||
| 44 | <configuration> | ||
| 45 | <group name="database"> | ||
| 46 | <server name="machine01" user="root" password="foob4r" sudo="true" hdd="300" loc="/opt" /> | ||
| 47 | </group> | ||
| 48 | <group name="application"> | ||
| 49 | <server name="machine01" user="root" password="foob4r" sudo="true" hdd="50" loc="/export" /> | ||
| 50 | <server name="machine02" user="root" password="foob5r" sudo="true"/> | ||
| 51 | </group> | ||
| 52 | <group name="profiler"> | ||
| 53 | <server name="machine03" user="root" password="blue123"/> | ||
| 54 | </group> | ||
| 55 | </configuration> | ||
| 56 | |||
| 57 | The XML file is validated against the DTD schema stored in C<Rex::Group::Lookup::XML::$schema_file> as string. | ||
| 58 | |||
| 59 | =cut | ||
| 60 | |||
| 61 | =head2 $schema_file | ||
| 62 | |||
| 63 | A variable that contains the XSD schema for which the XML is validated against. | ||
| 64 | |||
| 65 | =cut | ||
| 66 | |||
| 67 | our $schema_file = <<"XSD"; | ||
| 68 | <xsd:schema attributeFormDefault="unqualified" elementFormDefault="qualified" version="1.0" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> | ||
| 69 | |||
| 70 | <xsd:element name="configuration"> | ||
| 71 | <xsd:complexType> | ||
| 72 | <xsd:sequence> | ||
| 73 | |||
| 74 | <xsd:element minOccurs="1" maxOccurs="unbounded" name="group"> | ||
| 75 | <xsd:complexType> | ||
| 76 | <xsd:sequence> | ||
| 77 | |||
| 78 | <xsd:element name="server" minOccurs="1" maxOccurs="unbounded"> | ||
| 79 | <xsd:complexType> | ||
| 80 | <xsd:attribute name="name" use="required" type="xsd:string" /> | ||
| 81 | <xsd:anyAttribute processContents="lax"/> | ||
| 82 | </xsd:complexType> | ||
| 83 | </xsd:element> | ||
| 84 | |||
| 85 | </xsd:sequence> | ||
| 86 | <xsd:attribute use="required" name="name" type="xsd:string" /> | ||
| 87 | </xsd:complexType> | ||
| 88 | </xsd:element> | ||
| 89 | |||
| 90 | </xsd:sequence> | ||
| 91 | </xsd:complexType> | ||
| 92 | </xsd:element> | ||
| 93 | |||
| 94 | </xsd:schema> | ||
| 95 | XSD | ||
| 96 | |||
| 97 | sub xml_validate { | ||
| 98 | my $xmldoc = shift; | ||
| 99 | my $schema = XML::LibXML::Schema->new( string => $schema_file ); | ||
| 100 | |||
| 101 | eval { $schema->validate($xmldoc); 1 } | ||
| 102 | or die "Could not validate XML file against the XSD schema: $@"; | ||
| 103 | } | ||
| 104 | |||
| 105 | sub groups_xml { | ||
| 106 | my $file = shift; | ||
| 107 | my $parser = XML::LibXML->new(); | ||
| 108 | my $xmldoc = $parser->parse_file($file); | ||
| 109 | my %groups; | ||
| 110 | |||
| 111 | xml_validate($xmldoc); | ||
| 112 | |||
| 113 | foreach my $server_node ( $xmldoc->findnodes('/configuration/group/server') ) | ||
| 114 | { | ||
| 115 | my ($group) = | ||
| 116 | map { $_->getValue() } | ||
| 117 | grep { $_->nodeName eq 'name' } $server_node->parentNode->attributes(); | ||
| 118 | my %atts = | ||
| 119 | map { $_->nodeName => $_->getValue() } $server_node->attributes(); | ||
| 120 | |||
| 121 | push( | ||
| 122 | @{ $groups{$group} }, | ||
| 123 | Rex::Group::Entry::Server->new( name => delete( $atts{name} ), %atts ) | ||
| 124 | ); | ||
| 125 | } | ||
| 126 | group( $_ => @{ $groups{$_} } ) foreach ( keys(%groups) ); | ||
| 127 | } | ||
| 128 | |||
| 129 | 1; |