S.Çağlar Onur | a95895d | 2015-02-09 13:34:11 -0500 | [diff] [blame] | 1 | #!/usr/bin/env python |
Scott Baker | 2290b05 | 2014-05-08 10:28:22 -0700 | [diff] [blame] | 2 | |
| 3 | import os |
| 4 | import sys |
| 5 | import base64 |
| 6 | import traceback |
| 7 | from django.db.models import F, Q |
Scott Baker | 166f4b8 | 2015-02-09 11:18:46 -0800 | [diff] [blame] | 8 | from planetstack.config import Config, XOS_DIR |
Scott Baker | 2290b05 | 2014-05-08 10:28:22 -0700 | [diff] [blame] | 9 | from observer.syncstep import SyncStep |
| 10 | from core.models import Service |
| 11 | from requestrouter.models import ServiceMap |
| 12 | from util.logger import Logger, logging |
| 13 | |
| 14 | parentdir = os.path.join(os.path.dirname(__file__),"..") |
| 15 | sys.path.insert(0,parentdir) |
| 16 | |
| 17 | from rrlib import RequestRouterLibrary |
| 18 | from configurationPush import ConfigurationPush |
| 19 | import rrlib_config |
| 20 | |
| 21 | logger = Logger(level=logging.INFO) |
| 22 | |
| 23 | class SyncServiceMap(SyncStep, RequestRouterLibrary, ConfigurationPush): |
| 24 | provides=[ServiceMap] |
| 25 | requested_interval=0 |
| 26 | |
| 27 | def __init__(self, **args): |
| 28 | SyncStep.__init__(self, **args) |
| 29 | RequestRouterLibrary.__init__(self) |
| 30 | ConfigurationPush.__init__(self) |
| 31 | |
| 32 | def fetch_pending(self): |
| 33 | try: |
| 34 | ret = ServiceMap.objects.filter(Q(enacted__lt=F('updated')) | Q(enacted=None)) |
| 35 | return ret |
| 36 | except Exception, e: |
| 37 | traceback.print_exc() |
| 38 | return None |
| 39 | |
| 40 | def sync_record(self, servicemap): |
| 41 | try: |
| 42 | print "sync! %s " % self.get_servicemap_uid(servicemap) |
| 43 | self.gen_dnsredir_serviceconf(servicemap) |
| 44 | self.gen_dnsdemux_serviceconf(servicemap) |
| 45 | # push generated files from temp_config |
| 46 | service_uid = self.get_servicemap_uid(servicemap) |
Scott Baker | 166f4b8 | 2015-02-09 11:18:46 -0800 | [diff] [blame] | 47 | self.config_push(service_uid, rrlib_config.REDIR_USER, XOS_DIR + "/rr_observer/playbook/site_redir.yml", "/etc/ansible/requestrouter/dnsredir/hosts") |
| 48 | self.config_push(service_uid, rrlib_config.DEMUX_USER, XOS_DIR + "/rr_observer/playbook/site_demux.yml", "/etc/ansible/requestrouter/dnsdemux/hosts") |
Scott Baker | 2290b05 | 2014-05-08 10:28:22 -0700 | [diff] [blame] | 49 | self.teardown_temp_configfiles(service_uid) |
| 50 | except Exception, e: |
| 51 | traceback.print_exc() |
| 52 | return False |
| 53 | |
| 54 | if __name__ == "__main__": |
| 55 | sv = SyncServiceMap() |
| 56 | |
| 57 | recs = sv.fetch_pending() |
| 58 | |
| 59 | for rec in recs: |
| 60 | sv.sync_record( rec ) |