From 0d6c5c827bbce67be000a1b7738487a6c9a2ccaa Mon Sep 17 00:00:00 2001
From: Rolf Niepraschk <Rolf.Niepraschk@ptb.de>
Date: Mon, 17 Aug 2020 13:39:05 +0200
Subject: [PATCH] better version handling; preparation of automatic update (1)

---
 README.md        | 50 +++++++++++++++++++++++++++++++++++++-----------
 requirements.txt | 10 +++++-----
 server           | 10 +++++++---
 server.py        | 23 ++++++++++++++--------
 utils.py         | 10 +++++++---
 5 files changed, 73 insertions(+), 30 deletions(-)

diff --git a/README.md b/README.md
index 25c38d0..741cfed 100644
--- a/README.md
+++ b/README.md
@@ -2,19 +2,50 @@
 
 REST service for the validation of ```xml``` against a XML Schema (```xsd```).
 
-## Installation as Daemon
+## install & activate
 
-Do the following with root permission:
+Create a file `.git/hooks/pre-push` in the GIT repository with the following 
+content:
 
 ```shell
-cd /usr/local/share
-git config --global http.sslVerify false  
-git clone https://gitlab1.ptb.de/niepra01/xml-validation.git 
-cd xml-validation 
+#!/bin/bash
+
+# Writes the short tag value to the VERSION file in the top-level directory
+# of the Git repository
+
+toplevel=$(git rev-parse --show-toplevel)
+f=${toplevel}/VERSION
+touch $f
+old_version=$(cat $f)
+new_version=$(git describe --tags --abbrev=0)
+
+if [ "$old_version" != "$new_version" ]; then
+  echo "$new_version" > $f
+  git commit -am "update version to '"$new_version"'"
+  git push origin
+fi
+
+exit 0
+```
+
+Then make this file executable:
+
+```shell
+chmod 755 .git/hooks/pre-push
+```
+
+Do the following steps with root permission on the server installation:
+
+```shell
+mkdir -p /usr/local/share/xml-validation
+cd /usr/local/share/xml-validation
+curl --silent --output - \
+  https://gitlab1.ptb.de/niepra01/xml-validation/-/archive/master/xml-validation-master.tar \
+  | tar xf - --strip-components 1           # only once
 python3 -m venv .
 source bin/activate
-pip install pip-tools
-pip-sync
+pip3 install pip-tools
+pip-compile
 chown -R nobody.nobody ../xml-validation
 systemctl daemon-reload                     # if already exist
 systemctl link $PWD/xml-validation.service
@@ -22,9 +53,6 @@ systemctl enable xml-validation.service     # make permanent
 systemctl start xml-validation.service      # running?
 systemctl status xml-validation.service
 journalctl -f --unit xml-validation.service # run-time check
-...
-sudo -u nobody git -c http.sslVerify=false pull
-sudo systemctl restart xml-validation.service
 ```
 
 ## Configuration
diff --git a/requirements.txt b/requirements.txt
index dd0878f..7cf0bf3 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -8,17 +8,17 @@ certifi==2019.11.28       # via requests
 chardet==3.0.4            # via requests
 click==7.0                # via flask
 elementpath==1.3.1        # via xmlschema
-flask-cors==3.0.8
-flask==1.1.1
+flask-cors==3.0.8         # via -r requirements.in
+flask==1.1.1              # via -r requirements.in, flask-cors
 gitdb2==2.0.6             # via gitpython
-gitpython==3.0.5
+gitpython==3.0.5          # via -r requirements.in
 idna==2.8                 # via requests
 itsdangerous==1.1.0       # via flask
 jinja2==2.10.3            # via flask
 markupsafe==1.1.1         # via jinja2
-requests==2.22.0
+requests==2.22.0          # via -r requirements.in
 six==1.13.0               # via flask-cors
 smmap2==2.0.5             # via gitdb2
 urllib3==1.25.7           # via requests
 werkzeug==0.16.0          # via flask
-xmlschema==1.0.16
+xmlschema==1.0.16         # via -r requirements.in
diff --git a/server b/server
index be09e05..721f6f7 100755
--- a/server
+++ b/server
@@ -1,6 +1,10 @@
-#!/bin/sh
+#!/bin/bash
 export FLASK_APP=server.py
 export FLASK_DEBUG=1
-export FLASK_ENV=development
+export FLASK_ENV=development # TODO: FLASK_ENV=production
 
-python server.py
\ No newline at end of file
+python3 -m venv ./
+source bin/activate
+pip-sync
+pip3 install -e ./
+python3 server.py
diff --git a/server.py b/server.py
index ba75c7e..a35b048 100644
--- a/server.py
+++ b/server.py
@@ -1,11 +1,10 @@
-from flask import Flask, request, send_from_directory
+from flask import Flask, request, jsonify, send_from_directory
 from flask_cors import CORS
 import utils as utils
 from trans import Trans
-#from pprint import pprint
+from pprint import pprint
 
 config = utils.get_config_dict()
-git_cmd = utils.git_cmd(config)
 
 app = Flask(__name__)
 CORS(app)
@@ -14,10 +13,18 @@ trans = Trans()
 @app.route('/version', methods=['GET'])
 def version():
     app.logger.debug('hit version')
-    ret = utils.return_version(version=git_cmd.describe())
-    
-    return utils.xml_response(ret)
-
+    version = utils.get_version()
+    if version:
+        return jsonify({'version':version})
+    else:
+        return utils.return_error('Version unknown')
+        
+@app.route('/update', methods=['POST'])
+def update():
+    app.logger.debug('hit update')
+    req = request.get_json()
+    pprint(req) 
+        
 @app.route('/validate', methods=['POST'])
 def validate():
     app.logger.debug('hit validate')
@@ -45,7 +52,7 @@ def validation():
     if l != 'de':
         l = 'en'
     x = "['2.3.0','2.2.0','2.1.1','2.1.0']"
-    return trans.show_html(version=git_cmd.describe(), language=l, 
+    return trans.show_html(version=utils.get_version(), language=l, 
       xsd_versions=x)
 
 @app.route('/js/<fn>', methods=['GET'])
diff --git a/utils.py b/utils.py
index 0258184..cffec5b 100644
--- a/utils.py
+++ b/utils.py
@@ -1,5 +1,4 @@
 import json
-import git
 import requests
 from flask import Response
 from xml.etree import ElementTree as ET
@@ -22,8 +21,13 @@ def get_config_dict():
 
     return config
 
-def git_cmd(config):
-    return git.cmd.Git(config['git']['dir'])
+def get_version():
+    try:
+        with open('./VERSION', 'r') as f:
+            version = f.read().rstrip()
+        return version
+    except:
+        return False
 
 def get_xsd(cfg, xsd_version=None, xml_str = None):
     filename =  cfg['xsd']['filename']
-- 
GitLab