def setup_solr_client(config, custom_log4j=True, custom_log_location=None, log4jcontent=None): solr_client_dir = '/usr/lib/ambari-infra-solr-client' solr_client_log_dir = default_config( config, '/configurations/infra-solr-client-log4j/infra_solr_client_log_dir', '/var/log/ambari-infra-solr-client' ) if custom_log_location is None else custom_log_location solr_client_log = format("{solr_client_log_dir}/solr-client.log") solr_client_log_maxfilesize = default_config( config, 'configurations/infra-solr-client-log4j/infra_client_log_maxfilesize', 80) solr_client_log_maxbackupindex = default_config( config, 'configurations/infra-solr-client-log4j/infra_client_log_maxbackupindex', 60) Directory(solr_client_log_dir, mode=0755, cd_access='a', create_parents=True) Directory(solr_client_dir, mode=0755, cd_access='a', create_parents=True, recursive_ownership=True) solrCliFilename = format("{solr_client_dir}/solrCloudCli.sh") File(solrCliFilename, mode=0755, content=StaticFile(solrCliFilename)) if custom_log4j: # use custom log4j content only, when infra is not installed on the cluster solr_client_log4j_content = config['configurations'][ 'infra-solr-client-log4j'][ 'content'] if log4jcontent is None else log4jcontent context = { 'solr_client_log': solr_client_log, 'solr_client_log_maxfilesize': solr_client_log_maxfilesize, 'solr_client_log_maxbackupindex': solr_client_log_maxbackupindex } template = JinjaEnvironment( line_statement_prefix='%', variable_start_string="{{", variable_end_string="}}")\ .from_string(solr_client_log4j_content) File(format("{solr_client_dir}/log4j.properties"), content=template.render(context), mode=0644) else: File(format("{solr_client_dir}/log4j.properties"), mode=0644) File(solr_client_log, mode=0664, content='')
from ambari_jinja2 import Environment tmpl = Environment().from_string("""\ <ul> {%- for item in [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] if item % 2 == 0 %} <li>{{ loop.index }} / {{ loop.length }}: {{ item }}</li> {%- endfor %} </ul> if condition: {{ 1 if foo else 0 }} """) print tmpl.render(foo=True)
<title>${page_title|e}</title> </head> <body> <div class="header"> <h1>${page_title|e}</h1> </div> <div class="table"> <table> % for row in table <tr> % for cell in row <td>${testmacro(cell)}</td> % endfor </tr> % endfor </table> </div> </body> </html>\ """ jinja_template = JinjaEnvironment(line_statement_prefix='%', variable_start_string="${", variable_end_string="}").from_string(source) print jinja_template.environment.compile(source, raw=True) p = Profile() p.runcall(lambda: jinja_template.render(context)) stats = Stats(p) stats.sort_stats('time', 'calls') stats.print_stats()
<div class="header"> <h1>${page_title|e}</h1> </div> <div class="table"> <table> % for row in table <tr> % for cell in row <td>${testmacro(cell)}</td> % endfor </tr> % endfor </table> </div> </body> </html>\ """ jinja_template = JinjaEnvironment( line_statement_prefix='%', variable_start_string="${", variable_end_string="}" ).from_string(source) print jinja_template.environment.compile(source, raw=True) p = Profile() p.runcall(lambda: jinja_template.render(context)) stats = Stats(p) stats.sort_stats('time', 'calls') stats.print_stats()