From 1e72a57d8de87d7bdc9cafbb742b8b0248767714 Mon Sep 17 00:00:00 2001 From: Pacman Ghost Date: Tue, 15 Nov 2022 09:34:08 +1100 Subject: [PATCH] Added options to store data files outside a Docker container. --- run-container.sh | 68 +++++++++++++++++++++++++++--- vasl_templates/webapp/downloads.py | 4 +- 2 files changed, 66 insertions(+), 6 deletions(-) diff --git a/run-container.sh b/run-container.sh index a3c7066..3a477f0 100755 --- a/run-container.sh +++ b/run-container.sh @@ -14,6 +14,9 @@ function main VASL_BOARDS= CHAPTER_H_NOTES= USER_FILES= + ASA_INDEX= + ROAR_INDEX= + VO_NOTES_IMAGE_CACHE= TEMPLATE_PACK= IMAGE_TAG=latest CONTAINER_NAME=vasl-templates @@ -31,7 +34,7 @@ function main print_help exit 0 fi - params="$(getopt -o p:v:e:k:t:d -l port:,control-tests-port:,vassal:,vasl:,vasl-extensions:,boards:,chapter-h:,template-pack:,user-files:,tag:,name:,detach,no-build,build-arg:,build-network:,run-network:,test-data-vassal:,test-data-vasl-mods:,help --name "$0" -- "$@")" + params="$(getopt -o p:v:e:k:t:d -l port:,control-tests-port:,vassal:,vasl:,vasl-extensions:,boards:,chapter-h:,template-pack:,user-files:,asa-index:,roar-index:,vo-notes-image-cache:,tag:,name:,detach,no-build,build-arg:,build-network:,run-network:,test-data-vassal:,test-data-vasl-mods:,help --name "$0" -- "$@")" if [ $? -ne 0 ]; then exit 1; fi eval set -- "$params" while true; do @@ -60,6 +63,15 @@ function main -k | --template-pack) TEMPLATE_PACK=$2 shift 2 ;; + --asa-index) + ASA_INDEX=$2 + shift 2 ;; + --roar-index) + ROAR_INDEX=$2 + shift 2 ;; + --vo-notes-image-cache) + VO_NOTES_IMAGE_CACHE=$2 + shift 2 ;; -t | --tag) IMAGE_TAG=$2 shift 2 ;; @@ -186,7 +198,47 @@ function main fi mpoint=/data/template-pack TEMPLATE_PACK_VOLUME="--volume $target:$mpoint" - TEMPLATE_PACK_ENV="--env DEFAULT_TEMPLATE_PACK=$mpoint --env DEFAULT_TEMPLATE_PACK_TARGET" + TEMPLATE_PACK_ENV="--env DEFAULT_TEMPLATE_PACK=$mpoint --env DEFAULT_TEMPLATE_PACK_TARGET=$target" + fi + + # check if external ASA/ROAR index files have been specified + # NOTE: We don't need to pass env.vars into the container, or anything like that. The code already + # saves the downloaded files in /tmp/ (inside the container), so all we need to do is map these files + # to the specified external files. + if [ -n "$ASA_INDEX" ]; then + target=$( realpath --no-symlinks "$ASA_INDEX" ) + if [ ! -f "$target" ]; then + if ! touch "$target" 2>/dev/null; then + echo "Can't find the ASA index file: $ASA_INDEX" + exit 1 + fi + fi + mpoint=/tmp/asl-scenario-archive.json + ASA_INDEX_VOLUME="--volume $target:$mpoint" + fi + if [ -n "$ROAR_INDEX" ]; then + target=$( realpath --no-symlinks "$ROAR_INDEX" ) + if [ ! -f "$target" ]; then + if ! touch "$target" 2>/dev/null ; then + echo "Can't find the ROAR index file: $ASA_INDEX" + exit 1 + fi + fi + mpoint=/tmp/roar-scenario-index.json + ROAR_INDEX_VOLUME="--volume $target:$mpoint" + fi + + # check if an external v/o notes image cache directory has been specified + if [ -n "$VO_NOTES_IMAGE_CACHE" ]; then + target=$( realpath --no-symlinks "$VO_NOTES_IMAGE_CACHE" ) + if [ ! -d "$target" ]; then + if ! mkdir "$target" 2>/dev/null; then + echo "Can't find the V/O notes image cache directory: $VO_NOTES_IMAGE_CACHE" + exit 1 + fi + fi + mpoint=/tmp/vo-notes-image-cache/ + VO_NOTES_IMAGE_CACHE_VOLUME="--volume $target:$mpoint" fi # check if testing has been enabled @@ -219,6 +271,7 @@ function main --env DOCKER_CONTAINER_NAME="$CONTAINER_NAME" \ $CONTROL_TESTS_PORT_RUN \ $VASSAL_VOLUME $VASL_MOD_VOLUME $VASL_EXTNS_VOLUME $VASL_BOARDS_VOLUME $CHAPTER_H_NOTES_VOLUME $TEMPLATE_PACK_VOLUME $USER_FILES_VOLUME \ + $ASA_INDEX_VOLUME $ROAR_INDEX_VOLUME $VO_NOTES_IMAGE_CACHE_VOLUME \ $VASSAL_ENV $VASL_MOD_ENV $VASL_EXTNS_ENV $VASL_BOARDS_ENV $CHAPTER_H_NOTES_ENV $TEMPLATE_PACK_ENV $USER_FILES_ENV \ $RUN_NETWORK $DETACH \ $TEST_DATA_VASSAL $TEST_DATA_VASL_MODS \ @@ -288,10 +341,15 @@ function print_help { --build-network Docker network to use when building the image. --run-network Docker network to use when running the container. + Options for storing data files outside the container (so that they can be re-used): + --asa-index Path to the ASL Scenario Archive index file (downloaded). + --roar-index Path to the ROAR index file (downloaded). + --vo-notes-image-cache Cache directory for images generated for vehicle/ordnance notes. + Options for the test suite: - --control-tests-port Remote test control port number. - --test-data-vassal Directory containing VASSAL releases. - --test-data-vasl-mods Directory containing VASL modules. + --control-tests-port Remote test control port number. + --test-data-vassal Directory containing VASSAL releases. + --test-data-vasl-mods Directory containing VASL modules. NOTE: If the port the webapp server is listening on *inside* the container is different to the port exposed *outside* the container, webdriver image generation (e.g. Shift-Click diff --git a/vasl_templates/webapp/downloads.py b/vasl_templates/webapp/downloads.py index 907643b..001fe78 100644 --- a/vasl_templates/webapp/downloads.py +++ b/vasl_templates/webapp/downloads.py @@ -65,6 +65,8 @@ class DownloadedFile: if len(data) < 1024 and os.path.isfile( data ): with open( data, "r", encoding="utf-8" ) as fp: data = fp.read() + if len( data ) == 0: + return self._data = json.loads( data ) # notify the owner if self.on_data: @@ -115,7 +117,7 @@ class DownloadedFile: _logger.info( "Download of the %s file has been disabled.", df.key ) continue ttl *= 60*60 - if os.path.isfile( df.cache_fname ): + if os.path.isfile( df.cache_fname ) and os.stat( df.cache_fname ).st_size > 0: # yup - check how long ago it was downloaded mtime = os.path.getmtime( df.cache_fname ) age = int( time.time() - mtime )