summaryrefslogtreecommitdiff
path: root/scripts/vyatta-show-version
blob: f8f291a78020edcf232ba51cf2547141f6fe8da9 (plain)
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
#!/usr/bin/perl -w
#
# Module: show_version
# 
# **** License ****
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.
# 
# 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.
# 
# This code was originally developed by Vyatta, Inc.
# Portions created by Vyatta are Copyright (C) 2005, 2006, 2007 Vyatta, Inc.
# All Rights Reserved.
# 
# Author: Rick Balocca
# Date: 2007
# Description:
# 
# **** End License ****
# 
use strict;
use warnings;

#
# Global hash of debians in the base install and now.
#
my $rHoH_base_debs;
my $rHoH_now_debs;

my $base          = '/opt/vyatta/etc';
my $versionfile   = "$base/version";
my $buildfile     = "$base/build.txt";
my $debsfile      = "$base/deb-versions.txt";

sub echo_file {
    my ($file) = @_;

    my @lines = ();
    if (!(-e $file)) {
	return @lines;
    }
    
    open(my $FH, '<', $file) or die "Unable to open [$file]\n";
    @lines=<$FH>;
    close($FH);
    return @lines;
}

#
# convert the "dpkg -l" output have same format as deb-versions.txt
#
sub get_pkg_version {
    my @lines = @_;
    
    my @new_lines = ();
    foreach my $line (@lines) {
	if ($line =~ /^[D\|\+]/) {
	    next;   # skip header
	}
	my ($status, $pkg, $version) = split(/[ \t\n]+/, $line, 4);
	if ($status =~ /^i/) {
	    push(@new_lines, "$pkg $version");
	}
    }
    return @new_lines;
}

sub read_pkg_file {
    my @pkgs_list = @_;

    my %HoH = ();
    my ($name, $version);
    foreach my $line (@pkgs_list) {
	($name, $version) = split(/[ \t\n]+/, $line, 3);
	$HoH{$name}{'version'} = $version;
    }
    return \%HoH;
}

sub show_added {
    for my $name (sort keys %$rHoH_now_debs) {
	if (!$rHoH_base_debs->{$name}) {
	    printf("Aii %-25s %-25s\n", 
		   $name, $rHoH_now_debs->{$name}->{'version'});
	}
    }
}

sub show_deleted {
    for my $name (sort keys %$rHoH_base_debs) {
	if (!$rHoH_now_debs->{$name}) {
	    printf("X   %-25s %-25s\n", 
		   $name, $rHoH_base_debs->{$name}->{'version'});
	}
    }
}

sub show_upgraded_downgraded {
    my ($up_down) = @_;
    
    my ($symbol, $op, $ver_base, $ver_now, $cmd);
    if ($up_down eq "upgraded") {
	$symbol = "U";
	$op = "lt";
    } else {
	$symbol = "D";
	$op = "gt";
    }
    for my $name (sort keys %$rHoH_base_debs) {
	if ($rHoH_now_debs->{$name}) {
	    $ver_base = $rHoH_base_debs->{$name}{'version'};
	    $ver_now  =  $rHoH_now_debs->{$name}{'version'};
	    if ($ver_base ne $ver_now) {
		$cmd = "dpkg --compare-versions \"$ver_base\" $op \"$ver_now\"";
		if (!system($cmd)) {
		    printf("%sii %-25s %-20s (baseline: %s)\n", 
			   $symbol, $name, $ver_now, $ver_base);
		}
            }
	} 
    }
}

sub show_upgraded {
    show_upgraded_downgraded("upgraded");
}

sub show_downgraded {
    show_upgraded_downgraded("downgraded");
}

sub show_all {
    show_added();
    show_deleted();
    show_upgraded();
    show_downgraded();
}

my %options = (
    "added"      => \&show_added,
    "deleted",   => \&show_deleted,
    "upgraded"   => \&show_upgraded,
    "downgraded" => \&show_downgraded,
    "all"        => \&show_all,
);

#
# main
#
print(&echo_file($versionfile));
print(&echo_file($buildfile));

my $booted = `grep -e '^unionfs.*/filesystem.squashfs' -e '^aufs.*/filesystem.squashfs' /proc/mounts`;
if (defined($booted) && $booted ne "") {
    $booted="livecd";
} else {
    $booted="disk";
}
print "Boot via :    $booted\n";
my $uptime = `uptime`;
if (defined $uptime && $uptime ne "") {
    print "Uptime   :   $uptime\n";
}
if (!(-e $debsfile)) {
    exit 0;
}

print "\n";
$rHoH_base_debs = read_pkg_file(&echo_file($debsfile));
$rHoH_now_debs  = read_pkg_file(get_pkg_version(`dpkg -l`));

if ($#ARGV == 0) {
    if ($options{$ARGV[0]}) {
        $options{$ARGV[0]}->();
    } else {
	print "Usage: showversion [added|deleted|upgraded|downgraded|all]\n";
	exit 1;
    }
}
 
# end of file