Commit 98a0d2ee by Karsa Zoltán István

jinja hash filter

parent d8ffc349
...@@ -78,8 +78,8 @@ ACCESS_METHODS = [(key, name) for key, (name, port, transport) ...@@ -78,8 +78,8 @@ ACCESS_METHODS = [(key, name) for key, (name, port, transport)
in list(ACCESS_PROTOCOLS.items())] in list(ACCESS_PROTOCOLS.items())]
CI_META_DATA_DEF = """ CI_META_DATA_DEF = """
instance-id: {{hostname}} instance-id: {{ hostname }}
local-hostname: {{hostname}} local-hostname: {{ hostname }}
""".strip() """.strip()
CI_USER_DATA_DEF = """ CI_USER_DATA_DEF = """
...@@ -87,14 +87,14 @@ CI_USER_DATA_DEF = """ ...@@ -87,14 +87,14 @@ CI_USER_DATA_DEF = """
users: users:
- default - default
- name: {{sysuser}} - name: {{ sysuser }}
sudo: ['ALL=(ALL) NOPASSWD:ALL'] sudo: ['ALL=(ALL) NOPASSWD:ALL']
groups: sudo groups: sudo
shell: /bin/bash shell: /bin/bash
ssh_pwauth: True ssh_pwauth: True
chpasswd: { expire: False } chpasswd: { expire: False }
lock-passwd: false lock-passwd: false
passwd: "{{password}}" passwd: "{{ password | hash }}"
""".strip() """.strip()
try: try:
...@@ -254,11 +254,18 @@ class InstanceTemplate(AclBase, VirtualMachineDescModel, TimeStampedModel): ...@@ -254,11 +254,18 @@ class InstanceTemplate(AclBase, VirtualMachineDescModel, TimeStampedModel):
return 'template.%d' % self.pk return 'template.%d' % self.pk
class CITemplate: class CITemplate:
def rndstr(self, len): def rndstr(self, len):
return ''.join(random.choice(string.ascii_letters) for i in range(int(len))) return ''.join(random.choice(string.ascii_letters) for i in range(int(len)))
def j2_hash(value, hash='sha512'):
return sha512_crypt.hash(value)
env = jinja2.Environment(trim_blocks=True, lstrip_blocks=True)
env.filters["hash"] = j2_hash
class AclTemplate: class AclTemplate:
def __init__(self, instance): def __init__(self, instance):
self.user_levels = list({ 'username': u.username, 'level': l } for u, l in instance.get_users_with_level()) self.user_levels = list({ 'username': u.username, 'level': l } for u, l in instance.get_users_with_level())
...@@ -408,7 +415,7 @@ class Instance(AclBase, VirtualMachineDescModel, StatusModel, OperatedMixin, ...@@ -408,7 +415,7 @@ class Instance(AclBase, VirtualMachineDescModel, StatusModel, OperatedMixin,
datas = { datas = {
"sysuser": "cloud", "sysuser": "cloud",
"hostname": self.short_hostname, "hostname": self.short_hostname,
"password": sha512_crypt.hash(self.pw), "password": self.pw,
"owner": str(self.owner.username), "owner": str(self.owner.username),
"net": NetTemplate(self), "net": NetTemplate(self),
"acl": AclTemplate(self), "acl": AclTemplate(self),
...@@ -421,19 +428,19 @@ class Instance(AclBase, VirtualMachineDescModel, StatusModel, OperatedMixin, ...@@ -421,19 +428,19 @@ class Instance(AclBase, VirtualMachineDescModel, StatusModel, OperatedMixin,
def get_user_data(self): def get_user_data(self):
data = str(self.ci_user_data) data = str(self.ci_user_data)
ci_datas = self.get_ci_data_dict() ci_datas = self.get_ci_data_dict()
template = jinja2.Template(data, trim_blocks=True, lstrip_blocks=True) template = env.from_string(data)
return template.render(ci_datas) return template.render(ci_datas)
@property @property
def get_meta_data(self): def get_meta_data(self):
data = str(self.ci_meta_data) data = str(self.ci_meta_data)
ci_datas = self.get_ci_data_dict() ci_datas = self.get_ci_data_dict()
template = jinja2.Template(data, trim_blocks=True, lstrip_blocks=True) template = env.from_string(data)
return template.render(ci_datas) return template.render(ci_datas)
def validate_ci_data(self, data): def validate_ci_data(self, data):
ci_datas = self.get_ci_data_dict() ci_datas = self.get_ci_data_dict()
template = jinja2.Template(data, trim_blocks=True, lstrip_blocks=True) template = env.from_string(data)
data = template.render(ci_datas) data = template.render(ci_datas)
yaml.dump(yaml.load(data, Loader=yaml.Loader)) yaml.dump(yaml.load(data, Loader=yaml.Loader))
return True return True
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment