]> git.proxmox.com Git - proxmox-ve.git/blob - debian/apthook/pve-apt-hook
add APT hook to prevent proxmox-ve removal
[proxmox-ve.git] / debian / apthook / pve-apt-hook
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use File::Basename;
7
8 my $fd = $ENV{APT_HOOK_INFO_FD};
9 my $check_file = '/please-remove-proxmox-ve';
10 my $check_package = 'proxmox-ve';
11 my $hook_name = basename($0);
12
13 my $log = sub {
14 my ($line) = @_;
15 print "W: ($hook_name) $line";
16 };
17
18 if (!defined $fd || $fd == 0) {
19 $log->("APT_HOOK_INFO_FD not correctly defined, skipping apt-pve-hook checks\n");
20 exit 0;
21 }
22
23 open(my $fh, "<&=${fd}") or die "E: could not open APT_HOOK_INFO_FD (${fd}) - $!\n";
24
25 my $cleanup = sub {
26 my ($rc) = @_;
27
28 close($fh);
29 exit $rc;
30 };
31
32 chomp (my $ver = <$fh>);
33 if ($ver ne "VERSION 2") {
34 $log->("apt-pve-hook misconfigured, expecting hook protocol version 2\n");
35 $cleanup->(0);
36 }
37
38 my $blank;
39 while (my $line = <$fh>) {
40 chomp $line;
41
42 if (!defined($blank)) {
43 $blank = 1 if !$line;
44 next;
45 }
46
47 my ($pkg, $old, $dir, $new, $action) = (split / /, $line, 5);
48 if (!defined($action)) {
49 $log->("apt-pve-hook encountered unexpected line: $line\n");
50 next;
51 }
52
53 if ($pkg eq 'proxmox-ve' && $action eq '**REMOVE**') {
54 if (-e $check_file) {
55 $log->("'$check_file' exists, proceeding with removal of package '${check_package}'\n");
56 unlink $check_file;
57 } else {
58 $log->("!! WARNING !!\n");
59 $log->("You are attempting to remove the meta-package '${check_package}'!\n");
60 $log->("\n");
61 $log->("If you really you want to permanently remove '${check_package}' from your system, run the following command\n");
62 $log->("\ttouch '${check_file}'\n");
63 $log->("and repeat your apt-get/apt invocation.\n");
64 $log->("\n");
65 $log->("If you are unsure why '$check_package' would be removed, please verify\n");
66 $log->("\t- your APT repository settings\n");
67 $log->("\t- that you are using 'apt-get dist-upgrade' or 'apt full-upgrade' to upgrade your system\n");
68 $cleanup->(1);
69 }
70 }
71 }
72
73 $cleanup->(0);