From f1f8b9bcb56cb9e7ba026b497029ff62c9d5beb5 Mon Sep 17 00:00:00 2001 From: Neil Orans Date: Mon, 17 Jun 2019 09:01:16 -0400 Subject: [PATCH] Fix env vars being ignored in command-line launch And disallow client setting mountpoint on server --- src/thumbtack/__init__.py | 18 ++++++++---------- src/thumbtack/config.py | 7 +++++-- src/thumbtack/resources.py | 7 ++----- src/thumbtack/utils.py | 8 +++++++- src/thumbtack/views.py | 4 ++-- 5 files changed, 24 insertions(+), 20 deletions(-) diff --git a/src/thumbtack/__init__.py b/src/thumbtack/__init__.py index 927a391..ffa0082 100644 --- a/src/thumbtack/__init__.py +++ b/src/thumbtack/__init__.py @@ -23,21 +23,20 @@ def create_app(image_dir=None, database=None): app = Flask(__name__) app.config.from_object('thumbtack.config') - # Since development doesn't have this environment variable, it won't do anything - app.config.from_envvar('THUMBTACK_CONFIG_PRODUCTION', silent=True) - if os.environ.get("IMAGE_DIR"): - app.config.update(IMAGE_DIR=os.environ.get("IMAGE_DIR")) if os.environ.get("MOUNT_DIR"): app.config.update(MOUNT_DIR=os.environ.get("MOUNT_DIR")) - if os.environ.get("DATABASE"): - app.config.update(DATABASE=os.environ.get("DATABASE")) - # these variables are from the thumbtack entry point, and should overwrite environment variable equivalents + # priority goes to command line args, then env variables, then val from config.py if image_dir: app.config.update(IMAGE_DIR=image_dir) + elif os.environ.get("IMAGE_DIR"): + app.config.update(IMAGE_DIR=os.environ.get("IMAGE_DIR")) + if database: app.config.update(DATABASE=database) + elif os.environ.get("DATABASE"): + app.config.update(DATABASE=os.environ.get("DATABASE")) configure(app) @@ -110,10 +109,9 @@ def configure_logging(app): show_default=True, help='Host to run Thumbtack server on') @click.option('-p', '--port', default='8208', show_default=True, help='Port to run Thumbtack server on') -@click.option('-i', '--image-dir', default=os.getcwd(), +@click.option('-i', '--image-dir', help='Directory of disk images for Thumbtack server to monitor [Default: $CWD]') -@click.option('--db', 'database', default='database.db', - show_default=True, help='SQLite database to store mount state') +@click.option('--db', 'database', help='SQLite database to store mount state') def start_app(debug, host, port, image_dir, database): app = create_app(image_dir=image_dir, database=database) app.run(debug=debug, host=host, port=port) diff --git a/src/thumbtack/config.py b/src/thumbtack/config.py index 9be5334..f8f5b28 100644 --- a/src/thumbtack/config.py +++ b/src/thumbtack/config.py @@ -1,3 +1,5 @@ +import os + bind = "0.0.0.0:8208" workers = 4 # env = None @@ -6,6 +8,7 @@ # group = group logfile = "/var/log/thumbtack/thumbtack.log" # loglevel = debug -IMAGE_DIR = '/vagrant/tests/test_images' +IMAGE_DIR = os.getcwd() +# IMAGE_DIR = '/vagrant/tests/test_images' MOUNT_DIR = '/mnt/thumbtack' -DATABASE = '/vagrant/database.db' +DATABASE = 'database.db' diff --git a/src/thumbtack/resources.py b/src/thumbtack/resources.py index b2362dc..e52e63d 100644 --- a/src/thumbtack/resources.py +++ b/src/thumbtack/resources.py @@ -1,6 +1,6 @@ import imagemounter.exceptions -from flask import current_app, request +from flask import current_app from flask_restful import Resource, marshal_with, abort, fields from .exceptions import UnexpectedDiskError, NoMountableVolumesError, ImageNotInDatabaseError @@ -48,10 +48,7 @@ def put(self, image_path): """ status = None try: - mount_dir = request.args.get('mount_dir', '/mnt/thumbtack') - current_app.logger.info('mount_dir: {}'.format(mount_dir)) - - mounted_disk = mount_image(image_path, mount_dir) + mounted_disk = mount_image(image_path) if mounted_disk and mounted_disk.mountpoint is not None: current_app.logger.info('Image mounted successfully: {}'.format(image_path)) diff --git a/src/thumbtack/utils.py b/src/thumbtack/utils.py index fd4ad6f..33ed1e6 100644 --- a/src/thumbtack/utils.py +++ b/src/thumbtack/utils.py @@ -96,7 +96,13 @@ def increment_ref_count(rel_path): current_app.logger.info('* Increased ref count for {}. Now: {}'.format(rel_path, new_ref_count)) -def mount_image(relative_image_path, mount_dir='/mnt/thumbtack'): +def mount_image(relative_image_path): + mount_dir = current_app.config['MOUNT_DIR'] + if not mount_dir: + msg = "Mount directory is not properly set by thumbtack server" + current_app.logger.error(msg) + raise NotADirectoryError(msg) + full_image_path = '{}/{}'.format(current_app.config['IMAGE_DIR'], relative_image_path) image_info = get_image_info(relative_image_path) diff --git a/src/thumbtack/views.py b/src/thumbtack/views.py index ad01616..aa0b08e 100644 --- a/src/thumbtack/views.py +++ b/src/thumbtack/views.py @@ -54,7 +54,8 @@ def mount_form(): status = 'Unexpected number of disks. Thumbtack can only handle disk images that contain one disk.' except NoMountableVolumesError: status = 'No volumes in {} were able to be mounted.'.format(rel_path) - + except NotADirectoryError: + status = 'Mount failed. Thumbtack server has no mount directory set.' if mounted_disk and mounted_disk.mountpoint is not None: status = 'Mounted successfully' @@ -75,4 +76,3 @@ def mount_form(): return render_template('form_complete.html', status=status) -