summaryrefslogtreecommitdiff
path: root/mirrors/models.py
blob: 57664562afddad11349257ca1c85314859768e89 (plain)
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
from datetime import timedelta
import socket
from urlparse import urlparse

from django.core.exceptions import ValidationError
from django.db import models
from django.db.models.signals import pre_save
from django_countries import CountryField

from .fields import IPNetworkField
from main.utils import set_created_field


class Mirror(models.Model):
    TIER_CHOICES = (
        (0, 'Tier 0'),
        (1, 'Tier 1'),
        (2, 'Tier 2'),
        (-1, 'Untiered'),
    )

    name = models.CharField(max_length=255, unique=True)
    tier = models.SmallIntegerField(default=2, choices=TIER_CHOICES)
    upstream = models.ForeignKey('self', null=True, on_delete=models.SET_NULL)
    admin_email = models.EmailField(max_length=255, blank=True)
    alternate_email = models.EmailField(max_length=255, blank=True)
    public = models.BooleanField(default=True)
    active = models.BooleanField(default=True)
    isos = models.BooleanField("ISOs", default=True)
    rsync_user = models.CharField(max_length=50, blank=True, default='')
    rsync_password = models.CharField(max_length=50, blank=True, default='')
    bug = models.PositiveIntegerField("Flyspray bug", null=True, blank=True)
    notes = models.TextField(blank=True)
    created = models.DateTimeField(editable=False)

    class Meta:
        ordering = ('name',)

    def __unicode__(self):
        return self.name

    def downstream(self):
        return Mirror.objects.filter(upstream=self).order_by('name')

    def get_absolute_url(self):
        return '/mirrors/%s/' % self.name


class MirrorProtocol(models.Model):
    protocol = models.CharField(max_length=10, unique=True)
    is_download = models.BooleanField(default=True,
            help_text="Is protocol useful for end-users, e.g. HTTP")
    default = models.BooleanField(default=True,
            help_text="Included by default when building mirror list?")
    created = models.DateTimeField(editable=False)

    def __unicode__(self):
        return self.protocol

    class Meta:
        ordering = ('protocol',)


class MirrorUrl(models.Model):
    url = models.CharField("URL", max_length=255, unique=True)
    protocol = models.ForeignKey(MirrorProtocol, related_name="urls",
            editable=False, on_delete=models.PROTECT)
    mirror = models.ForeignKey(Mirror, related_name="urls")
    country = CountryField(blank=True, db_index=True)
    has_ipv4 = models.BooleanField("IPv4 capable", default=True,
            editable=False)
    has_ipv6 = models.BooleanField("IPv6 capable", default=False,
            editable=False)
    created = models.DateTimeField(editable=False)
    active = models.BooleanField(default=True)

    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:
            # Auto-map the protocol field by looking at the URL
            protocol = urlparse(self.url).scheme
            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:
            # 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

    class Meta:
        verbose_name = 'mirror URL'


class MirrorRsync(models.Model):
    # max length is 40 chars for full-form IPv6 addr + subnet
    ip = IPNetworkField("IP")
    mirror = models.ForeignKey(Mirror, related_name="rsync_ips")
    created = models.DateTimeField(editable=False)

    def __unicode__(self):
        return self.ip

    class Meta:
        verbose_name = 'mirror rsync IP'
        ordering = ('ip',)


class CheckLocation(models.Model):
    hostname = models.CharField(max_length=255)
    source_ip = models.GenericIPAddressField(verbose_name='source IP',
            unpack_ipv4=True, unique=True)
    country = CountryField()
    created = models.DateTimeField(editable=False)

    class Meta:
        ordering = ('hostname', 'source_ip')

    def __unicode__(self):
        return self.hostname

    @property
    def family(self):
        info = socket.getaddrinfo(self.source_ip, None, 0, 0, 0,
                socket.AI_NUMERICHOST)
        families = [x[0] for x in info]
        return families[0]

    @property
    def ip_version(self):
        '''Returns integer '4' or '6'.'''
        if self.family == socket.AF_INET6:
            return 6
        if self.family == socket.AF_INET:
            return 4
        return None


class MirrorLog(models.Model):
    url = models.ForeignKey(MirrorUrl, related_name="logs")
    location = models.ForeignKey(CheckLocation, related_name="logs", null=True)
    check_time = models.DateTimeField(db_index=True)
    last_sync = models.DateTimeField(null=True)
    duration = models.FloatField(null=True)
    is_success = models.BooleanField(default=True)
    error = models.TextField(blank=True, default='')

    def delay(self):
        if self.last_sync is None:
            return None
        # sanity check, this shouldn't happen
        if self.check_time < self.last_sync:
            return timedelta()
        return self.check_time - self.last_sync

    def __unicode__(self):
        return "Check of %s at %s" % (self.url.url, self.check_time)

    class Meta:
        verbose_name = 'mirror check log'
        get_latest_by = 'check_time'


for model in (Mirror, MirrorProtocol, MirrorUrl, MirrorRsync, CheckLocation):
    pre_save.connect(set_created_field, sender=model,
            dispatch_uid="mirrors.models")

# vim: set ts=4 sw=4 et: