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, OriginServer |
| 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 SyncOriginServer(SyncStep, HpcLibrary): |
| 20 | provides=[OriginServer] |
| 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_ors_ids = [x["origin_server_id"] for x in self.client.onev.ListAll("OriginServer")] |
| 35 | for ors in OriginServer.objects.all(): |
| 36 | if (ors.origin_server_id is not None) and (ors.origin_server_id not in all_ors_ids): |
| 37 | # we have an origin server ID, but it doesn't exist in the CMI |
| 38 | # something went wrong |
| 39 | # start over |
| 40 | logger.info("origin server %s was not found on CMI" % ors.origin_server_id) |
| 41 | ors.origin_server_id=None |
| 42 | ors.save() |
| 43 | |
Scott Baker | 88be16c | 2014-01-06 23:52:26 -0800 | [diff] [blame] | 44 | def sync_record(self, ors): |
| 45 | logger.info("sync'ing origin server %s" % str(ors)) |
| 46 | |
| 47 | if (not ors.contentProvider) or (not ors.contentProvider.content_provider_id): |
| 48 | return |
| 49 | |
| 50 | cpid = ors.contentProvider.content_provider_id |
| 51 | |
| 52 | # validation requires URL start with http:// |
| 53 | url = ors.url |
| 54 | if not url.startswith("http://"): |
| 55 | url = "http://" + url |
| 56 | |
| 57 | ors_dict = {"authenticated_content": ors.authenticated, "zone_redirects": ors.redirects, "content_provider_id": cpid, "url": url, "service_type": "HyperCache", "caching_type": "Optimistic", "description": ors.description} |
| 58 | |
| 59 | #print os_dict |
| 60 | |
| 61 | if not ors.origin_server_id: |
| 62 | id = self.client.onev.Create("OriginServer", ors_dict) |
| 63 | ors.origin_server_id = id |
| 64 | else: |
Scott Baker | 3ea1127 | 2014-09-23 17:24:42 -0700 | [diff] [blame] | 65 | self.client.onev.Update("OriginServer", ors.origin_server_id, ors_dict) |
Scott Baker | 88be16c | 2014-01-06 23:52:26 -0800 | [diff] [blame] | 66 | |
| 67 | # ... something breaks (analytics) if the URL starts with http://, so we |
| 68 | # change it in cob after we added it via onev. |
| 69 | url = url[7:] |
| 70 | self.client.cob.UpdateContent(ors.origin_server_id, {"url": url}) |
| 71 | |
| 72 | ors.save() |
Sapan Bhatia | 1b9051a | 2014-07-23 10:25:00 -0400 | [diff] [blame] | 73 | |
| 74 | def delete(self, m): |
| 75 | self.client.onev.Delete("OriginServer", m.origin_server_id) |