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
|
#!/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 strict;
use warnings;
my $CFGFILE = '/etc/apt-dater-host.conf';
my $GETROOT = 'sudo';
my $CLEANUP = 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";
&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 call_yum {
my $nofatal = ($_[0] && $_[0] eq "nofatal" && shift @_);
open (my $fh, "-|", @_) or do {
return if $nofatal;
print "\nADPERR: Failed to execute '@_' ($!).\n";
exit(1);
};
my $out = do {
local $/; # slurp
<$fh>;
};
close $fh;
if($?) {
return if $nofatal;
print "\nADPERR: Error executing '@_' ($?).\n";
exit(1);
}
$out =~ s/\n+(\s+)/$1/g;
return split(/\n+/, $out);
}
sub get_virt() {
return "Unknown" unless (-x '/usr/bin/imvirt');
my $imvirt;
chomp($imvirt = `/usr/bin/imvirt`);
return $imvirt;
}
sub get_uname() {
my $kernel;
my $machine;
chomp($kernel = `uname -s`);
chomp($machine = `uname -m`);
return "$kernel|$machine";
}
sub do_status() {
# 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); NOT SUPPORTED
$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;
foreach(call_yum("$GETROOT yum list updates")) {
$updates{$1} = $2 if (/^(\S+)\s+(\d\S*)\s+(\S+)/);
}
# get packages which are obsolete (extra - no downloadable version available)
my %extras;
foreach(call_yum("$GETROOT yum list extras")) {
$extras{$1} = $2 if (/^(\S+)\s+(\d\S*)\s+(\S+)/);
}
# get version of installed packages
my %installed;
my %status;
foreach(call_yum("$GETROOT yum list installed")) {
next unless (/^(\S+)\s+(\d\S*)\s+(\S+)/);
$installed{$1} = $2 ;
if($updates{$1}) {
$status{$1} = 'u';
}
elsif($extras{$1}) {
$status{$1} = 'x';
}
else {
$status{$1} = 'i';
}
}
foreach my $pkg (keys %installed) {
print "STATUS: $pkg|$installed{$pkg}|$status{$pkg}";
if (exists($updates{$pkg})) {
print "=$updates{$pkg}" ;
}
print "\n";
}
}
sub do_upgrade() {
system("$GETROOT yum update");
}
sub do_install() {
if($GETROOT) {
system($GETROOT, 'yum', 'install', @_);
}
else {
system('yum', 'install', @_);
}
}
sub do_kernel() {
my $infostr = 'KERNELINFO:';
my $version = `uname -r`;
chomp($version);
my $add = '';
$add = "-$1" if($version =~ /([a-z]+)$/);
my $pos = 0;
my $kinstalled;
my %distri;
my @ret;
if (!(@ret = call_yum('nofatal', "$GETROOT yum list 'kernel*'")) || $?) {
print "$infostr 9 $version\n";
return;
};
foreach(@ret) {
if(/^Installed Packages/) {
$pos = 1;
next;
}
if(/^Available Packages/) {
$pos = 2;
next;
}
if(($pos == 1) && /^(\S+$add\.\S+)\s+(\d\S*)\s/) {
my $ver = $2;
$distri{$ver.substr($add,1)} = 1;
if(!$kinstalled) {
$kinstalled = $ver;
}
else {
$kinstalled = $ver if(&versioncmp($kinstalled, $ver) < 0);
}
}
}
unless($distri{$version}) {
print "$infostr 2 $version\n";
return;
}
unless($kinstalled.substr($add, 1) cmp $version) {
print "$infostr 0 $version\n";
return;
}
print "$infostr 1 $version\n";
}
##
# Taken from Sort::Versions 1.4
# Copyright (c) 1996, Kenneth J. Albanowski.
##
sub versioncmp() {
my @A = ($_[0] =~ /([-.]|\d+|[^-.\d]+)/g);
my @B = ($_[1] =~ /([-.]|\d+|[^-.\d]+)/g);
my ($A, $B);
while (@A and @B) {
$A = shift @A;
$B = shift @B;
if ($A eq '-' and $B eq '-') {
next;
} elsif ( $A eq '-' ) {
return -1;
} elsif ( $B eq '-') {
return 1;
} elsif ($A eq '.' and $B eq '.') {
next;
} elsif ( $A eq '.' ) {
return -1;
} elsif ( $B eq '.' ) {
return 1;
} elsif ($A =~ /^\d+$/ and $B =~ /^\d+$/) {
if ($A =~ /^0/ || $B =~ /^0/) {
return $A cmp $B if $A cmp $B;
} else {
return $A <=> $B if $A <=> $B;
}
} else {
$A = uc $A;
$B = uc $B;
return $A cmp $B if $A cmp $B;
}
}
@A <=> @B;
}
sub do_cleanup() {
system(($GETROOT ? $GETROOT : ()), $NEEDRESTART) if($RUNNEEDREST);
return unless $CLEANUP;
system("$GETROOT yum clean packages");
}
|