Skip to content

Commit

Permalink
Merge pull request #276 from mitre/272-remove-old-directories
Browse files Browse the repository at this point in the history
Add option to remove empty mount directories on startup
  • Loading branch information
agibbons27 authored Jul 17, 2024
2 parents cb38e14 + c9a1ec0 commit 9c093f0
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 14 deletions.
9 changes: 4 additions & 5 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,12 @@ Quick Reference
-h, --host TEXT Host to run Thumbtack server on [default: 127.0.0.1]
-p, --port TEXT Port to run Thumbtack server on [default: 8208]
-m, --mount-dir TEXT Directory to mount disk images [Default: /mnt/thumbtack]
-i, --image-dir TEXT Directory of disk images for Thumbtack server to
monitor [default: $CWD]
--db TEXT SQLite database to store mount state [default:
database.db]
-b, --base-url TEXT Base URL where Thumbtack is hosted on the server
-i, --image-dir TEXT Directory of disk images for Thumbtack server to monitor [Default: $CWD]
--db TEXT SQLite database to store mount state
-b, --base-url TEXT Base URL where Thumbtack is hosted on the server [default: /]
--path-contains TEXT Only select files containing specified string in the path
-s, --skip-subdirectory TEXT Subdirectory to ignore when monitoring files
-r, --remove-directories Remove empty directories in the mount directory on startup.
--help Show this message and exit.
LICENSE
Expand Down
10 changes: 5 additions & 5 deletions docs/tutorial.rst
Original file line number Diff line number Diff line change
Expand Up @@ -112,13 +112,13 @@ See them below.
-d, --debug Run the Thumbtack server in debug mode
-h, --host TEXT Host to run Thumbtack server on [default: 127.0.0.1]
-p, --port TEXT Port to run Thumbtack server on [default: 8208]
-i, --image-dir TEXT Directory of disk images for Thumbtack server to
monitor [default: $CWD]
--db TEXT SQLite database to store mount state [default:
database.db]
-b, --base-url TEXT Base URL where Thumbtack is hosted on the server
-m, --mount-dir TEXT Directory to mount disk images [Default: /mnt/thumbtack]
-i, --image-dir TEXT Directory of disk images for Thumbtack server to monitor [Default: $CWD]
--db TEXT SQLite database to store mount state
-b, --base-url TEXT Base URL where Thumbtack is hosted on the server [default: /]
--path-contains TEXT Only select files containing specified string in the path
-s, --skip-subdirectory TEXT Subdirectory to ignore when monitoring files
-r, --remove-directories Remove empty directories in the mount directory on startup.
--help Show this message and exit.
Development Environment
Expand Down
16 changes: 13 additions & 3 deletions src/thumbtack/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
__version__ = "Could not find version"


def create_app(mount_dir=None, image_dir=None, database=None, base_url=None, path_contains=None, skip_subdirectory=None):
def create_app(mount_dir=None, image_dir=None, database=None, base_url=None, path_contains=None, skip_subdirectory=None, remove_directories=None):

if base_url:
static_url_path = f"{base_url}/static"
Expand Down Expand Up @@ -59,6 +59,9 @@ def create_app(mount_dir=None, image_dir=None, database=None, base_url=None, pat
if skip_subdirectory:
app.config.update(SKIP_SUBDIRECTORY=skip_subdirectory)

if remove_directories:
app.config.update(REMOVE_DIRECTORIES=remove_directories)

app.mnt_mutex = threading.Lock()

# configure the rest
Expand Down Expand Up @@ -170,9 +173,16 @@ def configure_logging(app):
show_default=True,
help="Subdirectory to ignore when monitoring files",
)
def start_app(debug, host, port, mount_dir, image_dir, database, base_url, path_contains, skip_subdirectory):
@click.option(
'-r',
"--remove-directories",
default=False,
is_flag=True,
help="Remove empty directories in the mount directory on startup.",
)
def start_app(debug, host, port, mount_dir, image_dir, database, base_url, path_contains, skip_subdirectory, remove_directories):
app = create_app(
mount_dir=mount_dir, image_dir=image_dir, database=database, base_url=base_url, path_contains=path_contains, skip_subdirectory=skip_subdirectory
mount_dir=mount_dir, image_dir=image_dir, database=database, base_url=base_url, path_contains=path_contains, skip_subdirectory=skip_subdirectory, remove_directories=remove_directories,
)
directory_monitoring_thread = DirectoryMonitoring(app)
directory_monitoring_thread.start()
Expand Down
7 changes: 6 additions & 1 deletion src/thumbtack/directory_monitoring.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import threading
import time

from .utils import monitor_image_dir
from .utils import monitor_image_dir, startup_remove_dirs


class DirectoryMonitoring(threading.Thread):
Expand All @@ -11,6 +11,11 @@ def __init__(self, app):

def run(self):
with self.app.app_context():
try:
if self.app.config["REMOVE_DIRECTORIES"]:
startup_remove_dirs()
except (KeyError, TypeError):
pass
while True:
time.sleep(3)
monitor_image_dir()
18 changes: 18 additions & 0 deletions src/thumbtack/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -600,6 +600,24 @@ def monitor_image_dir():
if Path(image["full_path"]) not in full_path_filenames
]

def startup_remove_dirs():
for item in os.listdir(current_app.config["MOUNT_DIR"]):
path = Path(current_app.config["MOUNT_DIR"], item)
current_app.logger.info(path)
try:
current_app.logger.info(f"Unmounting: {path}")
subprocess.run(["umount", path])
except Exception as e:
current_app.logger.error(e)

if os.path.isdir(path):
current_app.logger.info(path)
try:
current_app.logger.info(f"Removing: {path}")
os.rmdir(path)
except Exception as e:
current_app.logger.error(e)


def get_db():
db = getattr(g, "_database", None)
Expand Down

0 comments on commit 9c093f0

Please sign in to comment.