astara-appliance/akanda/router/drivers/loadbalancer/nginx.py
Adam Gandelman 433a4c7190 Introduces advanced service drivers to akanda-appliance
This introduces the ability to create service manager drivers to handle
managing advanced services within the akanda-appliance.

It splits some common things into a System manager.  Existing
stuff that is router-specific is moved to a Router manager and we begin
implementing LBAAS drivers using Nginx.

At the moment, configuration for which drivers are loaded by the appliance
code itself is stored in /etc/default/akanda-appliance.  This is setup by
a DIB_* variable and accessed by the appliance via environment variable. We
should improve this later when we need to expose richer configuration to the
appliance.

We could and should work on the API for this.  Currently, our v1
API is entirely router-specific.  This adds to that and allows the
RUG to attach other advanced service configuratino data to the config
object it pushes.  If the corresponding service's driver has been enabled
in the appliance, it will attempt to find that data and configure the
advanced service accordingly.  Ideally, longterm we want a v2 API
that can reference all services the same.  There's a few ugly compat
hacks added here to maintain compatability with where the RUG expects
certain router resources to be.  We can evolve this over time.

Partially-implements: blueprint appliance-provisioning-driver
Depends-on: Ic19a883f56fb6d65a83b1f4d93b581f9e242d97f
Change-Id: I6048789ec15fad1dbc899cbbd82508433cb96d44
2015-10-14 15:02:16 -07:00

76 lines
2.3 KiB
Python

# Copyright (c) 2015 Akanda, Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import jinja2
import os
from akanda.router.drivers import base
from akanda.router.utils import execute
class NginxTemplateNotFound(Exception):
# TODO(adam_g): These should return 50x errors and not logged
# exceptions.
pass
class NginxLB(base.Manager):
NAME = 'nginx'
CONFIG_PATH = '/etc/nginx/sites-enabled/'
CONFIG_FILE_TEMPLATE = os.path.join(
os.path.dirname(__file__), 'nginx.conf.template')
INIT = 'nginx'
def __init__(self, root_helper='sudo'):
"""
Initializes DHCPManager class.
:type root_helper: str
:param root_helper: System utility used to gain escalate privileges.
"""
super(NginxLB, self).__init__(root_helper)
self._load_template()
def _load_template(self):
if not os.path.exists(self.CONFIG_FILE_TEMPLATE):
raise NginxTemplateNotFound(
'NGINX Config template not found @ %s' %
self.CONFIG_FILE_TEMPLATE
)
self.config_tmpl = jinja2.Template(
open(self.CONFIG_FILE_TEMPLATE).read())
def _render_config_template(self, path, config):
self._load_template()
with open(path, 'w') as out:
out.write(
self.config_tmpl.render(loadbalancer=config)
)
def restart(self):
execute(['service', self.INIT, 'restart'], self.root_helper)
pass
def update_config(self, config):
path = os.path.join(
self.CONFIG_PATH, 'ak-loadbalancer-%s.conf' % config.id)
self._render_config_template(path=path, config=config)
self.restart()
class NginxPlusLB(NginxLB):
NAME = 'nginxplus'
CONFIG_FILE = '/tmp/nginx_plus.conf'
INIT = 'nginxplus'