forked from Sysdeploy/cloud-init-cherrypy
made suggested changes after PR review
This commit is contained in:
parent
46b499d45f
commit
cbbf9f2f66
|
@ -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
|
||||
user_data = user-data.txt
|
||||
meta_data = meta-data.txt
|
1
data/localhost/meta-data.txt
Normal file
1
data/localhost/meta-data.txt
Normal file
|
@ -0,0 +1 @@
|
|||
suspended:httpd
|
2
data/user-data.txt
Normal file
2
data/user-data.txt
Normal file
|
@ -0,0 +1,2 @@
|
|||
This is a sample static file to be served
|
||||
from cherrypy server at user-data url.
|
54
main.py
54
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)
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user