blob: eff3b5d44c726ae77eb0e66a53c9dc0d017811f3 [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
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 SyncCDNPrefix(SyncStep, HpcLibrary):
20 provides=[CDNPrefix]
21 observes=CDNPrefix
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 return [x for x in objs if x.contentProvider.serviceProvider.hpcService == hpcService]
32
33 def fetch_pending(self, deleted):
34 #self.consistency_check()
35
36 return self.filter_hpc_service(SyncStep.fetch_pending(self, deleted))
37
38 def consistency_check(self):
39 # set to true if something changed
40 result=False
41
42 # sanity check to make sure our PS objects have CMI objects behind them
43 all_p_ids = [x["cdn_prefix_id"] for x in self.client.onev.ListAll("CDNPrefix")]
44
45 all_p_ids = []
46 all_origins = {}
47 for x in self.client.onev.ListAll("CDNPrefix"):
48 id = x["cdn_prefix_id"]
49 all_p_ids.append(id)
50 all_origins[id] = x.get("default_origin_server", None)
51
52 for p in CDNPrefix.objects.all():
53 if (p.cdn_prefix_id is None):
54 continue
55
56 if (p.cdn_prefix_id not in all_p_ids):
57 logger.info("CDN Prefix %s was not found on CMI" % p.cdn_prefix_id)
58 p.cdn_prefix_id=None
59 p.save()
60 result = True
61
62 if (p.defaultOriginServer!=None) and (all_origins.get(p.cdn_prefix_id,None) != p.defaultOriginServer.url):
63 logger.info("CDN Prefix %s does not have default origin server on CMI" % str(p))
64 p.save() # this will set updated>enacted and force observer to re-sync
65 result = True
66
67 return result
68
69 def sync_record(self, cp):
70 logger.info("sync'ing cdn prefix %s" % str(cp),extra=cp.tologdict())
71
72 if (not cp.contentProvider) or (not cp.contentProvider.content_provider_id):
73 raise Exception("CDN Prefix %s is linked to a contentProvider without an id" % str(cp))
74
75 cpid = cp.contentProvider.content_provider_id
76
77 cp_dict = {"service": "HyperCache", "enabled": cp.enabled, "content_provider_id": cpid, "cdn_prefix": cp.prefix}
78
79 if cp.defaultOriginServer and cp.defaultOriginServer.url:
80 if (not cp.defaultOriginServer.origin_server_id):
81 # It's probably a bad idea to try to set defaultOriginServer before
82 # we've crated defaultOriginServer.
83 raise Exception("cdn prefix %s is waiting for it's default origin server to get an id" % str(cp))
84
85 cp_dict["default_origin_server"] = cp.defaultOriginServer.url
86
87 #print cp_dict
88
89 if not cp.cdn_prefix_id:
90 id = self.client.onev.Create("CDNPrefix", cp_dict)
91 cp.cdn_prefix_id = id
92 else:
93 del cp_dict["content_provider_id"] # this can't be updated
94 del cp_dict["cdn_prefix"] # this can't be updated either
95 self.client.onev.Update("CDNPrefix", cp.cdn_prefix_id, cp_dict)
96
97 cp.save()
98
99 def delete_record(self, m):
100 if m.cdn_prefix_id is not None:
101 self.client.onev.Delete("CDNPrefix", m.cdn_prefix_id)