sync-from-pdns.py 1.4 KB

123456789101112131415161718192021222324252627282930313233343536
  1. from django.core.management import BaseCommand, CommandError
  2. from desecapi.models import Domain
  3. class Command(BaseCommand):
  4. help = 'Import authoritative data from pdns, making the local database consistent with pdns.'
  5. def add_arguments(self, parser):
  6. parser.add_argument('domain-name', nargs='*',
  7. help='Domain name to import. If omitted, will import all domains that are known locally.')
  8. def handle(self, *args, **options):
  9. domains = Domain.objects.all()
  10. if options['domain-name']:
  11. domains = domains.filter(name__in=options['domain-name'])
  12. domain_names = domains.values_list('name', flat=True)
  13. for domain_name in options['domain-name']:
  14. if domain_name not in domain_names:
  15. raise CommandError('{} is not a known domain'.format(domain_name))
  16. for domain in domains:
  17. self.stdout.write('%s ...' % domain.name, ending='')
  18. try:
  19. domain.sync_from_pdns()
  20. self.stdout.write(' synced')
  21. except Exception as e:
  22. if str(e).startswith('Could not find domain ') \
  23. and domain.owner.locked:
  24. self.stdout.write(' skipped')
  25. else:
  26. self.stdout.write(' failed')
  27. msg = 'Error while processing {}: {}'.format(domain.name, e)
  28. raise CommandError(msg)