envtoconf.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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.excluded_envs = ['HADOOP_CONF_DIR']
  34. self.configurables = {}
  35. def destination_file_path(self, name, extension):
  36. """destination file path"""
  37. return os.path.join(self.output_dir, "{}.{}".format(name, extension))
  38. def write_env_var(self, name, extension, key, value):
  39. """Write environment variables"""
  40. with open(self.destination_file_path(name, extension) + ".raw", "a") as myfile:
  41. myfile.write("{}: {}\n".format(key, value))
  42. def process_envs(self):
  43. """Process environment variables"""
  44. for key in os.environ.keys():
  45. if key in self.excluded_envs:
  46. continue
  47. pattern = re.compile("[_\\.]")
  48. parts = pattern.split(key)
  49. extension = None
  50. name = parts[0].lower()
  51. if len(parts) > 1:
  52. extension = parts[1].lower()
  53. config_key = key[len(name) + len(extension) + 2:].strip()
  54. if extension and "!" in extension:
  55. splitted = extension.split("!")
  56. extension = splitted[0]
  57. fmt = splitted[1]
  58. config_key = key[len(name) + len(extension) + len(fmt) + 3:].strip()
  59. else:
  60. fmt = extension
  61. if extension and extension in self.known_formats:
  62. if name not in self.configurables.keys():
  63. with open(self.destination_file_path(name, extension) + ".raw", "w") as myfile:
  64. myfile.write("")
  65. self.configurables[name] = (extension, fmt)
  66. self.write_env_var(name, extension, config_key, os.environ[key])
  67. else:
  68. for configurable_name in self.configurables:
  69. if key.lower().startswith(configurable_name.lower()):
  70. self.write_env_var(configurable_name,
  71. self.configurables[configurable_name],
  72. key[len(configurable_name) + 1:],
  73. os.environ[key])
  74. def transform(self):
  75. """transform"""
  76. for configurable_name in self.configurables:
  77. name = configurable_name
  78. extension, fmt = self.configurables[name]
  79. destination_path = self.destination_file_path(name, extension)
  80. with open(destination_path + ".raw", "r") as myfile:
  81. content = myfile.read()
  82. transformer_func = getattr(transformation, "to_" + fmt)
  83. content = transformer_func(content)
  84. with open(destination_path, "w") as myfile:
  85. myfile.write(content)
  86. def main(self):
  87. """main"""
  88. # add the
  89. self.process_envs()
  90. # copy file.ext.raw to file.ext in the destination directory, and
  91. # transform to the right format (eg. key: value ===> XML)
  92. self.transform()
  93. def main():
  94. """main"""
  95. Simple(sys.argv[1:]).main()
  96. if __name__ == '__main__':
  97. Simple(sys.argv[1:]).main()