Scott Baker | 88be16c | 2014-01-06 23:52:26 -0800 | [diff] [blame] | 1 | import os |
| 2 | import sys |
| 3 | import base64 |
Scott Baker | b74f004 | 2014-09-30 11:31:56 -0700 | [diff] [blame] | 4 | |
Scott Baker | 88be16c | 2014-01-06 23:52:26 -0800 | [diff] [blame] | 5 | from django.db.models import F, Q |
| 6 | from planetstack.config import Config |
| 7 | from observer.syncstep import SyncStep |
| 8 | from core.models import Service |
| 9 | from hpc.models import ServiceProvider, ContentProvider, CDNPrefix, OriginServer |
| 10 | from util.logger import Logger, logging |
| 11 | |
| 12 | # hpclibrary will be in steps/.. |
| 13 | parentdir = os.path.join(os.path.dirname(__file__),"..") |
| 14 | sys.path.insert(0,parentdir)
|
| 15 | |
| 16 | from hpclib import HpcLibrary |
| 17 | |
| 18 | logger = Logger(level=logging.INFO) |
| 19 | |
| 20 | class 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 Baker | 897eadc | 2014-09-23 17:23:02 -0700 | [diff] [blame] | 28 | def fetch_pending(self, deleted): |
Scott Baker | b74f004 | 2014-09-30 11:31:56 -0700 | [diff] [blame] | 29 | #self.consistency_check() |
Scott Baker | 897eadc | 2014-09-23 17:23:02 -0700 | [diff] [blame] | 30 | |
| 31 | return SyncStep.fetch_pending(self, deleted) |
| 32 | |
Scott Baker | b74f004 | 2014-09-30 11:31:56 -0700 | [diff] [blame] | 33 | def consistency_check(self): |
| 34 | # set to true if something changed |
| 35 | result=False |
| 36 | |
Scott Baker | 897eadc | 2014-09-23 17:23:02 -0700 | [diff] [blame] | 37 | # 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 Baker | b74f004 | 2014-09-30 11:31:56 -0700 | [diff] [blame] | 47 | result = True |
| 48 | |
| 49 | return result |
Scott Baker | 897eadc | 2014-09-23 17:23:02 -0700 | [diff] [blame] | 50 | |
Scott Baker | 88be16c | 2014-01-06 23:52:26 -0800 | [diff] [blame] | 51 | 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 Baker | 3ea1127 | 2014-09-23 17:24:42 -0700 | [diff] [blame] | 72 | self.client.onev.Update("OriginServer", ors.origin_server_id, ors_dict) |
Scott Baker | 88be16c | 2014-01-06 23:52:26 -0800 | [diff] [blame] | 73 | |
| 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 Baker | 82c0bed | 2014-09-23 22:53:51 -0700 | [diff] [blame] | 79 | ors.silent = True |
Scott Baker | 88be16c | 2014-01-06 23:52:26 -0800 | [diff] [blame] | 80 | ors.save() |
Sapan Bhatia | 1b9051a | 2014-07-23 10:25:00 -0400 | [diff] [blame] | 81 | |
Scott Baker | 82c0bed | 2014-09-23 22:53:51 -0700 | [diff] [blame] | 82 | def delete_record(self, m): |
| 83 | if m.origin_server_id is not None: |
| 84 | self.client.onev.Delete("OriginServer", m.origin_server_id) |