container_tamer.py 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. #!/usr/bin/env python
  2. import json
  3. import os
  4. from jinja2 import Environment, FileSystemLoader
  5. # Load configuration from config.json
  6. with open('config.json', 'r') as config_file:
  7. config = json.load(config_file)
  8. dictfile = {}
  9. # Read variables from the TEMPLATE_VARS dictionary
  10. dictfile = config["TEMPLATE_VARS"]
  11. def do_template(dict, out, templ, run_script=False, run_dir=None, shell=None):
  12. env = Environment(loader=FileSystemLoader('./'))
  13. template = env.get_template(templ)
  14. rendered_content = template.render(dict)
  15. print("Dictionary applied", json.dumps(dict, indent=4), "To, ", out)
  16. with open(out, 'w') as outfile:
  17. outfile.write(rendered_content)
  18. print("Runscript:", str(run_script))
  19. if run_script:
  20. os.chmod(out, 0o755)
  21. if run_dir != "none" and shell != "none":
  22. os.system(f"cd {run_dir} && {shell} {out}")
  23. else:
  24. os.system(out)
  25. # Run jinja2 templates
  26. templates = config["JINJA_TEMP"]
  27. for item in templates:
  28. out_tpl = item["OUT_FILE"]
  29. in_tpl = item["IN_FILE"]
  30. run_script = item.get("RUN_SCRIPT", False)
  31. run_dir = item.get("RUN_DIR")
  32. shell = item.get("SHELL")
  33. do_template(dictfile, out_tpl, in_tpl, run_script=run_script, run_dir=run_dir, shell=shell)