File: expire-caller.pl

package info (click to toggle)
apt-cacher-ng 0.7.27-1~bpo70+1
  • links: PTS
  • area: main
  • in suites: wheezy-backports
  • size: 1,740 kB
  • sloc: cpp: 13,987; sh: 519; perl: 442; ansic: 414; makefile: 77
file content (160 lines) | stat: -rw-r--r-- 3,601 bytes parent folder | download | duplicates (6)
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
#!/usr/bin/perl
use strict;
use Socket;

my %cfg;
my $retcode=0;

$cfg{port} = 3142;
$cfg{adminauth}="";

sub gethostname
{
   return $ENV{"HOSTNAME"} if defined $ENV{"HOSTNAME"};
   eval
   {
      require Sys::Hostname;
      return Sys::Hostname::hostname();
   }
      or do
   {
      #print "ups, $@, using cmd\n";
      my $ret=`hostname`;
      chomp($ret);
      return $ret;
   }
}

sub toBase64
{
   my $src=shift;
   eval
   {
      require MIME::Base64;
      return encode_base64($src);
   }
      or do
   {
      # dirty little helper
      $ENV{"TOBASE64"}=$src;
      return `/usr/sbin/apt-cacher-ng`;
   }
}

for my $file (</etc/apt-cacher-ng/*.conf>)
{
   my $fd;
   open($fd, $file) or next;
   while(<$fd>)
   {
      chomp;
      s/^\s+//;
      s/\s+$//;
      next if /^#/;
			my ($key, $value) = split(/\s*=\s*/);
      if(!defined($value) && /(\w+)\s*:\s*(\S+)/)
      {
         $key=$1;
         $value=$2;
      }
      next unless defined($value);
      #print "$key -> $value\n";
      $cfg{lc($key)}=$value;
   }
}

if($cfg{adminauth})
{
   my $auth="Authorization: Basic ".toBase64($cfg{adminauth});
   chomp($auth);
   $auth.="\r\n";
   $cfg{adminauth}=$auth;
}

my $debug=$ENV{"DEBUG"};

if($ENV{"ACNGIP"})
{
   $cfg{remotehost} = $ENV{"ACNGIP"};
}
elsif($cfg{bindaddress})
{
   $cfg{remotehost}=$cfg{bindaddress};
   $cfg{remotehost}=~s/\s*(\S+)\s*.*/$1/; # only keep the first host
}
else
{
   $cfg{remotehost} = 'localhost';
}

my $SOCK;

sub conTcp
{
   if ($cfg{port} =~ /\D/) { $cfg{port} = getservbyname($cfg{port}, 'tcp') }
   die "No port" unless $cfg{port};
   my $iaddr   = inet_aton($cfg{remotehost})               || return "no such host: $cfg{remotehost}";
   my $paddr   = sockaddr_in($cfg{port}, $iaddr);
   my $proto   = getprotobyname('tcp');
   socket($SOCK, PF_INET, SOCK_STREAM, $proto)  || return "socket: $!\n";
   connect($SOCK, $paddr) || return "connect: $!\n";
   return "";
}

sub conUnix
{
   socket($SOCK, PF_UNIX, SOCK_STREAM,0) || return "socket: $!\n";
   connect($SOCK, sockaddr_un($cfg{socketpath})) || return "connect: $!\n";
   # identify myself
   syswrite $SOCK, "GET / HTTP/1.0\r\nX-Original-Source: localhost\r\n\r\n";
   return "";
}

my $errmsg="";
if($cfg{port})
{
   $errmsg=conTcp;
   if($errmsg)
   {
      die "Cannot connect to server and no alternative (socket file) is available\n$errmsg\n" if(!$cfg{socketpath});
      my $errmsgU=conUnix;
      die "$errmsg\nTrying via socket file... failed:\n$errmsgU\n" if($errmsgU);
   }
}
else
{
   $errmsg=conUnix;
   die "No TCP port specified. Trying via socket file... failed:\n$errmsg\n" if($errmsg);
}

my $acngreq=$ENV{"ACNGREQ"};
if(!$acngreq)
{
   my $aoeflag='&abortOnErrors=aOe'; # checking is default
   $aoeflag='' if(defined $cfg{"exabortonproblems"} and ! $cfg{"exabortonproblems"});
   $acngreq="?doExpire=Start+Expiration$aoeflag";
}

$acngreq="?$acngreq" if ! ($acngreq =~ /^\?/);

syswrite $SOCK, "GET /$cfg{reportpage}$acngreq HTTP/1.0\r\nHost:localhost\r\nConnection:close\r\n$cfg{adminauth}\r\n";
my $line;
while (defined($line = <$SOCK>)) {
   print $line if $debug;
   if($line=~/<!-- TELL:THE:ADMIN -->/)
   #if(1)
   {
      my $HOSTNAME=gethostname;
      
      print STDERR "Error(s) occured while updating volatile index files for apt-cacher-ng.
Please visit http://$HOSTNAME:$cfg{port}/$cfg{reportpage} to rerun the
expiration manually or check the error message(s) in the current log file(s).
\n";
      close STDERR; # stop bothering and just run to end
      $retcode=1;
   }
}

close ($SOCK)            || die "close: $!";
exit $retcode;