blob: 55b2326cecb45fbfb8b0ed6eb8a2cd7b8f2b8e9d [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):
28 self.sanity_check()
29
30 return SyncStep.fetch_pending(self, deleted)
31
32 def sanity_check(self):
33 # sanity check to make sure our PS objects have CMI objects behind them
34 all_p_ids = [x["cdn_prefix_id"] for x in self.client.onev.ListAll("CDNPrefix")]
Scott Baker3ca5f2b2014-09-26 00:49:55 -070035
36 all_p_ids = []
37 all_origins = {}
38 for x in self.client.onev.ListAll("CDNPrefix"):
39 id = x["cdn_prefix_id"]
40 all_p_ids.append(id)
41 all_origins[id] = x.get("default_origin_server", None)
42
Scott Baker897eadc2014-09-23 17:23:02 -070043 for p in CDNPrefix.objects.all():
Scott Baker3ca5f2b2014-09-26 00:49:55 -070044 if (p.cdn_prefix_id is None):
45 continue
46
47 if (p.cdn_prefix_id not in all_p_ids):
Scott Baker897eadc2014-09-23 17:23:02 -070048 logger.info("CDN Prefix %s was not found on CMI" % p.cdn_prefix_id)
49 p.cdn_prefix_id=None
50 p.save()
51
Scott Baker3ca5f2b2014-09-26 00:49:55 -070052 if (p.defaultOriginServer!=None) and (all_origins.get(p.cdn_prefix_id,None) != p.defaultOriginServer.url):
53 logger.info("CDN Prefix %s does not have default origin server on CMI" % str(p))
54 p.save() # this will set updated>enacted and force run
55
Scott Baker88be16c2014-01-06 23:52:26 -080056 def sync_record(self, cp):
57 logger.info("sync'ing cdn prefix %s" % str(cp))
58
59 if (not cp.contentProvider) or (not cp.contentProvider.content_provider_id):
60 return
61
62 cpid = cp.contentProvider.content_provider_id
63
64 cp_dict = {"service": "HyperCache", "enabled": cp.enabled, "content_provider_id": cpid, "cdn_prefix": cp.prefix}
65
Scott Baker3ca5f2b2014-09-26 00:49:55 -070066 if cp.defaultOriginServer and cp.defaultOriginServer.url:
67 if (not cp.defaultOriginServer.origin_server_id):
68 # It's probably a bad idea to try to set defaultOriginServer before
69 # we've crated defaultOriginServer.
70 logger.info(" cdn prefix %s is waiting for it's default origin server to get an id" % str(cp))
71 return
72
Scott Baker9713d202014-08-26 14:38:25 -070073 cp_dict["default_origin_server"] = cp.defaultOriginServer.url
74
Scott Baker88be16c2014-01-06 23:52:26 -080075 #print cp_dict
76
77 if not cp.cdn_prefix_id:
78 id = self.client.onev.Create("CDNPrefix", cp_dict)
79 cp.cdn_prefix_id = id
80 else:
Scott Baker3ca5f2b2014-09-26 00:49:55 -070081 del cp_dict["content_provider_id"] # this can't be updated
82 del cp_dict["cdn_prefix"] # this can't be updated either
Scott Baker88be16c2014-01-06 23:52:26 -080083 self.client.onev.Update("CDNPrefix", cp.cdn_prefix_id, cp_dict)
84
85 cp.save()
Sapan Bhatia6ac3ecc2014-07-23 10:12:42 -040086
87 def delete_record(self, m):
Scott Baker82c0bed2014-09-23 22:53:51 -070088 if m.cdn_prefix_id is not None:
89 self.client.onev.Delete("CDNPrefix", m.cdn_prefix_id)