add: redirect and vendor-data

- add redirect and vendor-data (empty only, to speed up boot)
- some CQ too
- default file names standardized
This commit is contained in:
Peter Šurda 2021-03-01 10:00:50 +01:00
parent 360917be5d
commit 2913a8aa24
Signed by: PeterSurda
GPG Key ID: 0C5F50C0B5F37D87

33
main.py
View File

@ -13,8 +13,9 @@ PATH = os.path.dirname(os.path.abspath(__file__))
config = configparser.ConfigParser() config = configparser.ConfigParser()
config.read(os.path.join(PATH, "config.ini")) config.read(os.path.join(PATH, "config.ini"))
user_data_filename = config["app"].get("user_data", "sample_file.txt") user_data_filename = config["app"].get("user_data", "user-data")
meta_data_filename = config["app"].get("meta_data", "meta_data_extra.txt") meta_data_filename = config["app"].get("meta_data", "meta-data")
redirect_filename = config["app"].get("redirect", "redirect")
class MainApp: class MainApp:
@ -27,7 +28,7 @@ class MainApp:
'X-Real-Ip', 'X-Real-Ip',
cherrypy.request.remote.ip cherrypy.request.remote.ip
) )
except: except BaseException:
self.remoteip = cherrypy.request.remote.ip self.remoteip = cherrypy.request.remote.ip
try: try:
@ -35,12 +36,25 @@ class MainApp:
except socket.herror: except socket.herror:
self.hostinfo = ('localhost', ) self.hostinfo = ('localhost', )
def _redirect_if_needed(self):
filepath = os.path.join(PATH, "data", self.hostinfo[0],
"redirect")
if os.path.exists(filepath):
try:
with open(filepath) as f:
content = f.read().splitlines()
raise cherrypy.HTTPRedirect(content[0], 301)
except BaseException:
return False
return False
@cherrypy.expose @cherrypy.expose
def user_data(self): def user_data(self):
""" """
Serves a static file Serves a static file
""" """
self._init_ip() self._init_ip()
self._redirect_if_needed()
filepath = os.path.join(PATH, "data", self.hostinfo[0], filepath = os.path.join(PATH, "data", self.hostinfo[0],
user_data_filename) user_data_filename)
if not os.path.exists(filepath): if not os.path.exists(filepath):
@ -53,8 +67,12 @@ class MainApp:
Return meta-data in YAML Return meta-data in YAML
""" """
self._init_ip() self._init_ip()
self._redirect_if_needed()
hostname = self.hostinfo[0] hostname = self.hostinfo[0]
data = {"instance-id": hostname.split(".")[0], "local-hostname": hostname} data = {
"instance-id": hostname.split(".")[0],
"local-hostname": hostname
}
filepath = os.path.join(PATH, "data", hostname, meta_data_filename) filepath = os.path.join(PATH, "data", hostname, meta_data_filename)
if os.path.exists(filepath): if os.path.exists(filepath):
@ -65,6 +83,13 @@ class MainApp:
return yaml.dump(data) return yaml.dump(data)
@cherrypy.expose
def vendor_data(self):
"""
Return empty vendor-data
"""
return ""
@cherrypy.expose @cherrypy.expose
def finished(self, data): def finished(self, data):
""" """