-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathfix_inv_tree.py
55 lines (50 loc) · 2.29 KB
/
fix_inv_tree.py
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
# ---------------------------------------------------------------------
# Fix inventory tree
# ---------------------------------------------------------------------
# Copyright (C) 2007-2018 The NOC Project
# See LICENSE for details
# ---------------------------------------------------------------------
# Python modules
import logging
# NOC modules
from noc.inv.models.object import Object
from noc.inv.models.objectmodel import ObjectModel
from noc.core.comp import smart_text
def fix():
# check "Lost&Found" container
logging.info("Checking Lost&Found object")
lostfound_model = ObjectModel.objects.get(uuid="b0fae773-b214-4edf-be35-3468b53b03f2")
lf = Object.objects.filter(model=lostfound_model.id).count()
if lf == 0:
# Create missed "Lost&Found"
logging.info(" ... creating missed Lost&Found")
Object(model=lostfound_model.id, name="Global Lost&Found", container=None).save()
elif lf == 1:
logging.info("Global Lost&Found object found") # OK
# check container
if Object.objects.get(name="Global Lost&Found").container:
logging.info("Global Lost&Found object not valid container - fix") # fix
o = Object.objects.get(name="Global Lost&Found")
o.container = None
o.save()
else:
logging.info("Global Lost&Found object container is valid")
else:
logging.info("Global Lost&Found object found greater that one!!!!")
# merge Lost&found
lfs = Object.objects.filter(model=lostfound_model.id).order_by("id")
l0 = lfs[0]
for l in lfs[1:]:
for ls in Object.objects.filter(container=l.id):
logging.info(" ... moving %s to primary Lost&Found", smart_text(ls))
ls.container = l0
ls.save()
logging.info(" ... removing duplicated Lost&Found %s", l)
l.delete()
if Object.objects.get(name="Global Lost&Found").container:
logging.info("Global Lost&Found object not valid container - fix") # fix
o = Object.objects.get(name="Global Lost&Found")
o.container = None
o.save()
else:
logging.info("Global Lost&Found object container is valid")