From 5c77fc70538a93155cfd19b6b48d791fd3887e64 Mon Sep 17 00:00:00 2001 From: cdinuta Date: Wed, 29 May 2019 23:32:42 +0300 Subject: [PATCH 1/7] fixing Python 3: ResourceWarning: unclosed file <_io.TextIOWrapper added integration command --- .travis.yml | 5 ++++- README.md | 2 +- render.py | 3 ++- render_test.py | 8 ++++---- variables/json.json | 3 ++- variables/yml.yml | 2 +- 6 files changed, 14 insertions(+), 9 deletions(-) diff --git a/.travis.yml b/.travis.yml index fc409a3..c456a82 100644 --- a/.travis.yml +++ b/.travis.yml @@ -13,4 +13,7 @@ before_install: script: - docker exec jinja2docker python3 -m unittest discover $SCRIPTS_DIR "*_test.py" - - docker-compose down -v \ No newline at end of file + - docker-compose down -v + - docker run -i -v $TRAVIS_BUILD_DIR\templates:/data + -v $TRAVIS_BUILD_DIR\variables:/variables -e TEMPLATE=standalone.j2 + -e VARIABLES=variables.yml -e DATABASE=mysql56 -e IMAGE=latest dinutac/jinja2docker:latest diff --git a/README.md b/README.md index 23fd6dc..6414600 100644 --- a/README.md +++ b/README.md @@ -26,7 +26,7 @@ Example: ``` run -i -v C:\Users\cdinuta\IdeaProjects\jinja2docker\templates:/data \ -v C:\Users\cdinuta\IdeaProjects\jinja2docker\variables:/variables -e TEMPLATE=standalone.j2 \ --e VARIABLES=variables.yml -e DATABASE=mysql56 dinutac/jinja2docker:latest > docker-compose.yml +-e VARIABLES=variables.yml -e DATABASE=mysql56 -e IMAGE=latest dinutac/jinja2docker:latest > docker-compose.yml ``` ### Example template ```json-template.j2``` diff --git a/render.py b/render.py index 588874f..dde78a7 100644 --- a/render.py +++ b/render.py @@ -33,7 +33,8 @@ def env_override(self, value, key): return os.getenv(key, value) def rend_template(self, argv): - data = yaml.load(open(self.VARS_DIR + "/" + self.variables, closefd=True), Loader=yaml.Loader) + with open(self.VARS_DIR + "/" + self.variables, closefd=True) as f: + data = yaml.load(f, Loader=yaml.Loader) self.env.filters['yaml'] = self.yaml_filter self.env.globals["environ"] = lambda key: os.environ.get(key) diff --git a/render_test.py b/render_test.py index 095c6bd..0669e8a 100644 --- a/render_test.py +++ b/render_test.py @@ -12,8 +12,8 @@ def test_json(self): r = Render(os.environ['TEMPLATE'], os.environ['VARIABLES']) template = yaml.load(r.rend_template("dummy"), Loader=yaml.Loader) - - data = yaml.load(open(r.VARS_DIR + "/" + r.variables, closefd=True), Loader=yaml.Loader) + with open(r.VARS_DIR + "/" + r.variables, closefd=True) as f: + data = yaml.load(f, Loader=yaml.Loader) self.assertEqual(template.get("os"), data.get("os"), ) self.assertEqual(template.get("version"), data.get("version")) self.assertEqual(template.get("installed_apps"), data.get("installed_apps")) @@ -24,8 +24,8 @@ def test_yml(self): r = Render(os.environ['TEMPLATE'], os.environ['VARIABLES']) template = yaml.load(r.rend_template("dummy"), Loader=yaml.Loader) - - data = yaml.load(open(r.VARS_DIR + "/" + r.variables, closefd=True), Loader=yaml.Loader) + with open(r.VARS_DIR + "/" + r.variables, closefd=True) as f: + data = yaml.load(f, Loader=yaml.Loader) self.assertEqual(template, data) diff --git a/variables/json.json b/variables/json.json index e40321a..01f4d9e 100644 --- a/variables/json.json +++ b/variables/json.json @@ -1,4 +1,5 @@ { "os" : "windows", - "version": 10 + "version": 10, + "installed_apps": "json" } \ No newline at end of file diff --git a/variables/yml.yml b/variables/yml.yml index bdc4669..0d06f5b 100644 --- a/variables/yml.yml +++ b/variables/yml.yml @@ -1,3 +1,3 @@ -installed_apps: "Intellij, Chrome" +installed_apps: "yml" os: windows version: 10 From 967be8faee33f16825c22c7b76ef1265156c0591 Mon Sep 17 00:00:00 2001 From: cdinuta Date: Wed, 29 May 2019 23:36:11 +0300 Subject: [PATCH 2/7] escaping --- .travis.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index c456a82..2014923 100644 --- a/.travis.yml +++ b/.travis.yml @@ -14,6 +14,6 @@ before_install: script: - docker exec jinja2docker python3 -m unittest discover $SCRIPTS_DIR "*_test.py" - docker-compose down -v - - docker run -i -v $TRAVIS_BUILD_DIR\templates:/data - -v $TRAVIS_BUILD_DIR\variables:/variables -e TEMPLATE=standalone.j2 + - docker run -i -v $TRAVIS_BUILD_DIR\\templates:/data + -v $TRAVIS_BUILD_DIR\\variables:/variables -e TEMPLATE=standalone.j2 -e VARIABLES=variables.yml -e DATABASE=mysql56 -e IMAGE=latest dinutac/jinja2docker:latest From 21c408a309710dabd6c09173466c35083ab5463c Mon Sep 17 00:00:00 2001 From: cdinuta Date: Wed, 29 May 2019 23:45:18 +0300 Subject: [PATCH 3/7] changed to fwd / --- .travis.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index 2014923..718d4fc 100644 --- a/.travis.yml +++ b/.travis.yml @@ -14,6 +14,6 @@ before_install: script: - docker exec jinja2docker python3 -m unittest discover $SCRIPTS_DIR "*_test.py" - docker-compose down -v - - docker run -i -v $TRAVIS_BUILD_DIR\\templates:/data - -v $TRAVIS_BUILD_DIR\\variables:/variables -e TEMPLATE=standalone.j2 + - docker run -i -v $TRAVIS_BUILD_DIR/templates:/data + -v $TRAVIS_BUILD_DIR/variables:/variables -e TEMPLATE=standalone.j2 -e VARIABLES=variables.yml -e DATABASE=mysql56 -e IMAGE=latest dinutac/jinja2docker:latest From 9eb0fa27c37c8f1d3c11120a9cf03c1506d144a7 Mon Sep 17 00:00:00 2001 From: cdinuta Date: Thu, 30 May 2019 11:39:50 +0300 Subject: [PATCH 4/7] added hybrid way for templating --- README.md | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 6414600..17f6822 100644 --- a/README.md +++ b/README.md @@ -24,7 +24,7 @@ docker run -i -v :/data \ Example: ``` -run -i -v C:\Users\cdinuta\IdeaProjects\jinja2docker\templates:/data \ +docker run -i -v C:\Users\cdinuta\IdeaProjects\jinja2docker\templates:/data \ -v C:\Users\cdinuta\IdeaProjects\jinja2docker\variables:/variables -e TEMPLATE=standalone.j2 \ -e VARIABLES=variables.yml -e DATABASE=mysql56 -e IMAGE=latest dinutac/jinja2docker:latest > docker-compose.yml ``` @@ -73,10 +73,12 @@ Example {{yourYamlVariableHere | yaml | safe }} The recommendation is either paste selectively smaller chunks of yaml or use json whenever possible. ## Latest updates + ### Integrated Jinja2 Cli https://github.com/mattrobenolt/jinja2-cli +#### 1. Generate template with container up - run the docker compose: ``docker-compose up`` - run the docker exec command with the jinja2-cli params as per documentation: https://github.com/mattrobenolt/jinja2-cli by specifying template and variables folders. @@ -87,3 +89,11 @@ Example: ``` docker exec -e DATABASE=mysql56 -e IMAGE=latest jinja2docker jinja2 /data/standalone.j2 /variables/variables.yml --format=yml > docker-compose.yml ``` + +#### 2. Hybrid call +``` +docker run --entrypoint jinja2 -v C:\Users\cdinuta\IdeaProjects\jinja2docker\templates:/data -v C:\Users\cdinuta\IdeaProjects\jinja2docker\variables:/variables d +inutac/jinja2docker:latest /data/json.j2 /variables/json.json --format=yml +``` + +! observe that jinja2 is called before image name and the arguments after From 7a84b498f05047246ca4d553a37e92cafd883c0a Mon Sep 17 00:00:00 2001 From: cdinuta Date: Thu, 30 May 2019 11:42:16 +0300 Subject: [PATCH 5/7] added hybrid way for templating --- README.md | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 17f6822..09f50bf 100644 --- a/README.md +++ b/README.md @@ -92,8 +92,10 @@ docker exec -e DATABASE=mysql56 -e IMAGE=latest jinja2docker jinja2 /data/standa #### 2. Hybrid call ``` -docker run --entrypoint jinja2 -v C:\Users\cdinuta\IdeaProjects\jinja2docker\templates:/data -v C:\Users\cdinuta\IdeaProjects\jinja2docker\variables:/variables d -inutac/jinja2docker:latest /data/json.j2 /variables/json.json --format=yml +docker run --entrypoint jinja2 -v C:\Users\cdinuta\IdeaProjects\jinja2docker\templates:/data \ +-v C:\Users\cdinuta\IdeaProjects\jinja2docker\variables:/variables \ +dinutac/jinja2docker:latest \ +/data/json.j2 /variables/json.json --format=json ``` ! observe that jinja2 is called before image name and the arguments after From bcb3913c9ee9a2ef02d40cf3f97c13dd5c5a3e5a Mon Sep 17 00:00:00 2001 From: cdinuta Date: Thu, 30 May 2019 11:43:08 +0300 Subject: [PATCH 6/7] added hybrid way for templating --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 09f50bf..744aa63 100644 --- a/README.md +++ b/README.md @@ -92,7 +92,8 @@ docker exec -e DATABASE=mysql56 -e IMAGE=latest jinja2docker jinja2 /data/standa #### 2. Hybrid call ``` -docker run --entrypoint jinja2 -v C:\Users\cdinuta\IdeaProjects\jinja2docker\templates:/data \ +docker run --entrypoint jinja2 \ +-v C:\Users\cdinuta\IdeaProjects\jinja2docker\templates:/data \ -v C:\Users\cdinuta\IdeaProjects\jinja2docker\variables:/variables \ dinutac/jinja2docker:latest \ /data/json.j2 /variables/json.json --format=json From c1f27f18fad7e614bed89e5e4bcbcfe673b4f8ef Mon Sep 17 00:00:00 2001 From: cdinuta Date: Thu, 30 May 2019 11:46:53 +0300 Subject: [PATCH 7/7] added hybrid call to travis --- .travis.yml | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 718d4fc..42eb4db 100644 --- a/.travis.yml +++ b/.travis.yml @@ -14,6 +14,16 @@ before_install: script: - docker exec jinja2docker python3 -m unittest discover $SCRIPTS_DIR "*_test.py" - docker-compose down -v - - docker run -i -v $TRAVIS_BUILD_DIR/templates:/data + + - docker run -i + -v $TRAVIS_BUILD_DIR/templates:/data -v $TRAVIS_BUILD_DIR/variables:/variables -e TEMPLATE=standalone.j2 -e VARIABLES=variables.yml -e DATABASE=mysql56 -e IMAGE=latest dinutac/jinja2docker:latest + + - docker run --entrypoint jinja2 + -v $TRAVIS_BUILD_DIR/templates:/data + -v $TRAVIS_BUILD_DIR/variables:/variables + dinutac/jinja2docker:latest + /data/json.j2 /variables/json.json --format=json + +