diff --git a/.gitignore b/.gitignore index 1b78f21..bdf5947 100644 --- a/.gitignore +++ b/.gitignore @@ -57,3 +57,5 @@ docs/_build/ target/ .test + +.spyderproject \ No newline at end of file diff --git a/README.md b/README.md index 60c612d..5a3b878 100644 --- a/README.md +++ b/README.md @@ -48,3 +48,14 @@ optional arguments: - To access environment variables use `{{'default'| env('VARIABLE_NAME')}}` where `VARIABLE_NAME` is the name of your environment variable and `default` is the default value you want it to be if the environment variable is not set. + + +## Sample +``` +python meta-compose.py -d samples/data/testdata -t samples/template/meta-compose.yml -o samples/output/docker-compose.yml + +python meta-compose.py -t samples/template/gateway.tmpl -d samples/data/gateway.json -o samples/output/gateway.properties + +python meta-compose.py -t samples/template/app.tmpl -d samples/data/app.json -o samples/output/app.js + +``` diff --git a/meta-compose b/meta-compose deleted file mode 100755 index 41ddfe9..0000000 --- a/meta-compose +++ /dev/null @@ -1,3 +0,0 @@ -#!/usr/bin/env python -from metacompose.cli.main import main -main() diff --git a/meta-compose.py b/meta-compose.py new file mode 100644 index 0000000..7310e28 --- /dev/null +++ b/meta-compose.py @@ -0,0 +1,3 @@ +#!/usr/bin/env python +from metacompose.cli import main +main() diff --git a/metacompose/__init__.py b/metacompose/__init__.py index 7525d19..66a87bb 100644 --- a/metacompose/__init__.py +++ b/metacompose/__init__.py @@ -1 +1 @@ -__version__ = '0.1.4' +__version__ = '0.1.5' diff --git a/metacompose/cli/__init__.py b/metacompose/cli/__init__.py index e69de29..0c87819 100644 --- a/metacompose/cli/__init__.py +++ b/metacompose/cli/__init__.py @@ -0,0 +1 @@ +from main import main \ No newline at end of file diff --git a/metacompose/cli/main.py b/metacompose/cli/main.py index c9f54d0..a3a7948 100644 --- a/metacompose/cli/main.py +++ b/metacompose/cli/main.py @@ -4,12 +4,17 @@ import yaml from jinja2 import Environment, FileSystemLoader, StrictUndefined import argparse +from pprint import pprint def env_override(value, key): + print "%s: %s" % ("debug",os.getenv(key, value)) + #pprint (vars(env_override())) return os.getenv(key, value) def fileglob(pathname): + print "%s: %s" % ("debug",glob.glob(pathname)) + #pprint (vars(env_override)) return glob.glob(pathname) @@ -42,6 +47,7 @@ def setup_jinja(template_file_name): jinja = Environment(loader=FileSystemLoader("."), undefined=StrictUndefined) jinja.filters['env'] = env_override jinja.filters['fileglob'] = fileglob + return jinja.get_template(template_file_name) def collect_data(data_file_names): @@ -54,18 +60,23 @@ def collect_data(data_file_names): return data def write_compose_file(composition, outputfile): - + print "..." with open(outputfile, "w") as fh: fh.write(composition) + print "compose file generated" def main(): args = parse_args() + print "args: " + print args template = setup_jinja(args.template) data = collect_data(["meta-compose-data.yml"] + args.datafile) + print "data: " + print data write_compose_file(template.render(data), args.outputfile) diff --git a/samples/data/app.json b/samples/data/app.json new file mode 100644 index 0000000..e680fd2 --- /dev/null +++ b/samples/data/app.json @@ -0,0 +1,6 @@ +{ + "app": a, + "nat1": b1, + "nat2": b2, + "rt": c +} diff --git a/samples/data/gateway.json b/samples/data/gateway.json new file mode 100644 index 0000000..c5bad4f --- /dev/null +++ b/samples/data/gateway.json @@ -0,0 +1,12 @@ +{ "gateway": + {"debug": "false","host": "gw.cp.wmcloud-docker.com","port": 8081,"db": {"host": "10.21.234.42","port": "6379","database": 0,"password": "","statistic": {"host": "10.21.234.42","port": 6379,"database": 3,"password": "" + },"maxTotal": 10,"maxIdle": 10 + },"statsd": {"host": "127.0.0.1","port": 8125 + },"idp": {"url": "http://app.cp.wmcloud-docker.com/login.html" },"connection": {"pool": 100 },"route": {"pool": 30 },"timeout": 90,"resource": {"base": "/errorpage" },"thread": {"pool": 4096 },"phantomScript": {"path": "/opt/seo.js" },"authorization": {"endpoint": "http://10.20.131.232:9764/authorizationService/authorize" },"cloud": {"domain": ".wmcloud-docker.com" },"mq": {"host": "10.21.234.43","vhost": "/gateway","port": 5672,"username": "gateway","password": "gateway_pass" + },"cors": {"hosts": "app.cp.wmcloud-docker.com,127.0.0.1,localhost,10.21.234.41" },"api": {"authurl": "http://127.0.0.1:8080/authority" },"health": {"check": {"port": 8081,"rate": 300000,"redis": {"host": "10.21.234.42","port": 6379,"db": 5,"password": "" + },"cluster": "gateway","nodeid": 1,"window": 320000 + },"status": {"green": 20,"yellow": 40 + } + },"aop": {"timer": { "on": "false" } } + } +} diff --git a/samples/data/testdata b/samples/data/testdata new file mode 100644 index 0000000..372f079 --- /dev/null +++ b/samples/data/testdata @@ -0,0 +1,30 @@ +{ + hello : { + world : "Hello World" , + 'on': false + }, + seq: [1,2,3,4,5,6], + + linksList: { + "Google":{ + "Web":"www.web.de", + "Apps":{ + "Drive": "DriveLink", + "Dropbox": "DropboxLink" + }, + "Google Main":"http://mail.google.com", + "G+":"http://plus.google.com" + }, + "Social":{ + "Facebook":"http://www.facebook.de", + "G+":"https://plus.google.com", + "Xing":"http://www.xing.de", + "LinkedIn":"http://www.linkedin.com", + "Tumblr":"http://www.tumblr.com" + }, + "Fun":{ + "Reddit":"http://www.reddit.com" + } + } + +} \ No newline at end of file diff --git a/samples/output/app.js b/samples/output/app.js new file mode 100644 index 0000000..56ffe12 --- /dev/null +++ b/samples/output/app.js @@ -0,0 +1,7 @@ +{ + a: { + "nat1": b1, + "nat2": b2, + "rt": c + } +} \ No newline at end of file diff --git a/samples/output/docker-compose.yml b/samples/output/docker-compose.yml new file mode 100644 index 0000000..8aa404d --- /dev/null +++ b/samples/output/docker-compose.yml @@ -0,0 +1,86 @@ +build: + args: + buildno: 1 + user: Hello World + check: False + javaHome: C:\Program Files\Java\jdk1.8.0_65 + +build: + args: + - buildno=1 + - user=Hello World + - javaHome=C:\Program Files\Java\jdk1.8.0_65build: + args: + buildno: 2 + user: Hello World + check: False + javaHome: C:\Program Files\Java\jdk1.8.0_65 + +build: + args: + - buildno=2 + - user=Hello World + - javaHome=C:\Program Files\Java\jdk1.8.0_65build: + args: + buildno: 3 + user: Hello World + check: False + javaHome: C:\Program Files\Java\jdk1.8.0_65 + +build: + args: + - buildno=3 + - user=Hello World + - javaHome=C:\Program Files\Java\jdk1.8.0_65build: + args: + buildno: 4 + user: Hello World + check: False + javaHome: C:\Program Files\Java\jdk1.8.0_65 + +build: + args: + - buildno=4 + - user=Hello World + - javaHome=C:\Program Files\Java\jdk1.8.0_65build: + args: + buildno: 5 + user: Hello World + check: False + javaHome: C:\Program Files\Java\jdk1.8.0_65 + +build: + args: + - buildno=5 + - user=Hello World + - javaHome=C:\Program Files\Java\jdk1.8.0_65build: + args: + buildno: 6 + user: Hello World + check: False + javaHome: C:\Program Files\Java\jdk1.8.0_65 + +build: + args: + - buildno=6 + - user=Hello World + - javaHome=C:\Program Files\Java\jdk1.8.0_65 + + + +1>Fun : +2>- Reddit : "http://www.reddit.com" +1>Google : +2>- Web : "www.web.de" +2>- Google Main : "http://mail.google.com" +2>Apps : +3>- Dropbox : "DropboxLink" +3>- Drive : "DriveLink" +2>- G+ : "http://plus.google.com" +1>Social : +2>- Tumblr : "http://www.tumblr.com" +2>- Facebook : "http://www.facebook.de" +2>- G+ : "https://plus.google.com" +2>- Xing : "http://www.xing.de" +2>- LinkedIn : "http://www.linkedin.com" + \ No newline at end of file diff --git a/samples/output/gateway.properties b/samples/output/gateway.properties new file mode 100644 index 0000000..67b6ab6 --- /dev/null +++ b/samples/output/gateway.properties @@ -0,0 +1,44 @@ +gateway.debug=false +gateway.host=gw.cp.wmcloud-docker.com +gateway.port=8081 +gateway.db.host=10.21.234.42 +gateway.db.port=6379 +gateway.db.database=0 +gateway.db.password= +gateway.db.statistic.host=10.21.234.42 +gateway.db.statistic.port=6379 +gateway.db.statistic.database=3 +gateway.db.statistic.password= +gateway.db.maxTotal=10 +gateway.db.maxIdle=10 +gateway.db.maxWait=10000 +gateway.statsd.host=127.0.0.1 +gateway.statsd.port=8125 +gateway.idp.url=http://app.cp.wmcloud-docker.com/login.html +gateway.connection.pool=100 +gateway.route.pool=30 +gateway.timeout=90 +gateway.resource.base=/errorpage +gateway.thread.pool=4096 +gateway.phantomScript.path=/opt/seo.js +gateway.authorization.endpoint=http://10.20.131.232:9764/authorizationService/authorize +gateway.cloud.domain=.wmcloud-docker.com +gateway.mq.host=10.21.234.43 +gateway.mq.vhost=/gateway +gateway.mq.port=5672 +gateway.mq.username=gateway +gateway.mq.password=gateway_pass +gateway.cors.hosts=app.cp.wmcloud-docker.com,127.0.0.1,localhost,10.21.234.41 +gateway.api.authurl=http://127.0.0.1:8080/authority +gateway.health.check.port=8081 +gateway.health.check.rate=300000 +gateway.health.check.redis.host=10.21.234.42 +gateway.health.check.redis.port=6379 +gateway.health.check.redis.db=5 +gateway.health.check.redis.password= +gateway.health.check.cluster=gateway +gateway.health.check.nodeid=1 +gateway.health.check.window=320000 +gateway.aop.timer.on=false +gateway.health.status.green=20 +gateway.health.status.yellow=40 \ No newline at end of file diff --git a/samples/template/app.tmpl b/samples/template/app.tmpl new file mode 100644 index 0000000..bb25b21 --- /dev/null +++ b/samples/template/app.tmpl @@ -0,0 +1,7 @@ +{ + {{ app }}: { + "nat1": {{ nat1 }}, + "nat2": {{ nat2 }}, + "rt": {{ rt }} + } +} \ No newline at end of file diff --git a/samples/template/gateway.tmpl b/samples/template/gateway.tmpl new file mode 100644 index 0000000..4836fd4 --- /dev/null +++ b/samples/template/gateway.tmpl @@ -0,0 +1,44 @@ +gateway.debug={{gateway.debug}} +gateway.host={{gateway.host}} +gateway.port={{gateway.port}} +gateway.db.host={{gateway.db.host}} +gateway.db.port={{gateway.db.port}} +gateway.db.database={{gateway.db.database}} +gateway.db.password={{gateway.db.password}} +gateway.db.statistic.host={{gateway.db.statistic.host}} +gateway.db.statistic.port={{gateway.db.statistic.port}} +gateway.db.statistic.database={{gateway.db.statistic.database}} +gateway.db.statistic.password={{gateway.db.statistic.password}} +gateway.db.maxTotal={{gateway.db.maxTotal}} +gateway.db.maxIdle={{gateway.db.maxIdle}} +gateway.db.maxWait=10000 +gateway.statsd.host={{gateway.statsd.host}} +gateway.statsd.port={{gateway.statsd.port}} +gateway.idp.url={{gateway.idp.url}} +gateway.connection.pool={{gateway.connection.pool}} +gateway.route.pool={{gateway.route.pool}} +gateway.timeout={{gateway.timeout}} +gateway.resource.base={{gateway.resource.base}} +gateway.thread.pool={{gateway.thread.pool}} +gateway.phantomScript.path={{gateway.phantomScript.path}} +gateway.authorization.endpoint={{gateway.authorization.endpoint}} +gateway.cloud.domain={{gateway.cloud.domain}} +gateway.mq.host={{gateway.mq.host}} +gateway.mq.vhost={{gateway.mq.vhost}} +gateway.mq.port={{gateway.mq.port}} +gateway.mq.username={{gateway.mq.username}} +gateway.mq.password={{gateway.mq.password}} +gateway.cors.hosts={{gateway.cors.hosts}} +gateway.api.authurl={{gateway.api.authurl}} +gateway.health.check.port={{gateway.health.check.port}} +gateway.health.check.rate={{gateway.health.check.rate}} +gateway.health.check.redis.host={{gateway.health.check.redis.host}} +gateway.health.check.redis.port={{gateway.health.check.redis.port}} +gateway.health.check.redis.db={{gateway.health.check.redis.db}} +gateway.health.check.redis.password={{gateway.health.check.redis.password}} +gateway.health.check.cluster={{gateway.health.check.cluster}} +gateway.health.check.nodeid={{gateway.health.check.nodeid}} +gateway.health.check.window={{gateway.health.check.window}} +gateway.aop.timer.on={{gateway.aop.timer.on}} +gateway.health.status.green={{gateway.health.status.green}} +gateway.health.status.yellow={{gateway.health.status.yellow}} diff --git a/samples/template/meta-compose.yml b/samples/template/meta-compose.yml new file mode 100644 index 0000000..2f18e89 --- /dev/null +++ b/samples/template/meta-compose.yml @@ -0,0 +1,26 @@ +{% for item in seq -%} + +build: + args: + buildno: {{item}} + user: {{hello.world}} + check: {{hello.on}} + javaHome: {{'cat'| env('JAVA_HOME')}} + +build: + args: + - buildno={{item}} + - user={{hello.world}} + - javaHome={{'dog'| env('JAVA_HOME')}} + +{%- endfor %} + + +{% for key, value in linksList.items() recursive %} +{{loop.depth}}> {%- if value is string -%} +- {{key}} : "{{ value }}" + {%- else -%} +{{ key }} : {{ loop(value.items()) }} + {%- endif -%} +{% endfor %} + \ No newline at end of file