Skip to content

Commit

Permalink
Tune dockerfile def (#17)
Browse files Browse the repository at this point in the history
* tunning the Dockerfile
  • Loading branch information
dinuta authored Jun 12, 2020
1 parent ff5024a commit a4406d3
Show file tree
Hide file tree
Showing 8 changed files with 149 additions and 163 deletions.
27 changes: 15 additions & 12 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,30 +1,33 @@
FROM alpine:3.11

# Install python3 and other deps
RUN apk add --no-cache python3
RUN pip3 install --upgrade pip==20.1 setuptools==46.1.3 --no-cache

RUN pip3 install pip==20.1.1 setuptools==47.1.1 --no-cache
RUN apk add --no-cache build-base sshpass

## Cleanup
RUN rm -rf /var/cache/apk/*

# Create a shared data volume
# Create folders
RUN mkdir /data/
RUN mkdir /variables/

## Expose some volumes
# Expose some volumes
VOLUME ["/data"]
VOLUME ["/variables"]

# Set needed env vars
ENV SCRIPTS_DIR /scripts
ENV TEMPLATES_DIR /data
ENV VARS_DIR /variables
ENV SCRIPTS_DIR /scripts
ENV OUT_DIR out
ENV TEMPLATE docker-compose.j2
ENV VARIABLES variables.yml

COPY . $SCRIPTS_DIR/
RUN chmod +x $SCRIPTS_DIR/entities/*.py

# Copy extra scripts: embedded render and main flask service
COPY entities/render.py $SCRIPTS_DIR/entities/render.py
COPY main_flask.py $SCRIPTS_DIR/main_flask.py
COPY requirements.txt $SCRIPTS_DIR/requirements.txt

RUN chmod +x $SCRIPTS_DIR/entities/render.py
RUN chmod +x $SCRIPTS_DIR/main_flask.py

WORKDIR /data

RUN pip3 install -r $SCRIPTS_DIR/requirements.txt
Expand Down
4 changes: 2 additions & 2 deletions entities/render.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def yaml_filter(self, value):
def env_override(self, value, key):
return os.getenv(key, value)

def rend_template(self, argv):
def rend_template(self):
with open(self.VARS_DIR + "/" + self.variables, closefd=True) as f:
data = yaml.safe_load(f)

Expand All @@ -48,4 +48,4 @@ def rend_template(self, argv):


if __name__ == '__main__':
Render(os.environ.get('TEMPLATE'), os.environ.get('VARIABLES')).rend_template(sys.argv[1:])
Render(os.environ.get('TEMPLATE'), os.environ.get('VARIABLES')).rend_template()
131 changes: 130 additions & 1 deletion main_flask.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,135 @@
#!/usr/bin/env python3

from rest.render_flask_app import app
import os

from flask import Flask, request
from flask_cors import CORS
from flask_swagger_ui import get_swaggerui_blueprint

from entities.render import Render

unmodifiable_env_vars = {
"TEMPLATES_DIR": os.environ.get('TEMPLATES_DIR'),
"VARS_DIR": os.environ.get('VARS_DIR'),
"PATH": os.environ.get('PATH')
}

app = Flask(__name__, instance_relative_config=False)
CORS(app)
app.register_blueprint(get_swaggerui_blueprint(
base_url='/api/docs',
api_url='/swagger/swagger.yml',
config={
'app_name': "jinja2docker"
}), url_prefix='/api/docs')


@app.route('/swagger/swagger.yml')
def get_swagger():
return swagger_file_content


@app.route('/env')
def get_vars():
return dict(os.environ), 200


@app.route('/render/<template>/<variables>', methods=['POST'])
def get_content_with_env(template, variables):
os.environ['TEMPLATE'] = template
os.environ['VARIABLES'] = variables
try:
input_json = request.get_json(force=True)
for key, value in input_json.items():
if key not in unmodifiable_env_vars:
os.environ[key] = value
except:
pass
r = Render(os.environ['TEMPLATE'], os.environ['VARIABLES'])
try:
result = r.rend_template()
except Exception as e:
result = "Exception({})".format(e.__str__())

return result, 200


if __name__ == "__main__":
app.run(host='0.0.0.0', port=8080)

swagger_file_content = '''
"swagger": '2.0'
info:
description: |
This is Jinja2 with Docker.
version: "1.0.1"
title: Jinja2Docker
termsOfService: http://swagger.io/terms/
contact:
email: apiteam@swagger.io
license:
name: Apache 2.0
url: http://www.apache.org/licenses/LICENSE-2.0.html
# host: localhost:5000
basePath: /
tags:
- name: Jinja2
description: Rend templates with Jinja2 and Docker
externalDocs:
description: Find out more about our store
url: http://swagger.io
schemes:
- http
paths:
/env:
get:
tags:
- jinja2
summary: Print env vars
produces:
- application/json
responses:
200:
description: List of env vars in key value pairs
/render/{template}/{variables}:
post:
tags:
- jinja2
summary: Jinja2 render with inserted env vars
consumes:
- application/json
- application/x-www-form-urlencoded
produces:
- application/json
parameters:
- name: template
in: path
description: Template file mounted in docker
required: true
type: string
- name: variables
in: path
description: Variables file mounted in docker
required: true
type: string
- name: EnvVars
in: body
description: E.g. {"envvar1":"value1", "envvar2":"value2"}
required: false
schema:
$ref: '#/components/schemas/EnvVar'
responses:
200:
description: Jinja2 rendered template or the exception which occured.
components:
schemas:
EnvVar:
type: object
properties:
your_env_var:
type: string
externalDocs:
description: Find out more about Swagger
url: http://swagger.io
'''
11 changes: 0 additions & 11 deletions rest/__init__.py

This file was deleted.

3 changes: 0 additions & 3 deletions rest/flask_config.py

This file was deleted.

58 changes: 0 additions & 58 deletions rest/render_flask_app.py

This file was deleted.

74 changes: 0 additions & 74 deletions rest/static/swagger.yml

This file was deleted.

4 changes: 2 additions & 2 deletions tests/render_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def test_json(self):
os.environ['VARIABLES'] = "json.json"
r = Render(os.environ['TEMPLATE'], os.environ['VARIABLES'])

template = yaml.safe_load(r.rend_template("dummy"))
template = yaml.safe_load(r.rend_template())
with open(r.VARS_DIR + "/" + r.variables, closefd=True) as f:
data = yaml.safe_load(f)
self.assertEqual(template.get("os"), data.get("os"), )
Expand All @@ -26,7 +26,7 @@ def test_yml(self):
os.environ['VARIABLES'] = "yml.yml"
r = Render(os.environ['TEMPLATE'], os.environ['VARIABLES'])

template = yaml.safe_load(r.rend_template("dummy"))
template = yaml.safe_load(r.rend_template())
with open(r.VARS_DIR + "/" + r.variables, closefd=True) as f:
data = yaml.safe_load(f)
self.assertEqual(template, data)
Expand Down

0 comments on commit a4406d3

Please sign in to comment.