From 3e87f673bd262f7808c1fac7b0f853cab71a098d Mon Sep 17 00:00:00 2001 From: Christian Pointner Date: Sun, 18 Feb 2018 17:44:07 +0100 Subject: revamp handling of sfive proxy --- src/flufigut.py | 77 +++++++++++++++++++++++++++++++++++---------------------- 1 file changed, 48 insertions(+), 29 deletions(-) (limited to 'src/flufigut.py') diff --git a/src/flufigut.py b/src/flufigut.py index 1be9ffd..366c13e 100755 --- a/src/flufigut.py +++ b/src/flufigut.py @@ -494,7 +494,7 @@ class Planet: self.flow['streamers'][comp_name]['plugs']['rrd']['bytes'] = "%s/%s_bytes.rrd" % ( self._desc.globals['stats']['rrd']['directory'], comp_name) - if 'sfive' in self._desc.globals['stats']: + if 'sfive' in self._desc.globals['stats'] and self._desc.globals['stats']['sfive']['type'] == 'flumotion-plug': self.flow['streamers'][comp_name]['plugs']['sfive'] = {} self.flow['streamers'][comp_name]['plugs']['sfive']['socket'] = self._desc.globals['stats']['sfive']['socket'] self.flow['streamers'][comp_name]['plugs']['sfive']['duration'] = self._desc.globals['stats']['sfive']['duration'] @@ -545,6 +545,8 @@ class Planet: self.__add_worker_flag_exclusive(worker, "stream", stream_name) self.__add_worker_flag_exclusive(worker, "stream-hostname", hostname) self.__add_worker_flag_exclusive(worker, "stream-index", idx) + if 'sfive' in self._desc.globals['stats']: + self.__add_worker_flag_exclusive(worker, "sfive", self._desc.globals['stats']['sfive']['type']) def __generate_stream_instance(self, stream_name, stream, idx, cnt): port = stream['port'] @@ -620,10 +622,44 @@ class Planet: out += "%s:%s\n" % (worker['name'], crypt.crypt(worker['password'], salt)) return out.encode('utf-8') + def sfive_proxy_config(self, worker_name, for_onion=False): + # TODO: hardcoded values + hostname_prefix = "streamer" + addr = "" + port = 8000 + if for_onion: + hostname_prefix = "streamer-onion" + addr = "127.0.0.1" + port = 8001 + + listen = "%s:%i" % (addr, port) + hostname = "%s-%s%d" % (hostname_prefix, self.workers[worker_name]['flags']['stream'], (self.workers[worker_name]['flags']['stream-index'] + 1)) + + conf = {'listen': listen, 'protocol': 'http'} + conf['connect'] = 'http://flumotion-worker-' + self.workers[worker_name]['name'] + ':8000' + conf['request_header'] = [{'op': 'del', 'header': 'X-Forwarded-For'}] + conf['response_header'] = [{'op': 'set', 'header': 'Cache-Control', 'value': 'no-cache'}, + {'op': 'add', 'header': 'Cache-Control', 'value': 'no-store'}, + {'op': 'add', 'header': 'Cache-Control', 'value': 'must-revalidate'}, + {'op': 'add', 'header': 'Cache-Control', 'value': 'max-age=0'}, + {'op': 'time', 'header': 'Expires', 'value': '-1s'}] + conf['backends'] = {} + conf['backends']['/hls/'] = {'connect': 'http://nginx-streamer-' + self.workers[worker_name]['name'] + ':8000'} + conf['backends']['/dash/'] = {'connect': 'http://nginx-streamer-' + self.workers[worker_name]['name'] + ':8000'} + + conf['sfive'] = {'hostname': hostname} + conf['sfive']['socket'] = self._desc.globals['stats']['sfive']['socket'] + conf['sfive']['duration'] = self._desc.globals['stats']['sfive']['duration'] + conf['sfive']['tags'] = self._desc.globals['stats']['sfive']['tags'] + conf['sfive']['matches'] = [{'format': '/${content}-${format}-${quality}', 'streams': 'av-orig/flash,webm/high,medium,low,mini'}, + {'format': '/${format}/${content}-${quality}', 'streams': 'av-orig/dash,hls/high,medium,low,mini'}] + + return conf # kubernetes handling ############################# # + class K8sDeployment: def __init__(self, desc, planet, namespace=None): @@ -703,38 +739,21 @@ class K8sDeployment: appsV1.create_namespaced_deployment(self._namespace, deploy) def _deploy_sfive_worker(self, template_dir, tmpl_env, v1, appsV1, worker): - needs_sfive = False - if 'sfive-proxy' in self._desc.globals['stats'] and 'stream' in worker['flags']: - needs_sfive = True - - if not needs_sfive: + if 'sfive' not in worker['flags']: return - # TODO: hardcoded values - proxy_conf = {'listen': ':8000', 'protocol': 'http'} - proxy_conf['connect'] = 'http://flumotion-worker-' + worker['name'] + ':8000' - proxy_conf['request_header'] = [{'op': 'del', 'header': 'X-Forwarded-For'}] - proxy_conf['response_header'] = [{'op': 'set', 'header': 'Cache-Control', 'value': 'no-cache'}, - {'op': 'add', 'header': 'Cache-Control', 'value': 'no-store'}, - {'op': 'add', 'header': 'Cache-Control', 'value': 'must-revalidate'}, - {'op': 'add', 'header': 'Cache-Control', 'value': 'max-age=0'}, - {'op': 'time', 'header': 'Expires', 'value': '-1s'}] - proxy_conf['backends'] = {} - proxy_conf['backends']['/hls/'] = {'connect': 'http://nginx-streamer-' + worker['name'] + ':8000'} - proxy_conf['backends']['/dash/'] = {'connect': 'http://nginx-streamer-' + worker['name'] + ':8000'} - - proxy_conf['sfive'] = {'hostname': "streamer-%s%d" % (worker['flags']['stream'], (worker['flags']['stream-index'] + 1))} - proxy_conf['sfive']['socket'] = self._desc.globals['stats']['sfive-proxy']['socket'] - proxy_conf['sfive']['duration'] = self._desc.globals['stats']['sfive-proxy']['duration'] - proxy_conf['sfive']['tags'] = self._desc.globals['stats']['sfive-proxy']['tags'] - proxy_conf['sfive']['matches'] = [{'format': '/${content}-${format}-${quality}', 'streams': 'av-orig/flash,webm/high,medium,low,mini'}, - {'format': '/${format}/${content}-${quality}', 'streams': 'av-orig/dash,hls/high,medium,low,mini'}] - - cm = self.__generate_object(tmpl_env, 'sfive-proxy-cm.yml', worker) - cm['data']['proxy.json'] = json.dumps(proxy_conf) + cm = self.__generate_object(tmpl_env, 'sfive-cm.yml', worker) + if 'data' not in cm or not cm['data']: + cm['data'] = {} + if worker['flags']['sfive'] == 'proxy' and 'stream' in worker['flags']: + cm['data']['proxy.json'] = json.dumps(self._planet.sfive_proxy_config(worker['name'])) + stream_name = worker['flags']['stream'] + if 'onion-service' in self._desc.streams[stream_name] and len(self._desc.streams[stream_name]['nginx-muxes']) > 0: + cm['data']['proxy-onion.json'] = json.dumps(self._planet.sfive_proxy_config(worker['name'], True)) + v1.create_namespaced_config_map(self._namespace, cm) - deploy = self.__generate_object(tmpl_env, 'sfive-proxy-deploy.yml', worker) + deploy = self.__generate_object(tmpl_env, 'sfive-deploy.yml', worker) appsV1.create_namespaced_deployment(self._namespace, deploy) def deploy(self, template_dir): -- cgit v1.2.3