offline jobber cat django djangodns djadns models py from django db im

  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
offline@jobber ~]$ cat django/djangodns/djadns/models.py
from django.db import models
from django.contrib.auth.models import User
from datetime import datetime
from django.conf import settings
from djadns.managers import CreateConfig
import re
# Create your models here.
NS_RECORD_CHOICES = [('A','"A" - address'), ('NS','"NS" - nameserver'),
('CNAME','"CNAME" - canonical name'), ('P TR','"PTR" - domain name pointer'),
('MX','"MX" - mail exchan ge'), ('TXT','"TXT" - text strings')]
NS_CLASS_CHOICES = [('IN','"IN" - the Internet')]
NS_TIME_CHOICES = [(900, '15 minutes'), (1800, '30 minutes'), (3600, 'Hour'), (7200, '2 hours'),
(14400, '4 hours'), (28800, '8 hours'), (86400, '1 Day'), (259200, '3 days'),
(604800, 'Week')]
def check_for_dot(self, test_value):
test_value = str(test_value)
digit_regex = re.compile(".*\d$")
if not digit_regex.match(test_value):
if not test_value.endswith("."):
test_value += "."
return test_value
class Domain(models.Model):
address = models.CharField(help_text='example: mysite.com', max_length=100) # need custom validation
owner = models.ForeignKey(User)
soa = models.CharField(help_text='authoritive ns server, example: ns1.mysite.com', max_length=100)
email = models.EmailField(help_text="Owner's email")
serial = models.IntegerField(editable=False)
refresh = models.IntegerField(help_text="How freqently slave server should check for changes", choices=NS_TIME_CHOICES)
retry = models.IntegerField(help_text="How frequently slave should try query master if it doesn't response", choices=NS_TIME_CHOICES)
expiry = models.IntegerField(help_text="When client should renew information about domain", choices=NS_TIME_CHOICES)
minimum = models.IntegerField(help_text="Minimum time for failed lookups", choices=NS_TIME_CHOICES)
objects = models.Manager()
create_config = CreateConfig()
def save(self):
if self.id:
self.serial + 1
else:
_now = datetime.now()
self.serial = _now.strftime("%Y%m%d")
super(Domain, self).save()
def __unicode__(self):
return self.address
class Admin:
list_display = ['address', 'owner', 'soa']
class Zone(models.Model):
domain = models.ForeignKey(Domain, help_text="<b>Record looks like:</b><br /> \
example.com IN A 212.9.32.11<br /><b>Where:</b><br />example.com - source\
<br />IN - class<br />A - record type<br />212.9.32.11 - Destination")
source = models.CharField(max_length=100)
ns_class = models.CharField(max_length=10, choices=NS_CLASS_CHOICES)
ns_record = models.CharField(max_length=10, choices=NS_RECORD_CHOICES)
destination = models.CharField(max_length=100)
def save(self):
domain = self.domain
super(Zone, self).save()
zonefile = open("/home/offline/django/djangodns/djadns/var/named/%sdb" % self.domain.address, "w")
zonefile.write("""$TTL %s
%s IN SOA %s %s (
\t%s ; Serial Number
\t%s ; Refresh
\t%s ; Retry
\t%s ; Expire
\t%s ) ; Minimum\n""".replace(" ", "") % (domain.expiry, domain.address, domain.soa, domain.email,
domain.serial, domain.refresh, domain.retry, domain.expiry, domain.minimum))
for zone in domain.zone_set.all():
zonefile.write("""%s %s %s %s\n""".replace(" ", "") % (check_for_dot(zone.source), zone.ns_class,
zone.ns_record, check_for_dot(zone.destination)))
zonefile.close()
class Admin:
list_display = ['domain', 'ns_record', 'source', 'destination']
[offline@jobber ~]$ cat django/djangodns/djadns/managers.py
from django.db import models
from django.conf import settings
class CreateConfig(models.Manager):
def create_conf(self):
named_conf = open("/home/offline/django/djangodns/djadns/etc/named.conf", "w")
named_conf.write("options {\n")
for k, v in settings.BIND_OPTIONS.items():
named_conf.write("\t%s '%s';\n" % (k, v))
named_conf.write("};\n")
domains = super(CreateConfig, self).get_query_set()
for domain in domains:
named_conf.write("""zone '%s' IN {
\ttype master;
\tfile '%s.db';
};\n
""".replace(" ", "") % (domain.address, domain.address))
named_conf.close()
[offline@jobber ~]$