blob: d18c6726bb4321c244ae29a805746bdf08de00ba [file] [log] [blame]
Scott Baker88be16c2014-01-06 23:52:26 -08001import os
2import sys
3import base64
Scott Bakera79222f2014-09-30 11:31:56 -07004
Scott Baker88be16c2014-01-06 23:52:26 -08005from django.db.models import F, Q
6from planetstack.config import Config
7from observer.syncstep import SyncStep
8from core.models import Service
9from hpc.models import ServiceProvider, ContentProvider, CDNPrefix, OriginServer
10from util.logger import Logger, logging
11
12# hpclibrary will be in steps/..
13parentdir = os.path.join(os.path.dirname(__file__),"..")
14sys.path.insert(0,parentdir)
15
16from hpclib import HpcLibrary
17
18logger = Logger(level=logging.INFO)
19
20class SyncOriginServer(SyncStep, HpcLibrary):
21 provides=[OriginServer]
22 requested_interval=0
23
24 def __init__(self, **args):
25 SyncStep.__init__(self, **args)
26 HpcLibrary.__init__(self)
27
Scott Baker217c8ed2014-09-23 17:23:02 -070028 def fetch_pending(self, deleted):
Scott Bakera79222f2014-09-30 11:31:56 -070029 #self.consistency_check()
Scott Baker217c8ed2014-09-23 17:23:02 -070030
31 return SyncStep.fetch_pending(self, deleted)
32
Scott Bakera79222f2014-09-30 11:31:56 -070033 def consistency_check(self):
34 # set to true if something changed
35 result=False
36
Scott Baker217c8ed2014-09-23 17:23:02 -070037 # sanity check to make sure our PS objects have CMI objects behind them
38 all_ors_ids = [x["origin_server_id"] for x in self.client.onev.ListAll("OriginServer")]
39 for ors in OriginServer.objects.all():
40 if (ors.origin_server_id is not None) and (ors.origin_server_id not in all_ors_ids):
41 # we have an origin server ID, but it doesn't exist in the CMI
42 # something went wrong
43 # start over
44 logger.info("origin server %s was not found on CMI" % ors.origin_server_id)
45 ors.origin_server_id=None
46 ors.save()
Scott Bakera79222f2014-09-30 11:31:56 -070047 result = True
48
49 return result
Scott Baker217c8ed2014-09-23 17:23:02 -070050
Scott Baker88be16c2014-01-06 23:52:26 -080051 def sync_record(self, ors):
52 logger.info("sync'ing origin server %s" % str(ors))
53
54 if (not ors.contentProvider) or (not ors.contentProvider.content_provider_id):
55 return
56
57 cpid = ors.contentProvider.content_provider_id
58
59 # validation requires URL start with http://
60 url = ors.url
61 if not url.startswith("http://"):
62 url = "http://" + url
63
64 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}
65
66 #print os_dict
67
68 if not ors.origin_server_id:
69 id = self.client.onev.Create("OriginServer", ors_dict)
70 ors.origin_server_id = id
71 else:
Scott Baker432330c2014-09-23 17:24:42 -070072 self.client.onev.Update("OriginServer", ors.origin_server_id, ors_dict)
Scott Baker88be16c2014-01-06 23:52:26 -080073
74 # ... something breaks (analytics) if the URL starts with http://, so we
75 # change it in cob after we added it via onev.
76 url = url[7:]
77 self.client.cob.UpdateContent(ors.origin_server_id, {"url": url})
78
Scott Baker456c8262014-09-23 22:53:51 -070079 ors.silent = True
Scott Baker88be16c2014-01-06 23:52:26 -080080 ors.save()
Sapan Bhatia1b9051a2014-07-23 10:25:00 -040081
Scott Baker456c8262014-09-23 22:53:51 -070082 def delete_record(self, m):
83 if m.origin_server_id is not None:
84 self.client.onev.Delete("OriginServer", m.origin_server_id)