386 | | CMT offers a powerful feature, powered by templates based on the [https://docs.djangoproject.com/en/dev/topics/templates/ Django Template Language]. |
387 | | With these template-files you're able to let CMT generate various kind of files, like configuration files or reports about your clusters. |
| 386 | CMT offers a powerful feature, powered by templates. |
| 387 | With these templates you're able to let CMT generate various kind of files, like configuration files or reports about your clusters. |
| 388 | |
| 389 | The syntax of the templates in CMT is based on Django templates, therefore the documentation of the [https://docs.djangoproject.com/en/dev/topics/templates/ Django template language] can be useful as a reference. |
| 390 | Django templates are usually meant for web pages, but since we use it for our configuration files some functionalities have been added for CMT-specific needs. |
| 391 | By extending the Django template language, it has been made possible to: |
| 392 | * Define an epilogue script |
| 393 | * Pre-load datasets from database |
| 394 | * Set a path to write the output to |
| 395 | |
| 396 | To make use of these functionalities you have to start your template with |
| 397 | |
| 398 | {{{ |
| 399 | {% load cmt_client %} |
| 400 | }}} |
| 401 | |
| 402 | and store the file in your template-directory as `<name>.cmt` |
| 403 | |
| 404 | |
| 405 | == Epilogue script == |
| 406 | |
| 407 | Some configuration files need to be reloaded by the program when they are modified. |
| 408 | Or maybe some other things should be done to activate the new configuration. |
| 409 | This is where an epilogue script comes in. |
| 410 | You can write the epilogue script in the template itself, between the ''epilogue'' and ''endepilogue'' tag: |
| 411 | |
| 412 | {{{ |
| 413 | {% epilogue %} |
| 414 | # this epilogue-script will restart the DHCP server |
| 415 | /etc/init.d/dhcp3-server restart |
| 416 | {% endepilogue %} |
| 417 | }}} |
| 418 | |
| 419 | |
| 420 | == Pre-load datasets from database == |
| 421 | |
| 422 | You may define collections of objects to use in your templates. |
| 423 | These collections can be defined with a filter, and assigning it to a variable name to use. |
| 424 | |
| 425 | {{{ |
| 426 | # syntax for the use-tag is: |
| 427 | # use <entity> with <filter> as <variable name> |
| 428 | {% use network with 'name=lisa consoles' as net_consoles %} |
| 429 | {% use interface with 'network__name=lisa consoles' as ifaces_consoles %} |
| 430 | }}} |
| 431 | |
| 432 | In the example two collections are assigned to a variable: |
| 433 | * `net_consoles` will contain the network with the name ''lisa consoles'' |
| 434 | * `ifaces_consoles` will contain all interfaces in that network |
| 435 | |
| 436 | Now these collections can be used in the template, to build a part of a DHCP config-file for example: |
| 437 | |
| 438 | {{{ |
| 439 | group { # Start Console network |
| 440 | option domain-name "{{net_consoles.domain}}"; |
| 441 | |
| 442 | {% for iface in ifaces_consoles %} |
| 443 | {% if iface.hwaddress != None %} |
| 444 | host {{iface.fqdn}} { |
| 445 | hardware ethernet {{iface.hwaddress}}; |
| 446 | fixed-address {{iface.ip}}; |
| 447 | option host-name "{{iface.fqdn}}"; |
| 448 | } |
| 449 | {% endif %} |
| 450 | {% endfor %} |
| 451 | } # End Console network |
| 452 | }}} |
| 453 | |
| 454 | |
| 455 | == Set a path to write the output to == |
| 456 | |
| 457 | To save the result of your template, you can store the output to a file. |
| 458 | This can be done by using the ''store''-tag, and give it a path: |
| 459 | {{{ |
| 460 | # syntax for the store-tag is: |
| 461 | # store <outputpath> as output |
| 462 | {% store /etc/dhcp/dhcpd.conf as output %} |
| 463 | }}} |
| 464 | |
| 465 | In this example the result will be written to `/etc/dhcp/dhcp.conf`. |