def generate_haproxy_spoe_conf(self): logger.info("generate_haproxy_spoe_conf") try: jinja2_env = Environment(loader=FileSystemLoader(JINJA_PATH)) template_spoe = jinja2_env.get_template(JINJA_SPOE_TEMPLATE) workflows = set() for w in self.workflow_set.all(): workflows.add(w) return template_spoe.render({ 'id': self.id, 'workflows': workflows }) except (TemplateNotFound, TemplatesNotFound): raise ServiceJinjaError( "The following file cannot be found : '{}'".format( JINJA_SPOE_TEMPLATE), "haproxy") except (TemplateAssertionError, TemplateRuntimeError): raise ServiceJinjaError( "Unknown error in template generation: {}".format( JINJA_SPOE_TEMPLATE), "haproxy") except UndefinedError: raise ServiceJinjaError( "A variable is undefined while trying to render the following template: " "{}".format(JINJA_SPOE_TEMPLATE), "haproxy") except TemplateSyntaxError: raise ServiceJinjaError( "Syntax error in the template: '{}'".format( JINJA_SPOE_TEMPLATE), "haproxy")
def generate_defender_conf(self): logger.info("generate_defender_conf") try: jinja2_env = Environment(loader=FileSystemLoader(JINJA_PATH)) template = jinja2_env.get_template(JINJA_TEMPLATE) ruleset = None if self.defender_ruleset: ruleset = self.defender_ruleset.to_dict() return template.render({ 'ruleset': ruleset, 'policy': self.to_template() }) except (TemplateNotFound, TemplatesNotFound): raise ServiceJinjaError( "The following file cannot be found : '{}'".format( JINJA_TEMPLATE), "haproxy") except (TemplateAssertionError, TemplateRuntimeError): raise ServiceJinjaError( "Unknown error in template generation: {}".format( JINJA_TEMPLATE), "haproxy") except UndefinedError: raise ServiceJinjaError( "A variable is undefined while trying to render the following template: " "{}".format(JINJA_TEMPLATE), "haproxy") except TemplateSyntaxError: raise ServiceJinjaError( "Syntax error in the template: '{}'".format(JINJA_TEMPLATE), "haproxy")
def generate_conf(self): """ Render the conf with Jinja template and self.to_template() method :return The generated configuration as string, or raise """ # The following var is only used by error, do not forget to adapt if needed template_name = JINJA_PATH + JINJA_TEMPLATE try: jinja2_env = Environment(loader=FileSystemLoader(JINJA_PATH)) template = jinja2_env.get_template(JINJA_TEMPLATE) return template.render({'conf': self.to_template_external(), 'global_config': Cluster.get_global_config()}) # In ALL exceptions, associate an error message # The exception instantiation MUST be IN except statement, to retrieve traceback in __init__ except TemplateNotFound: exception = ServiceJinjaError("The following file cannot be found : '{}'".format(template_name), "haproxy") except TemplatesNotFound: exception = ServiceJinjaError("The following files cannot be found : '{}'".format(template_name), "haproxy") except (TemplateAssertionError, TemplateRuntimeError): exception = ServiceJinjaError("Unknown error in template generation: {}".format(template_name), "haproxy") except UndefinedError: exception = ServiceJinjaError("A variable is undefined while trying to render the following template: " "{}".format(template_name), "haproxy") except TemplateSyntaxError: exception = ServiceJinjaError("Syntax error in the template: '{}'".format(template_name), "haproxy") # If there was an exception, raise a more general exception with the message and the traceback raise exception
def generate_conf(self): """ Render the conf with Jinja template and self.to_template() method :return The generated configuration as string, or raise """ try: jinja2_env = Environment(loader=FileSystemLoader(JINJA_PATH)) template_ipsec = jinja2_env.get_template(JINJA_TEMPLATE_IPSEC) template_secrets = jinja2_env.get_template(JINJA_TEMPLATE_SECRETS) template_strongswan = jinja2_env.get_template( JINJA_TEMPLATE_STRONGSWAN) conf = self.to_template() return { 'template_ipsec': template_ipsec.render(conf), 'template_secrets': template_secrets.render(conf), 'template_strongswan': template_strongswan.render(conf) } # In ALL exceptions, associate an error message except Exception as e: # If there was an exception, raise a more general exception with the message and the traceback raise ServiceJinjaError( "Strongswan config generation error: '{}'".format(str(e)), "strongswan")
def generate_haproxy_backend_conf(self): logger.info("generate_haproxy_backend_conf") try: jinja2_env = Environment(loader=FileSystemLoader(JINJA_PATH)) template_backend = jinja2_env.get_template(JINJA_BACKEND_TEMPLATE) return template_backend.render({'id': self.id}) except (TemplateNotFound, TemplatesNotFound): raise ServiceJinjaError( "The following file cannot be found : '{}'".format( JINJA_BACKEND_TEMPLATE), "haproxy") except (TemplateAssertionError, TemplateRuntimeError): raise ServiceJinjaError( "Unknown error in template generation: {}".format( JINJA_BACKEND_TEMPLATE), "haproxy") except UndefinedError: raise ServiceJinjaError( "A variable is undefined while trying to render the following template: " "{}".format(JINJA_BACKEND_TEMPLATE), "haproxy") except TemplateSyntaxError: raise ServiceJinjaError( "Syntax error in the template: '{}'".format( JINJA_BACKEND_TEMPLATE), "haproxy")
def generate_conf(self): """ Render the conf with Jinja template and self.to_template() method :return The generated configuration as string, or raise """ try: jinja2_env = Environment(loader=FileSystemLoader(JINJA_PATH)) template_client = jinja2_env.get_template(JINJA_TEMPLATE_OPENVPN) conf = self.to_template() return {'template_client': template_client.render(conf)} # In ALL exceptions, associate an error message except Exception as e: # If there was an exception, raise a more general exception with the message and the traceback raise ServiceJinjaError( "Openvpn config generation error: '{}'".format(str(e)), "openvpn")