Skip to content

Instantly share code, notes, and snippets.

@dirkjot
Created March 23, 2017 15:31
Show Gist options
  • Save dirkjot/76a3613765705f7c2e3e365baa3755ef to your computer and use it in GitHub Desktop.
Save dirkjot/76a3613765705f7c2e3e365baa3755ef to your computer and use it in GitHub Desktop.
Copying configuration property files into mule-app.properties for deployment to Mulesoft MMC
#!/usr/bin/env python
"""
Copy env from config file to mule-app.properties, this is useful for deploying to an MMC. On cloudhub, you can set a
variable (mule.env) to determine which property file will be loaded but on MMC this does not work. This deployment script
may help.
Dirk P. Janssen
Spring 2017
"""
import argparse
import doctest
import logging
import re
logging.basicConfig()
logger = logging.getLogger("copy_env")
config_src_template = "src/main/resources/config/%s.properties"
config_dest = "src/main/app/mule-app.properties"
cut_here = "##-cut here-##"
cut_here_re = re.compile(cut_here)
def smart_concat(linesFrom, linesTo):
""""Copy linesFrom to the end of linesTo, removing parts of lineTo after
optional break indicator ('##-cut here-##') and inserting such an indicator"""
for lineno, line in enumerate(linesTo):
if cut_here_re.match(line):
result = linesTo[:lineno]
break
else:
result = linesTo[:]
result.append(cut_here + "\n")
result.extend(linesFrom)
return result
def apply_vars(environment, verbose=False):
environmentFilename = config_src_template % environment
logger.info("Reading environment file %s", environmentFilename)
newstuff = open(environmentFilename).readlines()
logger.info("Environment file: %d lines", len(newstuff))
with open(config_dest) as destfile:
contents = destfile.readlines()
logger.info("Destination file: %d lines before operation", len(contents))
contents = smart_concat(newstuff, contents)
logger.info("Destination file: %d lines after operation", len(contents))
contents = "".join(contents)
with open(config_dest, "w") as destfile:
destfile.write(contents)
def main():
parser = argparse.ArgumentParser(description='Apply environment variables')
parser.add_argument('environment', help="Environment name")
parser.add_argument('--verbose', '-v', action="store_true",
help="Show details of operation")
args = parser.parse_args()
logger.setLevel(logging.INFO if args.verbose else logging.WARNING)
apply_vars(args.environment)
if __name__ == '__main__':
main()
import copy_env
import unittest
class Test_smart_concat(unittest.TestCase):
def test_basic_op(self):
linesTo = """lineA/lineB/##-cut here-## old stuff/line C""".split("/")
linesFrom = """fromA/fromB/fromC""".split("/")
result = copy_env.smart_concat(linesFrom, linesTo)
expected = """lineA/lineB/##-cut here-##\n/fromA/fromB/fromC""".split("/")
self.assertEqual(result, expected)
def test_no_cut(self):
linesTo = """lineA/lineB""".split("/")
linesFrom = """fromA/fromB/fromC""".split("/")
result = copy_env.smart_concat(linesFrom, linesTo)
expected = """lineA/lineB/##-cut here-##\n/fromA/fromB/fromC""".split("/")
self.assertEqual(result, expected)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment