Compare commits
No commits in common. "master" and "master" have entirely different histories.
2
.gitmodules
vendored
2
.gitmodules
vendored
|
@ -1,3 +1,3 @@
|
||||||
[submodule "data"]
|
[submodule "data"]
|
||||||
path = data
|
path = data
|
||||||
url = git@git.bitmessage.org:Sysdeploy/cloud-init-cherrypy-data.git
|
url = git@git.bitmessage.org:Bitmessage/cloud-init-cherrypy-data.git
|
||||||
|
|
293
main.py
293
main.py
|
@ -7,14 +7,12 @@ import configparser
|
||||||
import os
|
import os
|
||||||
import socket
|
import socket
|
||||||
import sys
|
import sys
|
||||||
import uuid as uuid_module
|
|
||||||
from ipaddress import AddressValueError, IPv4Address, IPv6Address
|
from ipaddress import AddressValueError, IPv4Address, IPv6Address
|
||||||
from os import access, R_OK
|
|
||||||
|
|
||||||
import cherrypy
|
|
||||||
# from cherrypy.lib.static import serve_file
|
|
||||||
from jinja2 import Template
|
|
||||||
import yaml
|
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__))
|
||||||
|
|
||||||
|
@ -26,34 +24,17 @@ 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 CloudInitRequest:
|
class CloudInitApp:
|
||||||
"""
|
"""
|
||||||
Request data for persistence across methods
|
Serve cloud init files
|
||||||
"""
|
"""
|
||||||
def __init__(self, request, uuid=None):
|
|
||||||
|
def __init__(self):
|
||||||
self.remoteip = None
|
self.remoteip = None
|
||||||
self.hostinfo = ('localhost', )
|
self.hostinfo = ('localhost', )
|
||||||
self.request = request
|
|
||||||
self.meta_data = None
|
|
||||||
self.meta_data_loaded = False
|
|
||||||
self.user_data = None
|
|
||||||
|
|
||||||
try:
|
|
||||||
self.uuid = str(uuid_module.UUID('{' + uuid + '}'))
|
|
||||||
# ValueError is wrong UUID syntax
|
|
||||||
# TypeError is None
|
|
||||||
except (ValueError, TypeError):
|
|
||||||
self.uuid = None
|
|
||||||
|
|
||||||
self._init_ip()
|
|
||||||
self._generate_default_meta_data()
|
|
||||||
|
|
||||||
def _can_ip_be_proxy(self):
|
def _can_ip_be_proxy(self):
|
||||||
"""
|
self.remoteip = cherrypy.request.remote.ip
|
||||||
Assuming the connection is through a proxy, is this proxy permitted?
|
|
||||||
Can't proxy from a publicly reachable IP.
|
|
||||||
"""
|
|
||||||
self.remoteip = self.request.remote.ip
|
|
||||||
try:
|
try:
|
||||||
ipobj = IPv4Address(self.remoteip)
|
ipobj = IPv4Address(self.remoteip)
|
||||||
except AddressValueError:
|
except AddressValueError:
|
||||||
|
@ -69,229 +50,90 @@ class CloudInitRequest:
|
||||||
"""
|
"""
|
||||||
if self._can_ip_be_proxy():
|
if self._can_ip_be_proxy():
|
||||||
try:
|
try:
|
||||||
self.remoteip = self.request.headers.get(
|
self.remoteip = cherrypy.request.headers.get(
|
||||||
'X-Real-Ip',
|
'X-Real-Ip',
|
||||||
self.request.remote.ip
|
cherrypy.request.remote.ip
|
||||||
)
|
)
|
||||||
except KeyError:
|
except KeyError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
try:
|
try:
|
||||||
self.hostinfo = socket.gethostbyaddr(self.remoteip)
|
self.hostinfo = socket.gethostbyaddr(self.remoteip)
|
||||||
forward_lookup = socket.gethostbyname(self.hostinfo[0])
|
|
||||||
if forward_lookup != self.remoteip:
|
|
||||||
self.hostinfo = ('localhost', )
|
|
||||||
except socket.herror:
|
except socket.herror:
|
||||||
self.hostinfo = ('localhost', )
|
pass
|
||||||
except socket.gaierror:
|
|
||||||
self.hostinfo = (self.remoteip, )
|
|
||||||
|
|
||||||
def _generate_default_meta_data(self):
|
def _redirect_if_needed(self):
|
||||||
"""
|
filepath = os.path.join(PATH, "data", self.hostinfo[0],
|
||||||
Build default meta-data based on the requesting IP
|
REDIRECT_FILENAME)
|
||||||
"""
|
if os.path.exists(filepath):
|
||||||
hostname = self.hostinfo[0]
|
try:
|
||||||
base = self.request.base
|
with open(filepath) as redirect:
|
||||||
self.meta_data = {
|
content = redirect.read().splitlines()
|
||||||
"instance-id": hostname.split(".")[0],
|
raise cherrypy.HTTPRedirect(content[0], 301)
|
||||||
"local-hostname": hostname,
|
except IOError:
|
||||||
"baseurl": base
|
return False
|
||||||
}
|
|
||||||
|
|
||||||
def _update_meta_data_from_file(self, filename):
|
|
||||||
"""
|
|
||||||
Load meta-data from a file
|
|
||||||
"""
|
|
||||||
with open(filename, "r") as metadata:
|
|
||||||
self.meta_data.update(yaml.safe_load(metadata))
|
|
||||||
|
|
||||||
def _get_filename(self, filetype):
|
|
||||||
"""
|
|
||||||
Get the best available filename for a given type
|
|
||||||
"""
|
|
||||||
check_dirs = ['localhost', ]
|
|
||||||
if self.uuid:
|
|
||||||
check_dirs.insert(0, self.uuid)
|
|
||||||
if self.meta_data['local-hostname'] != 'localhost':
|
|
||||||
check_dirs.insert(0, self.meta_data['local-hostname'])
|
|
||||||
for subdir in check_dirs:
|
|
||||||
filepath = os.path.join(PATH, "data",
|
|
||||||
subdir,
|
|
||||||
filetype)
|
|
||||||
if not os.path.isfile(filepath) \
|
|
||||||
or not access(filepath, R_OK):
|
|
||||||
continue
|
|
||||||
return filepath
|
|
||||||
return None
|
|
||||||
|
|
||||||
def get_meta_data(self):
|
|
||||||
"""
|
|
||||||
Return the appropriate meta-data for this request
|
|
||||||
"""
|
|
||||||
if self.meta_data_loaded:
|
|
||||||
return self.meta_data
|
|
||||||
self._generate_default_meta_data()
|
|
||||||
filename = self._get_filename(META_DATA_FILENAME)
|
|
||||||
if filename:
|
|
||||||
self._update_meta_data_from_file(
|
|
||||||
filename
|
|
||||||
)
|
|
||||||
self.meta_data_loaded = True
|
|
||||||
return self.meta_data
|
|
||||||
|
|
||||||
def save_meta_data(self, data):
|
|
||||||
"""
|
|
||||||
Save metadata
|
|
||||||
"""
|
|
||||||
filename = self._get_filename(META_DATA_FILENAME)
|
|
||||||
with open(filename, "wt") as filehandle:
|
|
||||||
filehandle.write(data)
|
|
||||||
|
|
||||||
def get_user_data(self):
|
|
||||||
"""
|
|
||||||
Return the appropriate meta-data for this request
|
|
||||||
"""
|
|
||||||
if self.user_data:
|
|
||||||
return self.user_data
|
|
||||||
filename = self._get_filename(USER_DATA_FILENAME)
|
|
||||||
if not filename:
|
|
||||||
return None
|
|
||||||
with open(filename, "r") as userdata:
|
|
||||||
self.user_data = userdata.read()
|
|
||||||
return self.user_data
|
|
||||||
return None
|
|
||||||
|
|
||||||
def optional_redirect(self):
|
|
||||||
"""
|
|
||||||
Return optional redirect, or None
|
|
||||||
"""
|
|
||||||
filename = self._get_filename(REDIRECT_FILENAME)
|
|
||||||
if not (filename and os.path.exists(filename)):
|
|
||||||
return None
|
|
||||||
try:
|
|
||||||
with open(filename) as redirect:
|
|
||||||
content = redirect.read().splitlines()
|
|
||||||
return content[0]
|
|
||||||
except IOError:
|
|
||||||
return None
|
|
||||||
return None
|
|
||||||
|
|
||||||
|
|
||||||
class CloudInitApp:
|
|
||||||
"""
|
|
||||||
Serve cloud init files
|
|
||||||
"""
|
|
||||||
|
|
||||||
@staticmethod
|
|
||||||
def _redirect_if_needed(request):
|
|
||||||
redirect = request.optional_redirect()
|
|
||||||
if redirect:
|
|
||||||
raise cherrypy.HTTPRedirect(redirect, 301)
|
|
||||||
return False
|
return False
|
||||||
|
|
||||||
@staticmethod
|
|
||||||
def _content_type(data):
|
|
||||||
if not data:
|
|
||||||
return "text/cloud-config"
|
|
||||||
if data.startswith("#include"):
|
|
||||||
return "text/x-include-url"
|
|
||||||
if data.startswith("## template: jinja"):
|
|
||||||
return "text/jinja2"
|
|
||||||
return "text/cloud-config"
|
|
||||||
|
|
||||||
@staticmethod
|
|
||||||
def _wrap_metadata(metadata):
|
|
||||||
return {'ds': {
|
|
||||||
'meta_data': metadata
|
|
||||||
}}
|
|
||||||
|
|
||||||
def _user_data(self, uuid=None, SYSUUID=None):
|
|
||||||
"""
|
|
||||||
Serves a static file
|
|
||||||
But can process a template if x-include-url
|
|
||||||
"""
|
|
||||||
if SYSUUID: # SYSLINUX support
|
|
||||||
uuid = SYSUUID
|
|
||||||
request = CloudInitRequest(cherrypy.request, uuid)
|
|
||||||
self._redirect_if_needed(request)
|
|
||||||
user_data = request.get_user_data()
|
|
||||||
|
|
||||||
c__ = self._content_type(user_data)
|
|
||||||
|
|
||||||
cherrypy.response.headers['Content-Type'] = c__
|
|
||||||
cherrypy.response.headers['Content-Disposition'] = \
|
|
||||||
'attachment; filename="user-data"'
|
|
||||||
|
|
||||||
if c__ == "text/x-include-url":
|
|
||||||
t__ = Template(user_data)
|
|
||||||
metadata = request.get_meta_data()
|
|
||||||
wrapped = CloudInitApp._wrap_metadata(metadata)
|
|
||||||
return t__.render(**wrapped)
|
|
||||||
return user_data
|
|
||||||
|
|
||||||
def _meta_data(self, uuid=None):
|
|
||||||
"""
|
|
||||||
Return meta-data in YAML
|
|
||||||
"""
|
|
||||||
request = CloudInitRequest(cherrypy.request, uuid)
|
|
||||||
self._redirect_if_needed(request)
|
|
||||||
|
|
||||||
cherrypy.response.headers['Content-Type'] = \
|
|
||||||
'text/yaml'
|
|
||||||
cherrypy.response.headers['Content-Disposition'] = \
|
|
||||||
'attachment; filename="meta-data"'
|
|
||||||
return yaml.dump(request.get_meta_data())
|
|
||||||
|
|
||||||
@staticmethod
|
|
||||||
def _vendor_data():
|
|
||||||
return ""
|
|
||||||
|
|
||||||
@cherrypy.expose
|
@cherrypy.expose
|
||||||
def user_data(self):
|
def user_data(self):
|
||||||
"""
|
"""
|
||||||
v1 api endpoint user-data
|
Serves a static file
|
||||||
"""
|
"""
|
||||||
return self._user_data()
|
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):
|
||||||
|
filepath = os.path.join(PATH, "data", USER_DATA_FILENAME)
|
||||||
|
return serve_file(filepath, "text/yaml", "attachment")
|
||||||
|
|
||||||
@cherrypy.expose
|
@cherrypy.expose
|
||||||
def meta_data(self):
|
def meta_data(self):
|
||||||
"""
|
"""
|
||||||
v1 api endpoint meta-data
|
Return meta-data in YAML
|
||||||
"""
|
"""
|
||||||
return self._meta_data()
|
self._init_ip()
|
||||||
|
self._redirect_if_needed()
|
||||||
|
hostname = self.hostinfo[0]
|
||||||
|
data = {
|
||||||
|
"instance-id": hostname.split(".")[0],
|
||||||
|
"local-hostname": hostname
|
||||||
|
}
|
||||||
|
|
||||||
|
filepath = os.path.join(PATH, "data", hostname, META_DATA_FILENAME)
|
||||||
|
if os.path.exists(filepath):
|
||||||
|
with open(filepath, "r") as metadata:
|
||||||
|
for line in metadata.readlines():
|
||||||
|
linesplit = list(map(lambda k: k.strip(), line.split(":")))
|
||||||
|
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)
|
||||||
|
|
||||||
@cherrypy.expose
|
@cherrypy.expose
|
||||||
def vendor_data(self):
|
def vendor_data(self):
|
||||||
"""
|
"""
|
||||||
v1 api endpoint vendor-data
|
Return empty vendor-data
|
||||||
"""
|
"""
|
||||||
return self._vendor_data()
|
|
||||||
|
|
||||||
@cherrypy.expose
|
|
||||||
def apiv2(self, uuid=None, filetype="user-data"):
|
|
||||||
"""
|
|
||||||
API endpoint v2
|
|
||||||
extract command from arguments
|
|
||||||
"""
|
|
||||||
if filetype == "user-data":
|
|
||||||
return self._user_data(uuid)
|
|
||||||
if filetype == "meta-data":
|
|
||||||
return self._meta_data(uuid)
|
|
||||||
if filetype == "vendor-data":
|
|
||||||
return self._vendor_data()
|
|
||||||
return ""
|
return ""
|
||||||
|
|
||||||
# unused
|
@cherrypy.expose
|
||||||
# @cherrypy.expose
|
|
||||||
def finished(self, data):
|
def finished(self, data):
|
||||||
"""
|
"""
|
||||||
Saves additional meta-data
|
Saves additional meta-data
|
||||||
|
|
||||||
:param data: meta-data to be added
|
:param data: meta-data to be added
|
||||||
"""
|
"""
|
||||||
request = CloudInitRequest(cherrypy.request)
|
self._init_ip()
|
||||||
self._redirect_if_needed(request)
|
folder = os.path.join(PATH, "data", self.hostinfo[0])
|
||||||
request.save_meta_data(data)
|
if not os.path.exists(folder):
|
||||||
|
os.makedirs(folder)
|
||||||
|
|
||||||
|
with open(os.path.join(folder, META_DATA_FILENAME), "w") as fin:
|
||||||
|
fin.write(data)
|
||||||
|
|
||||||
|
|
||||||
ROOT = CloudInitApp()
|
ROOT = CloudInitApp()
|
||||||
|
@ -303,27 +145,14 @@ if __name__ == "__main__":
|
||||||
CONFIG["server"].getint("server_port", 8081)
|
CONFIG["server"].getint("server_port", 8081)
|
||||||
ENGINE = cherrypy.engine
|
ENGINE = cherrypy.engine
|
||||||
|
|
||||||
CURRENT_DIR = os.path.dirname(os.path.abspath(__file__))
|
cherrypy.tree.mount(ROOT)
|
||||||
CONFIG = {
|
|
||||||
'/include': {
|
|
||||||
'tools.staticdir.on': True,
|
|
||||||
'tools.staticdir.dir': os.path.join(CURRENT_DIR,
|
|
||||||
'data',
|
|
||||||
'include'),
|
|
||||||
'tools.staticdir.content_types': {
|
|
||||||
'yml': 'text/yaml'
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
cherrypy.tree.mount(ROOT, config=CONFIG)
|
|
||||||
|
|
||||||
if hasattr(ENGINE, "signal_handler"):
|
if hasattr(ENGINE, "signal_handler"):
|
||||||
ENGINE.signal_handler.subscribe()
|
ENGINE.signal_handler.subscribe()
|
||||||
if hasattr(ENGINE, "console_control_handler"):
|
if hasattr(ENGINE, "console_control_handler"):
|
||||||
ENGINE.console_control_handler.subscribe()
|
ENGINE.console_control_handler.subscribe()
|
||||||
try:
|
try:
|
||||||
ENGINE.start()
|
ENGINE.start()
|
||||||
except Exception: # pylint: disable=broad-except
|
except Exception:
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
else:
|
else:
|
||||||
ENGINE.block()
|
ENGINE.block()
|
||||||
|
|
Loading…
Reference in New Issue
Block a user