You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

52 lines
1.6 KiB

  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. """Generate Odoo server configuration from templates"""
  4. import os
  5. from contextlib import closing
  6. from string import Template
  7. from doodbalib import logger
  8. try:
  9. # Python 2, where io.StringIO fails because it is unicode-only
  10. from StringIO import StringIO
  11. except ImportError:
  12. from io import StringIO
  13. try:
  14. from configparser import RawConfigParser
  15. parser = RawConfigParser(strict=False)
  16. except ImportError:
  17. # Python 2, where strict=True doesn't exist
  18. from ConfigParser import RawConfigParser
  19. parser = RawConfigParser()
  20. ODOO_VERSION = os.environ.get("ODOO_VERSION")
  21. TARGET_FILE = os.environ.get("ODOO_RC", "/opt/odoo/auto/odoo.conf")
  22. CONFIG_DIRS = ("/opt/odoo/common/conf.d", "/opt/odoo/custom/conf.d")
  23. CONFIG_FILES = []
  24. # Read all configuraiton files found in those folders
  25. logger.info("Merging found configuration files in %s", TARGET_FILE)
  26. for dir_ in CONFIG_DIRS:
  27. try:
  28. for file_ in sorted(os.listdir(dir_)):
  29. parser.read(os.path.join(dir_, file_))
  30. except OSError: # TODO Use FileNotFoundError when we drop python 2
  31. continue
  32. # Write it to a memory string object
  33. with closing(StringIO()) as resultfp:
  34. parser.write(resultfp)
  35. resultfp.seek(0)
  36. # Obtain the config string
  37. result = resultfp.read()
  38. # Expand environment variables found within
  39. result = Template(result).substitute(os.environ)
  40. logger.debug("Resulting configuration:\n%s", result)
  41. # Write it to destination
  42. with open(TARGET_FILE, "w") as targetfp:
  43. targetfp.write(result)