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)
in list(ACCESS_PROTOCOLS.items())]
CI_META_DATA_DEF = """
instance-id: {{hostname}}
local-hostname: {{hostname}}
instance-id: {{ hostname }}
local-hostname: {{ hostname }}
""".strip()
CI_USER_DATA_DEF = """
......@@ -87,14 +87,14 @@ CI_USER_DATA_DEF = """
users:
- default
- name: {{sysuser}}
- name: {{ sysuser }}
sudo: ['ALL=(ALL) NOPASSWD:ALL']
groups: sudo
shell: /bin/bash
ssh_pwauth: True
chpasswd: { expire: False }
lock-passwd: false
passwd: "{{password}}"
passwd: "{{ password | hash }}"
""".strip()
try:
......@@ -254,11 +254,18 @@ class InstanceTemplate(AclBase, VirtualMachineDescModel, TimeStampedModel):
return 'template.%d' % self.pk
class CITemplate:
def rndstr(self, 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:
def __init__(self, instance):
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,
datas = {
"sysuser": "cloud",
"hostname": self.short_hostname,
"password": sha512_crypt.hash(self.pw),
"password": self.pw,
"owner": str(self.owner.username),
"net": NetTemplate(self),
"acl": AclTemplate(self),
......@@ -421,19 +428,19 @@ class Instance(AclBase, VirtualMachineDescModel, StatusModel, OperatedMixin,
def get_user_data(self):
data = str(self.ci_user_data)
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)
@property
def get_meta_data(self):
data = str(self.ci_meta_data)
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)
def validate_ci_data(self, data):
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)
yaml.dump(yaml.load(data, Loader=yaml.Loader))
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