mirror of
				https://github.com/peeringdb/peeringdb.git
				synced 2024-05-11 05:55:09 +00:00 
			
		
		
		
	* use new peeringdb client (1.0.0) for pdb_load_data sync (#599) * drop django-mobi for lack of py3/dj2 support (#492) remove django-forms-bootstrap for lack of py3/dj2 support (#492) * black formatted * django2.2 and py3 upgrade (#492) * drop ixlans (#21) ui and api changes * drop local_asn (#168) * org search (#193) * phone number validation (#50) * implement help text tooltips (#228) * Mark own ASN as transit-free (#394) * py3 fix for `pdb_migrate_ixlans` command when writing migration report * pdb_migrate_ixlans: properly handle py3 Runtime error if ixlan dict changes during iteration * set rest DEFAULT_SCHEMA_CLASS to coreapi to fix swagger apidocs fix migration 0027 missing from facsimile manifest * fix swagger doc strings * fix tests that were broken from api doc fixes * fix UniqueFieldValidator for netixlan ipaddress validation that broke during django/drf upgrade * fix org merge tool layout issues * travis config * update pipfile and lock * black formatting * update travis dist * beta mode banner (#411) * add beta banner template (#411) * automatically scheduled sync may not always be on, add a flag that lets us reflect that state in the beta banner message clean up beta banner implementation (#411) * add tests for beta banner (#411)
		
			
				
	
	
		
			111 lines
		
	
	
		
			3.7 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
			
		
		
	
	
			111 lines
		
	
	
		
			3.7 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
| import ipaddress
 | |
| 
 | |
| from django.core.management.base import BaseCommand
 | |
| from django.core.exceptions import ValidationError
 | |
| import reversion
 | |
| 
 | |
| from peeringdb_server.models import IXLanPrefix, NetworkIXLan
 | |
| from peeringdb_server.inet import renumber_ipaddress
 | |
| 
 | |
| 
 | |
| class Command(BaseCommand):
 | |
|     help = "Renumber addresses, by providing the first three octects of a current ip4 address and the first three octets to change to."
 | |
| 
 | |
|     def add_arguments(self, parser):
 | |
|         parser.add_argument(
 | |
|             "--commit",
 | |
|             action="store_true",
 | |
|             help="commit changes, otherwise run in pretend mode",
 | |
|         )
 | |
|         parser.add_argument(
 | |
|             "--ixlan",
 | |
|             default=0,
 | |
|             help="ixlan id, if set only renumber matches in this specific ixlan",
 | |
|         )
 | |
|         parser.add_argument("ix", nargs="?", type=int)
 | |
|         parser.add_argument("old", nargs="?", type=str)
 | |
|         parser.add_argument("new", nargs="?", type=str)
 | |
| 
 | |
|     def log(self, msg):
 | |
|         if not self.commit:
 | |
|             self.stdout.write("[pretend] {}".format(msg))
 | |
|         else:
 | |
|             self.stdout.write(msg)
 | |
| 
 | |
|     @reversion.create_revision()
 | |
|     def renumber_lans(self, old, new):
 | |
|         """
 | |
|         Renumber prefix and netixlan's that fall into that prefix
 | |
|         """
 | |
| 
 | |
|         old_prefix = ipaddress.ip_network(old)
 | |
|         new_prefix = ipaddress.ip_network(new)
 | |
| 
 | |
|         if old_prefix.version != new_prefix.version:
 | |
|             self.log(
 | |
|                 "[error] {}".format(
 | |
|                     "New prefix needs to be of same " "protocol as old prefix"
 | |
|                 )
 | |
|             )
 | |
| 
 | |
|         prefixes = IXLanPrefix.objects.filter(
 | |
|             prefix=old, ixlan__ix_id=self.ix, status="ok"
 | |
|         )
 | |
|         netixlans = NetworkIXLan.objects.filter(ixlan__ix_id=self.ix, status="ok")
 | |
| 
 | |
|         if self.ixlan:
 | |
|             self.log("Only replacing in ixlan {}".format(self.ixlan.descriptive_name))
 | |
|             prefixes = prefixes.filter(ixlan=ixlan)
 | |
|             netixlans = netixlans.filter(ixlan=ixlan)
 | |
| 
 | |
|         for prefix in prefixes:
 | |
|             self.log("Renumbering {} -> {}".format(prefix.descriptive_name, new_prefix))
 | |
|             prefix.prefix = new_prefix
 | |
|             prefix.full_clean()
 | |
|             if self.commit:
 | |
|                 prefix.save()
 | |
| 
 | |
|         for netixlan in netixlans:
 | |
|             old_addr = netixlan.ipaddr(old_prefix.version)
 | |
| 
 | |
|             try:
 | |
|                 new_addr = renumber_ipaddress(old_addr, old_prefix, new_prefix)
 | |
|             except Exception as exc:
 | |
|                 self.log("[error] {}: {}".format(old_addr, exc))
 | |
|                 continue
 | |
| 
 | |
|             self.log(
 | |
|                 "Renumbering {} -> {}".format(
 | |
|                     netixlan.descriptive_name_ipv(new_addr.version), new_addr
 | |
|                 )
 | |
|             )
 | |
| 
 | |
|             if new_addr.version == 4:
 | |
|                 netixlan.ipaddr4 = new_addr
 | |
|             else:
 | |
|                 netixlan.ipaddr6 = new_addr
 | |
| 
 | |
|             try:
 | |
|                 netixlan.full_clean()
 | |
|             except ValidationError as exc:
 | |
|                 if not self.commit and str(exc).find("outside of prefix") > -1:
 | |
|                     continue
 | |
|                 else:
 | |
|                     self.log("[error] could not renumber {}: {}".format(old_addr, exc))
 | |
|                     continue
 | |
|             except Exception as exc:
 | |
|                 self.log("[error] could not renumber {}: {}".format(old_addr, exc))
 | |
|                 continue
 | |
| 
 | |
|             if self.commit:
 | |
|                 netixlan.save()
 | |
| 
 | |
|     def handle(self, *args, **options):
 | |
|         self.commit = options.get("commit", False)
 | |
|         self.ixlan = int(options.get("ixlan", 0))
 | |
|         self.ix = int(options.get("ix", 0))
 | |
|         old = "{}".format(options.get("old"))
 | |
|         new = "{}".format(options.get("new"))
 | |
| 
 | |
|         self.renumber_lans(old, new)
 |