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 |
Scott Baker | 76a840e | 2015-02-11 21:38:09 -0800 | [diff] [blame] | 5 | from xos.config import Config |
Scott Baker | 88be16c | 2014-01-06 23:52:26 -0800 | [diff] [blame] | 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 | 217c8ed | 2014-09-23 17:23:02 -0700 | [diff] [blame] | 27 | def fetch_pending(self, deleted): |
Scott Baker | a79222f | 2014-09-30 11:31:56 -0700 | [diff] [blame] | 28 | #self.consistency_check() |
Scott Baker | 217c8ed | 2014-09-23 17:23:02 -0700 | [diff] [blame] | 29 | |
| 30 | return SyncStep.fetch_pending(self, deleted) |
| 31 | |
Scott Baker | a79222f | 2014-09-30 11:31:56 -0700 | [diff] [blame] | 32 | def consistency_check(self): |
| 33 | # set to true if something changed |
| 34 | result=False |
| 35 | |
Scott Baker | 217c8ed | 2014-09-23 17:23:02 -0700 | [diff] [blame] | 36 | # 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 Baker | c0382a4 | 2014-09-26 00:49:55 -0700 | [diff] [blame] | 38 | |
| 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 Baker | 217c8ed | 2014-09-23 17:23:02 -0700 | [diff] [blame] | 46 | for p in CDNPrefix.objects.all(): |
Scott Baker | c0382a4 | 2014-09-26 00:49:55 -0700 | [diff] [blame] | 47 | if (p.cdn_prefix_id is None): |
| 48 | continue |
| 49 | |
| 50 | if (p.cdn_prefix_id not in all_p_ids): |
Scott Baker | 217c8ed | 2014-09-23 17:23:02 -0700 | [diff] [blame] | 51 | logger.info("CDN Prefix %s was not found on CMI" % p.cdn_prefix_id) |
| 52 | p.cdn_prefix_id=None |
| 53 | p.save() |
Scott Baker | a79222f | 2014-09-30 11:31:56 -0700 | [diff] [blame] | 54 | result = True |
Scott Baker | 217c8ed | 2014-09-23 17:23:02 -0700 | [diff] [blame] | 55 | |
Scott Baker | c0382a4 | 2014-09-26 00:49:55 -0700 | [diff] [blame] | 56 | 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 Baker | a79222f | 2014-09-30 11:31:56 -0700 | [diff] [blame] | 58 | p.save() # this will set updated>enacted and force observer to re-sync |
| 59 | result = True |
| 60 | |
| 61 | return result |
Scott Baker | c0382a4 | 2014-09-26 00:49:55 -0700 | [diff] [blame] | 62 | |
Scott Baker | 88be16c | 2014-01-06 23:52:26 -0800 | [diff] [blame] | 63 | 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 Baker | c0382a4 | 2014-09-26 00:49:55 -0700 | [diff] [blame] | 73 | 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 Baker | 9713d20 | 2014-08-26 14:38:25 -0700 | [diff] [blame] | 80 | cp_dict["default_origin_server"] = cp.defaultOriginServer.url |
| 81 | |
Scott Baker | 88be16c | 2014-01-06 23:52:26 -0800 | [diff] [blame] | 82 | #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 Baker | c0382a4 | 2014-09-26 00:49:55 -0700 | [diff] [blame] | 88 | del cp_dict["content_provider_id"] # this can't be updated |
| 89 | del cp_dict["cdn_prefix"] # this can't be updated either |
Scott Baker | 88be16c | 2014-01-06 23:52:26 -0800 | [diff] [blame] | 90 | self.client.onev.Update("CDNPrefix", cp.cdn_prefix_id, cp_dict) |
| 91 | |
| 92 | cp.save() |
Sapan Bhatia | 6ac3ecc | 2014-07-23 10:12:42 -0400 | [diff] [blame] | 93 | |
| 94 | def delete_record(self, m): |
Scott Baker | 456c826 | 2014-09-23 22:53:51 -0700 | [diff] [blame] | 95 | if m.cdn_prefix_id is not None: |
| 96 | self.client.onev.Delete("CDNPrefix", m.cdn_prefix_id) |