-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement daemon with logging and TFTP server
- Loading branch information
1 parent
ff1a347
commit a374e83
Showing
13 changed files
with
306 additions
and
34 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
""" | ||
This package contains the actual TFTP server. | ||
""" | ||
|
||
import logging | ||
import logging.config | ||
|
||
from cobbler_tftp.server.tftp import TFTPServer | ||
from cobbler_tftp.settings import Settings | ||
|
||
try: | ||
from importlib.resources import files | ||
except ImportError: | ||
from importlib_resources import files | ||
|
||
|
||
def run_server(application_settings: Settings): | ||
"""Set up logging, initialize the server and run it.""" | ||
|
||
logging_conf = application_settings.logging_conf | ||
if logging_conf is None or not logging_conf.exists(): | ||
logging_conf = files("cobbler_tftp.settings.data").joinpath("logging.conf") | ||
logging.config.fileConfig(str(logging_conf)) | ||
logging.debug("Server starting...") | ||
try: | ||
address = application_settings.tftp_addr | ||
port = application_settings.tftp_port | ||
retries = application_settings.tftp_retries | ||
timeout = application_settings.tftp_timeout | ||
server = TFTPServer(address, port, retries, timeout) | ||
except: # pylint: disable=bare-except | ||
logging.exception("Fatal exception while setting up server") | ||
return | ||
try: | ||
server.run() | ||
except: # pylint: disable=bare-except | ||
logging.exception("Fatal exception in server") | ||
# fbtftp doesn't clean up after exceptions, so we do it here ourselves | ||
server._metrics_timer.cancel() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
""" | ||
This module contains the main TFTP server class. | ||
""" | ||
|
||
import logging | ||
|
||
from fbtftp import BaseHandler, BaseServer, ResponseData | ||
|
||
|
||
class CobblerResponseData(ResponseData): | ||
"""File-like object representing the response from the TFTP server.""" | ||
|
||
def __init__(self): | ||
pass | ||
|
||
def read(self, n): | ||
return b"" | ||
|
||
def size(self): | ||
return 0 | ||
|
||
def close(self): | ||
pass | ||
|
||
|
||
def handler_stats_cb(stats): | ||
duration = stats.duration() * 1000 | ||
logging.info( | ||
"Spent %fms processing request for %r from %r", | ||
duration, | ||
stats.file_path, | ||
stats.peer, | ||
) | ||
logging.info( | ||
"Error: %r, sent %d bytes with %d retransmits", | ||
stats.error, | ||
stats.bytes_sent, | ||
stats.retransmits, | ||
) | ||
|
||
|
||
def server_stats_cb(stats): | ||
""" | ||
Called by the fbtftp to log server stats. Currently unused. | ||
""" | ||
|
||
|
||
class CobblerRequestHandler(BaseHandler): | ||
""" | ||
Handles TFTP requests using the Cobbler API. | ||
""" | ||
|
||
def __init__(self, server_addr, peer, path, options): | ||
""" | ||
Initialize a handler for a specific request. | ||
:param server_addr: Tuple containing the server address and port. | ||
:param peer: Tuple containing the client address and port. | ||
:param path: Request file path. | ||
:param options: Options requested by the client. | ||
""" | ||
# Future arguments can be handled here | ||
super().__init__(server_addr, peer, path, options, handler_stats_cb) | ||
|
||
def get_response_data(self): | ||
return CobblerResponseData() | ||
|
||
|
||
class TFTPServer(BaseServer): | ||
""" | ||
Implements a TFTP server for the Cobbler API using the CobblerRequestHandler. | ||
""" | ||
|
||
def __init__(self, address, port, retries, timeout): | ||
""" | ||
Initialize the TFTP server. | ||
:param address: IP address to listen on. | ||
:param port: UDP Port to listen on. | ||
:param retries: Maximum number of retries when sending a packet fails. | ||
:param timeout: Timeout for sending packets. | ||
""" | ||
# Future arguments can be handled here | ||
self.handler_stats_cb = handler_stats_cb | ||
super().__init__(address, port, retries, timeout, server_stats_cb) | ||
|
||
def get_handler(self, server_addr, peer, path, options): | ||
return CobblerRequestHandler(server_addr, peer, path, options) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# based on https://github.com/cobbler/cobbler/blob/main/config/cobbler/logging_config.conf | ||
[loggers] | ||
keys=root | ||
|
||
[handlers] | ||
keys=FileLogger,stdout | ||
|
||
[formatters] | ||
keys=Logfile,stdout | ||
|
||
[logger_root] | ||
level=DEBUG | ||
handlers=FileLogger,stdout | ||
|
||
[logger_parser] | ||
level=DEBUG | ||
handlers=FileLogger | ||
propagate=1 | ||
qualname=compiler.parser | ||
|
||
[handler_stdout] | ||
class=StreamHandler | ||
level=INFO | ||
formatter=stdout | ||
args=(sys.stdout,) | ||
|
||
[handler_FileLogger] | ||
class=FileHandler | ||
level=INFO | ||
formatter=Logfile | ||
args=('/var/log/cobbler-tftp.log', 'a') | ||
|
||
[formatter_Logfile] | ||
format=[%(threadName)s] %(asctime)s - %(levelname)s | %(message)s | ||
datefmt=%Y-%m-%dT%H:%M:%S | ||
class=logging.Formatter | ||
|
||
[formatter_stdout] | ||
format=%(levelname)s | %(message)s | ||
class=logging.Formatter |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.