diff options
author | Dan McGee <dan@archlinux.org> | 2011-02-27 12:44:30 -0600 |
---|---|---|
committer | Dan McGee <dan@archlinux.org> | 2011-02-27 12:44:30 -0600 |
commit | 6d5909ca5dc8934cacd969e964573989dc6b0417 (patch) | |
tree | 69bba97e24b6ae67861884bf93362f5274c78309 /mirrors | |
parent | db1524fd64e8b5c0f43cfed7643034ee764f55fd (diff) |
Auto-resolve mirror URLs on save
This prevents people from having to mess with these checkboxes at all in
the admin, and we incur no delay on their initial values being correct
waiting for the cron job to run.
Signed-off-by: Dan McGee <dan@archlinux.org>
Diffstat (limited to 'mirrors')
-rw-r--r-- | mirrors/admin.py | 2 | ||||
-rw-r--r-- | mirrors/management/commands/mirrorresolv.py | 9 | ||||
-rw-r--r-- | mirrors/models.py | 25 |
3 files changed, 27 insertions, 9 deletions
diff --git a/mirrors/admin.py b/mirrors/admin.py index f6a72cf0..b9c2876a 100644 --- a/mirrors/admin.py +++ b/mirrors/admin.py @@ -29,7 +29,7 @@ class MirrorUrlForm(forms.ModelForm): class MirrorUrlInlineAdmin(admin.TabularInline): model = MirrorUrl form = MirrorUrlForm - readonly_fields = ('protocol',) + readonly_fields = ('protocol', 'has_ipv4', 'has_ipv6') extra = 3 # ripped off from django.forms.fields, adding netmask ability diff --git a/mirrors/management/commands/mirrorresolv.py b/mirrors/management/commands/mirrorresolv.py index 77e7a223..8a628bd4 100644 --- a/mirrors/management/commands/mirrorresolv.py +++ b/mirrors/management/commands/mirrorresolv.py @@ -12,7 +12,6 @@ from django.core.management.base import NoArgsCommand import sys import logging -from urlparse import urlparse import socket from mirrors.models import MirrorUrl @@ -42,13 +41,11 @@ def resolve_mirrors(): logger.debug("requesting list of mirror URLs") for mirrorurl in MirrorUrl.objects.filter(mirror__active=True): try: - hostname = urlparse(mirrorurl.url).hostname - logger.debug("resolving %3i (%s)", mirrorurl.id, hostname) - info = socket.getaddrinfo(hostname, None, 0, socket.SOCK_STREAM) - families = [x[0] for x in info] + logger.debug("resolving %3i (%s)", mirrorurl.id, mirrorurl.hostname) + families = mirrorurl.address_families() mirrorurl.has_ipv4 = socket.AF_INET in families mirrorurl.has_ipv6 = socket.AF_INET6 in families - logger.debug("%s: v4: %s v6: %s", hostname, + logger.debug("%s: v4: %s v6: %s", mirrorurl.hostname, mirrorurl.has_ipv4, mirrorurl.has_ipv6) mirrorurl.save(force_update=True) except socket.error, e: diff --git a/mirrors/models.py b/mirrors/models.py index b763fb63..7c269898 100644 --- a/mirrors/models.py +++ b/mirrors/models.py @@ -1,6 +1,7 @@ from django.db import models from django.core.exceptions import ValidationError +import socket from urlparse import urlparse TIER_CHOICES = ( @@ -56,8 +57,20 @@ class MirrorUrl(models.Model): protocol = models.ForeignKey(MirrorProtocol, related_name="urls", editable=False) mirror = models.ForeignKey(Mirror, related_name="urls") - has_ipv4 = models.BooleanField("IPv4 capable", default=True) - has_ipv6 = models.BooleanField("IPv6 capable", default=False) + has_ipv4 = models.BooleanField("IPv4 capable", default=True, + editable=False) + has_ipv6 = models.BooleanField("IPv6 capable", default=False, + editable=False) + + def address_families(self): + hostname = urlparse(self.url).hostname + info = socket.getaddrinfo(hostname, None, 0, socket.SOCK_STREAM) + families = [x[0] for x in info] + return families + + @property + def hostname(self): + return urlparse(self.url).hostname def clean(self): try: @@ -66,6 +79,14 @@ class MirrorUrl(models.Model): self.protocol = MirrorProtocol.objects.get(protocol=protocol) except Exception as e: raise ValidationError(e) + try: + families = self.address_families() + self.has_ipv4 = socket.AF_INET in families + self.has_ipv6 = socket.AF_INET6 in families + except socket.error as e: + # We don't fail in this case; we'll just set both to False + self.has_ipv4 = False + self.has_ipv6 = False def __unicode__(self): return self.url |