diff options
author | xengineering <mail2xengineering@protonmail.com> | 2021-02-11 17:37:58 +0100 |
---|---|---|
committer | xengineering <mail2xengineering@protonmail.com> | 2021-02-11 17:37:58 +0100 |
commit | f830274cb0c4f1efec2313f72007741957b86e76 (patch) | |
tree | 19aa8036db3f955d48e97b612881fede83966227 | |
parent | bc6b03ab29703dfff857b63727657ca3127eb381 (diff) | |
download | web-template-f830274cb0c4f1efec2313f72007741957b86e76.tar web-template-f830274cb0c4f1efec2313f72007741957b86e76.tar.zst web-template-f830274cb0c4f1efec2313f72007741957b86e76.zip |
Implement Framework Installation
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | Makefile | 12 | ||||
-rw-r--r-- | README.md | 41 | ||||
-rw-r--r-- | example_settings.json | 13 | ||||
-rwxr-xr-x | flask/main.py.jinja2 | 5 | ||||
-rw-r--r-- | manage.py | 112 | ||||
-rw-r--r-- | systemd/webtemplate.service.jinja2 | 4 |
7 files changed, 145 insertions, 43 deletions
@@ -1,2 +1,3 @@ settings.json archive +build @@ -2,10 +2,8 @@ all: - mkdir -p systemd/build - python3 manage.py build_systemd_files - mkdir -p flask/build - python3 manage.py build_flask_files + python3 manage.py build_systemd_file + python3 manage.py build_framework clean: rm -rf systemd/build @@ -13,9 +11,11 @@ clean: install: all python3 manage.py install_webroot - #find systemd/build -type f -exec sudo install -Dm 644 "{}" "/etc/systemd/system" \; + python3 manage.py install_framework + python3 manage.py install_systemd_file uninstall: python3 manage.py uninstall_webroot - #python3 manage.py uninstall_systemd_files + python3 manage.py uninstall_framework + python3 manage.py uninstall_systemd_file @@ -5,22 +5,49 @@ A template project for mixed static / dynamic web applications. +## Expected Environment + +- GNU/Linux +- systemd +- Git, make and python3 are installed + + +## Current State + +This project is in **early development** state. Please be very careful. The scripts behind the make targets will install and uninstall files on your system without any checks at the moment. Have a look at the ```Makefile``` and ```manage.py``` for details. + + ## Usage -Just follow these steps: -1. Edit config.json -2. Edit the files in the webroot folder -3. Upload the webroot to your target server with ```python3 manage.py deploy``` via SSH / rsync +You can run this example or a web service based on this example like this: + +``` +git clone https://gitea.xengineering.eu/xengineering/web-template.git +cd web-template +nano settings.json # tweak to your needs +make all +make install +``` + +**Make sure to not edit settings.json until you uninstalled the project!** + +You can remove it like this: + +``` +make uninstall +make clean +``` + +You can also make your own project based on web-template: -### Use it for your Project ``` -# start 'myproject' (rename it if you want to) git clone https://gitea.xengineering.eu/xengineering/web-template.git myproject cd myproject git remote rename origin template ``` -### Get the latest Updates from this template Project +You can then get updates from this template repository: + ``` git fetch template git merge template/master diff --git a/example_settings.json b/example_settings.json index c08c89c..ec9da38 100644 --- a/example_settings.json +++ b/example_settings.json @@ -2,12 +2,13 @@ { "project_name":"webtemplate", "project_description":"A Template Web Application", + + "webroot":"/srv/http", + + "framework":"flask", + "framework_port":"3009", + "framework_bind":"127.0.0.1", "user":"http", - "group":"http", - "webroot_installation_path":"/srv/http/example.com/public", - "web_framework":"flask", - "web_framework_installation_path":"/opt", - "framework_port":"8080", - "framework_bind":"127.0.0.1" + "group":"http" } diff --git a/flask/main.py.jinja2 b/flask/main.py.jinja2 index 1e1a653..7128948 100755 --- a/flask/main.py.jinja2 +++ b/flask/main.py.jinja2 @@ -28,6 +28,11 @@ from flask import Flask, request, jsonify app = Flask(__name__) +@app.route('/', methods=['GET']) +def hello(): + return "API is working!" + + @app.route('/', methods=['POST']) def api(): data = request.get_json(force=True) @@ -25,8 +25,8 @@ def read_settings(): return settings -def build_systemd_files(settings): - """Generate all Systemd Unit Files with Settings from SETTINGS_PATH""" +def build_systemd_file(settings): + """Generate Systemd Unit File with Settings from SETTINGS_PATH""" with open(SYSTEMD_TEMPLATE, "r") as template: template = template.read() @@ -34,28 +34,28 @@ def build_systemd_files(settings): template = jinja2.Template(template) systemd_file = template.render(settings) + subprocess.call("mkdir -p systemd/build", shell=True) file_path = "systemd/build/{}.service".format(settings["project_name"]) with open(file_path, "w") as _file: _file.write(systemd_file) -def build_flask_files(settings): - """Generate all Python Flask Files with Settings from SETTINGS_PATH""" +def build_flask(settings): + """Generate Python Flask File with Settings from SETTINGS_PATH""" - if settings["web_framework"] == "flask": - with open(FLASK_TEMPLATE, "r") as template: - template = template.read() - - template = jinja2.Template(template) - flask_file = template.render(settings) + with open(FLASK_TEMPLATE, "r") as template: + template = template.read() - file_path = "flask/build/{}".format(settings["project_name"]) + template = jinja2.Template(template) + flask_file = template.render(settings) + + subprocess.call("mkdir -p flask/build", shell=True) + + file_path = "flask/build/{}".format(settings["project_name"]) - with open(file_path, "w") as _file: - _file.write(flask_file) - else: - print("Flask not enabled in settings.json - skipping file generation for Flask") + with open(file_path, "w") as _file: + _file.write(flask_file) def install_webroot(settings): @@ -65,17 +65,50 @@ def install_webroot(settings): for _file in files: path_to_file = os.path.join(directory, _file) relative_path = os.path.relpath(path_to_file, REPOSITORY_WEBROOT) - target_path = os.path.join(settings["webroot_installation_path"], relative_path) + target_path = os.path.join(settings["webroot"], relative_path) subprocess.run("install -Dm 644 {} {}".format(path_to_file, target_path), shell=True) def uninstall_webroot(settings): """Uninstalls every File from the Systems Webroot""" - path_to_delete = os.path.join(settings["webroot_installation_path"], "*") + path_to_delete = os.path.join(settings["webroot"], "*") subprocess.run("rm -rf {}".format(path_to_delete), shell=True) +def install_flask(settings): + """Install the Executable for the Dynamic Web Server""" + + subprocess.call("sudo mkdir -p /opt/{}".format(settings["project_name"]), shell=True) + subprocess.call("sudo install -Dm 744 flask/build/{0} /opt/{0}/{0}".format(settings["project_name"]), shell=True) + order = "sudo find /opt/{0} -exec chown -R {1}:{2}".format( + settings["project_name"], + settings["user"], + settings["group"] + ) + order += " {} \;" + subprocess.call(order, shell=True) + + +def uninstall_flask(settings): + """Uninstall the Executable for the Dynamic Web Server""" + + subprocess.call("sudo rm -rf /opt/{}".format(settings["project_name"]), shell=True) + + +def install_systemd_file(settings): + """Install the Systemd Unit for launching the dynamic Web Server""" + + subprocess.call("sudo install -Dm 644 systemd/build/{0}.service /etc/systemd/system/{0}.service".format(settings["project_name"]), shell=True) + subprocess.call("sudo systemctl daemon-reload", shell=True) + + +def uninstall_systemd_file(settings): + """Uninstall the Systemd Unit for launching the dynamic Web Server""" + + subprocess.call("sudo rm /etc/systemd/system/{0}.service".format(settings["project_name"]), shell=True) + + def main(): """The main Function""" @@ -88,17 +121,52 @@ def main(): # execute given command command = sys.argv[1] - if command == "build_systemd_files": - build_systemd_files(settings) - elif command == "build_flask_files": - build_flask_files(settings) + if command == "build_systemd_file": + build_systemd_file(settings) + elif command == "build_framework": + if settings["framework"] == "flask": + build_flask(settings) + elif settings["framework"] == "actix-web": + print("actix-web is not yet implemented ...") + sys.exit(1) + elif settings["framework"] == None: + pass + else: + print("Unknown web framework selected") + sys.exit(1) elif command == "install_webroot": install_webroot(settings) elif command == "uninstall_webroot": uninstall_webroot(settings) + elif command == "install_framework": + if settings["framework"] == "flask": + install_flask(settings) + elif settings["framework"] == "actix-web": + print("actix-web is not yet implemented ...") + sys.exit(1) + elif settings["framework"] == None: + pass + else: + print("Unknown web framework selected") + sys.exit(1) + elif command == "uninstall_framework": + if settings["framework"] == "flask": + uninstall_flask(settings) + elif settings["framework"] == "actix-web": + print("actix-web is not yet implemented ...") + sys.exit(1) + elif settings["framework"] == None: + pass + else: + print("Unknown web framework selected") + sys.exit(1) + elif command == "install_systemd_file": + install_systemd_file(settings) + elif command == "uninstall_systemd_file": + uninstall_systemd_file(settings) else: print("Unknown command") - exit(1) + sys.exit(1) if __name__ == "__main__": diff --git a/systemd/webtemplate.service.jinja2 b/systemd/webtemplate.service.jinja2 index 6861b57..f1aa9c5 100644 --- a/systemd/webtemplate.service.jinja2 +++ b/systemd/webtemplate.service.jinja2 @@ -6,8 +6,8 @@ After=network.target [Service] User={{ user }} Group={{ group }} -WorkingDirectory={{ web_framework_installation_path }}/{{ project_name }}/ -ExecStart={{ web_framework_installation_path }}/{{ project_name }}/{{ project_name }} +WorkingDirectory=/opt/{{ project_name }}/ +ExecStart=/opt/{{ project_name }}/{{ project_name }} [Install] WantedBy=multi-user.target |