Compare commits

...

2 Commits

Author SHA1 Message Date
a2b36e7299
PEP8
Some checks failed
buildbot/travis_bionic Build done.
2021-12-02 15:50:34 +08:00
a51b499cb3
Add UUID syntax validation 2021-12-02 15:43:43 +08:00

21
main.py
View File

@ -7,6 +7,7 @@ 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 from os import access, R_OK
@ -33,10 +34,17 @@ class CloudInitRequest:
self.remoteip = None self.remoteip = None
self.hostinfo = ('localhost', ) self.hostinfo = ('localhost', )
self.request = request self.request = request
self.uuid = uuid
self.meta_data = None self.meta_data = None
self.meta_data_loaded = False self.meta_data_loaded = False
self.user_data = None 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._init_ip()
self._generate_default_meta_data() self._generate_default_meta_data()
@ -195,8 +203,7 @@ class CloudInitApp:
def _wrap_metadata(metadata): def _wrap_metadata(metadata):
return {'ds': { return {'ds': {
'meta_data': metadata 'meta_data': metadata
} }}
}
def _user_data(self, uuid=None): def _user_data(self, uuid=None):
""" """
@ -294,11 +301,11 @@ 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__)) CURRENT_DIR = os.path.dirname(os.path.abspath(__file__))
config = { CONFIG = {
'/include': { '/include': {
'tools.staticdir.on': True, 'tools.staticdir.on': True,
'tools.staticdir.dir': os.path.join(current_dir, 'tools.staticdir.dir': os.path.join(CURRENT_DIR,
'data', 'data',
'include'), 'include'),
'tools.staticdir.content_types': { 'tools.staticdir.content_types': {
@ -306,7 +313,7 @@ if __name__ == "__main__":
} }
} }
} }
cherrypy.tree.mount(ROOT, config=config) cherrypy.tree.mount(ROOT, config=CONFIG)
if hasattr(ENGINE, "signal_handler"): if hasattr(ENGINE, "signal_handler"):
ENGINE.signal_handler.subscribe() ENGINE.signal_handler.subscribe()