From 3f865d15bf1d73ff36531b6d09ff659740140114 Mon Sep 17 00:00:00 2001 From: Alex Date: Fri, 4 Oct 2024 17:44:26 -0600 Subject: [PATCH] Added Docker support --- DOCKER_THESIS_DEFENSE_LATEX.sh | 99 +++++++++++++++++++ Docker_Make/Dockerfile | 12 +++ LATEX/DOCKER_RUN.sh | 17 ++++ Defense.tex => LATEX/Defense.tex | 0 .../Supporting}/Example_Present.tex | 0 .../Supporting}/FinalThesis.bib | 0 LATEX/biblatex.sh | 2 + LATEX/build.sh | 7 ++ LATEX/clean.sh | 9 ++ 9 files changed, 146 insertions(+) create mode 100644 DOCKER_THESIS_DEFENSE_LATEX.sh create mode 100644 Docker_Make/Dockerfile create mode 100755 LATEX/DOCKER_RUN.sh rename Defense.tex => LATEX/Defense.tex (100%) rename {Supporting => LATEX/Supporting}/Example_Present.tex (100%) rename {Supporting => LATEX/Supporting}/FinalThesis.bib (100%) create mode 100755 LATEX/biblatex.sh create mode 100755 LATEX/build.sh create mode 100755 LATEX/clean.sh diff --git a/DOCKER_THESIS_DEFENSE_LATEX.sh b/DOCKER_THESIS_DEFENSE_LATEX.sh new file mode 100644 index 0000000..67b6845 --- /dev/null +++ b/DOCKER_THESIS_DEFENSE_LATEX.sh @@ -0,0 +1,99 @@ +#!/bin/bash +#Variable to indicate if a docker image should be saved (user choice) + #If it is passed in as yes or no from a script use than. Otherwise use a input +if [ ! -n "$SAVE" ] +then +echo "Do you want to save a new docker image as a backup?" + read -p " Yes/No: " SAVE +fi +#Convert to upper case. +SAVE=${SAVE^^} +#Exit if not valid +if [ ! $SAVE = YES ] && [ ! $SAVE = NO ] ; then + echo "The option to save the docker image must be set to Yes or No. Exiting." + exit +fi + +DOCKER_CONTAINER_NAME="beamer_container" +DOCKER_IMAGE_NAME="acg/thesis_defense_latex:1.0" + + #If the docker image to run beamer LaTex doesn't exist make it + if [ -z "$(docker images -q $DOCKER_IMAGE_NAME 2> /dev/null)" ]; then + echo "Building a LaTex docker image, for the thesis." + #Move to the directory with the Dockerfile used to run the regression analysis, and create result figures. + cd ./Docker_Make/ + #Move back tot he main working directory + docker build -t $DOCKER_IMAGE_NAME . + cd ../ + echo "Docker LaTex image built" + fi + #Check if a docker container of the image is running, and if an exited container blocks, removed if before running a new one. + if [ "$(docker ps -a -q -f name=${DOCKER_CONTAINER_NAME})" ]; then + echo "Removing existing container of thesis image." + if [ "$(docker ps -aq -f status=exited -f name=${DOCKER_CONTAINER_NAME})" ]; then + # cleanup + docker rm ${DOCKER_CONTAINER_NAME} + # run the container + echo "Running docker image as a container" + docker run -d -t --name "$DOCKER_CONTAINER_NAME" "$DOCKER_IMAGE_NAME" + + fi + else + # run the container + echo "Running docker image as a container" + docker run -d -t --name "$DOCKER_CONTAINER_NAME" "$DOCKER_IMAGE_NAME" + fi + + + + #Save a image snapshot with all LaTex code. The SAVE variable is made to be lower case. + if [ $SAVE = YES ] + then + timestamp=$(date +%Y-%m-%d) + SAVE_DIR="ACG_DEFENSE_BEAMER_DOCKER_${timestamp}" + SAVE_DIR_PATH="Saved_Images/${SAVE_DIR}" + echo "Making Save Location ${SAVE_DIR_PATH}" + mkdir -p ${SAVE_DIR_PATH} + echo "Copying LaTex Code to ${SAVE_DIR_PATH}" + cp -r ./LATEX/* ${SAVE_DIR_PATH} + cd ${SAVE_DIR_PATH} + #Run the cleaning script to remove any files that do not need to be upload to the docker container. + bash clean.sh + mkdir -p DOCKER_IMAGE + echo "Writing script file to run the Docker LaTex code" + echo '#If the image does not exist make it' > DOCKER_RUN.sh + echo "if [ ! \"\$(docker images -q ${DOCKER_IMAGE_NAME} 2> /dev/null)\" ]; then" >> DOCKER_RUN.sh + echo " docker load -i DOCKER_IMAGE/LATEX_DOCKER.tar" >> DOCKER_RUN.sh + echo "fi" >> DOCKER_RUN.sh + echo "#If container is stoped remove." >> DOCKER_RUN.sh + echo "if [ \"\$(docker ps -aq -f status=exited -f name=${DOCKER_CONTAINER_NAME})\" ]; then" >> DOCKER_RUN.sh + echo " docker rm ${DOCKER_CONTAINER_NAME}" >> DOCKER_RUN.sh + echo " docker run -d -t --name ${DOCKER_CONTAINER_NAME} ${DOCKER_IMAGE_NAME}" >> DOCKER_RUN.sh + echo "fi" >> DOCKER_RUN.sh + echo "#If container does not exists make it" >> DOCKER_RUN.sh + echo "if [ ! \"\$(docker ps -a -q -f name=${DOCKER_CONTAINER_NAME})\" ]; then" >> DOCKER_RUN.sh + echo " docker run -d -t --name ${DOCKER_CONTAINER_NAME} ${DOCKER_IMAGE_NAME}" >> DOCKER_RUN.sh + echo "fi" >> DOCKER_RUN.sh >> DOCKER_RUN.sh + echo "#Run LaTex Docker on the build files, export the pdf, and exit" >> DOCKER_RUN.sh +echo "mv DOCKER_IMAGE/ ../" >> DOCKER_RUN.sh + + echo "docker cp ./ ${DOCKER_CONTAINER_NAME}:/LaTex" >> DOCKER_RUN.sh + echo "docker exec ${DOCKER_CONTAINER_NAME} bash /LaTex/build.sh" >> DOCKER_RUN.sh + echo "docker cp ${DOCKER_CONTAINER_NAME}:/LaTex/Mines_Thesis.pdf ./" >> DOCKER_RUN.sh +echo "mv ../DOCKER_IMAGE/ ./" >> DOCKER_RUN.sh + + echo "docker stop ${DOCKER_CONTAINER_NAME}" >> DOCKER_RUN.sh + echo "docker remove ${DOCKER_CONTAINER_NAME}" >> DOCKER_RUN.sh + + cd DOCKER_IMAGE + echo "Creating Docker Image Saved to ${SAVE_DIR_PATH}/DOCKER_IMAGE/LATEX_DOCKER.tar" + docker save "${DOCKER_IMAGE_NAME}" > LATEX_DOCKER.tar + cd ../../ + echo "Creating a compressed tar.gz file of all contents of ${SAVE_DIR_PATH}" + tar -czf "${SAVE_DIR}.tar.gz" "${SAVE_DIR}" + echo "Removing original contents of ${SAVE_DIR_PATH} to save space" + rm -r ${SAVE_DIR} + echo "Snapshot saved!" + fi + + diff --git a/Docker_Make/Dockerfile b/Docker_Make/Dockerfile new file mode 100644 index 0000000..62e5b66 --- /dev/null +++ b/Docker_Make/Dockerfile @@ -0,0 +1,12 @@ +#Date Created: October 4th 2024 +#Author Alex Gebben +FROM debian:trixie +LABEL org.opencontainers.image.authors=agebben@uwyo.edu +ARG DEBIAN_FRONTEND=noninteractive +#Update Container +RUN apt update; apt dist-upgrade -y +#Install latex packages. The specific version used when this file was created is assigned. Remove this to try and install a newer version. That may fix future problems if this code does not run. +#RUN apt install -y texlive-full=2024.20240829-2 vim biber=2.20-2 +RUN apt install -y texlive-latex-extra=2024.20240829-1 vim biber=2.20-2 + +WORKDIR /LaTex diff --git a/LATEX/DOCKER_RUN.sh b/LATEX/DOCKER_RUN.sh new file mode 100755 index 0000000..b88cc37 --- /dev/null +++ b/LATEX/DOCKER_RUN.sh @@ -0,0 +1,17 @@ +#!/bin/bash +#If the needed docker image does not exist run the script to build it. Pass in that a backup .tar.gz image should not be made. +if [ ! "$(docker images -q acg/thesis_defense_latex:1.0 2> /dev/null)" ]; then + cd .. + export SAVE="No" + bash DOCKER_THESIS_DEFENSE_LATEX.sh + cd ./LATEX +fi +#If the docker container exists compile the Latex using the container, and output the pdf locally. + if [ "$(docker images -q acg/thesis_defense_latex:1.0 2> /dev/null)" ]; then + docker run -d -t --name beamer_container acg/thesis_defense_latex:1.0 + docker cp ./ beamer_container:/LaTex + docker exec beamer_container bash /LaTex/build.sh + docker cp beamer_container:/LaTex/Defense.pdf ./ + docker stop beamer_container + docker remove beamer_container + fi diff --git a/Defense.tex b/LATEX/Defense.tex similarity index 100% rename from Defense.tex rename to LATEX/Defense.tex diff --git a/Supporting/Example_Present.tex b/LATEX/Supporting/Example_Present.tex similarity index 100% rename from Supporting/Example_Present.tex rename to LATEX/Supporting/Example_Present.tex diff --git a/Supporting/FinalThesis.bib b/LATEX/Supporting/FinalThesis.bib similarity index 100% rename from Supporting/FinalThesis.bib rename to LATEX/Supporting/FinalThesis.bib diff --git a/LATEX/biblatex.sh b/LATEX/biblatex.sh new file mode 100755 index 0000000..f7b3604 --- /dev/null +++ b/LATEX/biblatex.sh @@ -0,0 +1,2 @@ +#!/bin/bash +biber 'Defense' diff --git a/LATEX/build.sh b/LATEX/build.sh new file mode 100755 index 0000000..4c32167 --- /dev/null +++ b/LATEX/build.sh @@ -0,0 +1,7 @@ +#!/bin/bash +./clean.sh +pdflatex -file-line-error -halt-on-error Defense.tex +./biblatex.sh +pdflatex -file-line-error -halt-on-error Defense.tex +pdflatex -file-line-error -halt-on-error Defense.tex +# pdflatex -file-line-error -halt-on-error Mines_Thesis.tex diff --git a/LATEX/clean.sh b/LATEX/clean.sh new file mode 100755 index 0000000..ff4fd6f --- /dev/null +++ b/LATEX/clean.sh @@ -0,0 +1,9 @@ +#!/bin/bash +rm -f Defense.aux +rm -f Defense.nav +rm -f Defense.run.xml +rm -f Defense.toc +rm -f Defense.bcf +rm -f Defense.out +rm -f Defense.snm +rm -f Defense.log