File: apt-proxy-to-apt-cacher

package info (click to toggle)
apt-cacher 1.7.23
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid
  • size: 548 kB
  • sloc: perl: 4,585; sh: 200; makefile: 5
file content (166 lines) | stat: -rwxr-xr-x 4,277 bytes parent folder | download | duplicates (3)
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
#!/usr/bin/perl

use strict;

use Getopt::Long qw(:config no_ignore_case bundling pass_through);
use Cwd 'abs_path';

my $help;
my $configfile = '/etc/apt-cacher/apt-cacher.conf';
my $apconfigfile = '/etc/apt-proxy/apt-proxy-v2.conf';

my %options = (
    "h|help" => \$help,
    "c|cfgfile=s"        => \$configfile,
    "C|apconfigfile=s"        => \$apconfigfile,
);

&help unless ( GetOptions(%options));
&help if ($help);

sub help {
die "
USAGE: $0 [ options ]
Transforms configuration and cached data from apt-proxy v2 to apt-cacher 1.x

Options:
 -c     apt-cacher's config file
 -C     apt-proxy's  config file
";
}

print "Reading apt-proxy's configuration from $apconfigfile\n";

open(my $apc, $apconfigfile) || die "Could not open $apconfigfile. Use the -C option\n";

my %config;

print "Adopting options:\n";
my $cache_dir;

my $prevkey;
LINE: while (<$apc>)
{
    chomp;
    if ( /^\t(.*)$/ && defined $prevkey ) {
        $config{$prevkey}.= " $1 ";
        next LINE;
    }

    s/^;.*$//;   # kill off comments
    s/^\s+//;	# kill off leading spaces
    s/\s+$//;	# kill off trailing spaces

    next if /^\[DEFAULT/;
    
    if(/^\[(.*)\]/) {
        $config{path_map} .=" ; " if $config{path_map};
        $config{path_map} .= " $1 ";
    }

    if ($_)
    {
        my ($key, $value) = split(/\s*=\s*/);	# split into key and value pair
        #print "key: $key, value: $value\n";
        $prevkey=$key;
        if($key eq "port") {
            $config{daemon_port} = $value;
            print "Port: $value\n";
        }
        if($key eq "address") {
            $config{daemon_addr} = $value;
            print "Address: $value\n";
        }
        if($key eq "http_proxy") {
            $config{http_proxy} = $value;
            $config{use_proxy} = 1;
            print "Proxy: $value\n";
        }
        if($key eq "backends") {
            $prevkey = "path_map";
            $config{path_map} .= " $value ";
        }
        if($key eq "cache_dir") {
            $cache_dir=$value;
        }
    }
}

my @map = split(/\s+/, $config{path_map});
for(@map) {
    # just try to use http on ftp servers and drop rsync versions
    # s#^ftp:#http:#;
    # s#^rsync.*##;
    s#^.*://##;
}
$config{path_map} = join(" ", @map);

#for(keys %config) {
#    print "hm, $_: $config{$_}\n";
#}

print "Reading apt-cacher's configuration from $configfile\n";

open(CONFIG, $configfile) || die "Unable to open the apt-cacher config file template\n";

my $buf;
read(CONFIG, $buf, 50000);
close(CONFIG);
$buf=~s/\\\n#/\n#/mg; # fix broken multilines
$buf=~s/\\\n//mg; # merge multilines

my @out = ("# This file has been modified by $0\n# Some lines may have been appended at the bottom of this file\n");

for(split(/\n/, $buf))
{
    my $orig=$_;

    s/#.*//;   # kill off comments
    s/^\s+//;	# kill off leading spaces
    s/\s+$//;	# kill off trailing spaces

    if ($_)
    {
        my ($key, $value) = split(/\s*=\s*/);	# split into key and value pair
        if(exists $config{$key}) {
            push @out, "$key = $config{$key}\n";
            delete $config{$key};
        }
        else {
            push @out, "$orig\n";
        }
    }
    else {
        push @out, "$orig\n";
    }
}

# append the remaining settings
for(keys %config) {
    push @out, "\n# extra setting from apt-proxy configuration\n$_ = $config{$_}\n";
}

print "\n$0 will now modify the apt-cacher.conf file\nand import the data from apt-proxy's cache. Do you wish to continue? [y/n] ";
my $answer= <STDIN>;
if($answer eq "y\n") {

    open(CONFIG, ">$configfile") || die "Unable to write apt-cacher config file\n";
    print CONFIG @out;
    close(CONFIG);
    #print join(" ", "Running: ", "/usr/share/apt-cacher/apt-cacher-import.pl", "-c", $configfile, "-r", "-R" , $cache_dir, "\n");

    system("/usr/share/apt-cacher/apt-cacher-import.pl", "-c", $configfile, "-r", "-R" , $cache_dir);
}
    
print "\nStop apt-proxy and start apt-cacher now? [y/n] ";
$answer= <STDIN>;
if($answer eq "y\n") {
    system "/etc/init.d/apt-proxy stop";
    system "/etc/init.d/apt-cacher restart";
}
    
print "\nDisable the apt-proxy in the init configuration (update-rc.d remove)? [y/n] ";
$answer= <STDIN>;
if($answer eq "y\n") {
    system "update-rc.d -f apt-proxy remove";
}