Skip to content
This repository has been archived by the owner on Oct 26, 2023. It is now read-only.

Commit

Permalink
Merge branch 'master' into wip/tanty/remove-signal-handlers-from-dhcp
Browse files Browse the repository at this point in the history
  • Loading branch information
icb- authored Apr 10, 2022
2 parents cb4e3d3 + 90c263d commit 827d85f
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 16 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ The following are arguments that can be passed to `pypxe.server` when running fr
|Argument|Description|Default|
|---|---|---|
|__`--tftp-server-ip TFTP_SERVER_IP`__|Specify TFTP server IP address|`0.0.0.0`|
|__`--tftp-port TFTP_PORT`__|Specify TFTP server Port|69|

##### HTTP Service Arguments

Expand Down
1 change: 1 addition & 0 deletions example_cfg.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"STATIC_CONFIG": "",
"SYSLOG_PORT": 514,
"SYSLOG_SERVER": null,
"TFTP_PORT": 69,
"TFTP_SERVER_IP": "192.168.2.2",
"USE_DHCP": true,
"USE_HTTP": false,
Expand Down
44 changes: 28 additions & 16 deletions pypxe/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
'STATIC_CONFIG':'',
'SYSLOG_SERVER':None,
'SYSLOG_PORT':514,
'TFTP_PORT':69,
'TFTP_SERVER_IP':'0.0.0.0',
'USE_IPXE':False,
'USE_HTTP':False,
Expand Down Expand Up @@ -119,6 +120,7 @@ def parse_cli_arguments():

# TFTP server arguments
tftp_group = parser.add_argument_group(title = 'TFTP', description = 'Arguments relevant to the TFTP server')
tftp_group.add_argument('--tftp-port', action = 'store', dest = 'TFTP_PORT', help = 'TFTP Server Port', default = SETTINGS['TFTP_PORT'])
tftp_group.add_argument('--tftp-server-ip', action = 'store', dest = 'TFTP_SERVER_IP', help = 'TFTP Server IP', default = SETTINGS['TFTP_SERVER_IP'])

return parser.parse_args()
Expand Down Expand Up @@ -171,22 +173,6 @@ def main():
if os.getuid() != 0:
print(sys.stderr, '\nWARNING: Not root. Servers will probably fail to bind.\n')


# ideally this would be in dhcp itself, but the chroot below *probably*
# breaks the ability to open the config file.
if args.STATIC_CONFIG:
try:
static_config = io.open(args.STATIC_CONFIG, 'r')
except IOError:
sys.exit("Failed to open {0}".format(args.STATIC_CONFIG))
try:
loaded_statics = json.load(static_config)
static_config.close()
except ValueError:
sys.exit("{0} does not contain valid json".format(args.STATIC_CONFIG))
else:
loaded_statics = dict()

# setup main logger
sys_logger = logging.getLogger('PyPXE')
if args.SYSLOG_SERVER:
Expand Down Expand Up @@ -242,6 +228,7 @@ def main():
mode_verbose = do_verbose('tftp'),
logger = tftp_logger,
netboot_directory = args.NETBOOT_DIR,
port = args.TFTP_PORT,
ip = args.TFTP_SERVER_IP)
tftpd = threading.Thread(target = tftp_server.listen)
tftpd.daemon = True
Expand All @@ -258,6 +245,31 @@ def dhcp_export_leases(signum, frame):
if signum == signal.SIGINT:
raise KeyboardInterrupt

def load_dhcp_static_config(filename):
result = ['', dict()]

try:
static_config = io.open(filename, 'r')
except IOError:
result[0] = "Failed to open {0}".format(filename)
return result

try:
result[1] = json.load(static_config)
static_config.close()
except ValueError:
result[0] = "{0} does not contain valid json".format(filename)
return result

return result

if args.STATIC_CONFIG:
message, loaded_statics = load_dhcp_static_config(args.STATIC_CONFIG)
if message:
sys.exit(message)
else:
loaded_statics = dict()

# setup DHCP logger
dhcp_logger = helpers.get_child_logger(sys_logger, 'DHCP')
if args.DHCP_MODE_PROXY:
Expand Down

0 comments on commit 827d85f

Please sign in to comment.