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

« back to all changes in this revision

Viewing changes to clients/rug/apt-dater-host

  • Committer: Package Import Robot
  • Author(s): Patrick Matthäi
  • Date: 2014-10-29 17:40:07 UTC
  • mfrom: (1.2.1) (33.1.5 experimental)
  • Revision ID: package-import@ubuntu.com-20141029174007-hlqaj5gfr40jincg
Tags: 1.0.0-1
* New upstream release.
* Uploading to unstable.
* Remove po/stamp-po in debian/clean to allow building twice in a row.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/usr/bin/perl
2
 
 
3
 
# apt-dater - terminal-based remote package update manager
4
 
#
5
 
# Authors:
6
 
#   Andre Ellguth <ellguth@ibh.de>
7
 
#   Thomas Liske <liske@ibh.de>
8
 
#
9
 
# Copyright Holder:
10
 
#   2008-2012 (C) IBH IT-Service GmbH [http://www.ibh.de/apt-dater/]
11
 
#
12
 
# License:
13
 
#   This program is free software; you can redistribute it and/or modify
14
 
#   it under the terms of the GNU General Public License as published by
15
 
#   the Free Software Foundation; either version 2 of the License, or
16
 
#   (at your option) any later version.
17
 
#
18
 
#   This program is distributed in the hope that it will be useful,
19
 
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
20
 
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21
 
#   GNU General Public License for more details.
22
 
#
23
 
#   You should have received a copy of the GNU General Public License
24
 
#   along with this package; if not, write to the Free Software
25
 
#   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
26
 
#
27
 
 
28
 
use strict;
29
 
use warnings;
30
 
 
31
 
my $CFGFILE = '/etc/apt-dater-host.conf';
32
 
my $GETROOT = 'sudo';
33
 
my $FORBID_UPGRADE = 0;
34
 
my $FORBID_INSTALL = 0;
35
 
my $UUIDFILE = '/etc/apt-dater-host.uuid';
36
 
my @CLUSTERS;
37
 
 
38
 
my $CMD = shift;
39
 
my $ADPROTO = '0.6';
40
 
 
41
 
$ENV{'LC_ALL'} = 'C';
42
 
 
43
 
if (-r $CFGFILE) {
44
 
    eval `cat "$CFGFILE"` ;
45
 
 
46
 
    if($@ ne '') {
47
 
        print "ADPROTO: $ADPROTO\n";
48
 
        print "ADPERR: Invalid config $CFGFILE: $@\n";
49
 
        exit;
50
 
    }
51
 
}
52
 
 
53
 
$GETROOT = '' if($> == 0);
54
 
 
55
 
die "Don't call this script directly!\n" unless (defined($CMD));
56
 
if($CMD eq 'sshkey') {
57
 
    die "Sorry, no shell access allowed!\n"
58
 
      unless(defined($ENV{'SSH_ORIGINAL_COMMAND'}));
59
 
 
60
 
    @ARGV = split(' ', $ENV{'SSH_ORIGINAL_COMMAND'});
61
 
 
62
 
    shift;
63
 
    $CMD = shift;
64
 
}
65
 
die "Invalid command '$CMD'!\n" unless ($CMD=~/^(refresh|status|upgrade|install|kernel)$/);
66
 
 
67
 
if ($CMD eq 'refresh') {
68
 
    print "ADPROTO: $ADPROTO\n";
69
 
    &do_status;
70
 
    &do_kernel;
71
 
}
72
 
elsif ($CMD eq 'status') {
73
 
    print "ADPROTO: $ADPROTO\n";
74
 
    &do_status;
75
 
    &do_kernel;
76
 
}
77
 
elsif ($CMD eq 'upgrade') {
78
 
    if ($FORBID_UPGRADE) {
79
 
        print STDERR "\n\n** Sorry, apt-dater based upgrades on this host are disabled! **\n\n"
80
 
    }
81
 
    else {
82
 
        &do_upgrade;
83
 
    }
84
 
}
85
 
elsif ($CMD eq 'install') {
86
 
    if ($FORBID_INSTALL) {
87
 
        print STDERR "\n\n** Sorry, apt-dater based installations on this host are disabled! **\n\n"
88
 
    }
89
 
    else {
90
 
        &do_install(@ARGV);
91
 
    }
92
 
}
93
 
elsif ($CMD eq 'kernel') {
94
 
    print "ADPROTO: $ADPROTO\n";
95
 
    &do_kernel;
96
 
}
97
 
else {
98
 
    die "Internal error!\n";
99
 
}
100
 
 
101
 
sub get_virt() {
102
 
    return "Unknown" unless (-x '/usr/bin/imvirt');
103
 
 
104
 
    my $imvirt;
105
 
    chomp($imvirt = `/usr/bin/imvirt`);
106
 
 
107
 
    return $imvirt;
108
 
}
109
 
 
110
 
sub get_uname() {
111
 
    my $kernel;
112
 
    my $machine;
113
 
 
114
 
    chomp($kernel = `uname -s`);
115
 
    chomp($machine = `uname -m`);
116
 
    return "$kernel|$machine";
117
 
}
118
 
 
119
 
sub do_status() {
120
 
    # retrieve lsb informations
121
 
    unless(open(HLSB, "lsb_release -a 2> /dev/null |")) {
122
 
        print "\nADPERR: Failed to execute 'lsb_release -a' ($!).\n";
123
 
        exit(1);
124
 
    }
125
 
 
126
 
    my %lsb;
127
 
    while(<HLSB>) {
128
 
        chomp;
129
 
 
130
 
        $lsb{$1}=$2 if (/^(Distributor ID|Release|Codename):\s+(\S.*)$/);
131
 
    }
132
 
    close(HLSB);
133
 
    if($?) {
134
 
        print "\nADPERR: Error executing 'lsb_release -a' ($?).\n";
135
 
        exit(1);
136
 
    }
137
 
 
138
 
    print "LSBREL: $lsb{'Distributor ID'}|$lsb{'Release'}|$lsb{'Codename'}\n";
139
 
 
140
 
    # retrieve virtualization informations
141
 
    print "VIRT: ".&get_virt."\n";
142
 
 
143
 
    # retrieve uname informations
144
 
    print "UNAME: ".&get_uname."\n";
145
 
 
146
 
    # calculate forbid mask
147
 
    my $mask = 1;
148
 
#    $mask |= 1 if ($FORBID_REFRESH); NOT SUPPORTED
149
 
    $mask |= 2 if ($FORBID_UPGRADE);
150
 
    $mask |= 4 if ($FORBID_INSTALL);
151
 
    print "FORBID: $mask\n";
152
 
 
153
 
    # add installation UUID if available
154
 
    if(-r $UUIDFILE && -s $UUIDFILE) {
155
 
        print "UUID: ", `head -n 1 "$UUIDFILE"`;
156
 
    }
157
 
 
158
 
    # add cluster name if available
159
 
    foreach my $CLUSTER (@CLUSTERS) {
160
 
        print "CLUSTER: $CLUSTER\n";
161
 
    }
162
 
 
163
 
    # get packages which might be upgraded
164
 
    my %updates;
165
 
    unless(open(HAPT, "$GETROOT rug --terse list-updates |")) {
166
 
        print "\nADPERR: Failed to execute '$GETROOT rug --terse list-updates' ($!).\n";
167
 
        exit(1);
168
 
    };
169
 
    while(<HAPT>) {
170
 
        chomp;
171
 
 
172
 
        $updates{$1} = $2 if (/^.*\|.*\|.*\|([^|]+)\|([^|]+)\|/);
173
 
    }
174
 
    close(HAPT);
175
 
    if($?) {
176
 
        print "\nADPERR: Error executing '$GETROOT rug --terse list-updates' ($?).\n";
177
 
        exit(1);
178
 
    }
179
 
 
180
 
    # get version of installed packages
181
 
    my %installed;
182
 
    my %status;
183
 
    unless(open(HDPKG, "rpm -qa --qf '%{NAME}|%{VERSION}-%{RELEASE}|\n' |")) {
184
 
        print "\nADPERR: Failed to execute \"rpm -qa --qf '%{NAME}|%{VERSION}-%{RELEASE}|\\n'\" ($!).\n";
185
 
        exit(1);
186
 
    };
187
 
 
188
 
    while(<HDPKG>) {
189
 
        chomp;
190
 
 
191
 
        next unless (/^(\S+)\|\S+\|/);
192
 
 
193
 
        print "STATUS: ";
194
 
        print;
195
 
          
196
 
        if($updates{$1}) {
197
 
            print 'u=', $updates{$1};
198
 
        }
199
 
        else {
200
 
            print 'i';
201
 
        }
202
 
 
203
 
        print "\n";
204
 
    }
205
 
    close(HDPKG);
206
 
    if($?) {
207
 
        print "\nADPERR: Error executing \"rpm -qa --qf '%{NAME}|%{VERSION}-%{RELEASE}|\\n'\" ($?).\n";
208
 
        exit(1);
209
 
    };
210
 
 
211
 
}
212
 
 
213
 
sub do_upgrade() {
214
 
    system("$GETROOT rug update");
215
 
}
216
 
 
217
 
sub do_install() {
218
 
    if($GETROOT) {
219
 
        system($GETROOT, 'rug', 'install', @_);
220
 
    }
221
 
    else {
222
 
        system('rug', 'install', @_);
223
 
    }
224
 
}
225
 
 
226
 
sub do_kernel() {
227
 
    my $infostr = 'KERNELINFO:';
228
 
    my $version = `uname -r`;
229
 
    chomp($version);
230
 
 
231
 
    my $add = '';
232
 
    $add = $1 if($version =~ /(-[a-z]+)$/);
233
 
 
234
 
    my $pos = 0;
235
 
 
236
 
    my $kinstalled;
237
 
    my %distri;
238
 
    unless(open(HKERNEL, "rpm -q --whatprovides kernel --qf '%{NAME}|%{VERSION}-%{RELEASE}\n' |")) {
239
 
        print "$infostr 9 $version\n";
240
 
        return;
241
 
    }
242
 
    while(<HKERNEL>) {
243
 
        chomp;
244
 
 
245
 
        if(/^kernel$add\|(.+)/) {
246
 
            my $ver = $1;
247
 
            $distri{$ver.$add} = 1;
248
 
            
249
 
            if(!$kinstalled) {
250
 
                $kinstalled = $ver;
251
 
            }
252
 
            else {
253
 
                $kinstalled = $ver if(&versioncmp($kinstalled, $ver) < 0);
254
 
            }
255
 
        }
256
 
    }
257
 
    close(HKERNEL);
258
 
    if($?) {
259
 
        print "$infostr 9 $version\n";
260
 
        return;
261
 
    }
262
 
 
263
 
    unless($distri{$version}) {
264
 
        print "$infostr 2 $version\n";
265
 
        return;
266
 
    }
267
 
 
268
 
    unless($kinstalled.$add cmp $version) {
269
 
        print "$infostr 0 $version\n";
270
 
        return;
271
 
    }
272
 
    print "$infostr 1 $version\n";
273
 
}
274
 
 
275
 
##
276
 
# Taken from Sort::Versions 1.4
277
 
# Copyright (c) 1996, Kenneth J. Albanowski.
278
 
##
279
 
sub versioncmp() {
280
 
    my @A = ($_[0] =~ /([-.]|\d+|[^-.\d]+)/g);
281
 
    my @B = ($_[1] =~ /([-.]|\d+|[^-.\d]+)/g);
282
 
 
283
 
    my ($A, $B);
284
 
    while (@A and @B) {
285
 
        $A = shift @A;
286
 
        $B = shift @B;
287
 
        if ($A eq '-' and $B eq '-') {
288
 
            next;
289
 
        } elsif ( $A eq '-' ) {
290
 
            return -1;
291
 
        } elsif ( $B eq '-') {
292
 
            return 1;
293
 
        } elsif ($A eq '.' and $B eq '.') {
294
 
            next;
295
 
        } elsif ( $A eq '.' ) {
296
 
            return -1;
297
 
        } elsif ( $B eq '.' ) {
298
 
            return 1;
299
 
        } elsif ($A =~ /^\d+$/ and $B =~ /^\d+$/) {
300
 
            if ($A =~ /^0/ || $B =~ /^0/) {
301
 
                return $A cmp $B if $A cmp $B;
302
 
            } else {
303
 
                return $A <=> $B if $A <=> $B;
304
 
            }
305
 
        } else {
306
 
            $A = uc $A;
307
 
            $B = uc $B;
308
 
            return $A cmp $B if $A cmp $B;
309
 
        }       
310
 
    }
311
 
    @A <=> @B;
312
 
}