|
| 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 | + |
| 19 | +"""convert environment variables to config""" |
| 20 | + |
| 21 | +import os |
| 22 | +import re |
| 23 | + |
| 24 | +import argparse |
| 25 | + |
| 26 | +import sys |
| 27 | +import transformation |
| 28 | + |
| 29 | +class Simple(object): |
| 30 | + """Simple conversion""" |
| 31 | + def __init__(self, args): |
| 32 | + parser = argparse.ArgumentParser() |
| 33 | + parser.add_argument("--destination", help="Destination directory", required=True) |
| 34 | + self.args = parser.parse_args(args=args) |
| 35 | + # copy the default files to file.raw in destination directory |
| 36 | + |
| 37 | + self.known_formats = ['xml', 'properties', 'yaml', 'yml', 'env', "sh", "cfg", 'conf'] |
| 38 | + self.output_dir = self.args.destination |
| 39 | + self.excluded_envs = ['HADOOP_CONF_DIR'] |
| 40 | + self.configurables = {} |
| 41 | + |
| 42 | + def destination_file_path(self, name, extension): |
| 43 | + """destination file path""" |
| 44 | + return os.path.join(self.output_dir, "{}.{}".format(name, extension)) |
| 45 | + |
| 46 | + def write_env_var(self, name, extension, key, value): |
| 47 | + """Write environment variables""" |
| 48 | + with open(self.destination_file_path(name, extension) + ".raw", "a") as myfile: |
| 49 | + myfile.write("{}: {}\n".format(key, value)) |
| 50 | + |
| 51 | + def process_envs(self): |
| 52 | + """Process environment variables""" |
| 53 | + for key in os.environ.keys(): |
| 54 | + if key in self.excluded_envs: |
| 55 | + continue |
| 56 | + pattern = re.compile("[_\\.]") |
| 57 | + parts = pattern.split(key) |
| 58 | + extension = None |
| 59 | + name = parts[0].lower() |
| 60 | + if len(parts) > 1: |
| 61 | + extension = parts[1].lower() |
| 62 | + config_key = key[len(name) + len(extension) + 2:].strip() |
| 63 | + if extension and "!" in extension: |
| 64 | + splitted = extension.split("!") |
| 65 | + extension = splitted[0] |
| 66 | + fmt = splitted[1] |
| 67 | + config_key = key[len(name) + len(extension) + len(fmt) + 3:].strip() |
| 68 | + else: |
| 69 | + fmt = extension |
| 70 | + |
| 71 | + if extension and extension in self.known_formats: |
| 72 | + if name not in self.configurables.keys(): |
| 73 | + with open(self.destination_file_path(name, extension) + ".raw", "w") as myfile: |
| 74 | + myfile.write("") |
| 75 | + self.configurables[name] = (extension, fmt) |
| 76 | + self.write_env_var(name, extension, config_key, os.environ[key]) |
| 77 | + else: |
| 78 | + for configurable_name in self.configurables: |
| 79 | + if key.lower().startswith(configurable_name.lower()): |
| 80 | + self.write_env_var(configurable_name, |
| 81 | + self.configurables[configurable_name], |
| 82 | + key[len(configurable_name) + 1:], |
| 83 | + os.environ[key]) |
| 84 | + |
| 85 | + def transform(self): |
| 86 | + """transform""" |
| 87 | + for configurable_name in self.configurables: |
| 88 | + name = configurable_name |
| 89 | + extension, fmt = self.configurables[name] |
| 90 | + |
| 91 | + destination_path = self.destination_file_path(name, extension) |
| 92 | + |
| 93 | + with open(destination_path + ".raw", "r") as myfile: |
| 94 | + content = myfile.read() |
| 95 | + transformer_func = getattr(transformation, "to_" + fmt) |
| 96 | + content = transformer_func(content) |
| 97 | + with open(destination_path, "w") as myfile: |
| 98 | + myfile.write(content) |
| 99 | + |
| 100 | + def main(self): |
| 101 | + """main""" |
| 102 | + |
| 103 | + # add the |
| 104 | + self.process_envs() |
| 105 | + |
| 106 | + # copy file.ext.raw to file.ext in the destination directory, and |
| 107 | + # transform to the right format (eg. key: value ===> XML) |
| 108 | + self.transform() |
| 109 | + |
| 110 | + |
| 111 | +def main(): |
| 112 | + """main""" |
| 113 | + Simple(sys.argv[1:]).main() |
| 114 | + |
| 115 | + |
| 116 | +if __name__ == '__main__': |
| 117 | + Simple(sys.argv[1:]).main() |
0 commit comments