blob: 4f0ba4ce915728f54c4d1304ed830b4380e1dd6d [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, OriginServer
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 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 Baker88be16c2014-01-06 23:52:26 -080027 def sync_record(self, ors):
28 logger.info("sync'ing origin server %s" % str(ors))
29
30 if (not ors.contentProvider) or (not ors.contentProvider.content_provider_id):
31 return
32
33 cpid = ors.contentProvider.content_provider_id
34
35 # validation requires URL start with http://
36 url = ors.url
37 if not url.startswith("http://"):
38 url = "http://" + url
39
40 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}
41
42 #print os_dict
43
44 if not ors.origin_server_id:
45 id = self.client.onev.Create("OriginServer", ors_dict)
46 ors.origin_server_id = id
47 else:
48 self.client.onev.Update("OriginServer", ors.origin_server_id, ors_dict)
49
50 # ... something breaks (analytics) if the URL starts with http://, so we
51 # change it in cob after we added it via onev.
52 url = url[7:]
53 self.client.cob.UpdateContent(ors.origin_server_id, {"url": url})
54
55 ors.save()
Sapan Bhatia1b9051a2014-07-23 10:25:00 -040056
57 def delete(self, m):
58 self.client.onev.Delete("OriginServer", m.origin_server_id)