Skip to content

Commit

Permalink
add jinja2 templating
Browse files Browse the repository at this point in the history
Signed-off-by: Jan-Marten Brüggemann <[email protected]>
  • Loading branch information
brueggemann committed Mar 14, 2024
1 parent 01757ab commit e1500b1
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 3 deletions.
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ fabric==3.2.2
google-auth==2.28.1
idna==3.6
invoke==2.2.0
Jinja2==3.1.3
kubernetes==29.0.0
MarkupSafe==2.1.5
oauthlib==3.2.2
paramiko==3.4.0
pyasn1==0.5.1
Expand Down
18 changes: 16 additions & 2 deletions src/rookify/__main__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# -*- coding: utf-8 -*-

import os
import rookify.modules

from types import MappingProxyType
Expand All @@ -21,19 +22,32 @@ def main() -> None:
except FileNotFoundError:
pass

# Get absolute path of the rookify instance
rookify_path = os.path.dirname(__file__)

# Run preflight requirement modules
for preflight_module in preflight_modules:
module_path = os.path.join(
rookify_path, "modules", preflight_module.MODULE_NAME
)
handler = preflight_module.HANDLER_CLASS(
config=MappingProxyType(config), data=MappingProxyType(module_data)
config=MappingProxyType(config),
data=MappingProxyType(module_data),
module_path=module_path,
)
result = handler.run()
module_data[preflight_module.MODULE_NAME] = result

# Run preflight and append handlers to list
handlers = list()
for migration_module in migration_modules:
module_path = os.path.join(
rookify_path, "modules", migration_module.MODULE_NAME
)
handler = migration_module.HANDLER_CLASS(
config=MappingProxyType(config), data=MappingProxyType(module_data)
config=MappingProxyType(config),
data=MappingProxyType(module_data),
module_path=module_path,
)
handler.preflight()
handlers.append((migration_module, handler))
Expand Down
33 changes: 32 additions & 1 deletion src/rookify/modules/module.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
# -*- coding: utf-8 -*-

import os
import yaml
import json
import abc
import rados
import kubernetes
import fabric
import jinja2
from typing import Any, Dict, List, Optional


Expand Down Expand Up @@ -82,7 +85,29 @@ def command(self, host: str, command: str) -> fabric.runners.Result:
).run(command, hide=True)
return result

def __init__(self, config: Dict[str, Any], data: Dict[str, Any]):
class __Template:
def __init__(self, template_path: str):
self.__result_raw = None
self.__result_yaml = None
self.__template_path = template_path
with open(template_path) as file:
self.__template = jinja2.Template(file.read())

def render(self, **variables):
self.__result_raw = self.__template.render(**variables)
self.__result_yaml = None

@property
def raw(self):
return self.__result_raw

@property
def yaml(self):
if not self.__result_yaml:
self.__result_yaml = yaml.safe_load(self.__result_raw)
return self.__result_yaml

def __init__(self, config: dict, data: dict, module_path: str):
"""
Construct a new 'ModuleHandler' object.
Expand Down Expand Up @@ -128,3 +153,9 @@ def ssh(self) -> __SSH:
if self.__ssh is None:
self.__ssh = ModuleHandler.__SSH(self._config["ssh"])
return self.__ssh

def load_template(self, filename: str, **variables) -> __Template:
template_path = os.path.join(self.__module_path, "templates", filename)
template = self.__Template(template_path)
template.render(**variables)
return template

0 comments on commit e1500b1

Please sign in to comment.