Use Mako instead of Jinja for existing templates

pull/2351/head
Bogdan Opanchuk 2020-10-11 17:03:56 -07:00
parent ad7935c725
commit 8a8ab6d163
6 changed files with 85 additions and 66 deletions

View File

@ -11,5 +11,5 @@ global-exclude *.py[cod]
recursive-include nucypher/blockchain/eth/aragon_artifacts *.json
recursive-include nucypher/blockchain/eth/contract_registry *.json *.md
prune nucypher/blockchain/eth/contract_registry/historical
recursive-include nucypher/network/templates *.html *.j2
recursive-include nucypher/network/templates *.html *.mako
recursive-include nucypher/acumen/ *json

View File

@ -24,7 +24,8 @@ from constant_sorrow import constants
from constant_sorrow.constants import FLEET_STATES_MATCH, NO_BLOCKCHAIN_CONNECTION, NO_KNOWN_NODES
from datetime import datetime, timedelta
from flask import Flask, Response, jsonify, request
from jinja2 import Template, TemplateError
from mako import exceptions as mako_exceptions
from mako.template import Template
from maya import MayaDT
from typing import Tuple, Set
from umbral.keys import UmbralPublicKey
@ -49,9 +50,7 @@ from nucypher.utilities.logging import Logger
HERE = BASE_DIR = os.path.abspath(os.path.dirname(__file__))
TEMPLATES_DIR = os.path.join(HERE, "templates")
with open(os.path.join(TEMPLATES_DIR, "basic_status.j2"), "r") as f:
_status_template_content = f.read()
status_template = Template(_status_template_content)
status_template = Template(filename=os.path.join(TEMPLATES_DIR, "basic_status.mako"))
class ProxyRESTServer:
@ -370,7 +369,7 @@ def _make_rest_app(datastore: Datastore, this_node, serving_domain: str, log: Lo
log.info(f"Bad TreasureMap HRAC Signature; not storing for HRAC {received_treasure_map._hrac.hex()}")
return Response("This TreasureMap's HRAC is not properly signed.", status=401)
# Additionally, we determine the map identifier from the type of node.
# Additionally, we determine the map identifier from the type of node.
# If the node is federated, we also set the expiration for a week.
if not this_node.federated_only:
map_identifier = received_treasure_map._hrac.hex()
@ -439,8 +438,10 @@ def _make_rest_app(datastore: Datastore, this_node, serving_domain: str, log: Lo
version=nucypher.__version__,
checksum_address=this_node.checksum_address)
except Exception as e:
log.debug("Template Rendering Exception: ".format(str(e)))
raise TemplateError(str(e)) from e
text_error = mako_exceptions.text_error_template().render()
html_error = mako_exceptions.html_error_template().render()
log.debug("Template Rendering Exception:\n" + text_error)
return Response(response=html_error, headers=headers, status=500)
return Response(response=content, headers=headers)
return rest_app

View File

@ -103,31 +103,31 @@
</style>
<div id="this-node">
<h2>{{ this_node.nickname }}</h2>
<h5>({{ checksum_address }})</h5>
{{ this_node.nickname_icon }}
<h4>v{{ version }}</h4>
<h4>Domain: {{ domain }}</h4>
<h2>${ this_node.nickname }</h2>
<h5>(${ checksum_address })</h5>
${ this_node.nickname_icon }
<h4>v${ version }</h4>
<h4>Domain: ${ domain }</h4>
<h3>Fleet State</h3>
<div class="state">
<h4>{{ known_nodes.nickname }}</h4>
{{ known_nodes.icon }}
<h4>${ known_nodes.nickname }</h4>
${ known_nodes.icon }
<br/>
<span class="small">{{ known_nodes.updated }}</span>
<span class="small">${ known_nodes.updated }</span>
</ul>
</div>
<div id="previous-states">
<h3>Previous States</h3>
{% for state in previous_states %}
%for state in previous_states:
<div class="state">
<h5>{{ state.nickname }}</h5>
{{ state.icon }}
<h5>${ state.nickname }</h5>
${ state.icon }
<br/>
<span class="small">{{ state.updated }}</span>
<span class="small">${ state.updated }</span>
</div>
{% endfor %}
%endfor
</div>
</div>
<div id="known-nodes">
@ -140,18 +140,18 @@
<td>Last Seen</td>
<td>Fleet State</td>
</thead>
{% for node in known_nodes -%}
%for node in known_nodes:
<tr>
<td>{{ node.nickname_icon }}</td>
<td>${ node.nickname_icon }</td>
<td>
<a href="https://{{ node.rest_url()}}/status">{{ node.nickname }}</a>
<br/><span class="small">{{ node.checksum_address }}</span>
<a href="https://${node.rest_url()}/status">${ node.nickname }</a>
<br/><span class="small">${ node.checksum_address }</span>
</td>
<td>{{ node.timestamp }}</td>
<td>{{ node.last_seen }}</td>
<td>{{ node.fleet_state_icon }}</td>
<td>${ node.timestamp }</td>
<td>${ node.last_seen }</td>
<td>${ node.fleet_state_icon }</td>
</tr>
{%- endfor %}
%endfor
</table>
</div>
</html>

View File

@ -22,7 +22,7 @@ import json
import maya
import time
from base64 import b64encode
from jinja2 import Template
from mako.template import Template
import requests
from ansible.playbook import Playbook
@ -233,9 +233,7 @@ class BaseCloudNodeConfigurator:
def generate_ansible_inventory(self, staker_addresses, wipe_nucypher=False):
status_template = Template(self._inventory_template)
inventory_content = status_template.render(
inventory_content = self._inventory_template.render(
deployer=self,
nodes=[value for key, value in self.config['instances'].items() if key in staker_addresses],
wipe_nucypher=wipe_nucypher
@ -276,7 +274,8 @@ class BaseCloudNodeConfigurator:
@property
def _inventory_template(self):
return open(os.path.join(os.path.dirname(__file__), 'templates', 'cloud_deploy_ansible_inventory.j2'), 'r').read()
template_path = os.path.join(os.path.dirname(__file__), 'templates', 'cloud_deploy_ansible_inventory.mako')
return Template(filename=template_path)
def deploy_nucypher_on_existing_nodes(self, staker_addresses, wipe_nucypher=False):

View File

@ -1,32 +0,0 @@
all:
children:
nucypher:
children:
{{deployer.network}}:
children:
nodes:
vars:
network_name: "{{deployer.network}}"
geth_options: "--{{deployer.chain_name}}"
geth_dir: '/home/nucypher/geth/.ethereum/{{deployer.chain_name}}/'
geth_container_geth_datadir: "/root/.ethereum/{{deployer.chain_name}}"
nucypher_container_geth_datadir: "/root/.local/share/geth/.ethereum/{{deployer.chain_name}}"
etherscan_domain: {{deployer.chain_name}}.etherscan.io
ansible_python_interpreter: /usr/bin/python3
ansible_connection: ssh
NUCYPHER_KEYRING_PASSWORD: {{deployer.config['keyringpassword']}}
NUCYPHER_WORKER_ETH_PASSWORD: {{deployer.config['ethpassword']}}
nucypher_image: {{deployer.config['nucypher_image']}}
blockchain_provider: {{deployer.config['blockchain_provider']}}
node_is_decentralized: {{deployer.nodes_are_decentralized}}
prometheus: {% if deployer.config.get('use-prometheus') %}--prometheus --metrics-port {{deployer.PROMETHEUS_PORT}}{% endif %}
SEED_NODE_URI:{% if deployer.config.get('seed_node') %} {{deployer.config['seed_node']}}{% endif %}
{% if deployer.config.get('sentry_dsn')%}SENTRY_DSN: {{deployer.config['sentry_dsn']}}{% endif %}
wipe_nucypher_config: {{wipe_nucypher}}
hosts:{% for node in nodes %}
{{node.publicaddress}}:{% for attr in node.provider_deploy_attrs %}
{{attr.key}}: {{attr.value}}{% endfor %}{% if node.blockchain_provider %}
blockchain_provider: {{node.blockchain_provider}}{% endif %}{% if node.nucypher_image %}
nucypher_image: {{node.nucypher_image}}{% endif %}{% if node.sentry_dsn %}
sentry_dsn: {{node.sentry_dsn}}{% endif %}
{% endfor %}

View File

@ -0,0 +1,51 @@
all:
children:
nucypher:
children:
${deployer.network}:
children:
nodes:
vars:
network_name: "${deployer.network}"
geth_options: "--${deployer.chain_name}"
geth_dir: '/home/nucypher/geth/.ethereum/${deployer.chain_name}/'
geth_container_geth_datadir: "/root/.ethereum/${deployer.chain_name}"
nucypher_container_geth_datadir: "/root/.local/share/geth/.ethereum/${deployer.chain_name}"
etherscan_domain: ${deployer.chain_name}.etherscan.io
ansible_python_interpreter: /usr/bin/python3
ansible_connection: ssh
NUCYPHER_KEYRING_PASSWORD: ${deployer.config['keyringpassword']}
NUCYPHER_WORKER_ETH_PASSWORD: ${deployer.config['ethpassword']}
nucypher_image: ${deployer.config['nucypher_image']}
blockchain_provider: ${deployer.config['blockchain_provider']}
node_is_decentralized: ${deployer.nodes_are_decentralized}
%if deployer.config.get('use-prometheus'):
prometheus: --prometheus --metrics-port ${deployer.PROMETHEUS_PORT}
%else:
prometheus:
%endif
%if deployer.config.get('seed_node'):
SEED_NODE_URI: ${deployer.config['seed_node']}
%else:
SEED_NODE_URI:
%endif
%if deployer.config.get('sentry_dsn'):
SENTRY_DSN: ${deployer.config['sentry_dsn']}
%endif
wipe_nucypher_config: ${wipe_nucypher}
hosts:
%for node in nodes:
${node.publicaddress}:
%for attr in node.provider_deploy_attrs:
${attr.key}: ${attr.value}
%endfor
% if node.blockchain_provider:
blockchain_provider: {{node.blockchain_provider}}
%endif
%if node.nucypher_image:
nucypher_image: ${node.nucypher_image}
%endif
%if node.sentry_dsn:
sentry_dsn: ${node.sentry_dsn}
%endif
%endfor