Skip to content

Commit

Permalink
Fix env vars being ignored in command-line launch
Browse files Browse the repository at this point in the history
And disallow client setting mountpoint on server
  • Loading branch information
Neil Orans committed Jun 17, 2019
1 parent a49f036 commit f1f8b9b
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 20 deletions.
18 changes: 8 additions & 10 deletions src/thumbtack/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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)
7 changes: 5 additions & 2 deletions src/thumbtack/config.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import os

bind = "0.0.0.0:8208"
workers = 4
# env = None
Expand All @@ -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'
7 changes: 2 additions & 5 deletions src/thumbtack/resources.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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))
Expand Down
8 changes: 7 additions & 1 deletion src/thumbtack/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions src/thumbtack/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand All @@ -75,4 +76,3 @@ def mount_form():

return render_template('form_complete.html',
status=status)

0 comments on commit f1f8b9b

Please sign in to comment.