Scott Baker | 7d018ca | 2014-01-23 15:41:45 -0800 | [diff] [blame] | 1 | import os |
| 2 | import sys |
| 3 | import base64 |
| 4 | import traceback |
| 5 | from collections import defaultdict |
| 6 | from django.db.models import F, Q |
| 7 | from planetstack.config import Config |
| 8 | from util.logger import Logger, logging |
| 9 | from observer.syncstep import SyncStep |
| 10 | from hpc.models import ServiceProvider, ContentProvider, CDNPrefix, OriginServer |
| 11 | from core.models import * |
| 12 | |
| 13 | # hpclibrary will be in steps/.. |
| 14 | parentdir = os.path.join(os.path.dirname(__file__),"..") |
| 15 | sys.path.insert(0,parentdir) |
| 16 | |
| 17 | from hpclib import HpcLibrary |
| 18 | |
| 19 | logger = Logger(level=logging.INFO) |
| 20 | |
| 21 | class GarbageCollector(SyncStep, HpcLibrary): |
| 22 | # requested_interval = 86400 |
| 23 | requested_interval = 0 |
| 24 | provides=[] |
| 25 | |
| 26 | def __init__(self, **args): |
| 27 | SyncStep.__init__(self, **args) |
| 28 | HpcLibrary.__init__(self) |
| 29 | |
| 30 | def call(self, **args): |
| 31 | logger.info("running garbage collector") |
| 32 | try: |
| 33 | self.gc_originservers() |
| 34 | self.gc_cdnprefixes() |
| 35 | self.gc_contentproviders() |
| 36 | self.gc_serviceproviders() |
| 37 | except: |
| 38 | traceback.print_exc() |
| 39 | |
| 40 | def gc_onev(self, ps_class, ps_idField, onev_className, onev_idField): |
| 41 | # get the CMI's objects |
| 42 | onev_objs = self.client.onev.ListAll(onev_className) |
| 43 | |
| 44 | # get the data model's objects, |
| 45 | ps_objs = ps_class.objects.filter(enacted__isnull=False) |
| 46 | ps_ids = [str(getattr(x,ps_idField,None)) for x in ps_objs] |
| 47 | |
| 48 | # for each onev object, if it's id does not exist in a data model |
| 49 | # object, then delete it. |
| 50 | for onev_obj in onev_objs: |
| 51 | onev_id = onev_obj[onev_idField] |
| 52 | if str(onev_id) not in ps_ids: |
| 53 | logger.info("garbage collecting %s %s" % (onev_className, str(onev_id))) |
| 54 | self.client.onev.Delete(onev_className, onev_id) |
| 55 | |
| 56 | def gc_originservers(self): |
| 57 | self.gc_onev(OriginServer, "origin_server_id", "OriginServer", "origin_server_id") |
| 58 | |
| 59 | def gc_cdnprefixes(self): |
| 60 | self.gc_onev(CDNPrefix, "cdn_prefix_id", "CDNPrefix", "cdn_prefix_id") |
| 61 | |
| 62 | def gc_contentproviders(self): |
| 63 | self.gc_onev(ContentProvider, "content_provider_id", "ContentProvider", "content_provider_id") |
| 64 | |
| 65 | def gc_serviceproviders(self): |
| 66 | self.gc_onev(ServiceProvider, "service_provider_id", "ServiceProvider", "service_provider_id") |
| 67 | |