Scott Baker | 88be16c | 2014-01-06 23:52:26 -0800 | [diff] [blame] | 1 | import os |
| 2 | import sys |
| 3 | import base64 |
| 4 | from django.db.models import F, Q |
| 5 | from planetstack.config import Config |
| 6 | from observer.syncstep import SyncStep |
| 7 | from core.models import Service |
| 8 | from hpc.models import ServiceProvider, ContentProvider, CDNPrefix |
| 9 | from util.logger import Logger, logging |
| 10 | |
| 11 | # hpclibrary will be in steps/.. |
| 12 | parentdir = os.path.join(os.path.dirname(__file__),"..") |
| 13 | sys.path.insert(0,parentdir)
|
| 14 | |
| 15 | from hpclib import HpcLibrary |
| 16 | |
| 17 | logger = Logger(level=logging.INFO) |
| 18 | |
| 19 | class 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 Baker | 897eadc | 2014-09-23 17:23:02 -0700 | [diff] [blame] | 27 | 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 Baker | 3ca5f2b | 2014-09-26 00:49:55 -0700 | [diff] [blame] | 35 | |
| 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 Baker | 897eadc | 2014-09-23 17:23:02 -0700 | [diff] [blame] | 43 | for p in CDNPrefix.objects.all(): |
Scott Baker | 3ca5f2b | 2014-09-26 00:49:55 -0700 | [diff] [blame] | 44 | if (p.cdn_prefix_id is None): |
| 45 | continue |
| 46 | |
| 47 | if (p.cdn_prefix_id not in all_p_ids): |
Scott Baker | 897eadc | 2014-09-23 17:23:02 -0700 | [diff] [blame] | 48 | 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 Baker | 3ca5f2b | 2014-09-26 00:49:55 -0700 | [diff] [blame] | 52 | 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 Baker | 88be16c | 2014-01-06 23:52:26 -0800 | [diff] [blame] | 56 | 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 Baker | 3ca5f2b | 2014-09-26 00:49:55 -0700 | [diff] [blame] | 66 | 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 Baker | 9713d20 | 2014-08-26 14:38:25 -0700 | [diff] [blame] | 73 | cp_dict["default_origin_server"] = cp.defaultOriginServer.url |
| 74 | |
Scott Baker | 88be16c | 2014-01-06 23:52:26 -0800 | [diff] [blame] | 75 | #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 Baker | 3ca5f2b | 2014-09-26 00:49:55 -0700 | [diff] [blame] | 81 | del cp_dict["content_provider_id"] # this can't be updated |
| 82 | del cp_dict["cdn_prefix"] # this can't be updated either |
Scott Baker | 88be16c | 2014-01-06 23:52:26 -0800 | [diff] [blame] | 83 | self.client.onev.Update("CDNPrefix", cp.cdn_prefix_id, cp_dict) |
| 84 | |
| 85 | cp.save() |
Sapan Bhatia | 6ac3ecc | 2014-07-23 10:12:42 -0400 | [diff] [blame] | 86 | |
| 87 | def delete_record(self, m): |
Scott Baker | 82c0bed | 2014-09-23 22:53:51 -0700 | [diff] [blame] | 88 | if m.cdn_prefix_id is not None: |
| 89 | self.client.onev.Delete("CDNPrefix", m.cdn_prefix_id) |