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 | |
| 27 | def fetch_pending(self): |
| 28 | return OriginServer.objects.filter(Q(enacted__lt=F('updated')) | Q(enacted=None)) |
| 29 | |
| 30 | def sync_record(self, ors): |
| 31 | logger.info("sync'ing origin server %s" % str(ors)) |
| 32 | |
| 33 | if (not ors.contentProvider) or (not ors.contentProvider.content_provider_id): |
| 34 | return |
| 35 | |
| 36 | cpid = ors.contentProvider.content_provider_id |
| 37 | |
| 38 | # validation requires URL start with http:// |
| 39 | url = ors.url |
| 40 | if not url.startswith("http://"): |
| 41 | url = "http://" + url |
| 42 | |
| 43 | 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} |
| 44 | |
| 45 | #print os_dict |
| 46 | |
| 47 | if not ors.origin_server_id: |
| 48 | id = self.client.onev.Create("OriginServer", ors_dict) |
| 49 | ors.origin_server_id = id |
| 50 | else: |
| 51 | self.client.onev.Update("OriginServer", ors.origin_server_id, ors_dict) |
| 52 | |
| 53 | # ... something breaks (analytics) if the URL starts with http://, so we |
| 54 | # change it in cob after we added it via onev. |
| 55 | url = url[7:] |
| 56 | self.client.cob.UpdateContent(ors.origin_server_id, {"url": url}) |
| 57 | |
| 58 | ors.save() |