File: apt-dater-host

package info (click to toggle)
apt-dater-host 1.0.1-3
  • links: PTS
  • area: main
  • in suites: bullseye, sid
  • size: 280 kB
  • sloc: perl: 1,202; sh: 151; makefile: 26
file content (399 lines) | stat: -rwxr-xr-x 9,629 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
#!/usr/bin/perl

# apt-dater - terminal-based remote package update manager
#
# Authors:
#   Andre Ellguth <ellguth@ibh.de>
#   Thomas Liske <liske@ibh.de>
#
# Copyright Holder:
#   2008-2014 (C) IBH IT-Service GmbH [http://www.ibh.de/apt-dater/]
#
# License:
#   This program is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; either version 2 of the License, or
#   (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this package; if not, write to the Free Software
#   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
#

use AptPkg::Config '$_config';
use AptPkg::System '$_system';
use AptPkg::Cache;
use ImVirt;
use strict;
use warnings;

my $CFGFILE = '/etc/apt-dater-host.conf';
my $DPKGTOOL = 'apt-get';
my $APTUPGRADE = 'dist-upgrade';
my $ASSUMEYES = 0;
my $GETROOT = 'sudo';
my $CLEANUP = 0;
my $FORBID_REFRESH = 0;
my $FORBID_UPGRADE = 0;
my $FORBID_INSTALL = 0;
my $UUIDFILE = '/etc/apt-dater-host.uuid';
my @CLUSTERS;
my $NEEDRESTART;
my $RUNNEEDREST;

my $CMD = shift;
my $ADPROTO = '0.7';

$ENV{'LC_ALL'} = 'C';

if (-r $CFGFILE) {
    eval `cat "$CFGFILE"` ;

    if($@ ne '') {
	print "ADPROTO: $ADPROTO\n";
	print "ADPERR: Invalid config $CFGFILE: $@\n";
	exit;
    }
}

$GETROOT = '' if($> == 0);

die "Don't call this script directly!\n" unless (defined($CMD));
if($CMD eq 'sshkey') {
    die "Sorry, no shell access allowed!\n"
      unless(defined($ENV{'SSH_ORIGINAL_COMMAND'}));

    @ARGV = split(' ', $ENV{'SSH_ORIGINAL_COMMAND'});

    shift;
    $CMD = shift;
}
die "Invalid command '$CMD'!\n" unless ($CMD=~/^(refresh|status|upgrade|install|kernel)$/);

if ($CMD eq 'refresh') {
    print "ADPROTO: $ADPROTO\n";
    if ($FORBID_REFRESH) {
	print STDERR "\n\n** Sorry, apt-dater based refreshs on this host are disabled! **\n\n"
    }
    else {
	&do_refresh;
    }
    &do_status;
    &do_kernel;
}
elsif ($CMD eq 'status') {
    print "ADPROTO: $ADPROTO\n";
    &do_status;
    &do_kernel;
}
elsif ($CMD eq 'upgrade') {
    if ($FORBID_UPGRADE) {
	print STDERR "\n\n** Sorry, apt-dater based upgrades on this host are disabled! **\n\n";
    }
    else {
	&do_upgrade;
	&do_cleanup;
    }
}
elsif ($CMD eq 'install') {
    if ($FORBID_INSTALL) {
	print STDERR "\n\n** Sorry, apt-dater based installations on this host are disabled! **\n\n";
    }
    else {
	&do_install(@ARGV);
	&do_cleanup;
    }
}
elsif ($CMD eq 'kernel') {
    print "ADPROTO: $ADPROTO\n";
    &do_kernel;
}
else {
    die "Internal error!\n";
}


sub do_refresh() {
    if(system(($GETROOT ? $GETROOT : ()), $DPKGTOOL, 'update')) {
	print "\nADPERR: Failed to execute '$GETROOT $DPKGTOOL update' ($?).\n";
	exit(1);
    }
}

sub get_virt() {
    return imv_get(IMV_PROB_DEFAULT);
}

sub get_uname() {
    my $kernel;
    my $machine;

    chomp($kernel = `uname -s`);
    chomp($machine = `uname -m`);
    return "$kernel|$machine";
}

sub do_status() {
    # initialise the global config object with the default values and
    # setup the $_system object
    $_config->init;
    $_system = $_config->system;

    # supress cache building messages
    $_config->{quiet} = 2;

    # set up the cache
    my $cache = AptPkg::Cache->new;

    # retrieve lsb informations
    unless (open(HLSB, "lsb_release -a 2> /dev/null |")) {
	print "\nADPERR: Failed to execute 'lsb_release -a' ($!).\n";
	exit(1);
    }
    my %lsb;
    while(<HLSB>) {
	chomp;

	$lsb{$1}=$2 if (/^(Distributor ID|Release|Codename):\s+(\S.*)$/);
    }
    close(HLSB);
    if($?) {
	print "\nADPERR: Error executing 'lsb_release -a' ($?).\n";
	exit(1);
    }
    print "LSBREL: $lsb{'Distributor ID'}|$lsb{'Release'}|$lsb{'Codename'}\n";

    # retrieve virtualization informations
    print "VIRT: ".&get_virt."\n";

    # retrieve uname informations
    print "UNAME: ".&get_uname."\n";

    # calculate forbid mask
    my $mask = 0;
    $mask |= 1 if ($FORBID_REFRESH);
    $mask |= 2 if ($FORBID_UPGRADE);
    $mask |= 4 if ($FORBID_INSTALL);
    print "FORBID: $mask\n";

    # add installation UUID if available
    if(-r $UUIDFILE && -s $UUIDFILE) {
	print "UUID: ", `head -n 1 "$UUIDFILE"`;
    }

    # add cluster name if available
    foreach my $CLUSTER (@CLUSTERS) {
	print "CLUSTER: $CLUSTER\n";
    }

    # add needrestart batch output
    system(($GETROOT ? $GETROOT : ()), $NEEDRESTART, '-b') if($NEEDRESTART);

    # get packages which might be upgraded
    my %updates;
    my %holds;
    my $pos = 0;
    my $DPKGARGS;

    if($DPKGTOOL eq "apt-get") {
	$DPKGARGS = "--quiet --simulate --fix-broken --allow-unauthenticated";
    }
    elsif($DPKGTOOL eq "aptitude") {
	$DPKGARGS = "--verbose --assume-yes --quiet --simulate -f --allow-untrusted";
    }
    else {
	# unkown DPKG frontend - fallback to apt-get
	$DPKGTOOL = "apt-get";
	$DPKGARGS = "--quiet --simulate --fix-broken --allow-unauthenticated";
    }

    unless(open(HAPT, "$DPKGTOOL $DPKGARGS dist-upgrade |")) {
	print "\nADPERR: Failed to execute '$DPKGTOOL $DPKGARGS dist-upgrade' ($!).\n";
	exit(1);
    }
    while(<HAPT>) {
	chomp;
	
	if($pos == 0) {
	    $pos=1 if (/^The following packages have been kept back/);
	    $pos=2 if (/^The following packages will be upgraded:/);
	    next;
	}

	if($pos == 1) {
	    unless (/^\s/) {
		$pos++;
		next;
	    }
	    while(/^\s*(\S+)(\s(.+))?$/) {
		$holds{$1} = 1;
		if(defined($2)) {
		    $_ = $2;
		}
		else {
		    $_ = '';
		}
	    }
	}

	$updates{$1} = $2 if (/^Inst (\S+) \[.+\] \((\S+) /);
    }
    close(HAPT);
    if($?) {
	print "\nADPERR: Error executing '$DPKGTOOL $DPKGARGS dist-upgrade' ($?).\n";
	exit(1);
    }

    # get version of installed packages
    my %installed;
    my %status;
    my $arch = `dpkg --print-architecture`;
    chomp($arch);
    unless(open(HDPKG, "dpkg-query --show --showformat='\${Package} \${Version} \${Status} \${Architecture}\\n' |")) {
	print "\nADPERR: Failed to execute \"dpkg-query --show --showformat='\${Package} \${Version} \${Status} \${Architecture}\\n'\" ($!).\n";
	exit(1);
    }

    while(<HDPKG>) {
	chomp;

	next unless (/^(\S+) (\S+) (\S+) (\S+) (\S+) (\S+)$/);

	my $pkg = $1;
	$pkg .= ":$6" if($6 ne q(all) && $6 ne $arch);

	$installed{$pkg} = $2 ;
	if($holds{$pkg}) {
	    $status{$pkg} = 'h';
	}
	elsif($updates{$pkg}) {
	    $status{$pkg} = 'u';
	}
	else {
	    $status{$pkg} = substr($3, 0, 1);
	    
	    if ($status{$pkg} eq 'i') {
		my $p = $cache->{$1};

		unless ($5 eq 'installed') {
		    $status{$pkg} = "b=$5";
		}
		elsif ($p) {
		    if (my $available = $p->{VersionList}) {
			my $extra = 1;
			for my $v (@$available) {
			    for my $f (map $_->{File}, @{$v->{FileList}}) {
				$extra = 0 unless ($f->{FileName} eq '/var/lib/dpkg/status');
			    }
			}
			$status{$pkg} = 'x' if($extra);
		    }
		}
	    }
	}
    }
    close(HDPKG);
    if($?) {
	print "\nADPERR: Error executing \"dpkg-query --show --showformat='\${Package} \${Version} \${Status}\\n'\" ($?).\n";
	exit(1);
    }

    foreach my $pkg (keys %installed) {
	print "STATUS: $pkg|$installed{$pkg}|$status{$pkg}";
	if (exists($updates{$pkg})) {
	    print "=$updates{$pkg}" ;
	}
	
	print "\n";
    }
}

sub do_upgrade() {
    my $UpgradeCmd = "$DPKGTOOL ";
    $UpgradeCmd .= '-o Aptitude::Delete-Unused=false ' if($DPKGTOOL eq 'aptitude');
    $UpgradeCmd .= $APTUPGRADE;

    # drop -y if any packages would be removed
    if($ASSUMEYES) {
	unless(open(HAPT, "$UpgradeCmd --simulate --assume-yes |")) {
	    print "\nADPERR: Failed to execute '$UpgradeCmd --simulate' ($!).\n";
	    exit(1);
	}
	while(<HAPT>) {
	    chomp;
	
	    if(/^The following packages will be REMOVED:/) {
		$ASSUMEYES = undef;
		last;
	    }
	    last if(/^\d+ upgraded, \d+ newly installed/);
	}
	close(HAPT);

	$UpgradeCmd .= ' --assume-yes' if($ASSUMEYES);
    }

    system("$GETROOT $UpgradeCmd");
}

sub do_install() {
    system(($GETROOT ? $GETROOT : ()), $DPKGTOOL, 'install', @_);
}

sub do_kernel() {
    my $infostr = 'KERNELINFO:';
    my $verfile = '/proc/version';
    my $versignfile = '/proc/version_signature';
    my $version = `uname -r`;
    chomp($version);

    unless(-r $verfile) {
	print "$infostr 9 $version\n";
	return;
    }

    my $vers = '';
    if (-r $versignfile) {
      unless(`cat $versignfile` =~ /^Ubuntu (\S+)-\S+ \S+$/) {
          print "$infostr 2 $version\n";
          return;
      }
      $vers = $1;
    }
    else {
      my $vstr = `cat $verfile`;
      unless($vstr =~ /^\S+ \S+ \S+ \(Debian ([^\)]+)\)/ ||
             $vstr =~ /^\S+ \S+ \S+ \(debian-kernel\@lists\.debian\.org\) .+ Debian (\S+)(?: \(\d{4}-\d\d-\d\d\))?$/) {
          print "$infostr 2 $version\n";
          return;
      }
      $vers = $1;
    }

    my $reboot = 0;
	unless(open(HDPKG, "dpkg-query -W -f='\${Version} \${Status;20} \${Maintainer} \${Provides}\n' 'linux-image*'|")) {
	print "$infostr 9 $version\n";
	return;
    }
    while(<HDPKG>) {
	next unless (/^(\S+)\s/);
	
	$reboot=1 unless (system("dpkg", "--compare-versions", $vers, "lt", $1) >> 8);
    }
    close(HDPKG);

    print "$infostr $reboot $version\n";
}

sub do_cleanup() {
    system(($GETROOT ? $GETROOT : ()), $NEEDRESTART) if($RUNNEEDREST);

    return unless $CLEANUP;

    system(($GETROOT ? $GETROOT : ()), $DPKGTOOL, 'clean');
}