Add support for #include user-data format

This commit is contained in:
Peter Šurda 2021-06-04 18:54:17 +08:00
parent 67a2395db3
commit 2d1eb2826a
Signed by: PeterSurda
GPG Key ID: 0C5F50C0B5F37D87

70
main.py
View File

@ -9,10 +9,10 @@ import socket
import sys import sys
from ipaddress import AddressValueError, IPv4Address, IPv6Address from ipaddress import AddressValueError, IPv4Address, IPv6Address
import yaml
import cherrypy import cherrypy
from cherrypy.lib.static import serve_file from cherrypy.lib.static import serve_file
from jinja2 import Template
import yaml
PATH = os.path.dirname(os.path.abspath(__file__)) PATH = os.path.dirname(os.path.abspath(__file__))
@ -74,6 +74,33 @@ class CloudInitApp:
return False return False
return False return False
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
}
}
@cherrypy.expose @cherrypy.expose
def user_data(self): def user_data(self):
""" """
@ -85,7 +112,22 @@ class CloudInitApp:
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, "text/yaml", "attachment") 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
@cherrypy.expose @cherrypy.expose
def meta_data(self): def meta_data(self):
@ -94,13 +136,10 @@ class CloudInitApp:
""" """
self._init_ip() self._init_ip()
self._redirect_if_needed() 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) data = self._generate_metadata()
filepath = os.path.join(PATH, "data", data['local-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():
@ -110,7 +149,7 @@ class CloudInitApp:
cherrypy.response.headers['Content-Type'] = \ cherrypy.response.headers['Content-Type'] = \
'text/yaml' 'text/yaml'
cherrypy.response.headers['Content-Disposition'] = \ cherrypy.response.headers['Content-Disposition'] = \
'attachment; filename="user-data"' 'attachment; filename="meta-data"'
return yaml.dump(data) return yaml.dump(data)
@cherrypy.expose @cherrypy.expose
@ -145,7 +184,16 @@ if __name__ == "__main__":
CONFIG["server"].getint("server_port", 8081) CONFIG["server"].getint("server_port", 8081)
ENGINE = cherrypy.engine ENGINE = cherrypy.engine
cherrypy.tree.mount(ROOT) 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)
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"):