blob: 76d2fdd66aa0c7a308d6021728fe0924bd73cf20 [file] [log] [blame]
Scott Baker2290b052014-05-08 10:28:22 -07001#!/usr/bin/python
2
3import os
4import sys
5import base64
6import traceback
7from django.db.models import F, Q
8from planetstack.config import Config
9from 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)
47 self.config_push(service_uid, rrlib_config.REDIR_USER, "/opt/planetstack/rr_observer/playbook/site_redir.yml", "/etc/ansible/requestrouter/dnsredir/hosts")
48 self.config_push(service_uid, rrlib_config.DEMUX_USER, "/opt/planetstack/rr_observer/playbook/site_demux.yml", "/etc/ansible/requestrouter/dnsdemux/hosts")
49 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 )