blob: f276bc48c31a627bd72a2cd321a44549bbf98386 [file] [log] [blame]
S.Çağlar Onura95895d2015-02-09 13:34:11 -05001#!/usr/bin/env python
Scott Baker2290b052014-05-08 10:28:22 -07002
3import os
4import sys
5import base64
6import traceback
7from django.db.models import F, Q
Scott Baker166f4b82015-02-09 11:18:46 -08008from planetstack.config import Config, XOS_DIR
Scott Baker2290b052014-05-08 10:28:22 -07009from observer.syncstep import SyncStep
10from core.models import Service
11from requestrouter.models import ServiceMap
12from util.logger import Logger, logging
13
14parentdir = os.path.join(os.path.dirname(__file__),"..")
15sys.path.insert(0,parentdir)
16
17from rrlib import RequestRouterLibrary
18from configurationPush import ConfigurationPush
19import rrlib_config
20
21logger = Logger(level=logging.INFO)
22
23class 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 Baker166f4b82015-02-09 11:18:46 -080047 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 Baker2290b052014-05-08 10:28:22 -070049 self.teardown_temp_configfiles(service_uid)
50 except Exception, e:
51 traceback.print_exc()
52 return False
53
54if __name__ == "__main__":
55 sv = SyncServiceMap()
56
57 recs = sv.fetch_pending()
58
59 for rec in recs:
60 sv.sync_record( rec )