3 """Convert Managed Config files to JCasC YAML"""
5 from pprint import pformat
14 yaml = ruamel.yaml.YAML()
15 yaml.allow_duplicate_keys = True
16 yaml.preserve_quotes = True
18 logging.basicConfig(format="%(asctime)s %(levelname)s %(message)s",
22 """Validate that path provided exists"""
24 if os.path.isdir(path):
27 raise argparse.ArgumentTypeError(
28 "'%s' is not a valid path" % path)
31 """Parse the commandline arguments"""
33 parser = argparse.ArgumentParser(
34 description = __doc__,
35 formatter_class = argparse.ArgumentDefaultsHelpFormatter
38 # default argument options
40 "-o", "--output", help = "Output file",
41 type = argparse.FileType("w")
44 "-p", "--path", help = "Path to configuration files to read",
45 type = dir_path, required = True
48 "-q", "--quiet", help = "Run quiet", dest = "quiet",
53 help = "Is configuration being created for a sandbox",
54 dest = "sandbox", action = "store_true"
57 "-v", "--verbose", dest = "verbose", action = "store_true",
58 help = "Enable verbose (debug) logging"
61 return parser.parse_args()
63 def processConfig(path, subpath, files, sandbox):
64 """Process the configuration file and return the configuration object"""
66 logging.debug("In processConfig")
67 logging.debug("\n" + pformat(path) + "\n" + pformat(subpath) + "\n" + pformat(files))
76 # skip all hidden files
82 # Only load credential mappings files if the file matches
83 # the type of jenkins silo (ie production or sandbox)
84 if 'CredentialMappings' in config:
85 if sandbox and 'sandbox' in config:
87 elif not sandbox and 'sandbox' not in config:
93 stream = open(os.path.join(path, config), 'r')
94 ret[subpath[0]].update(yaml.load(stream))
96 if 'content' in config:
97 stream = open(os.path.join(path, config), 'r')
98 ret[subpath[0]].update({'content': stream.read()})
100 # custom files need to have ${ prefixed with ^ for some reason
101 if 'custom' in subpath[0] or 'properties' in subpath[0]:
102 ret[subpath[0]]['content'] = \
103 ret[subpath[0]]['content'].replace('${', '^${')
111 logging.getLogger().setLevel(logging.DEBUG)
114 logging.getLogger().setLevel(logging.ERROR)
116 output = { 'unclassified':
117 { 'globalConfigFiles':
124 pathsplit = args.path.split('/')
126 for (dirpath, dirnames, filenames) in os.walk(args.path):
127 logging.debug("\n" + pformat((dirpath, dirnames, filenames)))
129 curpath = [x for x in dirpath.split('/') if x not in pathsplit]
131 configs.append(processConfig(dirpath, curpath, filenames, args.sandbox))
134 output['unclassified']['globalConfigFiles']['configs'] = configs
137 yaml.dump(output, args.output)
139 yaml.dump(output, sys.stdout)
141 if __name__ == "__main__":