envtoconf.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #!/usr/bin/python
  2. #
  3. # Licensed to the Apache Software Foundation (ASF) under one or more
  4. # contributor license agreements. See the NOTICE file distributed with
  5. # this work for additional information regarding copyright ownership.
  6. # The ASF licenses this file to You under the Apache License, Version 2.0
  7. # (the "License"); you may not use this file except in compliance with
  8. # the License. You may obtain a copy of the License at
  9. #
  10. # http://www.apache.org/licenses/LICENSE-2.0
  11. #
  12. # Unless required by applicable law or agreed to in writing, software
  13. # distributed under the License is distributed on an "AS IS" BASIS,
  14. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. # See the License for the specific language governing permissions and
  16. # limitations under the License.
  17. #
  18. """convert environment variables to config"""
  19. import os
  20. import re
  21. import argparse
  22. import sys
  23. import transformation
  24. class Simple(object):
  25. """Simple conversion"""
  26. def __init__(self, args):
  27. parser = argparse.ArgumentParser()
  28. parser.add_argument("--destination", help="Destination directory", required=True)
  29. self.args = parser.parse_args(args=args)
  30. # copy the default files to file.raw in destination directory
  31. self.known_formats = ['xml', 'properties', 'yaml', 'yml', 'env', "sh", "cfg", 'conf']
  32. self.output_dir = self.args.destination
  33. self.configurables = {}
  34. def destination_file_path(self, name, extension):
  35. """destination file path"""
  36. return os.path.join(self.output_dir, "{}.{}".format(name, extension))
  37. def write_env_var(self, name, extension, key, value):
  38. """Write environment variables"""
  39. with open(self.destination_file_path(name, extension) + ".raw", "a") as myfile:
  40. myfile.write("{}: {}\n".format(key, value))
  41. def process_envs(self):
  42. """Process environment variables"""
  43. for key in os.environ.keys():
  44. pattern = re.compile("[_\\.]")
  45. parts = pattern.split(key)
  46. extension = None
  47. name = parts[0].lower()
  48. if len(parts) > 1:
  49. extension = parts[1].lower()
  50. config_key = key[len(name) + len(extension) + 2:].strip()
  51. if extension and "!" in extension:
  52. splitted = extension.split("!")
  53. extension = splitted[0]
  54. fmt = splitted[1]
  55. config_key = key[len(name) + len(extension) + len(fmt) + 3:].strip()
  56. else:
  57. fmt = extension
  58. if extension and extension in self.known_formats:
  59. if name not in self.configurables.keys():
  60. with open(self.destination_file_path(name, extension) + ".raw", "w") as myfile:
  61. myfile.write("")
  62. self.configurables[name] = (extension, fmt)
  63. self.write_env_var(name, extension, config_key, os.environ[key])
  64. else:
  65. for configurable_name in self.configurables:
  66. if key.lower().startswith(configurable_name.lower()):
  67. self.write_env_var(configurable_name,
  68. self.configurables[configurable_name],
  69. key[len(configurable_name) + 1:],
  70. os.environ[key])
  71. def transform(self):
  72. """transform"""
  73. for configurable_name in self.configurables:
  74. name = configurable_name
  75. extension, fmt = self.configurables[name]
  76. destination_path = self.destination_file_path(name, extension)
  77. with open(destination_path + ".raw", "r") as myfile:
  78. content = myfile.read()
  79. transformer_func = getattr(transformation, "to_" + fmt)
  80. content = transformer_func(content)
  81. with open(destination_path, "w") as myfile:
  82. myfile.write(content)
  83. def main(self):
  84. """main"""
  85. # add the
  86. self.process_envs()
  87. # copy file.ext.raw to file.ext in the destination directory, and
  88. # transform to the right format (eg. key: value ===> XML)
  89. self.transform()
  90. def main():
  91. """main"""
  92. Simple(sys.argv[1:]).main()
  93. if __name__ == '__main__':
  94. Simple(sys.argv[1:]).main()