From 929b5263a00a1880fffe697f8f077e00498d2326 Mon Sep 17 00:00:00 2001 From: Michele Carignani <michele.carignani@etsi.org> Date: Tue, 27 Oct 2020 16:04:56 +0100 Subject: [PATCH] Prepare for release --- .gitignore | 1 + Readme.md | 8 ++++++++ relaunch.sh | 3 ++- src/config.py | 18 ++++++++++++++++++ src/web_app.py | 50 ++++++++++++++++++++++++++++++++------------------ 5 files changed, 61 insertions(+), 19 deletions(-) diff --git a/.gitignore b/.gitignore index 0d20b64..e35672b 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ *.pyc +env diff --git a/Readme.md b/Readme.md index d3dd6f7..9e71065 100644 --- a/Readme.md +++ b/Readme.md @@ -1,5 +1,9 @@ # Tosca Import/Export +## Create `env` file + + cp env_example env + $EDITOR env # Customize SECRET ## Run with docker @@ -20,3 +24,7 @@ Install prerequisites Run the web_app python src/web_app.py + +## Licensing + +See LICENSE file. \ No newline at end of file diff --git a/relaunch.sh b/relaunch.sh index 5024d45..fbb6bb6 100755 --- a/relaunch.sh +++ b/relaunch.sh @@ -1,3 +1,4 @@ +#!/bin/bash CNT=$(docker ps | grep tosca-ie | cut -d " " -f1) if [ "$CNT" != "" ] ; then @@ -10,5 +11,5 @@ fi docker build -t tosca-ie-sample:latest . -docker run -d -t -p 5000:5000 tosca-ie-sample +docker run -d --restart unless-stopped -t -p 5000:5000 --env-file ./env tosca-ie-sample diff --git a/src/config.py b/src/config.py index fe96268..f2bce3d 100644 --- a/src/config.py +++ b/src/config.py @@ -1 +1,19 @@ +''' +Configuration values +''' + +import os + +def env_or_false(key): + ''' + Lookup environment key, returns False is not present + ''' + try: + return os.environ[key] + except KeyError: + return False + VERSION = "0.0.4" + +SECRET = env_or_false("TOSCAIE_SECRET") or 'super_secret_key' + diff --git a/src/web_app.py b/src/web_app.py index cfb0e25..191ca62 100644 --- a/src/web_app.py +++ b/src/web_app.py @@ -1,4 +1,8 @@ #!/bin/python3 +''' +Web app to offer a frontend to the doc2tosca and tosca2doc tools + +''' import os import shutil @@ -15,8 +19,8 @@ import doc2tosca class ReverseProxied(object): - def __init__(self, app, script_name=None, scheme=None, server=None): - self.app = app + def __init__(self, the_app, script_name=None, scheme=None, server=None): + self.app = the_app self.script_name = script_name self.scheme = scheme self.server = server @@ -36,7 +40,6 @@ class ReverseProxied(object): environ['HTTP_HOST'] = server return self.app(environ, start_response) - app = Flask(__name__) app.wsgi_app = ReverseProxied(app.wsgi_app, script_name='/tosca-ie') @@ -45,11 +48,15 @@ UPLOAD_FOLDER = '/tmp/upload' ALLOWED_EXTENSIONS = set(['txt', 'yaml', 'docx']) app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER -app.secret_key = 'super secret key' +app.secret_key = config.SECRET app.config['SESSION_TYPE'] = 'filesystem' TOSCA_DEFS = ['VNFD', 'NSD', 'PNFD'] +MISSING_MODULE_ERR_MSG = \ + 'Error: some TOSCA module missing. Make sure to \ + upload definitions for NSD, VNFD, PNFD.' + def allowed_file(filename): ''' Check filename is in the list of allowed extensions @@ -57,13 +64,16 @@ def allowed_file(filename): return '.' in filename and \ filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS - @app.route("/") def hello(): ''' Render home page ''' - return render_template("./home.html", VERSION=config.VERSION, doc_allowed_versions=doc2tosca.allowed_versions) + return render_template( + "./home.html", + VERSION=config.VERSION, + doc_allowed_versions=doc2tosca.allowed_versions + ) @app.route("/doc2tosca-info") def doc2tosca_info(): @@ -81,6 +91,9 @@ def tosca2doc_info(): @app.route("/tosca2doc", methods=['POST']) def mk_tosca2doc(): + ''' + Execute tosca2doc on the uploaded files + ''' if 'file' not in request.files: flash('Error: No file uploaded.') @@ -94,12 +107,8 @@ def mk_tosca2doc(): if tosca_def in uploaded_file.filename or \ tosca_def.lower() in uploaded_file.filename: found=True - #if uploaded_file: - # print "-------------------" - # print uploaded_file.read() - # print "-------------------" if not found: - flash('Error: some TOSCA module missing. Make sure to upload definitions for NSD, VNFD, PNFD.') + flash(MISSING_MODULE_ERR_MSG) print('Error: TOSCA module missing.') return redirect("/tosca-ie") @@ -133,27 +142,29 @@ def mk_tosca2doc(): #return ("No content found") def get_all_yaml_file_paths(directory): + ''' + Finds yaml files within a directory and sub directories and + returns the list of paths + ''' - # initializing empty file paths list file_paths = [] - # crawling through directory and subdirectories - for root, directories, files in os.walk(directory): + for root, _, files in os.walk(directory): for filename in files: if ".yaml" in filename: - # join the two strings in order to form the full filepath. filepath = os.path.join(root, filename) file_paths.append(filepath) - print(filepath) - # returning all file paths return file_paths @app.after_request def after_request(response): + ''' + Clean up files created by doc2tosca + ''' if request.path != '/doc2tosca': return response - if g.get('tmp_dir') == None: + if g.get('tmp_dir') is None: return response shutil.rmtree(g.tmp_dir, ignore_errors=True) print("Deleted {}\n\n".format(g.tmp_dir)) @@ -161,6 +172,9 @@ def after_request(response): @app.route("/doc2tosca", methods=['POST']) def mk_doc2tosca(): + ''' + Executes doc2tosca on the uploaded file + ''' zip_fn = "tosca_defs.zip" -- GitLab