1
0
mirror of https://github.com/peeringdb/peeringdb.git synced 2024-05-11 05:55:09 +00:00
Files
peeringdb-peeringdb/peeringdb_server/management/commands/pdb_fac_merge_undo.py
Matt Griswold ba6f9b6432 Qu1003 (#621)
* 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)
2020-01-08 13:29:58 -06:00

132 lines
5.2 KiB
Python

import re
import reversion
from django.core.management.base import BaseCommand
from peeringdb_server.models import (
CommandLineTool,
Facility,
NetworkFacility,
InternetExchangeFacility,
)
class Command(BaseCommand):
help = (
"Undo a facility merge from merge log (either --log or --clt needs to provided)"
)
def add_arguments(self, parser):
parser.add_argument(
"--commit", action="store_true", help="will commit the fac merge"
)
parser.add_argument("--log", help="merge log file")
parser.add_argument(
"--clt",
help="command line tool instance - this allows you to undo if the command was run from the admin UI",
)
def log(self, msg):
if not self.commit:
self.stdout.write("[pretend] {}".format(msg))
else:
self.stdout.write(msg)
@reversion.create_revision()
def handle(self, *args, **options):
self.commit = options.get("commit", False)
self.log_file = options.get("log")
self.clt_id = options.get("clt")
if self.log_file:
with open(self.log_file, "r") as fh:
log = fh.readlines()
elif self.clt_id:
clt = CommandLineTool.objects.get(id=self.clt_id, tool="pdb_fac_merge")
log = clt.result.split("\n")
else:
self.log("[error] no suitable log provided")
return
regex_facilities = "Merging facilities (.+) -> (\d+)"
regex_netfac = " - netfac NetworkFacility-netfac(\d+)$"
regex_ixfac = " - ixfac InternetExchangeFacility-ixfac(\d+)$"
regex_source = "Merging (.+) \((\d+)\) .."
regex_delete_netfac = "soft deleting NetworkFacility-netfac(\d+)"
regex_delete_ixfac = "soft deleting InternetExchangeFacility-ixfac(\d+)"
sources = {}
source = None
for line in log:
if re.match(regex_facilities, line):
match = re.match(regex_facilities, line)
sources = dict(
[
(fac.id, fac)
for fac in Facility.objects.filter(
id__in=match.group(1).split(", ")
)
]
)
target = Facility.objects.get(id=match.group(2))
for source in list(sources.values()):
if source.org.status != "ok":
self.log(
"[error] Parent organization {} of facility {} currently has status `{}`, as such the facility cannot be undeleted, please fix the organization and run the script again".format(
source.org, source, source.org.status
)
)
return
for source in list(sources.values()):
if source.status == "ok" and not self.commit:
self.log(
"[warning] Looks like this merge has already been undone one way or another, please double check before committing this command"
)
source.status = "ok"
self.log("Undeleting facility {} (#{})".format(source, source.id))
if self.commit:
source.save()
source = None
elif re.match(regex_source, line):
match = re.match(regex_source, line)
source = sources[int(match.group(2))]
self.log("======================")
self.log("Undoing merge {} (#{})".format(source, source.id))
elif re.match(regex_netfac, line):
match = re.match(regex_netfac, line)
netfac = NetworkFacility.objects.get(id=match.group(1))
netfac.status = "ok"
netfac.facility = source
self.log("Undoing network facility merge (#{})".format(netfac.id))
if self.commit:
netfac.save()
elif re.match(regex_delete_netfac, line):
match = re.match(regex_delete_netfac, line)
netfac = NetworkFacility.objects.get(id=match.group(1))
netfac.status = "ok"
self.log("Undoing network facility deletion (#{})".format(netfac.id))
if self.commit:
netfac.save()
elif re.match(regex_ixfac, line):
match = re.match(regex_ixfac, line)
ixfac = InternetExchangeFacility.objects.get(id=match.group(1))
ixfac.status = "ok"
ixfac.facility = source
self.log("Undoing ix facility merge (#{})".format(ixfac.id))
if self.commit:
ixfac.save()
elif re.match(regex_delete_ixfac, line):
match = re.match(regex_delete_ixfac, line)
ixfac = InternetExchangeFacility.objects.get(id=match.group(1))
ixfac.status = "ok"
self.log("Undoing ix facility deletion (#{})".format(ixfac.id))
if self.commit:
ixfac.save()