From 746a0ed4ad2273d0e67b896efbb6d7a69f9f4c77 Mon Sep 17 00:00:00 2001 From: Steven Hardy Date: Fri, 3 Jan 2014 16:56:58 +0000 Subject: [PATCH] Make LoadBalancer nested stack template configurable Reviving work originally posted by Angus Salkeld under https://review.openstack.org/#/c/21405/ This is to enable the various distros to easily override the hard-coded default nested template (which currently only supports Fedora 17) with a template approporate to their distro, and enables deployers to specify their own template (although the latter use-case will probably be catered for via a provider template implementation long-term). Change-Id: I0a4eccb23b2eebbf31b7aabf0fe85ab31f0ca971 Partial-Bug: #1234375 Co-Authored-By: Angus Salkeld --- etc/heat/heat.conf.sample | 9 +++++++ heat/engine/resources/loadbalancer.py | 39 +++++++++++++++++++++------ heat/tests/test_loadbalancer.py | 15 ++++++++++- 3 files changed, 54 insertions(+), 9 deletions(-) diff --git a/etc/heat/heat.conf.sample b/etc/heat/heat.conf.sample index 784cb7019f..df649c97f8 100644 --- a/etc/heat/heat.conf.sample +++ b/etc/heat/heat.conf.sample @@ -116,6 +116,15 @@ #cloud_backend=heat.engine.clients.OpenStackClients +# +# Options defined in heat.engine.resources.loadbalancer +# + +# Custom template for the built-in loadbalancer nested stack +# (string value) +#loadbalancer_template= + + # # Options defined in heat.openstack.common.db.sqlalchemy.session # diff --git a/heat/engine/resources/loadbalancer.py b/heat/engine/resources/loadbalancer.py index 3ef8142d43..64672bf966 100644 --- a/heat/engine/resources/loadbalancer.py +++ b/heat/engine/resources/loadbalancer.py @@ -12,7 +12,11 @@ # 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 os +from oslo.config import cfg + +from heat.common import exception from heat.common import template_format from heat.engine import constraints from heat.engine import properties @@ -24,7 +28,7 @@ from heat.openstack.common.gettextutils import _ logger = logging.getLogger(__name__) -lb_template = r''' +lb_template_default = r''' { "AWSTemplateFormatVersion": "2010-09-09", "Description": "Built in HAProxy server", @@ -192,11 +196,15 @@ lb_template = r''' ''' -# -# TODO(asalkeld) the above inline template _could_ be placed in an external -# file at the moment this is because we will probably need to implement a -# LoadBalancer based on keepalived as well (for for ssl support). -# +# Allow user to provide alternative nested stack template to the above +loadbalancer_opts = [ + cfg.StrOpt('loadbalancer_template', + default=None, + help='Custom template for the built-in ' + 'loadbalancer nested stack')] +cfg.CONF.register_opts(loadbalancer_opts) + + class LoadBalancer(stack_resource.StackResource): PROPERTIES = ( @@ -414,8 +422,18 @@ class LoadBalancer(stack_resource.StackResource): return '%s%s%s%s\n' % (gl, frontend, backend, '\n'.join(servers)) + def get_parsed_template(self): + if cfg.CONF.loadbalancer_template: + with open(cfg.CONF.loadbalancer_template) as templ_fd: + logger.info(_('Using custom loadbalancer template %s') + % cfg.CONF.loadbalancer_template) + contents = templ_fd.read() + else: + contents = lb_template_default + return template_format.parse(contents) + def handle_create(self): - templ = template_format.parse(lb_template) + templ = self.get_parsed_template() if self.properties[self.INSTANCES]: md = templ['Resources']['LB_instance']['Metadata'] @@ -441,7 +459,7 @@ class LoadBalancer(stack_resource.StackResource): rely on the cfn-hup to reconfigure HAProxy ''' if self.INSTANCES in prop_diff: - templ = template_format.parse(lb_template) + templ = self.get_parsed_template() cfg = self._haproxy_config(templ, prop_diff[self.INSTANCES]) md = self.nested()['LB_instance'].metadata @@ -461,6 +479,11 @@ class LoadBalancer(stack_resource.StackResource): if res: return res + if cfg.CONF.loadbalancer_template and \ + not os.access(cfg.CONF.loadbalancer_template, os.R_OK): + msg = _('Custom LoadBalancer template can not be found') + raise exception.StackValidationFailed(message=msg) + health_chk = self.properties[self.HEALTH_CHECK] if health_chk: interval = float(health_chk[self.HEALTH_CHECK_INTERVAL]) diff --git a/heat/tests/test_loadbalancer.py b/heat/tests/test_loadbalancer.py index bb0c3e1fc0..7d8de6d3d5 100644 --- a/heat/tests/test_loadbalancer.py +++ b/heat/tests/test_loadbalancer.py @@ -179,8 +179,9 @@ class LoadBalancerTest(HeatTestCase): self.assertEqual('LoadBalancer', rsrc.FnGetRefId()) - templ = template_format.parse(lb.lb_template) + templ = template_format.parse(lb.lb_template_default) ha_cfg = rsrc._haproxy_config(templ, rsrc.properties['Instances']) + self.assertRegexpMatches(ha_cfg, 'bind \*:80') self.assertRegexpMatches(ha_cfg, 'server server1 1\.2\.3\.4:80 ' 'check inter 30s fall 5 rise 3') @@ -228,3 +229,15 @@ class LoadBalancerTest(HeatTestCase): msg = '%s: %r not found in %r' % (msg, expected_regexp.pattern, text) raise self.failureException(msg) + + def test_loadbalancer_validate_badtemplate(self): + cfg.CONF.set_override('loadbalancer_template', '/a/noexist/x.y') + + t = template_format.parse(lb_template) + s = utils.parse_stack(t) + s.store() + + rsrc = lb.LoadBalancer('LoadBalancer', + t['Resources']['LoadBalancer'], + s) + self.assertRaises(exception.StackValidationFailed, rsrc.validate)