From cbbf9f2f666c2f9e9f1b36ca0a9ab4ce4264a372 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 + data/user-data.txt | 2 ++ main.py | 54 ++++++++++++++++++++++++------------ 4 files changed, 42 insertions(+), 19 deletions(-) create mode 100644 data/localhost/meta-data.txt create mode 100644 data/user-data.txt 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/user-data.txt b/data/user-data.txt new file mode 100644 index 0000000..3d8c6f6 --- /dev/null +++ b/data/user-data.txt @@ -0,0 +1,2 @@ +This is a sample static file to be served +from cherrypy server at user-data url. 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)