-
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 f2ff027
Showing
13 changed files
with
280 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,38 @@ | ||
""" | ||
This package contains the actual TFTP server. | ||
""" | ||
|
||
import logging | ||
import logging.config | ||
import sys | ||
|
||
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): | ||
"""Main entry point for the server.""" | ||
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: | ||
Check warning on line 31 in src/cobbler_tftp/server/__init__.py Codacy Production / Codacy Static Code Analysissrc/cobbler_tftp/server/__init__.py#L31
|
||
logging.exception("Fatal exception while setting up server") | ||
return | ||
try: | ||
server.run() | ||
except: | ||
logging.exception("Fatal exception in server") | ||
server.close() |
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,60 @@ | ||
""" | ||
This module contains the main TFTP server class. | ||
""" | ||
|
||
import logging | ||
Check warning on line 5 in src/cobbler_tftp/server/tftp.py Codacy Production / Codacy Static Code Analysissrc/cobbler_tftp/server/tftp.py#L5
|
||
|
||
from fbtftp import BaseHandler, BaseServer, ResponseData | ||
|
||
|
||
class CobblerResponseData(ResponseData): | ||
def __init__(self): | ||
pass | ||
|
||
def read(self, n): | ||
Check notice on line 14 in src/cobbler_tftp/server/tftp.py Codacy Production / Codacy Static Code Analysissrc/cobbler_tftp/server/tftp.py#L14
|
||
return b"" | ||
|
||
def size(self): | ||
return 0 | ||
|
||
def close(self): | ||
pass | ||
|
||
|
||
def handler_stats_cb(stats): | ||
""" | ||
Called by the fbtftp to log handler stats. Currently unused. | ||
""" | ||
|
||
|
||
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): | ||
# 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): | ||
# 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
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
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.