~ubuntu-branches/debian/sid/apt-dater/sid

« back to all changes in this revision

Viewing changes to lib/hosts2xml.in

  • Committer: Package Import Robot
  • Author(s): Patrick Matthäi
  • Date: 2015-07-07 17:33:08 UTC
  • mfrom: (1.2.5)
  • Revision ID: package-import@ubuntu.com-20150707173308-7rkjhxnlq7gsykif
Tags: 1.0.2-1
* New upstream release.
  - Set the default of the opt-cmd-flags host option to "-t" since this is
    essential.
    Closes: #776392
  - Add new build dependency vim-common.
* Replace screen dependency with tmux.
* Create apt-dater group for session sharing.
* Add missing directories.
* Make the build reproducible.
  Closes: #789648
* Drop automake and autoconf build dependencies.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/perl
 
2
 
 
3
use warnings;
 
4
use strict;
 
5
 
 
6
use Getopt::Std;
 
7
use Glib;
 
8
use XML::Writer;
 
9
 
 
10
$Getopt::Std::STANDARD_HELP_VERSION++;
 
11
my $LOGPREF = '[hosts2xml]';
 
12
 
 
13
sub HELP_MESSAGE {
 
14
    print <<USG;
 
15
Usage:
 
16
 
 
17
  hosts2xml [-i <fn>] [-o <fn>]
 
18
 
 
19
    -i          input filename
 
20
    -o          output filename
 
21
 
 
22
    --help      show this help
 
23
    --version   show version information
 
24
 
 
25
USG
 
26
}
 
27
 
 
28
sub VERSION_MESSAGE {
 
29
    print <<LIC;
 
30
 
 
31
apt-dater - terminal-based remote package update manager
 
32
 
 
33
Authors:
 
34
  Thomas Liske <liske\@ibh.de>
 
35
 
 
36
Copyright Holder:
 
37
  2008-2015 (C) IBH IT-Service GmbH [https://www.ibh.de/apt-dater/]
 
38
 
 
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.
 
43
 
 
44
LIC
 
45
#/
 
46
}
 
47
 
 
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:')) {
 
51
    HELP_MESSAGE;
 
52
    exit 1;
 
53
}
 
54
 
 
55
my $fhin = IO::File->new("< $opt_i");
 
56
die "$LOGPREF Failed to open '$opt_i': $!\n"
 
57
    unless($fhin);
 
58
 
 
59
my %xparams = (
 
60
    DATA_MODE => 1,
 
61
    DATA_INDENT => 4,
 
62
    );
 
63
 
 
64
if($opt_o) {
 
65
    $xparams{OUTPUT} = IO::File->new(">$opt_o");
 
66
    die "$LOGPREF Failed to open '$opt_o': $!\n"
 
67
        unless($xparams{OUTPUT});
 
68
}
 
69
 
 
70
my $xml = XML::Writer->new(%xparams);
 
71
$xml->doctype('hosts', undef, '@XMLSCHEMAURI@/hosts.dtd');
 
72
$xml->comment("
 
73
    Hosts file of apt-dater (parsed by libxml2)
 
74
    ===========================================
 
75
 
 
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
 
79
    /hosts/default node.
 
80
 
 
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
 
89
 
 
90
    Example:
 
91
 
 
92
    <hosts>
 
93
      <default ssh-user=\"admin\"/>
 
94
 
 
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\" />
 
99
      </group>
 
100
 
 
101
      <group name=\"External Hosts\">
 
102
        <host name=\"external.ibh.net\" ssh-port=\"443\"/>
 
103
      </group>
 
104
 
 
105
      ...
 
106
 
 
107
    </hosts>
 
108
");
 
109
$xml->startTag('hosts', 'xmlns:xi' => q(http://www.w3.org/2001/XInclude));
 
110
 
 
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');
 
115
 
 
116
my %sections;
 
117
my @sections;
 
118
my $csect;
 
119
while(<$fhin>) {
 
120
    chomp;
 
121
    s/(^\s+|\s+$|#.*$)//g;
 
122
    next unless($_);
 
123
 
 
124
    if(/^\[([^\]]+)\]/) {
 
125
        $csect = $1;
 
126
        push(@sections, $csect);
 
127
        next;
 
128
    }
 
129
 
 
130
    if(/^(Hosts)=(.+)/i) {
 
131
        $sections{$csect}->{lc($1)} = $2;
 
132
        next;
 
133
    }
 
134
 
 
135
    warn("$LOGPREF Garbage line: $_\n");
 
136
}
 
137
$fhin->close;
 
138
 
 
139
foreach my $csect (sort @sections) {
 
140
    my @group = ('group', name => $csect);
 
141
 
 
142
    if($sections{$csect}->{type}) {
 
143
        push(@group, type => $sections{$csect}->{type});
 
144
    }
 
145
 
 
146
    $xml->startTag(@group);
 
147
    foreach my $host (sort split(/;/, $sections{$csect}->{hosts})) {
 
148
        my $sport;
 
149
        $sport = $1 if($host =~ s/:(\d+)//);
 
150
        
 
151
        my $suser;
 
152
        $suser = $1 if($host =~ s/(.+)@//);
 
153
 
 
154
        my @host = ('host', name => $host);
 
155
        push(@host, 'ssh-user' => $suser)
 
156
            if($suser);
 
157
        push(@host, 'ssh-port' => $sport)
 
158
            if($sport);
 
159
 
 
160
        $xml->emptyTag(@host);
 
161
    }
 
162
    $xml->endTag('group');
 
163
}
 
164
 
 
165
$xml->endTag('hosts');
 
166
$xml->end();
 
167
$xparams{OUTPUT}->close()
 
168
    if($xparams{OUTPUT});
 
169
 
 
170
print STDERR "$LOGPREF '$opt_i' has been converted to '$opt_o'!\n";