Changeset 10110 in genesis for tools


Ignore:
Timestamp:
Mar 11, 2012, 12:52:55 PM (13 years ago)
Author:
rick
Message:

Templating gives so much clearer and readable code.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • tools/gformat.py

    r10108 r10110  
    4444except ImportError:
    4545    from yaml import Loader, Dumper
     46
     47from jinja2 import Template
    4648
    4749import logging
     
    268270def generate_rc_conf_local(datadump):
    269271  """ Generate configuration file '/etc/rc.conf.local' """
     272  datadump['ileiden_enable'] = 'yes' if datadump['ileiden'] else 'no'
     273
    270274  output = generate_header("#");
    271   output += """\
    272 hostname='%(autogen_fqdn)s.%(domain)s'
    273 location='%(location)s'
    274 """ % datadump
    275 
    276   # TProxy configuration
    277   output += "\n"
    278   try:
    279     if datadump['tproxy']:
    280       output += """\
     275  output += Template("""\
     276hostname='{{ autogen_fqdn }}.{{ domain }}'
     277location='{{ location }}'
     278nodetype="{{ nodetype }}"
     279
     280{% if tproxy -%}
    281281tproxy_enable='YES'
    282 tproxy_range='%(tproxy)s'
    283 """ % datadump
    284   except KeyError:
    285     output += "tproxy_enable='NO'\n"
    286 
    287   # Extra Proxy configuration
    288   datadump['ileiden_enable'] = 'yes' if datadump['ileiden'] else 'no'
    289   if datadump['nodetype'] == 'Proxy':
    290     output += """
     282tproxy_range='{{ tproxy }}'
     283{% else -%}
     284tproxy_enable='NO'
     285{% endif -%}
     286
     287{% if nodetype == "Proxy" %}
    291288#
    292289# PROXY Configuration
    293290#
    294291sshtun_enable="YES"
    295 sshtun_flags="-R 22%(proxyid)02i:localhost:22"
     292sshtun_flags="-R 22{{ "%02i"|format(proxyid) }}:localhost:22"
    296293
    297294static_routes="wleiden"
    298 route_wleiden="-net 172.16.0.0/12 %(internalroute)s"
    299 defaultrouter="%(gateway)s"
    300 
    301 internalif="%(internalif)s"
     295route_wleiden="-net 172.16.0.0/12 {{ internalroute }}"
     296{% if gateway -%}
     297defaultrouter="{{ gateway }}"
     298{% else -%}
     299#defaultrouter="NOTSET"
     300{% endif -%}
     301
     302internalif="{{ internalif }}"
    302303
    303304# PROXY iLeiden Configuration
    304 ileiden_enable="%(ileiden_enable)s"
    305 gateway_enable="%(ileiden_enable)s"
    306 firewall_enable="%(ileiden_enable)s"
     305ileiden_enable="{{ ileiden_enable }}"
     306gateway_enable="{{ ileiden_enable }}"
     307firewall_enable="{{ ileiden_enable }}"
    307308firewall_script="/etc/ipfw.sh"
    308 firewall_nat_enable="%(ileiden_enable)s"
    309 """ % datadump
    310 
    311   if datadump['nodetype'] == 'CNode' and datadump['ileiden']:
    312     output += """
     309firewall_nat_enable="{{ ileiden_enable }}"
     310{% endif -%}
     311
     312{% if nodetype == "CNode" %}
    313313# NODE iLeiden Configuration
    314 ileiden_enable="%(ileiden_enable)s"
    315 
    316314captive_portal_whitelist=""
    317 captive_portal_interfaces="%(autogen_dhcp_interfaces)s"
    318 """ % datadump
     315captive_portal_interfaces="{{ autogen_dhcp_interfaces }}"
     316{% endif %}
     317
     318""").render(datadump)
    319319
    320320  # lo0 configuration:
Note: See TracChangeset for help on using the changeset viewer.