From 0c9015a09557ab265082aeede8c7ee6ff68bd561 Mon Sep 17 00:00:00 2001 From: poikilos <7557867+poikilos@users.noreply.github.com> Date: Thu, 12 May 2022 21:35:48 -0400 Subject: [PATCH] Add container scripts. --- .gitignore | 1 + containers/lmk.devuan-chimaera.sh | 257 ++++++++++++++++++++ containers/lmk.devuan-chimaera/Dockerfile | 12 + containers/lmk.devuan-chimaera/build-lmk.sh | 9 + containers/lmk.devuan-chimaera/lmk.rc | 33 +++ install-minetest-build-deps.sh | 11 +- 6 files changed, 322 insertions(+), 1 deletion(-) create mode 100755 containers/lmk.devuan-chimaera.sh create mode 100644 containers/lmk.devuan-chimaera/Dockerfile create mode 100755 containers/lmk.devuan-chimaera/build-lmk.sh create mode 100644 containers/lmk.devuan-chimaera/lmk.rc diff --git a/.gitignore b/.gitignore index 54e6b48..9a16bcc 100644 --- a/.gitignore +++ b/.gitignore @@ -13,3 +13,4 @@ nohup.out /webapp/settingtypes-list.txt err.txt *.continuified.tmp +linux-minetest-kit.zip diff --git a/containers/lmk.devuan-chimaera.sh b/containers/lmk.devuan-chimaera.sh new file mode 100755 index 0000000..589b7c3 --- /dev/null +++ b/containers/lmk.devuan-chimaera.sh @@ -0,0 +1,257 @@ +#!/bin/bash +# See https://nextbreakpoint.com/posts/article-compile-code-with-docker.html +# sudo docker build -t lmk-devuan-chimaera-img dyne/devuan:chimaera +me=lmk.devuan-chimera.sh +docker_path="`sudo command -v docker`" +if [ ! -f "$docker_path" ]; then + cat < +or Docker's own documentation at +. +END +fi +container_name="lmk-devuan-chimaera" +image_name="lmk-devuan-chimaera-img" + +source $image_name/project.rc +if [ $? -ne 0 ]; then + exit 1 +fi + +# sudo docker image inspect $image_name > /dev/null +sudo docker image inspect $image_name --format "* docker is looking for the image..." +# ^ appending ":latest" to the name also works. +# ^ Get matching images as a JSON list (where each has "Id" and other +# metadata). +if [ $? -ne 0 ]; then + if [ ! -d "$local_img_dir" ]; then + echo "Error: \"$local_img_dir\" (local_img_dir for storing $SRC_URL) doesn't exist." + exit 1 + fi + + if [ ! -f "$container_build_blob" ]; then + echo "* downloading $SRC_URL to $DL_SRC_PATH..." + curl "$SRC_URL" --progress-bar --output "$DL_SRC_PATH" + if [ $? -ne 0 ]; then + exit 1 + fi + if [ ! -f "$container_build_blob" ]; then + echo "Error: This script requires \"$container_build_blob\"." + exit 1 + fi + else + echo "* using existing \"$container_build_blob\" to build the container image" + fi + move_back="false" + prerelease_path=~/Downloads/minetest.org/insider-prerelease/linux-minetest-kit-220509.zip + if [ ! -f $container_build_blob ]; then + if [ -f "$prerelease_path" ]; then + move_back="true" + echo "mv $prerelease_path $container_build_blob" + mv $prerelease_path $container_build_blob + if [ $? -ne 0 ]; then + echo "* Error: the mv command failed." + exit 1 + fi + fi + fi + sudo docker build -t $image_name $local_img_dir + code=$? + if [ "@move_back" = "@true" ]; then + echo "mv \"$prerelease_path\" \"$container_build_blob\"" + mv "$container_build_blob" "$prerelease_path" + if [ $? -ne 0 ]; then + echo "* Warning: the mv command failed." + fi + fi + if [ $code -ne 0 ]; then + exit 1 + fi +else + echo "* The container will be built using the existing docker image $image_name" +fi + +cat <) + # You must use the container name (as determined using the "ps" subcommand), not the image name. + + sudo docker container run -it $image_name /bin/bash + # ^ Run an interactive terminal (Type 'exit' to exit) + # (based on ) + + sudo docker attach $container_name + # ^ Attach your current terminal to a running container (See + # ). + + + sudo docker rm --force $container_name + # ^ Delete a container by its name. + # --force: kill and delete running containers as well. + +END + +# sudo docker container run -d --name $image_name unzip xvf $contained_arc -C $contained_repos +container_Id=`sudo docker ps -aqf "name=$container_name"` +# ^ ONLY works with container_name not image_name + +cat > /dev/null < /dev/null +if [ $? -ne 0 ]; then + printf "NO...checking for unzip..." + container_unzip="`sudo docker exec $container_name which unzip`" + if [ "@$container_unzip" = "@" ]; then + echo "NO. Installing..." + # This should never happen if the Dockerfile was used. + sudo docker exec $container_name apt-get update + if [ $? -ne 0 ]; then exit 1; fi + sudo docker exec $container_name apt-get install -y unzip + if [ $? -ne 0 ]; then exit 1; fi + container_unzip="`sudo docker exec $container_name which unzip`" + if [ "@$container_unzip" = "@" ]; then + echo "Error: Installing unzip in the container did not succeed. Install unzip inside the container manually then try again, or extract linux-minetest-kit such that $contained_good_repo_flag_path exists." + exit 1 + fi + else + echo "FOUND" + fi + # sudo docker container run $image_name unzip xvf $contained_arc -d $contained_repos + echo "* extracting $contained_arc" + sudo docker exec $container_name unzip $contained_arc -d $contained_repos + # -d: is destination, like -C or --directory for tar. + # -v: verbose (prevents extraction) + if [ $? -ne 0 ]; then + echo "Error: unzip failed within the container. Install unzip inside the container manually then try again, or extract linux-minetest-kit such that $contained_good_repo_flag_path exists." + exit 1 + fi +else + echo "FOUND (already extracted)" +fi + +# sudo docker exec $container_name ls $contained_repo > /dev/null +sudo docker exec $container_name ls $contained_good_repo_flag_path > /dev/null +if [ $? -ne 0 ]; then + echo "Error: extracting linux-minetest-kit.zip in the container didn't work. Extract linux-minetest-kit.zip to $contained_repos such that $contained_good_repo_flag_path exists in the container and try again." + exit 1 +else + echo "* detected $contained_good_repo_flag_path (So the source directory is assumed to be ok)" +fi + + + +if [ "@$contained_user" = "@" ]; then + echo "Error: contained_user can't be blank, or checking for the user within the container will not work." + exit 1 +fi + +sudo docker exec $container_name id -u $contained_user +if [ $? -ne 0 ]; then + printf "* creating $contained_user in container $container_name..." + sudo docker exec $container_name adduser --disabled-password --gecos "" $contained_user --home $contained_home + if [ $? -ne 0 ]; then + echo "FAILED" + exit 1 + else + echo "OK" + fi +else + echo "* using the $container_name container's existing contained_user: $contained_user" +fi + +# sudo docker container run --name $container_name $image_name ls $contained_repos + +# sudo docker exec $container_name chown -R $contained_user $contained_repos +# ^ Usually you could do this, but run as root since this script is used to test the safety of linux-minetest-kit: + +# sudo docker exec $container_name curl https://raw.githubusercontent.com/poikilos/EnlivenMinetest/master/install-minetest-build-deps.sh --output /opt/install-minetest-build-deps.sh +# sudo docker exec $container_name chmod +x $repo_build_assumptions_cmd +# sudo docker exec $container_name $repo_build_assumptions_cmd + +# ^ moved to Dockerfile + +echo "* building libraries using $repo_build_libs_cmd..." +sudo docker exec -w $contained_src $container_name $repo_build_libs_cmd +if [ $? -ne 0 ]; then exit 1; fi +# echo "* building program using $repo_build_cmd..." +# sudo docker exec -w $contained_src $container_name $repo_build_cmd +if [ $? -ne 0 ]; then exit 1; fi +# -w: working directory + + +# How to use docker-compose (See ): +# "1. Define your app’s environment with a Dockerfile so it can be reproduced anywhere." +# "2. Define the services that make up your app in docker-compose.yml so they can be run together in an isolated environment." +# "3. Run docker compose up and the Docker compose command starts and runs your entire app. You can alternatively run docker-compose up using the docker-compose binary." diff --git a/containers/lmk.devuan-chimaera/Dockerfile b/containers/lmk.devuan-chimaera/Dockerfile new file mode 100644 index 0000000..6335531 --- /dev/null +++ b/containers/lmk.devuan-chimaera/Dockerfile @@ -0,0 +1,12 @@ +FROM dyne/devuan:chimaera +# An absolute path for COPY doesn't work ('/' is removed from the beginning). +COPY linux-minetest-kit.zip /opt +COPY lmk.rc /opt +COPY build-lmk.sh /opt +RUN apt-get update +RUN apt-get install -y unzip +RUN unzip /opt/linux-minetest-kit.zip -d /opt +# RUN apt-get install -y sudo +RUN curl https://raw.githubusercontent.com/poikilos/EnlivenMinetest/master/install-minetest-build-deps.sh --output /opt/install-minetest-build-deps.sh +RUN chmod +x /opt/install-minetest-build-deps.sh +RUN /opt/install-minetest-build-deps.sh diff --git a/containers/lmk.devuan-chimaera/build-lmk.sh b/containers/lmk.devuan-chimaera/build-lmk.sh new file mode 100755 index 0000000..b66d976 --- /dev/null +++ b/containers/lmk.devuan-chimaera/build-lmk.sh @@ -0,0 +1,9 @@ +#!/bin/bash +cd /opt +if [ $? -ne 0 ]; then + exit 1 +fi +source lmk.rc +if [ $? -ne 0 ]; then + exit 1 +fi diff --git a/containers/lmk.devuan-chimaera/lmk.rc b/containers/lmk.devuan-chimaera/lmk.rc new file mode 100644 index 0000000..afd1707 --- /dev/null +++ b/containers/lmk.devuan-chimaera/lmk.rc @@ -0,0 +1,33 @@ +contained_repos=/opt +contained_arc="$contained_repos/linux-minetest-kit.zip" +# ^ This must always be in sync with "COPY linux-minetest-kit.zip /opt" in Dockerfile +contained_repo=$contained_repos/linux-minetest-kit +contained_user=minebest +contained_home=/home/$contained_user +if [ "@$SRC_URL" = "@" ]; then + SRC_URL="https://downloads.minetest.org/linux-minetest-kit.zip" +fi +#if [ "@$DL_SRC_NAME" = "@" ]; then +DL_SRC_NAME=linux-minetest-kit.zip +#fi +local_img_dir="lmk.devuan-chimaera" +if [ ! -d "$local_img_dir" ]; then + echo "* $me must run from the directory containing the container image directory: $local_img_dir" + exit 1 +fi +container_build_blob=$local_img_dir/linux-minetest-kit.zip +# ^ local_img_dir has to be in the same directory as Dockerfile or +# the COPY command in the Dockerfile won't work. +if [ "@$DL_SRC_PATH" = "@" ]; then + # DL_SRC_PATH="$HOME/Downloads/$DL_SRC_NAME" + DL_SRC_PATH="$local_img_dir" + # ^ $DL_SRC_PATH has to be in the same directory as Dockerfile or + # the COPY command in the Dockerfile won't work. + # The file should be added to .gitignore for the reason that it + # is in the repo in the docker image directory. +fi + +good_repo_flag_name="mtcompile-program.pl" +repo_build_assumptions_cmd="/opt/install-minetest-build-deps.sh" +repo_build_libs_cmd="./mtcompile-libraries.sh" +repo_build_cmd="./mtcompile-program.pl --build --classic --client" diff --git a/install-minetest-build-deps.sh b/install-minetest-build-deps.sh index 496f9a6..606f929 100755 --- a/install-minetest-build-deps.sh +++ b/install-minetest-build-deps.sh @@ -2,9 +2,18 @@ # Do not use sudo, in case sudo is not installed (such as in some # default installations including the dyne/devuan:chimaera docker image # on docker hub. +customExit(){ + >&2 echo "Error:" + echo "$1" + code=1 + if [ "@$2" -ne "@" ]; then + code=$2 + fi + exit $code +} me=install-minetest-build-deps.sh if [ "$EUID" -ne 0 ]; then - echo "Error: $me must run as root." + >&2 echo "Error: $me must run as root." exit 1 fi enable_postgres="false"