blob: dfa6ef807e4bbbc40327c20a27dde7d382db2aaa [file] [log] [blame]
Scott Baker88be16c2014-01-06 23:52:26 -08001import os
2import sys
3import base64
4from django.db.models import F, Q
5from planetstack.config import Config
6from observer.syncstep import SyncStep
7from core.models import Service
8from hpc.models import ServiceProvider, ContentProvider, CDNPrefix
9from util.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 requested_interval=0
22
23 def __init__(self, **args):
24 SyncStep.__init__(self, **args)
25 HpcLibrary.__init__(self)
26
Scott Baker897eadc2014-09-23 17:23:02 -070027 def fetch_pending(self, deleted):
Scott Bakerb74f0042014-09-30 11:31:56 -070028 #self.consistency_check()
Scott Baker897eadc2014-09-23 17:23:02 -070029
30 return SyncStep.fetch_pending(self, deleted)
31
Scott Bakerb74f0042014-09-30 11:31:56 -070032 def consistency_check(self):
33 # set to true if something changed
34 result=False
35
Scott Baker897eadc2014-09-23 17:23:02 -070036 # sanity check to make sure our PS objects have CMI objects behind them
37 all_p_ids = [x["cdn_prefix_id"] for x in self.client.onev.ListAll("CDNPrefix")]
Scott Baker3ca5f2b2014-09-26 00:49:55 -070038
39 all_p_ids = []
40 all_origins = {}
41 for x in self.client.onev.ListAll("CDNPrefix"):
42 id = x["cdn_prefix_id"]
43 all_p_ids.append(id)
44 all_origins[id] = x.get("default_origin_server", None)
45
Scott Baker897eadc2014-09-23 17:23:02 -070046 for p in CDNPrefix.objects.all():
Scott Baker3ca5f2b2014-09-26 00:49:55 -070047 if (p.cdn_prefix_id is None):
48 continue
49
50 if (p.cdn_prefix_id not in all_p_ids):
Scott Baker897eadc2014-09-23 17:23:02 -070051 logger.info("CDN Prefix %s was not found on CMI" % p.cdn_prefix_id)
52 p.cdn_prefix_id=None
53 p.save()
Scott Bakerb74f0042014-09-30 11:31:56 -070054 result = True
Scott Baker897eadc2014-09-23 17:23:02 -070055
Scott Baker3ca5f2b2014-09-26 00:49:55 -070056 if (p.defaultOriginServer!=None) and (all_origins.get(p.cdn_prefix_id,None) != p.defaultOriginServer.url):
57 logger.info("CDN Prefix %s does not have default origin server on CMI" % str(p))
Scott Bakerb74f0042014-09-30 11:31:56 -070058 p.save() # this will set updated>enacted and force observer to re-sync
59 result = True
60
61 return result
Scott Baker3ca5f2b2014-09-26 00:49:55 -070062
Scott Baker88be16c2014-01-06 23:52:26 -080063 def sync_record(self, cp):
64 logger.info("sync'ing cdn prefix %s" % str(cp))
65
66 if (not cp.contentProvider) or (not cp.contentProvider.content_provider_id):
67 return
68
69 cpid = cp.contentProvider.content_provider_id
70
71 cp_dict = {"service": "HyperCache", "enabled": cp.enabled, "content_provider_id": cpid, "cdn_prefix": cp.prefix}
72
Scott Baker3ca5f2b2014-09-26 00:49:55 -070073 if cp.defaultOriginServer and cp.defaultOriginServer.url:
74 if (not cp.defaultOriginServer.origin_server_id):
75 # It's probably a bad idea to try to set defaultOriginServer before
76 # we've crated defaultOriginServer.
77 logger.info(" cdn prefix %s is waiting for it's default origin server to get an id" % str(cp))
78 return
79
Scott Baker9713d202014-08-26 14:38:25 -070080 cp_dict["default_origin_server"] = cp.defaultOriginServer.url
81
Scott Baker88be16c2014-01-06 23:52:26 -080082 #print cp_dict
83
84 if not cp.cdn_prefix_id:
85 id = self.client.onev.Create("CDNPrefix", cp_dict)
86 cp.cdn_prefix_id = id
87 else:
Scott Baker3ca5f2b2014-09-26 00:49:55 -070088 del cp_dict["content_provider_id"] # this can't be updated
89 del cp_dict["cdn_prefix"] # this can't be updated either
Scott Baker88be16c2014-01-06 23:52:26 -080090 self.client.onev.Update("CDNPrefix", cp.cdn_prefix_id, cp_dict)
91
92 cp.save()
Sapan Bhatia6ac3ecc2014-07-23 10:12:42 -040093
94 def delete_record(self, m):
Scott Baker82c0bed2014-09-23 22:53:51 -070095 if m.cdn_prefix_id is not None:
96 self.client.onev.Delete("CDNPrefix", m.cdn_prefix_id)