10
$Getopt::Std::STANDARD_HELP_VERSION++;
11
my $LOGPREF = '[hosts2xml]';
17
hosts2xml [-i <fn>] [-o <fn>]
23
--version show version information
31
apt-dater - terminal-based remote package update manager
34
Thomas Liske <liske\@ibh.de>
37
2008-2015 (C) IBH IT-Service GmbH [https://www.ibh.de/apt-dater/]
39
This program is free software; you can redistribute it and/or modify
40
it under the terms of the GNU General Public License as published by
41
the Free Software Foundation; either version 2 of the License, or
42
(at your option) any later version.
48
our $opt_i = glob(q(~/.config/apt-dater/hosts.conf));
49
our $opt_o = glob(q(~/.config/apt-dater/hosts.xml));
50
unless(getopts('i:o:')) {
55
my $fhin = IO::File->new("< $opt_i");
56
die "$LOGPREF Failed to open '$opt_i': $!\n"
65
$xparams{OUTPUT} = IO::File->new(">$opt_o");
66
die "$LOGPREF Failed to open '$opt_o': $!\n"
67
unless($xparams{OUTPUT});
70
my $xml = XML::Writer->new(%xparams);
71
$xml->doctype('hosts', undef, '@XMLSCHEMAURI@/hosts.dtd');
73
Hosts file of apt-dater (parsed by libxml2)
74
===========================================
76
hosts.xml configures the hosts which are managed by
77
apt-dater. Host options (except 'name') are lookuped as attributes
78
at the host node itself, the parent group node and the global
81
The following attributes are known:
82
- name : visible name of the host or group (required)
83
- comment : text shown in 'host details' screen
84
- type : transport type (default: 'generic-ssh')
85
- ssh-user: overwrite SSH username
86
- ssh-host: overwrite SSH host (defaults to \@name)
87
- ssh-port: overwrite SSH port
88
- ssh-id : overwrite SSH identification file
93
<default ssh-user=\"admin\"/>
95
<group name=\"Internal Hosts\" ssh-user=\"root\">
96
<host name=\"server1.internal\"/>
97
<host name=\"server2.internal\"/>
98
<host name=\"John's Machine\" ssh-host=\"workstation.internal\" />
101
<group name=\"External Hosts\">
102
<host name=\"external.ibh.net\" ssh-port=\"443\"/>
109
$xml->startTag('hosts', 'xmlns:xi' => q(http://www.w3.org/2001/XInclude));
111
$xml->comment('Include global config file if available.');
112
$xml->startTag('xi:include', href => q(file:///etc/apt-dater/hosts.xml), xpointer => q(xpointer(/hosts/*)));
113
$xml->emptyTag('xi:fallback');
114
$xml->endTag('xi:include');
121
s/(^\s+|\s+$|#.*$)//g;
124
if(/^\[([^\]]+)\]/) {
126
push(@sections, $csect);
130
if(/^(Hosts)=(.+)/i) {
131
$sections{$csect}->{lc($1)} = $2;
135
warn("$LOGPREF Garbage line: $_\n");
139
foreach my $csect (sort @sections) {
140
my @group = ('group', name => $csect);
142
if($sections{$csect}->{type}) {
143
push(@group, type => $sections{$csect}->{type});
146
$xml->startTag(@group);
147
foreach my $host (sort split(/;/, $sections{$csect}->{hosts})) {
149
$sport = $1 if($host =~ s/:(\d+)//);
152
$suser = $1 if($host =~ s/(.+)@//);
154
my @host = ('host', name => $host);
155
push(@host, 'ssh-user' => $suser)
157
push(@host, 'ssh-port' => $sport)
160
$xml->emptyTag(@host);
162
$xml->endTag('group');
165
$xml->endTag('hosts');
167
$xparams{OUTPUT}->close()
168
if($xparams{OUTPUT});
170
print STDERR "$LOGPREF '$opt_i' has been converted to '$opt_o'!\n";