Scott Baker | 25467ff | 2016-08-04 09:50:22 -0700 | [diff] [blame] | 1 | import os |
| 2 | import sys |
| 3 | import base64 |
| 4 | |
| 5 | from django.db.models import F, Q |
| 6 | from xos.config import Config |
| 7 | from synchronizers.base.syncstep import SyncStep |
| 8 | from core.models import Service |
| 9 | from services.hpc.models import ServiceProvider, ContentProvider, CDNPrefix, OriginServer |
| 10 | from xos.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 | observes=OriginServer |
| 23 | requested_interval=0 |
| 24 | |
| 25 | def __init__(self, **args): |
| 26 | SyncStep.__init__(self, **args) |
| 27 | HpcLibrary.__init__(self) |
| 28 | |
| 29 | def filter_hpc_service(self, objs): |
| 30 | hpcService = self.get_hpc_service() |
| 31 | |
| 32 | return [x for x in objs if x.contentProvider.serviceProvider.hpcService == hpcService] |
| 33 | |
| 34 | def fetch_pending(self, deleted): |
| 35 | #self.consistency_check() |
| 36 | |
| 37 | return self.filter_hpc_service(SyncStep.fetch_pending(self, deleted)) |
| 38 | |
| 39 | def consistency_check(self): |
| 40 | # set to true if something changed |
| 41 | result=False |
| 42 | |
| 43 | # sanity check to make sure our PS objects have CMI objects behind them |
| 44 | all_ors_ids = [x["origin_server_id"] for x in self.client.onev.ListAll("OriginServer")] |
| 45 | for ors in OriginServer.objects.all(): |
| 46 | if (ors.origin_server_id is not None) and (ors.origin_server_id not in all_ors_ids): |
| 47 | # we have an origin server ID, but it doesn't exist in the CMI |
| 48 | # something went wrong |
| 49 | # start over |
| 50 | logger.info("origin server %s was not found on CMI" % ors.origin_server_id) |
| 51 | ors.origin_server_id=None |
| 52 | ors.save() |
| 53 | result = True |
| 54 | |
| 55 | return result |
| 56 | |
| 57 | def sync_record(self, ors): |
| 58 | logger.info("sync'ing origin server %s" % str(ors),extra=ors.tologdict()) |
| 59 | |
| 60 | if (not ors.contentProvider) or (not ors.contentProvider.content_provider_id): |
| 61 | raise Exception("Origin Server %s is linked to a contentProvider with no id" % str(ors)) |
| 62 | |
| 63 | cpid = ors.contentProvider.content_provider_id |
| 64 | |
| 65 | # validation requires URL start with http:// |
| 66 | url = ors.url |
| 67 | if not url.startswith("http://"): |
| 68 | url = "http://" + url |
| 69 | |
| 70 | 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} |
| 71 | if not ors_dict["description"]: |
| 72 | ors_dict["description"] = "blank" |
| 73 | |
| 74 | #print os_dict |
| 75 | |
| 76 | if not ors.origin_server_id: |
| 77 | id = self.client.onev.Create("OriginServer", ors_dict) |
| 78 | ors.origin_server_id = id |
| 79 | else: |
| 80 | self.client.onev.Update("OriginServer", ors.origin_server_id, ors_dict) |
| 81 | |
| 82 | # ... something breaks (analytics) if the URL starts with http://, so we |
| 83 | # change it in cob after we added it via onev. |
| 84 | url = url[7:] |
| 85 | self.client.cob.UpdateContent(ors.origin_server_id, {"url": url}) |
| 86 | |
| 87 | ors.silent = True |
| 88 | ors.save() |
| 89 | |
| 90 | def delete_record(self, m): |
| 91 | if m.origin_server_id is not None: |
| 92 | self.client.onev.Delete("OriginServer", m.origin_server_id) |