blob: a1d177bf177b4b9b1c22f69482de1743fd4ed22b [file] [log] [blame]
Scott Baker25467ff2016-08-04 09:50:22 -07001import os
2import sys
3import base64
4from django.db.models import F, Q
5from xos.config import Config
6from synchronizers.base.syncstep import SyncStep
7from core.models import Service
8from services.hpc.models import ServiceProvider, ContentProvider, CDNPrefix, SiteMap
9from xos.logger import Logger, logging
10
11# hpclibrary will be in steps/..
12parentdir = os.path.join(os.path.dirname(__file__),"..")
13sys.path.insert(0,parentdir)
14
15from hpclib import HpcLibrary
16
17logger = Logger(level=logging.INFO)
18
19class SyncSiteMap(SyncStep, HpcLibrary):
20 provides=[SiteMap]
21 observes=SiteMap
22 requested_interval=0
23
24 def __init__(self, **args):
25 SyncStep.__init__(self, **args)
26 HpcLibrary.__init__(self)
27
28 def filter_hpc_service(self, objs):
29 hpcService = self.get_hpc_service()
30
31 filtered_objs = []
32 for x in objs:
33 if ((x.hpcService == hpcService) or
34 ((x.serviceProvider != None) and (x.serviceProvider.hpcService == hpcService)) or
35 ((x.contentProvider != None) and (x.contentProvider.serviceProvider.hpcService == hpcService)) or
36 ((x.cdnPrefix!= None) and (x.cdnPrefix.contentProvider.serviceProvider.hpcService == hpcService))):
37 filtered_objs.append(x)
38
39 return filtered_objs
40
41 def fetch_pending(self, deleted):
42 return self.filter_hpc_service(SyncStep.fetch_pending(self, deleted))
43
44 def consistency_check(self):
45 # set to true if something changed
46 result=False
47
48 # sanity check to make sure our PS objects have CMI objects behind them
49 all_map_ids = [x["map_id"] for x in self.client.onev.ListAll("Map")]
50 for map in SiteMap.objects.all():
51 if (map.map_id is not None) and (map.map_id not in all_map_ids):
52 logger.info("Map %s was not found on CMI" % map.map_id,extra=map.tologdict())
53 map.map_id=None
54 map.save()
55 result = True
56
57 return result
58
59 def update_bind(self, map, map_dict, field_name, to_name, ids):
60 for id in ids:
61 if (not id in map_dict.get(field_name, [])):
62 print "Bind Map", map.map_id, "to", to_name, id
63 self.client.onev.Bind("Map", map.map_id, to_name, id)
64
65 for id in map_dict.get(field_name, []):
66 if (not id in ids):
67 print "Unbind Map", map.map_id, "from", to_name, id
68 self.client.onev.UnBind("map", map.map_id, to_name, id)
69
70 def sync_record(self, map):
71 logger.info("sync'ing SiteMap %s" % str(map),extra=map.tologdict())
72
73 if not map.map:
74 # no contents
75 return
76
77 content = map.map.read()
78
79 map_dict = {"name": map.name, "type": "site", "content": content}
80
81 cdn_prefix_ids=[]
82 service_provider_ids=[]
83 content_provider_ids=[]
84
85 if (map.contentProvider):
86 if not map.contentProvider.content_provider_id:
87 raise Exception("Map %s links to a contentProvider with no id" % map.name)
88 conent_provider_ids = [map.contentProvider.content_provider_id]
89
90 if (map.serviceProvider):
91 if not map.serviceProvider.service_provider_id:
92 raise Exception("Map %s links to a serviceProvider with no id" % map.name)
93 service_provider_ids = [map.serviceProvider.service_provider_id]
94
95 if (map.cdnPrefix):
96 if not map.cdnPrefix.cdn_prefix_id:
97 raise Exception("Map %s links to a cdnPrefix with no id" % map.name)
98 cdn_prefix_ids = [map.cdnPrefix.cdn_prefix_id]
99
100 if not map.map_id:
101 print "Create Map", map_dict
102 id = self.client.onev.Create("Map", map_dict)
103 map.map_id = id
104 else:
105 print "Update Map", map_dict
106 # these things we probably cannot update
107 del map_dict["name"]
108 self.client.onev.Update("Map", map.map_id, map_dict)
109
110 cmi_map_dict = self.client.onev.Read("Map", map.map_id)
111
112 self.update_bind(map, cmi_map_dict, "cdn_prefix_ids", "CDNPrefix", cdn_prefix_ids)
113
114 map.save()
115
116 def delete_record(self, m):
117 if m.map_id is not None:
118 self.client.onev.Delete("Map", m.map_id)