From 744d1f266e8adda07d8b74e95e15dc5fd0c0ad7b Mon Sep 17 00:00:00 2001 From: coolguy-cell Date: Sat, 23 Jan 2021 21:54:06 +0530 Subject: [PATCH] made suggested changes after PR review --- config.ini | 4 +- data/localhost/meta-data.txt | 1 + .../metadata_extra/meta_data_extra.txt | 1 - .../sample_file.txt => user-data.txt} | 0 main.py | 54 +++++++++++++------ 5 files changed, 40 insertions(+), 20 deletions(-) create mode 100644 data/localhost/meta-data.txt delete mode 100644 data/localhost/metadata_extra/meta_data_extra.txt rename data/{localhost/user-data/sample_file.txt => user-data.txt} (100%) diff --git a/config.ini b/config.ini index ed3c95e..8f5af62 100644 --- a/config.ini +++ b/config.ini @@ -5,5 +5,5 @@ server_host = 127.0.0.1 server_port = 8081 [app] -user_data = sample_file.txt -meta_data = meta_data_extra.txt \ No newline at end of file +user_data = user-data.txt +meta_data = meta-data.txt \ No newline at end of file diff --git a/data/localhost/meta-data.txt b/data/localhost/meta-data.txt new file mode 100644 index 0000000..32695e1 --- /dev/null +++ b/data/localhost/meta-data.txt @@ -0,0 +1 @@ +suspended:httpd \ No newline at end of file diff --git a/data/localhost/metadata_extra/meta_data_extra.txt b/data/localhost/metadata_extra/meta_data_extra.txt deleted file mode 100644 index fc89819..0000000 --- a/data/localhost/metadata_extra/meta_data_extra.txt +++ /dev/null @@ -1 +0,0 @@ -suspend_cmd:suspend \ No newline at end of file diff --git a/data/localhost/user-data/sample_file.txt b/data/user-data.txt similarity index 100% rename from data/localhost/user-data/sample_file.txt rename to data/user-data.txt diff --git a/main.py b/main.py index 487c2c9..6ad221f 100644 --- a/main.py +++ b/main.py @@ -3,7 +3,7 @@ import sys import cherrypy from cherrypy.lib.static import serve_file -import json +import yaml import socket import configparser @@ -18,36 +18,56 @@ meta_data_filename = config["app"].get("meta_data", "meta_data_extra.txt") class MainApp: + def __init__(self, *args): + self._init_ip() + + def _init_ip(self): + """ + Get remote IP + """ + try: + self.remoteip = cherrypy.request.headers.get( + 'X-Real-Ip', + cherrypy.request.remote.ip + ) + except: + self.remoteip = cherrypy.request.remote.ip + + self.hostinfo = socket.gethostbyaddr(self.remoteip) + @cherrypy.expose def user_data(self): - hostname = socket.gethostbyaddr(str(cherrypy.request.remote.ip))[0] - filename = user_data_filename - filepath = os.path.join(PATH, "data", hostname, "user-data", filename) - + """ + Serves a static file + """ + filepath = os.path.join(PATH, "data", user_data_filename) return serve_file(filepath, "application/x-download", "attachment") @cherrypy.expose - @cherrypy.tools.json_out() def meta_data(self): - host_info = socket.gethostbyaddr(str(cherrypy.request.remote.ip)) - hostname = host_info[0] + """ + Return meta-data in YAML + """ + hostname =self.hostinfo[0] data = {"instance-id": hostname.split(".")[0], "local-hostname": hostname} - folder = os.path.join(PATH, "data", hostname, "metadata_extra") - if os.path.exists(folder): - with open(os.path.join(folder, meta_data_filename), "r") as f: - lines = f.readlines() - - for line in lines: + filepath = os.path.join(PATH, "data", hostname, meta_data_filename) + if os.path.exists(filepath): + with open(filepath, "r") as f: + line = f.readlines()[0] ls = list(map(lambda k: k.strip(), line.split(":"))) data[ls[0]] = ls[1] - return data + return yaml.dump(data) @cherrypy.expose def finished(self, data): - hostname = socket.gethostbyaddr(str(cherrypy.request.remote.ip))[0] - folder = os.path.join(PATH, "data", hostname, "metadata_extra") + """ + Saves additional meta-data + + :param data: meta-data to be added + """ + folder = os.path.join(PATH, "data", self.hostinfo[0]) if not os.path.exists(folder): os.makedirs(folder)