diff --git a/app/Entities.py b/app/Entities.py new file mode 100644 index 0000000..13f37a2 --- /dev/null +++ b/app/Entities.py @@ -0,0 +1,5 @@ +class Mailbox: + +class QuarMail: + +class Attachment: diff --git a/app/Gulag.py b/app/Gulag.py index 8c0589e..0a64e5c 100644 --- a/app/Gulag.py +++ b/app/Gulag.py @@ -9,10 +9,12 @@ class GulagException(Exception): self.message = message class Gulag: + version = None config = None db = None def __init__(self, path_to_config_file): + self.version = "VERSION-TODO!" try: with open(path_to_config_file, 'r') as f: self.config = json.load(f) diff --git a/app/Resources.py b/app/Resources.py index f1080b6..dc0333a 100644 --- a/app/Resources.py +++ b/app/Resources.py @@ -1,5 +1,5 @@ from flask import request -from flask_restful import Resource, Api, abort +from flask_restful import Resource, abort import json class GulagResource(Resource): @@ -29,9 +29,29 @@ class GulagResource(Resource): class ResRoot(GulagResource): def get(self): - return {"resource": "root :)"} + return {"resource": "Root :)"} + +class ResMailboxes(GulagResource): + def get(self): + return {"resource": "Mailboxes"} + +class ResMailbox(GulagResource): + def get(self,id): + return {"resource": "Mailbox by ID"} class ResQuarMails(GulagResource): def get(self): - return {"abc": "1234"} -# return self.gulag.get_quarmails() + return {"resource": "QuarMails"} + +class ResQuarMail(GulagResource): + def get(self,id): + return {"resource": "QuarMail by ID"} + +class ResAttachments(GulagResource): + def get(self): + return {"resource": "Attachments"} + +class ResAttachment(GulagResource): + def get(self,id): + return {"resource": "Attachment by ID"} + diff --git a/app/__pycache__/Gulag.cpython-35.pyc b/app/__pycache__/Gulag.cpython-35.pyc new file mode 100644 index 0000000..df7ccbd Binary files /dev/null and b/app/__pycache__/Gulag.cpython-35.pyc differ diff --git a/app/__pycache__/GulagDB.cpython-35.pyc b/app/__pycache__/GulagDB.cpython-35.pyc new file mode 100644 index 0000000..3a80b36 Binary files /dev/null and b/app/__pycache__/GulagDB.cpython-35.pyc differ diff --git a/app/__pycache__/GulagMailbox.cpython-35.pyc b/app/__pycache__/GulagMailbox.cpython-35.pyc new file mode 100644 index 0000000..6cac5b4 Binary files /dev/null and b/app/__pycache__/GulagMailbox.cpython-35.pyc differ diff --git a/app/__pycache__/Resources.cpython-35.pyc b/app/__pycache__/Resources.cpython-35.pyc new file mode 100644 index 0000000..ce7be94 Binary files /dev/null and b/app/__pycache__/Resources.cpython-35.pyc differ diff --git a/app/gulag_helpers.py b/app/gulag_helpers.py new file mode 100755 index 0000000..9b8ce2b --- /dev/null +++ b/app/gulag_helpers.py @@ -0,0 +1,54 @@ +#!/usr/bin/env python3 + +import argparse,sys,os,time,signal +from Gulag import Gulag,GulagException + +parser = argparse.ArgumentParser() +parser.add_argument('--config', required=True, help="Path to config file") +args = parser.parse_args() + +child_pids = [] +importer_pid = os.fork() +if(importer_pid == 0): + # Child process: importer + try: + gulag = Gulag(args.config) + except GulagException as e: + print(e.message) + sys.exit(1) + while True: + try: + gulag.import_quarmails() + except GulagException as e: + print("Importer-Exception: " + e.message) + time.sleep(gulag.config['importer']['interval']) + +cleaner_pid = os.fork() +if(cleaner_pid == 0): + # Child process: cleaner + try: + gulag = Gulag(args.config) + except GulagException as e: + print(e.message) + sys.exit(1) + while True: + try: + gulag.cleanup_quarmails() + except GulagException as e: + print("Cleaner-Exception: " + e.message) + time.sleep(gulag.config['cleaner']['interval']) + +# Parent +child_pids.append(importer_pid) +child_pids.append(cleaner_pid) +try: + print("Entered helpers main loop...") + while True: + time.sleep(10) +except: + print("MAIN-EXCEPTION: " + str(sys.exc_info())) + # Destroy childs + for child_pid in child_pids: + print("Killing child pid: %s", child_pid) + os.kill(child_pid, signal.SIGTERM) + diff --git a/app/gulag_server.py b/app/gulag_server.py index c45d6f1..f2a8e6b 100755 --- a/app/gulag_server.py +++ b/app/gulag_server.py @@ -1,49 +1,15 @@ #!/usr/bin/env python3 -import argparse,sys,os,time,signal +import argparse,sys from flask import Flask from flask_restful import Api from Gulag import Gulag,GulagException -from Resources import ResRoot,ResQuarMails +from Resources import ResRoot,ResMailboxes,ResQuarMails,ResAttachments parser = argparse.ArgumentParser() parser.add_argument('--config', required=True, help="Path to config file") args = parser.parse_args() -#child_pids = [] -#importer_pid = os.fork() -#if(importer_pid == 0): -# # Child process: importer -# try: -# gulag = Gulag(args.config) -# except GulagException as e: -# print(e.message) -# sys.exit(1) -# while True: -# try: -# gulag.import_quarmails() -# except GulagException as e: -# print("Importer-Exception: " + e.message) -# time.sleep(gulag.config['importer']['interval']) -# -#cleaner_pid = os.fork() -#if(cleaner_pid == 0): -# # Child process: cleaner -# try: -# gulag = Gulag(args.config) -# except GulagException as e: -# print(e.message) -# sys.exit(1) -# while True: -# try: -# gulag.cleanup_quarmails() -# except GulagException as e: -# print("Cleaner-Exception: " + e.message) -# time.sleep(gulag.config['cleaner']['interval']) - -# Parent -#child_pids.append(importer_pid) -#child_pids.append(cleaner_pid) try: try: gulag = Gulag(args.config) @@ -55,10 +21,18 @@ try: '/api/v1/', resource_class_kwargs={'gulag_object': gulag} ) + api.add_resource(ResMailboxes, + '/api/v1/mailboxes/', + resource_class_kwargs={'gulag_object': gulag} + ) api.add_resource(ResQuarMails, '/api/v1/quarmails/', resource_class_kwargs={'gulag_object': gulag} ) + api.add_resource(ResAttachments, + '/api/v1/attachments/', + resource_class_kwargs={'gulag_object': gulag} + ) if __name__ == '__main__': app.run(debug=False, # will be overriden by uwsgi.ini @@ -69,9 +43,3 @@ try: sys.exit(0) except: print("MAIN-EXCEPTION: " + str(sys.exc_info())) -# # Destroy childs -# for child_pid in child_pids: -# print("Killing child pid: %s", child_pid) -# os.kill(child_pid, signal.SIGTERM) - - diff --git a/config/gulag-config.json b/config/gulag-config.json new file mode 100644 index 0000000..db16f69 --- /dev/null +++ b/config/gulag-config.json @@ -0,0 +1,38 @@ +{ + "daemon":{ + "listen_host": "127.0.0.1", + "listen_port": 5001 + }, + "trusted_proxies": { + "rprx01":[ + "172.16.100.5", "fd00:100::5" + ], + "rprx02":[ + "172.16.100.6", "fd00:100::6" + ] + }, + "api_keys": { + "HIGHLY_SECURE_API_KEY": { + "user": "GULAG APP" + } + }, + "uri_prefixes": { + "root": "https:///api/v1/", + "mailboxes": "https:///api/v1/mailboxes/", + "quarmails": "https:///api/v1/quarmails/", + "attachments": "https:///api/v1/attachments/" + }, + "db":{ + "server": "127.0.0.1", + "user": "root", + "password": "", + "name": "Gulag" + }, + "cleaner":{ + "retention_period": "12 hour", + "interval": 10 + }, + "importer":{ + "interval": 10 + } +} diff --git a/config/vassals/gulag_helpers.ini b/config/vassals/gulag_helpers.ini new file mode 100644 index 0000000..200b354 --- /dev/null +++ b/config/vassals/gulag_helpers.ini @@ -0,0 +1,5 @@ +[uwsgi] +#uid = 65534 +#gid = 65534 +processes = 1 +privileged-binary-patch-arg = /app/gulag_helpers.py --config /config/gulag-config.json diff --git a/config/vassals/gulag_server.ini b/config/vassals/gulag_server.ini new file mode 100644 index 0000000..efa0ddc --- /dev/null +++ b/config/vassals/gulag_server.ini @@ -0,0 +1,10 @@ +[uwsgi] +processes = 4 +cheaper = 1 +cheaper-initial = 1 +cheaper-step = 1 +plugin = python3 +python-path = /app +wsgi-file = /app/uwsgi.py +pyargv = --config /config/gulag-config.json +socket = /socket/uwsgi-gulag_server.sock diff --git a/docker/gulag-server/debian/Dockerfile b/docker/gulag-server/debian/Dockerfile index 3628d9c..bd83831 100644 --- a/docker/gulag-server/debian/Dockerfile +++ b/docker/gulag-server/debian/Dockerfile @@ -9,7 +9,7 @@ RUN set -ex ; \ && apt-get -qq --no-install-recommends install \ uwsgi-plugin-python3 python3-setuptools python3-flask \ python3-flask-restful python3-mysql.connector \ - uwsgi uwsgi-plugin-python3 + uwsgi uwsgi-plugin-python3 procps net-tools RUN /bin/mkdir /config /socket /app COPY app/*.py /app/