diff options
author | xengineering <mail2xengineering@protonmail.com> | 2021-02-11 13:01:43 +0100 |
---|---|---|
committer | xengineering <mail2xengineering@protonmail.com> | 2021-02-11 13:01:43 +0100 |
commit | bc6b03ab29703dfff857b63727657ca3127eb381 (patch) | |
tree | 009fc7df794754917ddb3fecfde8b2f739e7b1df /manage.py | |
parent | 7293dae63a07ec23e779fef20fbe81104ebc7d3f (diff) | |
download | web-template-bc6b03ab29703dfff857b63727657ca3127eb381.tar web-template-bc6b03ab29703dfff857b63727657ca3127eb381.tar.zst web-template-bc6b03ab29703dfff857b63727657ca3127eb381.zip |
Refactoring: Reimplement Webroot Deployment
Diffstat (limited to 'manage.py')
-rw-r--r--[-rwxr-xr-x] | manage.py | 103 |
1 files changed, 82 insertions, 21 deletions
diff --git a/manage.py b/manage.py index 50249e5..6df9902 100755..100644 --- a/manage.py +++ b/manage.py @@ -1,45 +1,106 @@ #!/usr/bin/python3 +# vim: tabstop=4 softtabstop=4 shiftwidth=4 expandtab import sys +import os import subprocess import json +import jinja2 + + +SETTINGS_PATH = "settings.json" +SYSTEMD_TEMPLATE = "systemd/webtemplate.service.jinja2" +FLASK_TEMPLATE = "flask/main.py.jinja2" +REPOSITORY_WEBROOT = "webroot" + + +def read_settings(): + """Read the SETTINGS_PATH File and return a Dictionary with its Content""" + + with open(SETTINGS_PATH, "r") as settings: + settings = settings.read() + + settings = json.loads(settings) + return settings + + +def build_systemd_files(settings): + """Generate all Systemd Unit Files with Settings from SETTINGS_PATH""" + + with open(SYSTEMD_TEMPLATE, "r") as template: + template = template.read() + + template = jinja2.Template(template) + systemd_file = template.render(settings) + + 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""" + + if settings["web_framework"] == "flask": + with open(FLASK_TEMPLATE, "r") as template: + template = template.read() + + template = jinja2.Template(template) + flask_file = template.render(settings) + + 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") + + +def install_webroot(settings): + """Install every File from the Repositories Webroot to the Systems Webroot""" + + for directory, subdirs, files in os.walk(REPOSITORY_WEBROOT, topdown=False): + 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) + subprocess.run("install -Dm 644 {} {}".format(path_to_file, target_path), shell=True) -CONFIG_PATH = "./config.json" +def uninstall_webroot(settings): + """Uninstalls every File from the Systems Webroot""" + + path_to_delete = os.path.join(settings["webroot_installation_path"], "*") + subprocess.run("rm -rf {}".format(path_to_delete), shell=True) def main(): + """The main Function""" + + settings = read_settings() # get settings from SETTINGS_PATH + # just one argument is allowed: if len(sys.argv) != 2: print("Provide exactly one parameter") sys.exit(1) - cfg = read_config() - + # execute given command command = sys.argv[1] - - if command == "deploy": - for deployment in cfg["deployments"]: - subprocess.call( - "rsync -av ./webroot/ {0}@{1}:{2}".format( - deployment["username"], - deployment["host"], - deployment["webroot"] - ), - shell=True - ) + if command == "build_systemd_files": + build_systemd_files(settings) + elif command == "build_flask_files": + build_flask_files(settings) + elif command == "install_webroot": + install_webroot(settings) + elif command == "uninstall_webroot": + uninstall_webroot(settings) else: print("Unknown command") + exit(1) -def read_config(): - with open(CONFIG_PATH, "r") as f: - content = f.read() - return json.loads(content) if __name__ == "__main__": main() - -# vim: tabstop=4 softtabstop=4 shiftwidth=4 expandtab - |