File: apt_proxy.py

package info (click to toggle)
apt-proxy 1.9.36.3%2Bnmu1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 640 kB
  • ctags: 803
  • sloc: python: 4,111; sh: 396; makefile: 63
file content (603 lines) | stat: -rw-r--r-- 21,433 bytes parent folder | download
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
#
# Copyright (C) 2002 Manuel Estrada Sainz <ranty@debian.org>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of version 2.1 of the GNU Lesser General Public
# License as published by the Free Software Foundation.
#
# This library 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
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

import os, stat, signal, fcntl, exceptions
from os.path import dirname, basename
import tempfile, glob, re, urlparse, time
from twisted.internet import reactor
from twisted.python.failure import Failure
from twisted.internet import error, protocol
from twisted.web import http

import memleak
import fetchers, cache, packages
from misc import log, MirrorRecycler
import twisted_compat
from clients import HttpRequestClient

#from posixfile import SEEK_SET, SEEK_CUR, SEEK_END
#since posixfile is considered obsolete I'll define the SEEK_* constants
#myself.
SEEK_SET = 0
SEEK_CUR = 1
SEEK_END = 2

from types import *

status_dir = '.apt-proxy'

class Backend:
    """
    A backend repository.  There is one Backend for each [...] section
    in apt-proxy.conf
    """

    "Sequence of BackendServers, in order of preference"
    uris = []

    "Hash of active cache entries"
    entries = {}

    "Packages database for this backend"
    packages = None
    name = None

    downloadQueuePerClient = True # Set to true if a download queue should be created per client

    def __init__(self, factory, config):
        log.debug("Creating Backend: " + config.name)
        self.factory = factory
        self.config = config # apBackendConfig configuration information
        self.base = config.name # Name of backend
        self.uris = [] # Sequence of BackendServers, in order of preference

        if self.downloadQueuePerClient:
            self.queue = fetchers.DownloadQueuePerClient()
        else:
            self.queue = fetchers.DownloadQueue()

        self.entries = {} # Hash of active cache entries
        self.packages = None # Packages database for this backend

        for uri in config.backends:
            self.addURI(uri)

        #self.get_packages_db().load()

    def addURI(self, uri):
        newBackend = BackendServer(self, uri)
        self.uris.append(newBackend)

    def get_first_server(self):
        "Provide first BackendServer for this Backend"
        return self.uris[0]

    def get_next_server(self, previous_server):
        "Return next server, or None if this is the last server"
        oldServerIdx = self.uris.index(previous_server)
        if(oldServerIdx+1 >= len(self.uris)):
            return None
        return self.uris[oldServerIdx+1]

    def __str__(self):
        return '('+self.base+')'+' servers:'+str(len(self.uris))

    def get_cache_entry(self, path):
        """
        Return CacheEntry for given path
        a new object is created if it does not already exist
        """
        if self.entries.has_key(path):
            log.debug("Cache entry exists: %s, %s entries" %(path,len(self.entries)))
            return self.entries[path]
        else:
            log.debug("New Cache entry: "+path)
            e = cache.CacheEntry(self, path)
            self.entries[path] = e
            return e
    def entry_done(self, entry):
        "A cache entry is finished and clients are disconnected"
        #if self.entries.has_key(entry.path):
        log.debug("entry_done: %s" %(entry.path), 'Backend')
        try:
            del self.entries[entry.path]
        except KeyError:
            pass # In case this is called twice

    def get_packages_db(self):
        "Return packages parser object for the backend, creating one if necessary"
        if self.packages == None:
            self.packages = packages.AptPackages(self.base, self.factory.config.cache_dir)
        return self.packages

    def get_path(self, path):
        """
        'path' is the original uri of the request.

        We return the path to be appended to the backend path to
        request the file from the backend server
        """
        return path[len(self.base)+2:]

    def file_served(self, entry):
        "A cache entry has served a file in this backend"
        self.get_packages_db().file_updated(entry)

    def start_download(self, entry):
        """
        A CacheEntry has requested that a file should be downloaded from the backend
        """
        self.queue.addFile(entry)

    def close(self):
        "Clean up backend and associated structures"
        if self.queue is not None:
            self.queue.stop()
            self.queue = None
        if self.packages is not None:
            del(self.packages)

class BackendServer:
    """
    A repository server.  A BackendServer is created for each URI defined in 'backends'
    for a Backend
    """
    
    backend = None        # Backend for this URI
    uri = None            # URI of server

    fetchers = {
        'http' : fetchers.HttpFetcher,
        'ftp'  : fetchers.FtpFetcher,
        'rsync': fetchers.RsyncFetcher,
        'file' : fetchers.FileFetcher,
        }
    ports = {
        'http' : 80,
        'ftp'  : 21,
        'rsync': 873,
        'file' : 0,
        }
    
    def __init__(self, backend, uri):
        self.backend = backend
        self.uri = uri
        log.debug("Created new BackendServer: " + uri)

        # hack because urlparse doesn't support rsync
        if uri[0:6] == 'rsync:':
            uri = 'http'+uri[5:]
            is_rsync=1
        else:
            is_rsync=0

        self.scheme, netloc, self.path, parameters, \
                     query, fragment = urlparse.urlparse(uri)
        if is_rsync:
            self.scheme = 'rsync'

        if '@' in netloc:
            auth = netloc[:netloc.rindex('@')]
            netloc = netloc[netloc.rindex('@')+1:]
            self.username, self.password = auth.split(':')
        else:
            self.username = None
            self.password = None
        if ':' in netloc:
            self.host, self.port = netloc.split(':')
        else:
            self.host = netloc
            self.port = self.ports[self.scheme]
        self.fetcher = self.fetchers[self.scheme]
        try:
            self.port = int(self.port)
        except ValueError:
            pass 

    def __str__(self):
        return ('(' + self.backend.base + ') ' + self.scheme + '://' +
               self.host + ':' + str(self.port))

class Channel(http.HTTPChannel):
    """
    This class encapsulates a channel (an HTTP socket connection with a single
    client).

    Each incoming request is passed to a new Request instance.
    """
    requestFactory = HttpRequestClient
    log_headers = None

    def headerReceived(self, line):
        "log and pass over to the base class"
        log.debug("Header: " + line)
        if self.log_headers == None:
            self.log_headers = line
        else:
            self.log_headers += ", " + line
        http.HTTPChannel.headerReceived(self, line)

    def allContentReceived(self):
        if self.log_headers != None:
            log.debug("Headers: " + self.log_headers)
            self.log_headers = None
        http.HTTPChannel.allContentReceived(self)

    def connectionLost(self, reason=None):
        "If the connection is lost, notify all my requests"
        __pychecker__ = 'unusednames=reason'
        log.debug("Client connection closed", 'Channel')
        http.HTTPChannel.connectionLost(self, reason)
        if log.isEnabled('memleak'):
            memleak.print_top_10()
        #reactor.stop()   # use for shutting down apt-proxy when a client disconnects

    #def requestDone(self, request):
        #log.debug("========Request Done=========", 'Channel')
        #http.HTTPChannel.requestDone(self, request)
        
class Factory(protocol.ServerFactory):
    """
    This is the center of apt-proxy, it holds all configuration and global data
    and gets attached everywhere.

    Factory receives incoming client connections and creates a Channel for
    each client request.

    interesting attributes:

    self.runningFetchers: a dictionary, uri/Fetcher pairs, that holds the
    active Fetcher for that uri if any. If there is an active Fetcher for
    a certain uri at a certain time the request is inserted into the Fetcher
    found here instead of instanciating a new one.

    Persisten dictionaries:
    self.update_times: last time we checked the freashness of a certain file.
    self.access_times: last time that a certain file was requested.
    self.packages: all versions of a certain package name.
    
    """



    def __init__ (self, config):
        self.runningFetchers = {}
        self.backends = {}
        self.config = config
        self.periodicCallback = None
        self.databases = databaseManager(self)
        self.recycler = None

    def __del__(self):
        pass
        #self.closeDatabases()

    def periodic(self):
        "Called periodically as configured mainly to do mirror maintanace."
        log.debug("Doing periodic cleaning up")
        self.periodicCallback = None
        self.clean_old_files()
        self.recycler.start()
        log.debug("Periodic cleaning done")
        self.startPeriodic()

    def startPeriodic(self):
        if (self.config.cleanup_freq != None and self.periodicCallback is None):
            log.debug("Will do periodic cleaup in %s sec" % (self.config.cleanup_freq))
            self.periodicCallback = reactor.callLater(self.config.cleanup_freq, self.periodic)

    def stopPeriodic(self):
        if self.periodicCallback is not None:
            self.periodicCallback.cancel()
            self.periodicCallback = None

    def __getattr__ (self, name):
        # Auto open database if requested
        if name in self.databases.table_names:
            db = self.databases.get(name)
            setattr(self, name, db)
            return db
        else:
            raise AttributeError(name)

    def startFactory(self):
        #start periodic updates
        self.configurationChanged()
        self.dumpdbs()
        self.recycler = MirrorRecycler(self, 1)
        #self.recycler.start()

    def configurationChanged(self, oldconfig = None):
        """
        Configuration has changed - update backends and background processes
        """
        if oldconfig is not None:
            for param in 'address', 'port', 'telnet_port', 'telnet_user', 'telnet_pass', 'cache_dir':
                if getattr(self.config, param) != getattr(oldconfig, param):
                    log.err('Configuration value %s has changed, ignored'%(param))
                    log.err('You must restart apt-proxy for the change to take effect')
                    setattr(self.config, param, getattr(oldconfig, param))

        if self.config.cleanup_freq != None and (oldconfig is None or oldconfig.cleanup_freq == None):
            self.startPeriodic()
        self.createBackends()

    def createBackends(self):
        self.backends = {}
        for name, bconf in self.config.backends.items():
            #print "[",name,"]"
            self.backends[name] = Backend(self, bconf)

    def getBackend(self, name):
        """
        Return backend with given name
        @param name Name of backend as specified in [backendName] section in config file
        @return Backend class, or None if not found
        """
        if self.backends.has_key(name):
            return self.backends[name]

        if not self.config.dynamic_backends:
            return None

        # We are using dynamic backends so we will use the name as
        # the hostname to get the files.
        backendServer = "http://" + name
        log.debug("Adding dynamic backend:" + name)
        backendConfig = self.config.addBackend(None, name, (backendServer,))
        backend = Backend(self, backendConfig)
        self.backends[name] = backend
        return backend

    def clean_versions(self, packages):
        """
        Remove entries for package versions which are not in cache, and delete
        some files if needed to respect the max_versions configuration.

        TODO: This must be properly done per distribution.
        """
        if self.config.max_versions == None:
            #max_versions is disabled
            return
        package_name = None
        cache_dir = self.config.cache_dir

        cached_packages = []   # all packages in cache directory
        current_packages = []  # packages referenced by Packages files

        import apt_pkg
        def reverse_compare(a, b):
            """ Compare package versions in reverse order """
            return apt_pkg.VersionCompare(b[0], a[0])

        if len(packages) <= self.config.max_versions:
            return

        from packages import AptDpkgInfo, get_mirror_versions
        for uri in packages[:]:
            if not os.path.exists(cache_dir +'/'+ uri):
                log.debug("clean_versions: file %s no longer exists"%(uri),
                        'versions')
                packages.remove(uri)
            else:
                try:
                    info = AptDpkgInfo(cache_dir +'/'+ uri)
                    cached_packages.append([info['Version'], uri])
                    package_name = info['Package']
                except SystemError:
                    log.msg("Found problems with %s, aborted cleaning"%(uri),
                            'versions')
                    return

        if len(cached_packages) > 0:
            import apt_pkg
            cached_packages.sort(reverse_compare)
            log.debug(str(cached_packages), 'versions')

            current_packages = get_mirror_versions(self, package_name)
            current_packages.sort(reverse_compare)
            log.debug("Current Versions: " + str(current_packages), 'versions')

            version_count = 0

            while len(cached_packages):
                #print 'current:',len(current_packages),'cached:',len(cached_packages), 'count:', version_count
                if len(current_packages):
                    compare_result = apt_pkg.VersionCompare(current_packages[0][0], cached_packages[0][0])
                    #print 'compare_result %s , %s = %s ' % (
                    #              current_packages[0][0], cached_packages[0][0], compare_result)
                else:
                    compare_result = -1

                if compare_result >= 0:
                    log.debug("reset at "+ current_packages[0][1], 'max_versions')
                    del current_packages[0]
                    version_count = 0
                else:
                    version_count += 1
                    if version_count > self.config.max_versions:
                        log.msg("Deleting " + cache_dir +'/'+ cached_packages[0][1], 'max_versions')
                        os.unlink(cache_dir +'/'+ cached_packages[0][1])
                        packages.remove(cached_packages[0][1])
                    del cached_packages[0]

    def clean_old_files(self):
        """
        Remove files which haven't been accessed for more than 'max_age' and
        all entries for files which are no longer there.
        """
        if self.config.max_age == None:
            #old file cleaning is disabled
            return
        cache_dir = self.config.cache_dir
        files = self.access_times.keys()
        min_time = time.time() - self.config.max_age

        for file in files:
            local_file = cache_dir + '/' + file
            if not os.path.exists(local_file):
                log.debug("old_file: non-existent "+file)
                del self.access_times[file]
            elif self.access_times[file] < min_time:
                log.debug("old_file: removing "+file)
                os.unlink(local_file)
                del self.access_times[file]

        #since we are at it, clear update times for non existent files
        files = self.update_times.keys()
        for file in files:
            if not os.path.exists(cache_dir+'/'+file):
                log.debug("old_file: non-existent "+file)
                del self.update_times[file]

    def file_served(self, cache_path):
        """
        Update the databases, this file has just been served.
        @param cache_path: path of file within cache e.g. debian/dists/stable/Release.gpg
        """
        log.debug("File served: %s" % (cache_path))
        path = os.sep + cache_path # Backwards compat
        #path = cache_path
        self.access_times[path]=time.time()
        if re.search("\.deb$", path):
            package = re.sub("^.*/", "", path)
            package = re.sub("_.*$", "", package)
            if not self.packages.has_key(package):
                packages = [path]
            else:
                packages = self.packages[package]
                if not path in packages:
                    packages.append(path)
                self.clean_versions(packages)
            self.packages[package] = packages
        self.dumpdbs()

    def closeDatabases(self):
        for db in self.databases.table_names:
            if getattr(self.databases, db) is not None:
                log.debug("closing " + db, 'db')
                getattr(self,db).close()
                delattr(self,db)
                setattr(self.databases, db, None)

    def stopFactory(self):
        log.debug('Main factory stop', 'factory')
        import packages
        # self.dumpdbs()
        
        # Stop all DownloadQueues and their fetchers
        for b in self.backends.values():
            b.close()
        self.backends = {}
        packages.cleanup(self)
        if self.recycler is not None:
            self.recycler.stop()
            self.recycler = None
        self.stopPeriodic()
        #self.closeDatabases()

    def dumpdbs (self):
        def dump_update(key, value):
            log.msg("%s: %s"%(key, time.ctime(value)),'db')
        def dump_access(key, value):
            log.msg("%s: %s"%(key, time.ctime(value)),'db')
        def dump_packages(key, list):
            log.msg("%s: "%(key),'db')
            for file in list:
                log.msg("\t%s"%(file),'db')
        def dump(db, func):
            keys = db.keys()
            for key in keys:
                func(key,db[key])
        if log.isEnabled('db'):
            log.msg("=========================",'db')
            log.msg("Dumping update times",'db')
            log.msg("=========================",'db')
            dump(self.update_times, dump_update)
            log.msg("=========================",'db')
            log.msg("Dumping access times",'db')
            log.msg("=========================",'db')
            dump(self.access_times, dump_access)
            log.msg("=========================",'db')
            log.msg("Dumping packages",'db')
            log.msg("=========================",'db')
            dump(self.packages, dump_packages)


    def buildProtocol(self, addr):
        __pychecker__ = 'unusednames=addr'
        proto = Channel()
        proto.factory = self;
        return proto

    def log(self, request):
        return

    def debug(self, message):
        log.debug(message)

class databaseManager:
    update_times = None
    access_times = None
    packages = None
    table_names=['update_times', 'access_times', 'packages']
    database_files=['update', 'access', 'packages']

    def __init__(self, factory):
        self.factory = factory

    def get(self, name):
        idx = self.table_names.index(name)
        db = getattr(self,name)
        if db is None:
            db = self.open_shelve(self.database_files[idx])
            setattr(self, name, db)
        return db

    def open_shelve(self, dbname):
        from bsddb import db,dbshelve

        shelve = dbshelve.DBShelf()
        db_dir = self.factory.config.cache_dir+'/'+status_dir+'/db'
        if not os.path.exists(db_dir):
            os.makedirs(db_dir)

        filename = db_dir + '/' + dbname + '.db'
        if os.path.exists(filename):
                try:
                    log.debug('Verifying database: ' + filename)
                    shelve.verify(filename)
                except:
                    os.rename(filename, filename+'.error')
                    log.msg(filename+' could not be opened, moved to '+filename+'.error','db', 1)
                    log.msg('Recreating '+ filename,'db', 1)
        try:
            log.debug('Opening database ' + filename)
            shelve = dbshelve.open(filename)

        # Handle upgrade to new format included on 1.9.20.
        except db.DBInvalidArgError:
            log.msg('Upgrading from previous database format: %s' % filename + '.previous')
            import bsddb.dbshelve
            os.rename(filename, filename + '.previous')
            previous_shelve = bsddb.dbshelve.open(filename + '.previous')
            shelve = dbshelve.open(filename)

            for k in previous_shelve.keys():
                shelve[k] = previous_shelve[k]
            log.msg('Upgrade complete')

        return shelve