Skip to content

Commit

Permalink
chg: dev: debugging tftpdaemon root directory handling !wip
Browse files Browse the repository at this point in the history
* TftpServer bits seem to only accept either absolute path or '.'
  but not a local relative path when daemonized

Signed-off-by: Stephen L Arnold <sarnold@vctlabs.com>
  • Loading branch information
sarnold committed Dec 18, 2024
1 parent 589496e commit c99985e
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 21 deletions.
37 changes: 19 additions & 18 deletions scripts/tftpdaemon
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,28 @@ import os
import sys
from pathlib import Path

import tftpy
from daemon import Daemon
from daemon.parent_logger import setup_logging
from tftpy import TftpServer

from pyserv import __version__
from pyserv.settings import (
DEBUG,
DOCROOT,
LOG,
PID,
SOCK_TIMEOUT,
get_userdirs,
init_dirs,
platform_check,
)

# from logging_tree import printout # debug logging
# pylint: disable=R0801


DEF_TFTP_PORT = os.getenv('PORT', default='9069')
IFACE = os.getenv('IFACE', default='')
LPNAME = os.getenv('LPNAME', default='tftpd')
IFACE = os.getenv('IFACE', default='0.0.0.0')
PORT = os.getenv('PORT', default='9069')
LOG = os.getenv('LOG', default=str(get_userdirs()[0].joinpath(f'{LPNAME}.log')))
PID = os.getenv('PID', default=str(get_userdirs()[1].joinpath(f'{LPNAME}.pid')))
DOCROOT = os.getenv('DOCROOT', default=str(get_userdirs()[2]))


class ServDaemon(Daemon):
Expand All @@ -38,24 +38,21 @@ class ServDaemon(Daemon):
server.
"""

port = int(PORT)
server = None
timeout = int(SOCK_TIMEOUT)
serv = None

def run(self):
"""
Daemon needs a run method.
"""
self.server = tftpy.TftpServer(DOCROOT)
self.server.listen(
listenip=IFACE,
listenport=self.port,
timeout=self.timeout,
)
self.cur_dir = os.getcwd()
os.chdir(DOCROOT)
self.serv = TftpServer('.')
self.serv.listen(IFACE, int(DEF_TFTP_PORT), int(SOCK_TIMEOUT))

def cleanup(self):
"""And we need a cleanup method."""
self.server.stop()
self.serv.stop()
os.chdir(self.cur_dir)


if __name__ == "__main__":
Expand All @@ -78,6 +75,10 @@ if __name__ == "__main__":
init_dirs([Path(LOG).parent, Path(PID).parent])
setup_logging(DEBUG, Path(LOG), 'tftpd')
# printout()
if DEBUG == '1':
print(f'LOG: {LOG}')
print(f'PID: {PID}')
print(f'DOCROOT: {DOCROOT}')

d = ServDaemon(Path(PID), home_dir=DOCROOT, verbose=0, use_cleanup=True)
d = ServDaemon(Path(PID), home_dir=DOCROOT, verbose=int(DEBUG), use_cleanup=True)
getattr(d, args.run[0])()
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ setup_requires =
setuptools_scm[toml]

install_requires =
daemonizer @ https://github.com/sarnold/python-daemonizer/releases/download/0.4.0/daemonizer-0.4.0.tar.gz
daemonizer @ https://github.com/sarnold/python-daemonizer/releases/download/0.4.0/daemonizer-0.4.0-py3-none-any.whl
platformdirs
tftpy @ https://github.com/VCTLabs/tftpy/releases/download/0.8.2.1/tftpy-0.8.2.1.tar.gz

Expand Down
2 changes: 1 addition & 1 deletion src/pyserv/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def show_uservars():
print(f"FAILED: {repr(exc)}")


DEBUG = os.getenv('DEBUG', default=None)
DEBUG = os.getenv('DEBUG', default='0')
PORT = os.getenv('PORT', default='8000')
IFACE = os.getenv('IFACE', default='127.0.0.1')
LPNAME = os.getenv('LPNAME', default='httpd')
Expand Down
Empty file added tests/data/.gitkeep
Empty file.
59 changes: 58 additions & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,6 @@ allowlist_externals =
deps =
{[base]deps}
logging_tree
-r requirements.txt
.

commands_pre =
Expand All @@ -222,6 +221,64 @@ commands =
commands_post =
tftpdaemon stop

[testenv:tftpd]
always_copy = true
skip_install = true

setenv =
DOCROOT = {env:DOCROOT:./}
IFACE = {env:IFACE:0.0.0.0}
HOST_IP = {env:HOST_IP:192.168.7.10}
PORT = {env:PORT:69}
URL = tftp://{env:HOST_IP}:{env:PORT}
LPNAME = {env:LPNAME:tftpd}
DEBUG = {env:DEBUG:}
LOG = {env:LOG:{envlogdir}/{env:LPNAME}.log}
PID = {env:PID:{envtmpdir}/{env:LPNAME}.pid}
TAIL = {env:TAIL:5}
TST_FILE = {env:TST_FILE:testbin.swu}
BLK_SIZE = {env:BLK_SIZE:8192}

passenv =
HOME
USERNAME
USER
XDG_*
PYTHONIOENCODING
PIP_DOWNLOAD_CACHE

allowlist_externals =
bash
cmp
curl
ls
tail

deps =
{[base]deps}
logging_tree
.

commands_pre =
#python -m pip install .
-bash -c 'dd if=/dev/zero of=$DOCROOT/$TST_FILE bs=1M count=40'
-bash -c 'sudo setcap cap_net_bind_service+ep {envpython}'

commands =
python -c 'from pyserv.settings import show_uservars; show_uservars()'
tftpdaemon -h
tftpdaemon start
bash -c 'sleep 2'
-curl --tftp-blksize {env:BLK_SIZE} --output tests/{env:TST_FILE} {env:URL}/{env:TST_FILE}
bash -c 'sleep 1'
tail -n {env:TAIL} {env:LOG}
cmp {env:DOCROOT}/{env:TST_FILE} tests/{env:TST_FILE}
ls -l {env:DOCROOT}/{env:TST_FILE} tests/{env:TST_FILE}
bash -c 'rm -f {env:DOCROOT}/{env:TST_FILE} tests/{env:TST_FILE}'

commands_post =
tftpdaemon stop

[testenv:{docs,ldocs,cdocs}]
# these tox env cmds share a virtual env using the following plugin
# https://github.com/masenf/tox-ignore-env-name-mismatch
Expand Down

0 comments on commit c99985e

Please sign in to comment.