2021-03-01 10:33:42 +01:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
"""
|
|
|
|
Serve cloud init files
|
|
|
|
"""
|
|
|
|
|
|
|
|
import configparser
|
2021-01-20 16:48:22 +01:00
|
|
|
import os
|
2021-03-01 10:33:42 +01:00
|
|
|
import socket
|
2021-01-20 16:48:22 +01:00
|
|
|
import sys
|
2021-03-01 10:33:42 +01:00
|
|
|
from ipaddress import AddressValueError, IPv4Address, IPv6Address
|
2021-01-20 16:48:22 +01:00
|
|
|
|
|
|
|
import cherrypy
|
|
|
|
from cherrypy.lib.static import serve_file
|
2021-06-04 12:54:17 +02:00
|
|
|
from jinja2 import Template
|
|
|
|
import yaml
|
2021-01-20 16:48:22 +01:00
|
|
|
|
|
|
|
PATH = os.path.dirname(os.path.abspath(__file__))
|
|
|
|
|
2021-03-01 10:33:42 +01:00
|
|
|
CONFIG = configparser.ConfigParser()
|
|
|
|
CONFIG.read(os.path.join(PATH, "config.ini"))
|
|
|
|
|
|
|
|
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")
|
|
|
|
|
2021-01-20 16:48:22 +01:00
|
|
|
|
2021-03-01 10:33:42 +01:00
|
|
|
class CloudInitApp:
|
|
|
|
"""
|
|
|
|
Serve cloud init files
|
|
|
|
"""
|
2021-01-20 16:48:22 +01:00
|
|
|
|
2021-03-01 10:33:42 +01:00
|
|
|
def __init__(self):
|
|
|
|
self.remoteip = None
|
|
|
|
self.hostinfo = ('localhost', )
|
2021-01-20 16:48:22 +01:00
|
|
|
|
2021-03-01 10:33:19 +01:00
|
|
|
def _can_ip_be_proxy(self):
|
|
|
|
self.remoteip = cherrypy.request.remote.ip
|
|
|
|
try:
|
|
|
|
ipobj = IPv4Address(self.remoteip)
|
|
|
|
except AddressValueError:
|
|
|
|
try:
|
|
|
|
ipobj = IPv6Address(self.remoteip)
|
|
|
|
except AddressValueError:
|
|
|
|
return False
|
|
|
|
return not ipobj.is_global
|
|
|
|
|
2021-01-23 17:24:06 +01:00
|
|
|
def _init_ip(self):
|
|
|
|
"""
|
|
|
|
Get remote IP
|
|
|
|
"""
|
2021-03-01 10:33:19 +01:00
|
|
|
if self._can_ip_be_proxy():
|
|
|
|
try:
|
|
|
|
self.remoteip = cherrypy.request.headers.get(
|
|
|
|
'X-Real-Ip',
|
|
|
|
cherrypy.request.remote.ip
|
|
|
|
)
|
|
|
|
except KeyError:
|
|
|
|
pass
|
2021-01-23 17:24:06 +01:00
|
|
|
|
2021-02-10 17:13:31 +01:00
|
|
|
try:
|
|
|
|
self.hostinfo = socket.gethostbyaddr(self.remoteip)
|
|
|
|
except socket.herror:
|
2021-03-01 10:33:42 +01:00
|
|
|
pass
|
2021-01-23 17:24:06 +01:00
|
|
|
|
2021-03-01 10:00:50 +01:00
|
|
|
def _redirect_if_needed(self):
|
|
|
|
filepath = os.path.join(PATH, "data", self.hostinfo[0],
|
2021-03-01 10:33:42 +01:00
|
|
|
REDIRECT_FILENAME)
|
2021-03-01 10:00:50 +01:00
|
|
|
if os.path.exists(filepath):
|
|
|
|
try:
|
2021-03-01 10:33:42 +01:00
|
|
|
with open(filepath) as redirect:
|
|
|
|
content = redirect.read().splitlines()
|
2021-03-01 10:00:50 +01:00
|
|
|
raise cherrypy.HTTPRedirect(content[0], 301)
|
2021-03-01 10:33:42 +01:00
|
|
|
except IOError:
|
2021-03-01 10:00:50 +01:00
|
|
|
return False
|
|
|
|
return False
|
|
|
|
|
2021-06-04 12:54:17 +02:00
|
|
|
def _generate_metadata(self):
|
|
|
|
self._init_ip()
|
|
|
|
hostname = self.hostinfo[0]
|
|
|
|
base = cherrypy.request.base
|
|
|
|
data = {
|
|
|
|
"instance-id": hostname.split(".")[0],
|
|
|
|
"local-hostname": hostname,
|
|
|
|
"baseurl": base
|
|
|
|
}
|
|
|
|
return data
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def _content_type(data):
|
|
|
|
if data.startswith("#include"):
|
|
|
|
return "text/x-include-url"
|
|
|
|
elif data.startswith("## template: jinja"):
|
|
|
|
return "text/jinja2"
|
|
|
|
else:
|
|
|
|
return "text/cloud-config"
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def _wrap_metadata(metadata):
|
|
|
|
return {'ds': {
|
|
|
|
'meta_data': metadata
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-20 16:48:22 +01:00
|
|
|
@cherrypy.expose
|
|
|
|
def user_data(self):
|
2021-01-23 17:24:06 +01:00
|
|
|
"""
|
|
|
|
Serves a static file
|
|
|
|
"""
|
2021-02-10 17:13:31 +01:00
|
|
|
self._init_ip()
|
2021-03-01 10:00:50 +01:00
|
|
|
self._redirect_if_needed()
|
2021-02-10 17:13:31 +01:00
|
|
|
filepath = os.path.join(PATH, "data", self.hostinfo[0],
|
2021-03-01 10:33:42 +01:00
|
|
|
USER_DATA_FILENAME)
|
2021-02-10 17:13:31 +01:00
|
|
|
if not os.path.exists(filepath):
|
2021-03-01 10:33:42 +01:00
|
|
|
filepath = os.path.join(PATH, "data", USER_DATA_FILENAME)
|
2021-06-04 12:54:17 +02:00
|
|
|
with open(filepath, "r") as userdata:
|
|
|
|
data = userdata.read()
|
|
|
|
|
|
|
|
c = CloudInitApp._content_type(data)
|
|
|
|
|
|
|
|
cherrypy.response.headers['Content-Type'] = c
|
|
|
|
cherrypy.response.headers['Content-Disposition'] = \
|
|
|
|
'attachment; filename="user-data"'
|
|
|
|
|
|
|
|
if c == "text/x-include-url":
|
|
|
|
t = Template(data)
|
|
|
|
metadata = self._generate_metadata()
|
|
|
|
wrapped = CloudInitApp._wrap_metadata(metadata)
|
|
|
|
return t.render(**wrapped)
|
|
|
|
else:
|
|
|
|
return data
|
2021-01-20 16:48:22 +01:00
|
|
|
|
|
|
|
@cherrypy.expose
|
|
|
|
def meta_data(self):
|
2021-01-23 17:24:06 +01:00
|
|
|
"""
|
|
|
|
Return meta-data in YAML
|
|
|
|
"""
|
2021-02-10 17:13:31 +01:00
|
|
|
self._init_ip()
|
2021-03-01 10:00:50 +01:00
|
|
|
self._redirect_if_needed()
|
2021-01-20 16:48:22 +01:00
|
|
|
|
2021-06-04 12:54:17 +02:00
|
|
|
data = self._generate_metadata()
|
|
|
|
|
|
|
|
filepath = os.path.join(PATH, "data", data['local-hostname'], META_DATA_FILENAME)
|
2021-01-23 17:24:06 +01:00
|
|
|
if os.path.exists(filepath):
|
2021-03-01 10:32:07 +01:00
|
|
|
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]
|
2021-01-20 16:48:22 +01:00
|
|
|
|
2021-03-01 10:33:42 +01:00
|
|
|
cherrypy.response.headers['Content-Type'] = \
|
|
|
|
'text/yaml'
|
|
|
|
cherrypy.response.headers['Content-Disposition'] = \
|
2021-06-04 12:54:17 +02:00
|
|
|
'attachment; filename="meta-data"'
|
2021-01-23 17:24:06 +01:00
|
|
|
return yaml.dump(data)
|
2021-01-20 16:48:22 +01:00
|
|
|
|
2021-03-01 10:00:50 +01:00
|
|
|
@cherrypy.expose
|
|
|
|
def vendor_data(self):
|
|
|
|
"""
|
|
|
|
Return empty vendor-data
|
|
|
|
"""
|
|
|
|
return ""
|
|
|
|
|
2021-01-20 16:48:22 +01:00
|
|
|
@cherrypy.expose
|
|
|
|
def finished(self, data):
|
2021-01-23 17:24:06 +01:00
|
|
|
"""
|
|
|
|
Saves additional meta-data
|
|
|
|
|
|
|
|
:param data: meta-data to be added
|
|
|
|
"""
|
2021-02-10 17:13:31 +01:00
|
|
|
self._init_ip()
|
2021-01-23 17:24:06 +01:00
|
|
|
folder = os.path.join(PATH, "data", self.hostinfo[0])
|
2021-01-20 16:48:22 +01:00
|
|
|
if not os.path.exists(folder):
|
|
|
|
os.makedirs(folder)
|
|
|
|
|
2021-03-01 10:33:42 +01:00
|
|
|
with open(os.path.join(folder, META_DATA_FILENAME), "w") as fin:
|
|
|
|
fin.write(data)
|
2021-01-20 16:48:22 +01:00
|
|
|
|
|
|
|
|
2021-03-01 10:33:42 +01:00
|
|
|
ROOT = CloudInitApp()
|
2021-01-20 16:48:22 +01:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2021-03-01 10:33:42 +01:00
|
|
|
cherrypy.server.socket_host = \
|
|
|
|
CONFIG["server"].get("server_host", "127.0.0.1")
|
|
|
|
cherrypy.server.socket_port = \
|
|
|
|
CONFIG["server"].getint("server_port", 8081)
|
2021-01-20 16:48:22 +01:00
|
|
|
ENGINE = cherrypy.engine
|
|
|
|
|
2021-06-04 12:54:17 +02:00
|
|
|
current_dir = os.path.dirname(os.path.abspath(__file__))
|
|
|
|
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)
|
|
|
|
|
2021-01-20 16:48:22 +01:00
|
|
|
if hasattr(ENGINE, "signal_handler"):
|
|
|
|
ENGINE.signal_handler.subscribe()
|
|
|
|
if hasattr(ENGINE, "console_control_handler"):
|
|
|
|
ENGINE.console_control_handler.subscribe()
|
|
|
|
try:
|
|
|
|
ENGINE.start()
|
|
|
|
except Exception:
|
|
|
|
sys.exit(1)
|
|
|
|
else:
|
|
|
|
ENGINE.block()
|