made suggested changes after PR review

This commit is contained in:
coolguy-cell 2021-01-23 21:54:06 +05:30
parent 46b499d45f
commit 744d1f266e
No known key found for this signature in database
GPG Key ID: CD3A42E1D470AD70
5 changed files with 40 additions and 20 deletions

View File

@ -5,5 +5,5 @@ server_host = 127.0.0.1
server_port = 8081 server_port = 8081
[app] [app]
user_data = sample_file.txt user_data = user-data.txt
meta_data = meta_data_extra.txt meta_data = meta-data.txt

View File

@ -0,0 +1 @@
suspended:httpd

View File

@ -1 +0,0 @@
suspend_cmd:suspend

54
main.py
View File

@ -3,7 +3,7 @@ import sys
import cherrypy import cherrypy
from cherrypy.lib.static import serve_file from cherrypy.lib.static import serve_file
import json import yaml
import socket import socket
import configparser import configparser
@ -18,36 +18,56 @@ meta_data_filename = config["app"].get("meta_data", "meta_data_extra.txt")
class MainApp: 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 @cherrypy.expose
def user_data(self): def user_data(self):
hostname = socket.gethostbyaddr(str(cherrypy.request.remote.ip))[0] """
filename = user_data_filename Serves a static file
filepath = os.path.join(PATH, "data", hostname, "user-data", filename) """
filepath = os.path.join(PATH, "data", user_data_filename)
return serve_file(filepath, "application/x-download", "attachment") return serve_file(filepath, "application/x-download", "attachment")
@cherrypy.expose @cherrypy.expose
@cherrypy.tools.json_out()
def meta_data(self): 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} data = {"instance-id": hostname.split(".")[0], "local-hostname": hostname}
folder = os.path.join(PATH, "data", hostname, "metadata_extra") filepath = os.path.join(PATH, "data", hostname, meta_data_filename)
if os.path.exists(folder): if os.path.exists(filepath):
with open(os.path.join(folder, meta_data_filename), "r") as f: with open(filepath, "r") as f:
lines = f.readlines() line = f.readlines()[0]
for line in lines:
ls = list(map(lambda k: k.strip(), line.split(":"))) ls = list(map(lambda k: k.strip(), line.split(":")))
data[ls[0]] = ls[1] data[ls[0]] = ls[1]
return data return yaml.dump(data)
@cherrypy.expose @cherrypy.expose
def finished(self, data): 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): if not os.path.exists(folder):
os.makedirs(folder) os.makedirs(folder)