From 3a477a7f59f3ce60225a693d2f176280d1ca9e7c Mon Sep 17 00:00:00 2001 From: "Peiming.Hu" Date: Wed, 11 May 2016 13:08:22 +0800 Subject: [PATCH 1/4] first commit --- README.md | 10 +++++ meta-compose => meta-compose.py | 0 metacompose/cli/main.py | 13 +++++- samples/data/gateway.json | 63 +++++++++++++++++++++++++++++ samples/data/testdata | 7 ++++ samples/output/docker-compose.yml | 67 +++++++++++++++++++++++++++++++ samples/output/gateway.properties | 44 ++++++++++++++++++++ samples/template/gateway.tmpl | 44 ++++++++++++++++++++ samples/template/meta-compose.yml | 16 ++++++++ 9 files changed, 263 insertions(+), 1 deletion(-) rename meta-compose => meta-compose.py (100%) mode change 100755 => 100644 create mode 100644 samples/data/gateway.json create mode 100644 samples/data/testdata create mode 100644 samples/output/docker-compose.yml create mode 100644 samples/output/gateway.properties create mode 100644 samples/template/gateway.tmpl create mode 100644 samples/template/meta-compose.yml diff --git a/README.md b/README.md index 60c612d..3998a7c 100644 --- a/README.md +++ b/README.md @@ -48,3 +48,13 @@ 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 + + +``` \ No newline at end of file diff --git a/meta-compose b/meta-compose.py old mode 100755 new mode 100644 similarity index 100% rename from meta-compose rename to meta-compose.py 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/gateway.json b/samples/data/gateway.json new file mode 100644 index 0000000..6360cf8 --- /dev/null +++ b/samples/data/gateway.json @@ -0,0 +1,63 @@ +{ + 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..c9bc66b --- /dev/null +++ b/samples/data/testdata @@ -0,0 +1,7 @@ +{ + hello : { + world : "Hello World" , + 'on': false + }, + seq: [1,2,3,4,5,6] +} \ 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..8466716 --- /dev/null +++ b/samples/output/docker-compose.yml @@ -0,0 +1,67 @@ +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 \ 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/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..d779ae4 --- /dev/null +++ b/samples/template/meta-compose.yml @@ -0,0 +1,16 @@ +{% 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 %} \ No newline at end of file From 3d4b07b84f6e6c368ea8d36affc4890a4ed8ebf0 Mon Sep 17 00:00:00 2001 From: "Peiming.Hu" Date: Mon, 16 May 2016 11:39:07 +0800 Subject: [PATCH 2/4] refine formats --- README.md | 2 +- samples/data/testdata | 25 ++++++++++++++++++++++++- samples/output/docker-compose.yml | 21 ++++++++++++++++++++- samples/template/meta-compose.yml | 12 +++++++++++- 4 files changed, 56 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 3998a7c..c29a847 100644 --- a/README.md +++ b/README.md @@ -57,4 +57,4 @@ python meta-compose.py -d samples/data/testdata -t samples/template/meta-compose python meta-compose.py -t samples/template/gateway.tmpl -d samples/data/gateway.json -o samples/output/gateway.properties -``` \ No newline at end of file +``` diff --git a/samples/data/testdata b/samples/data/testdata index c9bc66b..372f079 100644 --- a/samples/data/testdata +++ b/samples/data/testdata @@ -3,5 +3,28 @@ world : "Hello World" , 'on': false }, - seq: [1,2,3,4,5,6] + 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/docker-compose.yml b/samples/output/docker-compose.yml index 8466716..8aa404d 100644 --- a/samples/output/docker-compose.yml +++ b/samples/output/docker-compose.yml @@ -64,4 +64,23 @@ build: args: - buildno=6 - user=Hello World - - javaHome=C:\Program Files\Java\jdk1.8.0_65 \ No newline at end of file + - 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/template/meta-compose.yml b/samples/template/meta-compose.yml index d779ae4..2f18e89 100644 --- a/samples/template/meta-compose.yml +++ b/samples/template/meta-compose.yml @@ -13,4 +13,14 @@ build: - user={{hello.world}} - javaHome={{'dog'| env('JAVA_HOME')}} -{%- endfor %} \ No newline at end of file +{%- 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 From 0e02cd0103b3cb38c72519b5f220ceb460cf3243 Mon Sep 17 00:00:00 2001 From: "Peiming.Hu" Date: Wed, 25 May 2016 15:23:49 +0800 Subject: [PATCH 3/4] change imported package --- meta-compose.py | 2 +- metacompose/__init__.py | 2 +- metacompose/cli/__init__.py | 1 + 3 files changed, 3 insertions(+), 2 deletions(-) diff --git a/meta-compose.py b/meta-compose.py index 41ddfe9..7310e28 100644 --- a/meta-compose.py +++ b/meta-compose.py @@ -1,3 +1,3 @@ #!/usr/bin/env python -from metacompose.cli.main import main +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 From 1fba42bb4ec8c61da43767e1c83d42f8e234a559 Mon Sep 17 00:00:00 2001 From: "Peiming.Hu" Date: Tue, 3 Oct 2017 21:11:05 +0800 Subject: [PATCH 4/4] add samples --- .gitignore | 2 ++ README.md | 1 + samples/data/app.json | 6 ++++ samples/data/gateway.json | 69 +++++---------------------------------- samples/output/app.js | 7 ++++ samples/template/app.tmpl | 7 ++++ 6 files changed, 32 insertions(+), 60 deletions(-) create mode 100644 samples/data/app.json create mode 100644 samples/output/app.js create mode 100644 samples/template/app.tmpl 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 c29a847..5a3b878 100644 --- a/README.md +++ b/README.md @@ -56,5 +56,6 @@ python meta-compose.py -d samples/data/testdata -t samples/template/meta-compose 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/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 index 6360cf8..c5bad4f 100644 --- a/samples/data/gateway.json +++ b/samples/data/gateway.json @@ -1,63 +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 +{ "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' } }, + },"aop": {"timer": { "on": "false" } } } } 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/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