fix: code quality
This commit is contained in:
parent
ce70c7144c
commit
67a2395db3
78
main.py
78
main.py
|
@ -1,24 +1,38 @@
|
||||||
import os
|
#!/usr/bin/env python3
|
||||||
import sys
|
"""
|
||||||
|
Serve cloud init files
|
||||||
import cherrypy
|
"""
|
||||||
from cherrypy.lib.static import serve_file
|
|
||||||
import yaml
|
|
||||||
import socket
|
|
||||||
|
|
||||||
import configparser
|
import configparser
|
||||||
|
import os
|
||||||
|
import socket
|
||||||
|
import sys
|
||||||
|
from ipaddress import AddressValueError, IPv4Address, IPv6Address
|
||||||
|
|
||||||
|
import yaml
|
||||||
|
import cherrypy
|
||||||
|
from cherrypy.lib.static import serve_file
|
||||||
|
|
||||||
|
|
||||||
PATH = os.path.dirname(os.path.abspath(__file__))
|
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", "user-data")
|
USER_DATA_FILENAME = CONFIG["app"].get("user_data", "user-data")
|
||||||
meta_data_filename = config["app"].get("meta_data", "meta-data")
|
META_DATA_FILENAME = CONFIG["app"].get("meta_data", "meta-data")
|
||||||
redirect_filename = config["app"].get("redirect", "redirect")
|
REDIRECT_FILENAME = CONFIG["app"].get("redirect", "redirect")
|
||||||
|
|
||||||
|
|
||||||
class MainApp:
|
class CloudInitApp:
|
||||||
|
"""
|
||||||
|
Serve cloud init files
|
||||||
|
"""
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
self.remoteip = None
|
||||||
|
self.hostinfo = ('localhost', )
|
||||||
|
|
||||||
def _can_ip_be_proxy(self):
|
def _can_ip_be_proxy(self):
|
||||||
self.remoteip = cherrypy.request.remote.ip
|
self.remoteip = cherrypy.request.remote.ip
|
||||||
try:
|
try:
|
||||||
|
@ -46,17 +60,17 @@ class MainApp:
|
||||||
try:
|
try:
|
||||||
self.hostinfo = socket.gethostbyaddr(self.remoteip)
|
self.hostinfo = socket.gethostbyaddr(self.remoteip)
|
||||||
except socket.herror:
|
except socket.herror:
|
||||||
self.hostinfo = ('localhost', )
|
pass
|
||||||
|
|
||||||
def _redirect_if_needed(self):
|
def _redirect_if_needed(self):
|
||||||
filepath = os.path.join(PATH, "data", self.hostinfo[0],
|
filepath = os.path.join(PATH, "data", self.hostinfo[0],
|
||||||
"redirect")
|
REDIRECT_FILENAME)
|
||||||
if os.path.exists(filepath):
|
if os.path.exists(filepath):
|
||||||
try:
|
try:
|
||||||
with open(filepath) as f:
|
with open(filepath) as redirect:
|
||||||
content = f.read().splitlines()
|
content = redirect.read().splitlines()
|
||||||
raise cherrypy.HTTPRedirect(content[0], 301)
|
raise cherrypy.HTTPRedirect(content[0], 301)
|
||||||
except BaseException:
|
except IOError:
|
||||||
return False
|
return False
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
@ -68,10 +82,10 @@ class MainApp:
|
||||||
self._init_ip()
|
self._init_ip()
|
||||||
self._redirect_if_needed()
|
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):
|
||||||
filepath = os.path.join(PATH, "data", user_data_filename)
|
filepath = os.path.join(PATH, "data", USER_DATA_FILENAME)
|
||||||
return serve_file(filepath, "application/x-download", "attachment")
|
return serve_file(filepath, "text/yaml", "attachment")
|
||||||
|
|
||||||
@cherrypy.expose
|
@cherrypy.expose
|
||||||
def meta_data(self):
|
def meta_data(self):
|
||||||
|
@ -82,17 +96,21 @@ class MainApp:
|
||||||
self._redirect_if_needed()
|
self._redirect_if_needed()
|
||||||
hostname = self.hostinfo[0]
|
hostname = self.hostinfo[0]
|
||||||
data = {
|
data = {
|
||||||
"instance-id": hostname.split(".")[0],
|
"instance-id": hostname.split(".")[0],
|
||||||
"local-hostname": hostname
|
"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):
|
||||||
with open(filepath, "r") as metadata:
|
with open(filepath, "r") as metadata:
|
||||||
for line in metadata.readlines():
|
for line in metadata.readlines():
|
||||||
linesplit = list(map(lambda k: k.strip(), line.split(":")))
|
linesplit = list(map(lambda k: k.strip(), line.split(":")))
|
||||||
data[linesplit[0]] = linesplit[1]
|
data[linesplit[0]] = linesplit[1]
|
||||||
|
|
||||||
|
cherrypy.response.headers['Content-Type'] = \
|
||||||
|
'text/yaml'
|
||||||
|
cherrypy.response.headers['Content-Disposition'] = \
|
||||||
|
'attachment; filename="user-data"'
|
||||||
return yaml.dump(data)
|
return yaml.dump(data)
|
||||||
|
|
||||||
@cherrypy.expose
|
@cherrypy.expose
|
||||||
|
@ -114,15 +132,17 @@ class MainApp:
|
||||||
if not os.path.exists(folder):
|
if not os.path.exists(folder):
|
||||||
os.makedirs(folder)
|
os.makedirs(folder)
|
||||||
|
|
||||||
with open(os.path.join(folder, meta_data_filename), "w") as f:
|
with open(os.path.join(folder, META_DATA_FILENAME), "w") as fin:
|
||||||
f.write(data)
|
fin.write(data)
|
||||||
|
|
||||||
|
|
||||||
ROOT = MainApp()
|
ROOT = CloudInitApp()
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
cherrypy.server.socket_host = config["server"].get("server_host", "127.0.0.1")
|
cherrypy.server.socket_host = \
|
||||||
cherrypy.server.socket_port = config["server"].getint("server_port", 8081)
|
CONFIG["server"].get("server_host", "127.0.0.1")
|
||||||
|
cherrypy.server.socket_port = \
|
||||||
|
CONFIG["server"].getint("server_port", 8081)
|
||||||
ENGINE = cherrypy.engine
|
ENGINE = cherrypy.engine
|
||||||
|
|
||||||
cherrypy.tree.mount(ROOT)
|
cherrypy.tree.mount(ROOT)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user