forked from Sysdeploy/cloud-init-cherrypy
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:
parent
360917be5d
commit
2913a8aa24
33
main.py
33
main.py
|
@ -13,8 +13,9 @@ PATH = os.path.dirname(os.path.abspath(__file__))
|
|||
config = configparser.ConfigParser()
|
||||
config.read(os.path.join(PATH, "config.ini"))
|
||||
|
||||
user_data_filename = config["app"].get("user_data", "sample_file.txt")
|
||||
meta_data_filename = config["app"].get("meta_data", "meta_data_extra.txt")
|
||||
user_data_filename = config["app"].get("user_data", "user-data")
|
||||
meta_data_filename = config["app"].get("meta_data", "meta-data")
|
||||
redirect_filename = config["app"].get("redirect", "redirect")
|
||||
|
||||
|
||||
class MainApp:
|
||||
|
@ -27,7 +28,7 @@ class MainApp:
|
|||
'X-Real-Ip',
|
||||
cherrypy.request.remote.ip
|
||||
)
|
||||
except:
|
||||
except BaseException:
|
||||
self.remoteip = cherrypy.request.remote.ip
|
||||
|
||||
try:
|
||||
|
@ -35,12 +36,25 @@ class MainApp:
|
|||
except socket.herror:
|
||||
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
|
||||
def user_data(self):
|
||||
"""
|
||||
Serves a static file
|
||||
"""
|
||||
self._init_ip()
|
||||
self._redirect_if_needed()
|
||||
filepath = os.path.join(PATH, "data", self.hostinfo[0],
|
||||
user_data_filename)
|
||||
if not os.path.exists(filepath):
|
||||
|
@ -53,8 +67,12 @@ class MainApp:
|
|||
Return meta-data in YAML
|
||||
"""
|
||||
self._init_ip()
|
||||
self._redirect_if_needed()
|
||||
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)
|
||||
if os.path.exists(filepath):
|
||||
|
@ -65,6 +83,13 @@ class MainApp:
|
|||
|
||||
return yaml.dump(data)
|
||||
|
||||
@cherrypy.expose
|
||||
def vendor_data(self):
|
||||
"""
|
||||
Return empty vendor-data
|
||||
"""
|
||||
return ""
|
||||
|
||||
@cherrypy.expose
|
||||
def finished(self, data):
|
||||
"""
|
||||
|
|
Loading…
Reference in New Issue
Block a user